1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package com.cosylab.gui.components.introspection;
21
22 import com.cosylab.util.StringUtilities;
23
24
25
26
27
28
29
30
31
32
33
34 public class ClassEntryPanel extends javax.swing.JPanel {
35
36 private static final long serialVersionUID = 1L;
37 private javax.swing.JTextField ivjClassNameField = null;
38 private javax.swing.JLabel ivjClassTextFieldLabel = null;
39 private java.lang.Class fieldUserClass = null;
40 private java.lang.Class fieldSuperClass = null;
41 private boolean fieldAllowInterface = true;
42 private javax.swing.JLabel ivjErrorLabel = null;
43
44
45 IvjEventHandler ivjEventHandler = new IvjEventHandler();
46
47
48
49
50
51 class IvjEventHandler implements java.awt.event.KeyListener, java.beans.PropertyChangeListener {
52
53
54
55
56 public void keyPressed(java.awt.event.KeyEvent e) {};
57
58
59
60
61 public void keyReleased(java.awt.event.KeyEvent e) {
62 if (e.getSource() == ClassEntryPanel.this.getClassNameField())
63 connEtoC1(e);
64 };
65
66
67
68
69 public void keyTyped(java.awt.event.KeyEvent e) {};
70
71
72
73
74 public void propertyChange(java.beans.PropertyChangeEvent evt) {
75 if (evt.getSource() == ClassEntryPanel.this.getErrorLabel() && (evt.getPropertyName().equals("background")))
76 connPtoP1SetTarget();
77 if (evt.getSource() == ClassEntryPanel.this && (evt.getPropertyName().equals("background")))
78 connPtoP2SetTarget();
79 if (evt.getSource() == ClassEntryPanel.this && (evt.getPropertyName().equals("foreground")))
80 connPtoP3SetTarget();
81 };
82 };
83
84
85
86 public ClassEntryPanel() {
87 super();
88 initialize();
89 }
90
91
92
93
94 public ClassEntryPanel(Class superClass) {
95 this();
96 setSuperClass(superClass);
97 }
98
99
100
101 private boolean checkClass(Class c) {
102 if (c == null)
103 return true;
104 if ((fieldSuperClass != null) && (!(fieldSuperClass.isAssignableFrom(c)))) {
105 getErrorLabel().setText("Class is not a " + fieldSuperClass.getName());
106 return false;
107 }
108 if (!(fieldAllowInterface) && (c.isInterface())) {
109 getErrorLabel().setText("Interfaces are not supported");
110 return false;
111 }
112 getErrorLabel().setText(" ");
113 getErrorLabel().repaint();
114 return true;
115 }
116
117 private void checkText() throws Exception{
118 this.setUserClass(StringUtilities.classFromString(getClassNameField().getText()));
119 }
120
121
122
123
124
125 private void connEtoC1(java.awt.event.KeyEvent arg1) {
126 try {
127
128
129 checkText();
130
131
132 } catch (java.lang.Throwable ivjExc) {
133
134
135 handleException(ivjExc);
136 }
137 }
138
139
140
141
142 private void connPtoP1SetTarget() {
143
144 try {
145 this.setBackground(getErrorLabel().getBackground());
146
147
148 } catch (java.lang.Throwable ivjExc) {
149
150
151 handleException(ivjExc);
152 }
153 }
154
155
156
157
158 private void connPtoP2SetTarget() {
159
160 try {
161 getClassTextFieldLabel().setBackground(this.getBackground());
162
163
164 } catch (java.lang.Throwable ivjExc) {
165
166
167 handleException(ivjExc);
168 }
169 }
170
171
172
173
174 private void connPtoP3SetTarget() {
175
176 try {
177 getClassTextFieldLabel().setForeground(this.getForeground());
178
179
180 } catch (java.lang.Throwable ivjExc) {
181
182
183 handleException(ivjExc);
184 }
185 }
186
187
188
189
190
191 public boolean getAllowInterface() {
192 return fieldAllowInterface;
193 }
194
195
196
197
198
199
200
201
202
203 public java.lang.String getCaption() {
204 return getClassTextFieldLabel().getText();
205 }
206
207
208
209
210 private javax.swing.JTextField getClassNameField() {
211 if (ivjClassNameField == null) {
212 try {
213 ivjClassNameField = new javax.swing.JTextField();
214 ivjClassNameField.setName("ClassNameField");
215 ivjClassNameField.setPreferredSize(new java.awt.Dimension(20, 20));
216 ivjClassNameField.setText("");
217 ivjClassNameField.setMinimumSize(new java.awt.Dimension(100, 20));
218 } catch (java.lang.Throwable ivjExc) {
219 handleException(ivjExc);
220 }
221 }
222 return ivjClassNameField;
223 }
224
225
226
227
228 private javax.swing.JLabel getClassTextFieldLabel() {
229 if (ivjClassTextFieldLabel == null) {
230 try {
231 ivjClassTextFieldLabel = new javax.swing.JLabel();
232 ivjClassTextFieldLabel.setName("ClassTextFieldLabel");
233 ivjClassTextFieldLabel.setText("Enter class name");
234 ivjClassTextFieldLabel.setForeground(java.awt.Color.black);
235 } catch (java.lang.Throwable ivjExc) {
236 handleException(ivjExc);
237 }
238 }
239 return ivjClassTextFieldLabel;
240 }
241
242
243
244
245
246 public java.awt.Color getErrorColor() {
247 return getErrorLabel().getForeground();
248 }
249
250
251
252
253
254 private javax.swing.JLabel getErrorLabel() {
255 if (ivjErrorLabel == null) {
256 try {
257 ivjErrorLabel = new javax.swing.JLabel();
258 ivjErrorLabel.setName("ErrorLabel");
259 ivjErrorLabel.setFont(new java.awt.Font("dialog", 0, 12));
260 ivjErrorLabel.setText("Class not found");
261 ivjErrorLabel.setForeground(java.awt.Color.red);
262
263
264 } catch (java.lang.Throwable ivjExc) {
265
266
267 handleException(ivjExc);
268 }
269 }
270 return ivjErrorLabel;
271 }
272
273
274
275
276
277 public boolean getShowCaption() {
278 return getClassTextFieldLabel().isVisible();
279 }
280
281
282
283
284
285
286 public boolean getShowErrorLabel() {
287 return getErrorLabel().isVisible();
288 }
289
290
291
292
293
294
295 public java.lang.Class getSuperClass() {
296 return fieldSuperClass;
297 }
298
299
300
301
302
303
304 public java.lang.Class getUserClass() {
305 return fieldUserClass;
306 }
307
308
309
310
311
312 private void handleException(java.lang.Throwable exception) {
313
314 if ((exception instanceof ClassNotFoundException) || (exception instanceof NoClassDefFoundError)) {
315 paintField(true);
316 if (fieldUserClass!=null) setUserClass(null);
317 getErrorLabel().setText("Class not found");
318 }
319 else if (exception instanceof NoClassDefFoundError) {
320 paintField(true);
321 if (fieldUserClass!=null) setUserClass(null);
322 getErrorLabel().setText("Class definition not found");
323 }
324 else if (exception instanceof ClassFormatError) {
325 paintField(true);
326 if (fieldUserClass!=null) setUserClass(null);
327 getErrorLabel().setText("Class format error");
328 }
329 else {
330 paintField(true);
331 if (fieldUserClass!=null) setUserClass(null);
332 if (exception.getCause()!=null) {
333 handleException(exception.getCause());
334 }
335 else {
336 getErrorLabel().setText("Exception: "+exception.toString());
337 }
338 }
339 }
340
341
342
343
344
345 private void initConnections() throws java.lang.Exception {
346
347
348 getClassNameField().addKeyListener(ivjEventHandler);
349 getErrorLabel().addPropertyChangeListener(ivjEventHandler);
350 this.addPropertyChangeListener(ivjEventHandler);
351 connPtoP1SetTarget();
352 connPtoP2SetTarget();
353 connPtoP3SetTarget();
354 }
355
356
357
358 private void initialize() {
359 try {
360 setName("ClassEntryPanel");
361 setLayout(new java.awt.GridBagLayout());
362 setSize(326, 57);
363
364 java.awt.GridBagConstraints constraintsClassNameField = new java.awt.GridBagConstraints();
365 constraintsClassNameField.gridx = 0; constraintsClassNameField.gridy = 1;
366 constraintsClassNameField.fill = java.awt.GridBagConstraints.HORIZONTAL;
367 constraintsClassNameField.weightx = 1.0;
368 constraintsClassNameField.insets = new java.awt.Insets(2, 4, 2, 4);
369 add(getClassNameField(), constraintsClassNameField);
370
371 java.awt.GridBagConstraints constraintsClassTextFieldLabel = new java.awt.GridBagConstraints();
372 constraintsClassTextFieldLabel.gridx = 0; constraintsClassTextFieldLabel.gridy = 0;
373 constraintsClassTextFieldLabel.fill = java.awt.GridBagConstraints.BOTH;
374 constraintsClassTextFieldLabel.insets = new java.awt.Insets(0, 4, 0, 4);
375 add(getClassTextFieldLabel(), constraintsClassTextFieldLabel);
376
377 java.awt.GridBagConstraints constraintsErrorLabel = new java.awt.GridBagConstraints();
378 constraintsErrorLabel.gridx = 0; constraintsErrorLabel.gridy = 2;
379 constraintsErrorLabel.fill = java.awt.GridBagConstraints.BOTH;
380 constraintsErrorLabel.insets = new java.awt.Insets(0, 4, 0, 4);
381 add(getErrorLabel(), constraintsErrorLabel);
382 initConnections();
383 } catch (java.lang.Throwable ivjExc) {
384 handleException(ivjExc);
385 }
386 }
387
388
389
390
391
392
393 public static void main(java.lang.String[] args) {
394 try {
395 javax.swing.JFrame frame = new javax.swing.JFrame();
396 ClassEntryPanel aCalculationSettingsEditor;
397 aCalculationSettingsEditor = new ClassEntryPanel(javax.swing.table.TableModel.class);
398 aCalculationSettingsEditor.setAllowInterface(false);
399 aCalculationSettingsEditor.setCaption("Enter a name of TableModel implementation");
400 aCalculationSettingsEditor.setShowErrorLabel(false);
401 frame.setContentPane(aCalculationSettingsEditor);
402 frame.setSize(aCalculationSettingsEditor.getSize());
403 frame.addWindowListener(new java.awt.event.WindowAdapter() {
404 public void windowClosing(java.awt.event.WindowEvent e) {
405 System.exit(0);
406 };
407 });
408 frame.setVisible(true);
409 java.awt.Insets insets = frame.getInsets();
410 frame.setSize(frame.getWidth() + insets.left + insets.right, frame.getHeight() + insets.top + insets.bottom);
411 frame.setVisible(true);
412 } catch (Throwable exception) {
413 System.err.println("Exception occurred in main() of javax.swing.JPanel");
414 exception.printStackTrace(System.out);
415 }
416 }
417
418
419
420
421
422 private void paintField(boolean isError) {
423 if (isError && !getShowErrorLabel()) getClassNameField().setForeground(getErrorColor());
424 else getClassNameField().setForeground(getForeground());
425 }
426
427
428
429
430
431
432 public void setAllowInterface(boolean allowInterface) {
433 fieldAllowInterface = allowInterface;
434 }
435
436
437
438
439
440 public void setCaption(java.lang.String arg1) {
441 getClassTextFieldLabel().setText(arg1);
442 }
443
444
445
446
447
448 public void setErrorColor(java.awt.Color arg1) {
449 getErrorLabel().setForeground(arg1);
450 }
451
452
453
454
455
456 public void setShowCaption(boolean arg1) {
457 getClassTextFieldLabel().setVisible(arg1);
458 }
459
460
461
462
463
464
465 public void setShowErrorLabel(boolean showErrorLabel) {
466 getErrorLabel().setVisible(showErrorLabel);
467 }
468
469
470
471
472
473
474 public void setSuperClass(java.lang.Class superClass) {
475 try{
476 fieldSuperClass = superClass;
477 checkText();
478 }
479 catch(Exception e){
480 handleException(e);
481 }
482 }
483
484
485
486
487
488 private void setUserClass(java.lang.Class userClass) {
489 if (checkClass(userClass)){
490 Class oldValue = fieldUserClass;
491 if (userClass!=null){
492 paintField(false);
493 }
494 if (!((""+oldValue).equals(""+userClass))) {
495 fieldUserClass = userClass;
496 firePropertyChange("userClass", oldValue, fieldUserClass);
497 }
498 }
499 else{
500 paintField(true);
501 if (fieldUserClass!=null) setUserClass(null);
502 }
503 }
504 }