View Javadoc

1   /*
2    * Copyright (c) 2003-2008 by Cosylab d. d.
3    *
4    * This file is part of CosyBeans-Common.
5    *
6    * CosyBeans-Common is free software: you can redistribute it and/or modify
7    * it under the terms of the GNU General Public License as published by
8    * the Free Software Foundation, either version 3 of the License, or
9    * (at your option) any later version.
10   *
11   * CosyBeans-Common is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   * GNU General Public License for more details.
15   *
16   * You should have received a copy of the GNU General Public License
17   * along with CosyBeans-Common.  If not, see <http://www.gnu.org/licenses/>.
18   */
19  
20  package com.cosylab.gui.components.introspection;
21  
22  import com.cosylab.util.StringUtilities;
23  
24  /**
25   * A JPanel which allows user to enter class name, which is then
26   * used to create a <code>java.lang.Class</code> instance which is
27   * this bean's property and can be used by application programmers
28   *
29   * Creation date: (9.1.2002 22:04:46)
30   * @author <a href="mailto:miha.kadunc@cosylab.com">Miha Kadunc</a>
31   * @version $id$
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   * @author  Miha Kadunc (miha.kadunc@cosylab.com)
50   */
51  class IvjEventHandler implements java.awt.event.KeyListener, java.beans.PropertyChangeListener {
52  		/**
53  		 * @param e java.awt.event.KeyEvent
54  		 * @see java.awt.event.KeyListener#keyPressed(KeyEvent)
55  		 */
56  		public void keyPressed(java.awt.event.KeyEvent e) {};
57  		/**
58  		 * @param e java.awt.event.KeyEvent
59  		 * @see java.awt.event.KeyListener#keyReleased(KeyEvent)
60  		 */
61  		public void keyReleased(java.awt.event.KeyEvent e) {
62  			if (e.getSource() == ClassEntryPanel.this.getClassNameField()) 
63  				connEtoC1(e);
64  		};
65  		/**
66  		 * @param e java.awt.event.KeyEvent
67  		 * @see java.awt.event.KeyListener#keyTyped(KeyEvent)
68  		 */
69  		public void keyTyped(java.awt.event.KeyEvent e) {};
70  		/**
71  		 * @param evt java.beans.PropertyChangeEvent
72  		 * @see java.beans.PropertyChangeListener#propertyChange(PropertyChangeEvent)
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   * ClassTextField constructor comment.
85   */
86  public ClassEntryPanel() {
87  	super();
88  	initialize();
89  }
90  /**
91   * ClassTextField constructor comment.
92   * @param superClass Class
93   */
94  public ClassEntryPanel(Class superClass) {
95  	this();
96  	setSuperClass(superClass);
97  }
98  /**
99   * Creation date: (9.1.2002 23:37:31)
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  * connEtoC1:  (ClassNameField.key.keyReleased(java.awt.event.KeyEvent) --> ClassTextField.setUserClass(Ljava.lang.Class;)V)
122  * @param arg1 java.awt.event.KeyEvent
123  */
124 /* WARNING: THIS METHOD WILL BE REGENERATED. */
125 private void connEtoC1(java.awt.event.KeyEvent arg1) {
126 	try {
127 		// user code begin {1}
128 		// user code end
129 		checkText();
130 		// user code begin {2}
131 		// user code end
132 	} catch (java.lang.Throwable ivjExc) {
133 		// user code begin {3}
134 		// user code end
135 		handleException(ivjExc);
136 	}
137 }
138 /**
139  * connPtoP1SetTarget:  (ErrorLabel.background <--> ClassTextField.background)
140  */
141 /* WARNING: THIS METHOD WILL BE REGENERATED. */
142 private void connPtoP1SetTarget() {
143 	/* Set the target from the source */
144 	try {
145 		this.setBackground(getErrorLabel().getBackground());
146 		// user code begin {1}
147 		// user code end
148 	} catch (java.lang.Throwable ivjExc) {
149 		// user code begin {3}
150 		// user code end
151 		handleException(ivjExc);
152 	}
153 }
154 /**
155  * connPtoP2SetTarget:  (ClassTextField.background <--> ClassTextFieldLabel.background)
156  */
157 /* WARNING: THIS METHOD WILL BE REGENERATED. */
158 private void connPtoP2SetTarget() {
159 	/* Set the target from the source */
160 	try {
161 		getClassTextFieldLabel().setBackground(this.getBackground());
162 		// user code begin {1}
163 		// user code end
164 	} catch (java.lang.Throwable ivjExc) {
165 		// user code begin {3}
166 		// user code end
167 		handleException(ivjExc);
168 	}
169 }
170 /**
171  * connPtoP3SetTarget:  (ClassTextField.foreground <--> ClassTextFieldLabel.foreground)
172  */
173 /* WARNING: THIS METHOD WILL BE REGENERATED. */
174 private void connPtoP3SetTarget() {
175 	/* Set the target from the source */
176 	try {
177 		getClassTextFieldLabel().setForeground(this.getForeground());
178 		// user code begin {1}
179 		// user code end
180 	} catch (java.lang.Throwable ivjExc) {
181 		// user code begin {3}
182 		// user code end
183 		handleException(ivjExc);
184 	}
185 }
186 /**
187  * Gets the allowInterface property (boolean) value.
188  * @return The allowInterface property value.
189  * @see #setAllowInterface
190  */
191 public boolean getAllowInterface() {
192 	return fieldAllowInterface;
193 }
194 /**
195  * 
196  */
197 /* WARNING: THIS METHOD WILL BE REGENERATED. */
198 /**
199  * Method generated to support the promotion of the caption attribute.
200  * 
201  * @return java.lang.String
202  */
203 public java.lang.String getCaption() {
204 	return getClassTextFieldLabel().getText();
205 }
206 /**
207  * Return the ClassNameField property value.
208  * @return javax.swing.JTextField
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  * Return the ClassTextFieldLabel property value.
226  * @return javax.swing.JLabel
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  * Method generated to support the promotion of the errorColor attribute.
243  * 
244  * @return java.awt.Color
245  */
246 public java.awt.Color getErrorColor() {
247 	return getErrorLabel().getForeground();
248 }
249 /**
250  * Return the ErrorLabel property value.
251  * @return javax.swing.JLabel
252  */
253 /* WARNING: THIS METHOD WILL BE REGENERATED. */
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 			// user code begin {1}
263 			// user code end
264 		} catch (java.lang.Throwable ivjExc) {
265 			// user code begin {2}
266 			// user code end
267 			handleException(ivjExc);
268 		}
269 	}
270 	return ivjErrorLabel;
271 }
272 /**
273  * Method generated to support the promotion of the showCaption attribute.
274  * 
275  * @return boolean
276  */
277 public boolean getShowCaption() {
278 	return getClassTextFieldLabel().isVisible();
279 }
280 /**
281  * Gets the showErrorLabel property (boolean) value.
282  * 
283  * @return The showErrorLabel property value.
284  * @see #setShowErrorLabel
285  */
286 public boolean getShowErrorLabel() {
287 	return getErrorLabel().isVisible();
288 }
289 /**
290  * Gets the superClass property (java.lang.Class) value.
291  * 
292  * @return The superClass property value.
293  * @see #setSuperClass
294  */
295 public java.lang.Class getSuperClass() {
296 	return fieldSuperClass;
297 }
298 /**
299  * Gets the userClass property (java.lang.Class) value.
300  * 
301  * @return The userClass property value.
302  * @see #setUserClass
303  */
304 public java.lang.Class getUserClass() {
305 	return fieldUserClass;
306 }
307 /**
308  * Called whenever the part throws an exception.
309  * 
310  * @param exception java.lang.Throwable
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  * Initializes connections
342  * @exception java.lang.Exception The exception description.
343  */
344 /* WARNING: THIS METHOD WILL BE REGENERATED. */
345 private void initConnections() throws java.lang.Exception {
346 	// user code begin {1}
347 	// user code end
348 	getClassNameField().addKeyListener(ivjEventHandler);
349 	getErrorLabel().addPropertyChangeListener(ivjEventHandler);
350 	this.addPropertyChangeListener(ivjEventHandler);
351 	connPtoP1SetTarget();
352 	connPtoP2SetTarget();
353 	connPtoP3SetTarget();
354 }
355 /**
356  * Initialize the class.
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  * Starts the application.
390  * 
391  * @param args an array of command-line arguments
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  * Method generated to support the promotion of the errorColor attribute.
419  * 
420  * @param arg1 java.awt.Color
421  */
422 private void paintField(boolean isError) {
423     if (isError && !getShowErrorLabel()) getClassNameField().setForeground(getErrorColor());
424     else getClassNameField().setForeground(getForeground());
425 }
426 /**
427  * Sets the allowInterface property (boolean) value.
428  * 
429  * @param allowInterface The new value for the property.
430  * @see #getAllowInterface
431  */
432 public void setAllowInterface(boolean allowInterface) {
433 	fieldAllowInterface = allowInterface;
434 }
435 /**
436  * Method generated to support the promotion of the caption attribute.
437  * 
438  * @param arg1 java.lang.String
439  */
440 public void setCaption(java.lang.String arg1) {
441 	getClassTextFieldLabel().setText(arg1);
442 }
443 /**
444  * Method generated to support the promotion of the errorColor attribute.
445  * 
446  * @param arg1 java.awt.Color
447  */
448 public void setErrorColor(java.awt.Color arg1) {
449 	getErrorLabel().setForeground(arg1);
450 }
451 /**
452  * Method generated to support the promotion of the showCaption attribute.
453  * 
454  * @param arg1 boolean
455  */
456 public void setShowCaption(boolean arg1) {
457 	getClassTextFieldLabel().setVisible(arg1);
458 }
459 /**
460  * Sets the showErrorLabel property (boolean) value.
461  * 
462  * @param showErrorLabel The new value for the property.
463  * @see #getShowErrorLabel
464  */
465 public void setShowErrorLabel(boolean showErrorLabel) {
466 	getErrorLabel().setVisible(showErrorLabel);
467 }
468 /**
469  * Sets the superClass property (java.lang.Class) value.
470  * 
471  * @param superClass The new value for the property.
472  * @see #getSuperClass
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  * Sets the userClass property (java.lang.Class) value.
485  * @param userClass The new value for the property.
486  * @see #getUserClass
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 }