1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.xulux.guilayer.swing.widgets;
17
18 import java.awt.Container;
19 import java.awt.event.ActionListener;
20 import java.util.ArrayList;
21 import java.util.Iterator;
22 import java.util.List;
23
24 import javax.swing.JMenuItem;
25 import javax.swing.JSeparator;
26
27 import org.xulux.api.gui.IWidgetInitializer;
28 import org.xulux.api.gui.IXuluxListener;
29 import org.xulux.gui.XuluxWidget;
30 import org.xulux.guilayer.swing.listeners.PrePostFieldListener;
31
32
33
34
35
36
37
38
39 public class MenuItem extends XuluxWidget {
40
41
42
43
44 protected JMenuItem item;
45
46
47
48 protected JSeparator separator;
49
50
51
52 protected ActionListener actionListener;
53
54
55
56 protected List listenerList;
57
58
59
60
61 public MenuItem(String name) {
62 super(name);
63 }
64
65
66
67
68 public void destroy() {
69 removeAllRules();
70 if (item != null && this.actionListener != null) {
71 this.item.removeActionListener(this.actionListener);
72 }
73 this.actionListener = null;
74 if (listenerList != null) {
75 for (Iterator it = listenerList.iterator(); it.hasNext();) {
76 this.item.removeActionListener((ActionListener) it.next());
77 }
78 }
79 if (item != null && item.getParent() != null) {
80 Container container = item.getParent();
81 container.remove(item);
82 }
83 item = null;
84 processDestroy();
85 }
86
87
88
89
90 public Object getNativeWidget() {
91 if (!initialized) {
92 initialize();
93 }
94 if (item != null) {
95 return item;
96 } else {
97 return separator;
98 }
99 }
100
101
102
103
104 public void initialize() {
105 if (initialized) {
106 return;
107 }
108 String type = getProperty("type");
109 if (type == null || (type != null && !type.equals("separator"))) {
110 item = new JMenuItem();
111
112 this.actionListener = new PrePostFieldListener(this);
113 item.addActionListener(this.actionListener);
114 } else {
115
116 separator = new JSeparator();
117 }
118 initialized = true;
119 refresh();
120 processInit();
121 }
122
123
124
125
126 public void refresh() {
127 if (isRefreshing()) {
128 return;
129 }
130 isRefreshing = true;
131 if (!initialized) {
132 initialize();
133 }
134 if (item != null) {
135 refreshItem();
136 } else if (separator != null) {
137 refreshSeperator();
138 }
139 isRefreshing = false;
140 }
141
142
143
144
145 private void refreshItem() {
146 String text = getProperty("text");
147 if (text != null) {
148 item.setText(text);
149 }
150 item.setEnabled(isEnabled());
151 item.setVisible(isVisible());
152 }
153
154
155
156
157
158 private void refreshSeperator() {
159
160 }
161
162
163
164
165 public Object getGuiValue() {
166 return null;
167 }
168
169
170
171
172 public void focus() {
173 item.requestFocus();
174 }
175
176
177
178
179 public boolean isValueEmpty() {
180 return false;
181 }
182
183
184
185
186 public boolean canContainValue() {
187 return false;
188 }
189
190
191
192
193 public void addXuluxListener(IXuluxListener listener) {
194 if (listener instanceof ActionListener) {
195 if (listenerList == null) {
196 listenerList = new ArrayList();
197 }
198 listenerList.add(listener);
199 initialize();
200 this.item.addActionListener((ActionListener) listener);
201 }
202 }
203
204 public IWidgetInitializer getWidgetInitializer() {
205 return null;
206 }
207
208 }