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.Insets;
20 import java.awt.event.ActionListener;
21 import java.awt.event.FocusEvent;
22 import java.awt.event.FocusListener;
23 import java.util.ArrayList;
24 import java.util.Iterator;
25 import java.util.List;
26
27 import javax.swing.Icon;
28 import javax.swing.ImageIcon;
29 import javax.swing.JButton;
30 import javax.swing.JRootPane;
31
32 import org.xulux.api.gui.IWidget;
33 import org.xulux.api.gui.IXuluxListener;
34 import org.xulux.guilayer.swing.SwingWidget;
35 import org.xulux.guilayer.swing.extensions.XuluxJButton;
36 import org.xulux.guilayer.swing.listeners.DefaultButtonChangeListener;
37 import org.xulux.guilayer.swing.listeners.PrePostFieldListener;
38 import org.xulux.guilayer.swing.listeners.UpdateButtonsListener;
39 import org.xulux.guilayer.swing.listeners.WidgetFocusListener;
40 import org.xulux.guilayer.swing.util.SwingUtils;
41 import org.xulux.utils.BooleanUtils;
42
43
44
45
46
47
48
49 public class Button extends SwingWidget {
50
51
52
53
54 private XuluxJButton button;
55
56
57
58 private PrePostFieldListener actionListener;
59
60
61
62 private FocusListener imageFocusListener;
63
64
65
66
67
68 private FocusListener focusListener;
69
70 private DefaultButtonChangeListener changeListener;
71
72
73
74 private List listenerList;
75
76
77
78
79
80 public Button(String name) {
81 super(name);
82 }
83
84
85
86
87 public Object getNativeWidget() {
88 initialize();
89 return button;
90 }
91
92
93
94
95 public void initialize() {
96 if (initialized) {
97 return;
98 }
99 button = new XuluxJButton();
100 initialized = true;
101 if ("cancel".equalsIgnoreCase(getProperty("defaultaction"))) {
102 IWidget p = getParent();
103 while (p.getParent() != null) {
104 p = p.getParent();
105 }
106 }
107 if ("false".equalsIgnoreCase(getProperty("focuspossible"))) {
108 button.setFocusTraversable(false);
109 }
110 if (!isRefreshing()) {
111 refresh();
112 }
113 processInit();
114 }
115
116
117
118
119 public void refresh() {
120 isRefreshing = true;
121 initialize();
122 button.setVisible(isVisible());
123 String image = getProperty("image");
124 if (image != null) {
125 ImageIcon icon = SwingUtils.getIcon(image, this);
126 button.setIcon(icon);
127 button.setFocusPainted(true);
128 }
129 String disabledImage = getProperty("image-disabled");
130 if (disabledImage != null) {
131 ImageIcon icon = SwingUtils.getIcon(disabledImage, this);
132 button.setDisabledIcon(icon);
133 button.setDisabledSelectedIcon(icon);
134 }
135 String rolloverImage = getProperty("image-rollover");
136 if (rolloverImage != null) {
137 ImageIcon icon = SwingUtils.getIcon(rolloverImage, this);
138 button.setRolloverIcon(icon);
139 button.setRolloverEnabled(true);
140 }
141 String selectedImage = getProperty("image-selected");
142 if (selectedImage != null) {
143 ImageIcon icon = SwingUtils.getIcon(selectedImage, this);
144 button.setSelectedIcon(icon);
145 button.setPressedIcon(icon);
146 button.setRolloverSelectedIcon(icon);
147 button.setRolloverEnabled(true);
148
149
150 if (this.imageFocusListener == null) {
151 this.imageFocusListener = new FocusListener() {
152 private Icon normalIcon;
153
154
155
156 public void focusGained(FocusEvent e) {
157 normalIcon = button.getIcon();
158 button.setIcon(button.getSelectedIcon());
159 }
160
161
162
163
164 public void focusLost(FocusEvent e) {
165 if (normalIcon != null) {
166 button.setIcon(normalIcon);
167 normalIcon = null;
168 }
169 }
170 };
171 button.addFocusListener(imageFocusListener);
172 }
173
174 }
175 if (getProperty("text") != null) {
176 button.setText(getProperty("text"));
177 }
178 if (actionListener == null) {
179 IXuluxListener listener = getPart().getFieldEventHandler(this);
180 if (listener == null) {
181 actionListener = new PrePostFieldListener(this);
182 } else {
183 actionListener = (PrePostFieldListener) listener;
184 }
185 button.addActionListener(actionListener);
186 }
187 if (focusListener == null) {
188 focusListener = new WidgetFocusListener(this);
189 button.addFocusListener(focusListener);
190 }
191 String alignment = getProperty("alignment");
192 if (alignment != null) {
193
194 int align = JButton.CENTER;
195 if (alignment.equalsIgnoreCase("left")) {
196 align = JButton.LEFT;
197 } else if (alignment.equalsIgnoreCase("right")) {
198 align = JButton.RIGHT;
199 }
200 button.setHorizontalAlignment(align);
201 }
202 String margin = getProperty("margin");
203 if (margin != null) {
204 Insets insets = SwingUtils.getInsets(margin);
205 if (insets != null) {
206 button.setMargin(insets);
207 }
208 }
209 button.setToolTipText(getProperty("tooltip"));
210 if (BooleanUtils.toBoolean(getProperty("defaultbutton"))) {
211 button.setDefaultCapable(true);
212 JRootPane pane = button.getRootPane();
213 if (pane != null) {
214 pane.setDefaultButton(button);
215 if (changeListener == null) {
216 changeListener = new DefaultButtonChangeListener();
217 }
218 button.addChangeListener(changeListener);
219 }
220 } else {
221
222 button.removeChangeListener(changeListener);
223 changeListener = null;
224 }
225 button.setEnabled(isEnabled());
226 if (getRectangle().getWidth() > 0 && getRectangle().getHeight() > 0) {
227 button.setPreferredSize(getRectangle().getRectangle().getSize());
228 button.setMinimumSize(getRectangle().getRectangle().getSize());
229 button.setMaximumSize(getRectangle().getRectangle().getSize());
230 button.setSize(getRectangle().getRectangle().getSize());
231 }
232 isRefreshing = false;
233 }
234
235
236
237
238 public void destroy() {
239 processDestroy();
240 removeAllRules();
241 if (button == null) {
242 getPart().removeWidget(this, this);
243 return;
244 }
245 Container container = button.getParent();
246 if (actionListener != null) {
247 button.removeActionListener(actionListener);
248 actionListener = null;
249 }
250 if (imageFocusListener != null) {
251 button.removeFocusListener(imageFocusListener);
252 }
253 if (changeListener != null) {
254 button.removeChangeListener(changeListener);
255 changeListener = null;
256 }
257 imageFocusListener = null;
258 if (focusListener != null) {
259 button.removeFocusListener(focusListener);
260 focusListener = null;
261 }
262 if (listenerList != null) {
263 for (Iterator it = listenerList.iterator(); it.hasNext();) {
264 Object l = it.next();
265 if (l instanceof ActionListener) {
266 button.removeActionListener((ActionListener) l);
267 } else if (l instanceof FocusListener) {
268 button.removeFocusListener((FocusListener) l);
269 }
270 }
271 }
272 button.setVisible(false);
273 if (container != null) {
274 container.remove(button);
275 }
276 getPart().removeWidget(this, this);
277 button = null;
278 }
279
280
281
282
283 public Object getGuiValue() {
284 return null;
285 }
286
287
288
289
290 public boolean canContainValue() {
291 return false;
292 }
293
294
295
296
297 public boolean isValueEmpty() {
298 return true;
299 }
300
301
302
303
304 public void addXuluxListener(IXuluxListener listener) {
305 if (listener instanceof ActionListener) {
306 if (listenerList == null) {
307 listenerList = new ArrayList();
308 }
309 listenerList.add(listener);
310 initialize();
311 if (listener instanceof ActionListener) {
312 this.button.addActionListener((ActionListener) listener);
313 } else if (listener instanceof FocusListener) {
314 button.addFocusListener((FocusListener) listener);
315 }
316 if (listener instanceof UpdateButtonsListener) {
317
318
319
320 button.removeActionListener(this.actionListener);
321 }
322 }
323 }
324
325
326 }