1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 package com.gridsystems.config.tools.swing;
25
26 import java.awt.LayoutManager;
27 import java.awt.event.ActionEvent;
28 import java.awt.event.ActionListener;
29 import java.awt.event.FocusEvent;
30 import java.awt.event.FocusListener;
31 import java.beans.PropertyChangeEvent;
32 import java.beans.PropertyChangeListener;
33 import java.text.MessageFormat;
34 import java.util.HashSet;
35 import java.util.Iterator;
36 import java.util.Locale;
37 import java.util.MissingResourceException;
38 import java.util.ResourceBundle;
39 import java.util.Set;
40 import java.util.Stack;
41
42 import javax.swing.AbstractButton;
43 import javax.swing.Icon;
44 import javax.swing.InputVerifier;
45 import javax.swing.JComboBox;
46 import javax.swing.JComponent;
47 import javax.swing.JPanel;
48 import javax.swing.JTextField;
49 import javax.swing.event.ChangeEvent;
50 import javax.swing.event.ChangeListener;
51
52 import com.gridsystems.config.Configurator;
53 import com.gridsystems.config.ConfiguratorModel;
54 import com.gridsystems.config.SwingConfiguratorView;
55 import com.gridsystems.config.tools.TextVerifier;
56
57
58
59
60
61
62
63
64
65
66
67
68
69 public class BasePanel extends JPanel implements ActionListener, FocusListener,
70 PropertyChangeListener,
71 SwingConfiguratorView {
72
73
74
75
76 private Set<ChangeListener> listeners = new HashSet<ChangeListener>();
77
78
79
80
81 private boolean modified = false;
82
83
84
85
86 private ResourceBundle bundle;
87
88
89
90
91 private Icon icon;
92
93
94
95
96 private Icon smallIcon;
97
98
99
100
101 private Configurator config;
102
103
104
105
106
107
108 public BasePanel(Configurator config) {
109 super();
110 this.config = config;
111 }
112
113
114
115
116
117
118
119 public BasePanel(LayoutManager layout, Configurator config) {
120 super(layout);
121 this.config = config;
122 }
123
124
125
126
127
128
129 public void setBundle(String name) {
130 Locale locale = Locale.getDefault();
131 this.bundle = ResourceBundle.getBundle(name, locale);
132 }
133
134
135
136
137
138
139 public void setBundle(ResourceBundle bundle) {
140 this.bundle = bundle;
141 }
142
143
144
145
146
147
148 public ResourceBundle getBundle() {
149 return this.bundle;
150 }
151
152
153
154
155
156
157 public Configurator getConfigurator() {
158 return this.config;
159 }
160
161
162
163
164 public Icon getIcon() {
165 return icon;
166 }
167
168
169
170
171
172
173 public void setIcon(Icon icon) {
174 this.icon = icon;
175 }
176
177
178
179
180 public Icon getSmallIcon() {
181 return smallIcon;
182 }
183
184
185
186
187
188
189 public void setSmallIcon(Icon icon) {
190 this.smallIcon = icon;
191 }
192
193
194
195
196 public JComponent getComponent() {
197 return this;
198 }
199
200
201
202
203 public String getTitle() {
204 return getString("config.name");
205 }
206
207
208
209
210 public String getSubtitle() {
211 return getString("config.description");
212 }
213
214
215
216
217 public void getValues(ConfiguratorModel model) {
218 throw new UnsupportedOperationException("Not implemented");
219 }
220
221
222
223
224 public void setValues(ConfiguratorModel model) {
225 throw new UnsupportedOperationException("Not implemented");
226 }
227
228
229
230
231
232
233
234
235 public String getString(String key) {
236 if (bundle == null) {
237 throw new NullPointerException("Bundle not set");
238 }
239
240 try {
241 return bundle.getString(key);
242 } catch (MissingResourceException e) {
243 return null;
244 }
245 }
246
247
248
249
250
251
252
253
254
255
256 public String getString(String key, Object[] params) {
257 if (bundle == null) {
258 throw new NullPointerException("Bundle not set");
259 }
260
261 try {
262 String pattern = bundle.getString(key);
263 return MessageFormat.format(pattern, params);
264 } catch (MissingResourceException e) {
265 return key + "#not found";
266 }
267 }
268
269
270
271
272
273
274 public boolean isModified() {
275 return modified;
276 }
277
278
279
280
281
282
283 public void addChangeListener(ChangeListener listener) {
284 synchronized (listeners) {
285 listeners.add(listener);
286 }
287 }
288
289
290
291
292
293
294
295 protected void setModified(Object src, boolean flag) {
296 this.modified = flag;
297
298 ChangeEvent event = new ChangeEvent(src);
299 synchronized (listeners) {
300 for (Iterator<ChangeListener> it = listeners.iterator(); it.hasNext();) {
301 ChangeListener l = it.next();
302 l.stateChanged(event);
303 }
304 }
305 }
306
307
308
309
310
311
312 protected void watch(JTextField field) {
313 field.addFocusListener(this);
314 }
315
316
317
318
319
320
321 protected void watch(PopupTextField field) {
322 field.addPropertyChangeListener(this);
323 }
324
325
326
327
328
329
330 protected void watch(JComboBox field) {
331 field.addActionListener(this);
332 }
333
334
335
336
337
338
339
340
341
342 protected void setGuarded(AbstractButton btn, JComponent[] guarded) {
343 btn.putClientProperty("guarded", guarded);
344 btn.addActionListener(this);
345 }
346
347
348
349
350
351
352
353
354 public void actionPerformed(ActionEvent event) {
355 Object src = event == null ? this : event.getSource();
356 if (src instanceof AbstractButton) {
357 AbstractButton btn = (AbstractButton)src;
358 boolean selected = btn.isSelected();
359
360 JComponent[] guarded = (JComponent[])btn.getClientProperty("guarded");
361 int count = (guarded == null) ? 0 : guarded.length;
362 for (int i = 0; i < count; i++) {
363 guarded[i].setEnabled(selected);
364 }
365 }
366 setModified(src, true);
367 }
368
369
370
371
372 public void focusGained(FocusEvent e) {
373 Object src = e.getSource();
374 if (src instanceof JTextField) {
375 JTextField field = (JTextField)src;
376 field.putClientProperty("originalText", field.getText());
377 }
378 }
379
380
381
382
383 public void focusLost(FocusEvent e) {
384 Object src = e.getSource();
385 if (src instanceof JTextField) {
386 JTextField field = (JTextField)src;
387 String text = field.getText();
388 String original = (String)field.getClientProperty("originalText");
389
390 InputVerifier verifier = field.getInputVerifier();
391 if (verifier != null) {
392 boolean valid = verifier.verify(field);
393 if (!valid) {
394
395 return;
396 }
397 }
398
399 if (!original.equals(text)) {
400 setModified(field, true);
401 }
402 }
403 }
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421 public void propertyChange(PropertyChangeEvent propertyChangeEvent) {
422 Object source = propertyChangeEvent.getSource();
423 if (source == null) {
424 return;
425 }
426 if (source instanceof PopupTextField) {
427 PopupTextField popupTextField = (PopupTextField) source;
428
429
430
431 if (!popupTextField.getPropertyName().equals(
432 propertyChangeEvent.getPropertyName())) {
433 return;
434 }
435
436 InputVerifier verifier = popupTextField.getInputVerifier();
437 if (verifier != null) {
438 if (verifier instanceof TextVerifier) {
439 TextVerifier textVerifier = (TextVerifier) verifier;
440 String newValueText = popupTextField.getValue() == null
441 ? null : popupTextField.getValue().toString();
442 if (!textVerifier.verify(newValueText)) {
443 popupTextField.discard();
444 return;
445 }
446 } else {
447 if (!verifier.verify(popupTextField)) {
448 popupTextField.setValue(propertyChangeEvent.getOldValue());
449 return;
450 }
451 }
452 }
453 }
454 this.setModified(source, true);
455 }
456
457
458
459
460 public boolean hasErrors() {
461 Stack stack = (Stack)this.getClientProperty("error.messages");
462 return stack != null && stack.size() > 0;
463 }
464 }