KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > beaninfo > editors > RectangleCustomEditor


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.beaninfo.editors;
21
22 import java.awt.Color JavaDoc;
23 import java.awt.Component JavaDoc;
24 import java.awt.Rectangle JavaDoc;
25 import java.awt.event.KeyListener JavaDoc;
26 import java.beans.PropertyChangeEvent JavaDoc;
27 import java.beans.PropertyChangeListener JavaDoc;
28 import java.util.HashMap JavaDoc;
29 import java.util.ResourceBundle JavaDoc;
30 import javax.swing.JLabel JavaDoc;
31 import javax.swing.JTextField JavaDoc;
32 import javax.swing.UIManager JavaDoc;
33 import org.netbeans.core.UIExceptions;
34 import org.openide.awt.Mnemonics;
35 import org.openide.explorer.propertysheet.PropertyEnv;
36 import org.openide.util.NbBundle;
37
38 /**
39 * @author Ian Formanek
40 */

41 public class RectangleCustomEditor extends javax.swing.JPanel JavaDoc
42 implements KeyListener JavaDoc, PropertyChangeListener JavaDoc {
43     static final long serialVersionUID =-9015667991684634296L;
44    
45     private HashMap JavaDoc<JTextField JavaDoc, JLabel JavaDoc> labelMap = new HashMap JavaDoc<JTextField JavaDoc, JLabel JavaDoc>();
46     private PropertyEnv env;
47     /** Initializes the Form */
48     public RectangleCustomEditor(RectangleEditor editor, PropertyEnv env) {
49         this.env=env;
50         initComponents ();
51         this.editor = editor;
52         Rectangle JavaDoc rectangle = (Rectangle JavaDoc)editor.getValue ();
53         if (rectangle == null) rectangle = new Rectangle JavaDoc (0, 0, 0, 0);
54         xField.setText (Integer.toString(rectangle.x)); // NOI18N
55
yField.setText (Integer.toString(rectangle.y)); // NOI18N
56
widthField.setText (Integer.toString(rectangle.width)); // NOI18N
57
heightField.setText (Integer.toString(rectangle.height)); // NOI18N
58

59         ResourceBundle JavaDoc b = NbBundle.getBundle(RectangleCustomEditor.class);
60         setBorder (new javax.swing.border.EmptyBorder JavaDoc (new java.awt.Insets JavaDoc(5, 5, 5, 5)));
61         jPanel2.setBorder (new javax.swing.border.CompoundBorder JavaDoc (
62                                new javax.swing.border.TitledBorder JavaDoc (
63                                    new javax.swing.border.EtchedBorder JavaDoc (),
64                                    " " + b.getString ("CTL_Rectangle") + " "),
65                                new javax.swing.border.EmptyBorder JavaDoc (new java.awt.Insets JavaDoc(5, 5, 5, 5))));
66
67         Mnemonics.setLocalizedText(xLabel, b.getString ("CTL_X"));
68         Mnemonics.setLocalizedText(yLabel, b.getString ("CTL_Y"));
69         Mnemonics.setLocalizedText(widthLabel, b.getString ("CTL_Width"));
70         Mnemonics.setLocalizedText(heightLabel, b.getString ("CTL_Height"));
71
72         xLabel.setLabelFor(xField);
73         yLabel.setLabelFor(yField);
74         widthLabel.setLabelFor(widthField);
75         heightLabel.setLabelFor(heightField);
76
77         xField.getAccessibleContext().setAccessibleDescription(b.getString ("ACSD_CTL_X"));
78         yField.getAccessibleContext().setAccessibleDescription(b.getString ("ACSD_CTL_Y"));
79         widthField.getAccessibleContext().setAccessibleDescription(b.getString ("ACSD_CTL_Width"));
80         heightField.getAccessibleContext().setAccessibleDescription(b.getString ("ACSD_CTL_Height"));
81         
82         getAccessibleContext().setAccessibleDescription(b.getString ("ACSD_CustomRectangleEditor"));
83
84         labelMap.put(widthField,widthLabel);
85         labelMap.put(xField,xLabel);
86         labelMap.put(yField,yLabel);
87         labelMap.put(heightField,heightLabel);
88 // HelpCtx.setHelpIDString (this, RectangleCustomEditor.class.getName ());
89

90         env.setState(PropertyEnv.STATE_NEEDS_VALIDATION);
91         env.addPropertyChangeListener(this);
92     }
93
94     public java.awt.Dimension JavaDoc getPreferredSize () {
95         return new java.awt.Dimension JavaDoc (280, 160);
96     }
97
98     private Object JavaDoc getPropertyValue () throws IllegalStateException JavaDoc {
99         try {
100             int x = Integer.parseInt (xField.getText ());
101             int y = Integer.parseInt (yField.getText ());
102             int width = Integer.parseInt (widthField.getText ());
103             int height = Integer.parseInt (heightField.getText ());
104             if ((x < 0) || (y < 0) || (width < 0) || (height < 0)) {
105                 IllegalStateException JavaDoc ise = new IllegalStateException JavaDoc();
106                 UIExceptions.annotateUser(ise, null,
107                                          NbBundle.getMessage(RectangleCustomEditor.class,
108                                                              "CTL_NegativeSize"),
109                                          null, null);
110                 throw ise;
111             }
112             return new Rectangle JavaDoc (x, y, width, height);
113         } catch (NumberFormatException JavaDoc e) {
114             IllegalStateException JavaDoc ise = new IllegalStateException JavaDoc();
115             UIExceptions.annotateUser(ise, null,
116                                      NbBundle.getMessage(RectangleCustomEditor.class,
117                                                          "CTL_InvalidValue"),
118                                      null, null);
119             throw ise;
120         }
121     }
122
123
124     private void initComponents () {
125         setLayout (new java.awt.BorderLayout JavaDoc ());
126
127         jPanel2 = new javax.swing.JPanel JavaDoc ();
128         jPanel2.setLayout (new java.awt.GridBagLayout JavaDoc ());
129         java.awt.GridBagConstraints JavaDoc gridBagConstraints1;
130
131         xLabel = new javax.swing.JLabel JavaDoc ();
132         xLabel.setText (null);
133
134         gridBagConstraints1 = new java.awt.GridBagConstraints JavaDoc ();
135         gridBagConstraints1.anchor = java.awt.GridBagConstraints.WEST;
136         jPanel2.add (xLabel, gridBagConstraints1);
137
138         xField = new javax.swing.JTextField JavaDoc ();
139         xField.addKeyListener (this);
140         
141         gridBagConstraints1 = new java.awt.GridBagConstraints JavaDoc ();
142         gridBagConstraints1.gridwidth = 0;
143         gridBagConstraints1.fill = java.awt.GridBagConstraints.HORIZONTAL;
144         gridBagConstraints1.insets = new java.awt.Insets JavaDoc (4, 8, 4, 0);
145         gridBagConstraints1.weightx = 1.0;
146         jPanel2.add (xField, gridBagConstraints1);
147
148         yLabel = new javax.swing.JLabel JavaDoc ();
149         yLabel.setText (null);
150
151         gridBagConstraints1 = new java.awt.GridBagConstraints JavaDoc ();
152         gridBagConstraints1.anchor = java.awt.GridBagConstraints.WEST;
153         jPanel2.add (yLabel, gridBagConstraints1);
154
155         yField = new javax.swing.JTextField JavaDoc ();
156         yField.addKeyListener(this);
157
158         gridBagConstraints1 = new java.awt.GridBagConstraints JavaDoc ();
159         gridBagConstraints1.gridwidth = 0;
160         gridBagConstraints1.fill = java.awt.GridBagConstraints.HORIZONTAL;
161         gridBagConstraints1.insets = new java.awt.Insets JavaDoc (4, 8, 4, 0);
162         gridBagConstraints1.weightx = 1.0;
163         jPanel2.add (yField, gridBagConstraints1);
164
165         widthLabel = new javax.swing.JLabel JavaDoc ();
166         widthLabel.setText (null);
167
168         gridBagConstraints1 = new java.awt.GridBagConstraints JavaDoc ();
169         gridBagConstraints1.anchor = java.awt.GridBagConstraints.WEST;
170         jPanel2.add (widthLabel, gridBagConstraints1);
171
172         widthField = new javax.swing.JTextField JavaDoc ();
173         widthField.addKeyListener(this);
174         
175         gridBagConstraints1 = new java.awt.GridBagConstraints JavaDoc ();
176         gridBagConstraints1.gridwidth = 0;
177         gridBagConstraints1.fill = java.awt.GridBagConstraints.HORIZONTAL;
178         gridBagConstraints1.insets = new java.awt.Insets JavaDoc (4, 8, 4, 0);
179         gridBagConstraints1.weightx = 1.0;
180         jPanel2.add (widthField, gridBagConstraints1);
181
182         heightLabel = new javax.swing.JLabel JavaDoc ();
183         heightLabel.setText (null);
184
185         gridBagConstraints1 = new java.awt.GridBagConstraints JavaDoc ();
186         gridBagConstraints1.anchor = java.awt.GridBagConstraints.WEST;
187         jPanel2.add (heightLabel, gridBagConstraints1);
188
189         heightField = new javax.swing.JTextField JavaDoc ();
190         heightField.addKeyListener(this);
191
192         gridBagConstraints1 = new java.awt.GridBagConstraints JavaDoc ();
193         gridBagConstraints1.gridwidth = 0;
194         gridBagConstraints1.fill = java.awt.GridBagConstraints.HORIZONTAL;
195         gridBagConstraints1.insets = new java.awt.Insets JavaDoc (4, 8, 4, 0);
196         gridBagConstraints1.weightx = 1.0;
197         jPanel2.add (heightField, gridBagConstraints1);
198
199
200         add (jPanel2, "Center"); // NOI18N
201

202     }
203
204
205     private void updateRectangle () {
206         try {
207             int x = Integer.parseInt (xField.getText ());
208             int y = Integer.parseInt (yField.getText ());
209             int width = Integer.parseInt (widthField.getText ());
210             int height = Integer.parseInt (heightField.getText ());
211             editor.setValue (new Rectangle JavaDoc (x, y, width, height));
212         } catch (NumberFormatException JavaDoc e) {
213             // [PENDING beep]
214
}
215     }
216
217     public void keyPressed(java.awt.event.KeyEvent JavaDoc e) {
218     }
219
220     public void keyReleased(java.awt.event.KeyEvent JavaDoc e) {
221         if (checkValues()) {
222             updateRectangle();
223         }
224     }
225     
226     public void keyTyped(java.awt.event.KeyEvent JavaDoc e) {
227     }
228     
229     private boolean checkValues() {
230         Component JavaDoc[] c = jPanel2.getComponents();
231         boolean valid=true;
232         for (int i=0; i < c.length; i++) {
233             if (c[i] instanceof JTextField JavaDoc) {
234                 valid &= validFor((JTextField JavaDoc) c[i]);
235             }
236         }
237         if (env != null) {
238            env.setState(valid ? env.STATE_NEEDS_VALIDATION : env.STATE_INVALID);
239         }
240         return valid;
241     }
242     
243     private boolean validFor(JTextField JavaDoc c) {
244         String JavaDoc s = c.getText().trim();
245         try {
246             Integer.parseInt(s);
247             handleValid(c);
248             return true;
249         } catch (NumberFormatException JavaDoc e) {
250             handleInvalid(c);
251             return false;
252         }
253     }
254     
255     private void handleInvalid(JTextField JavaDoc c) {
256         c.setForeground(getErrorColor());
257         findLabelFor(c).setForeground(getErrorColor());
258     }
259     
260     private void handleValid(JTextField JavaDoc c) {
261         c.setForeground(getForeground());
262         findLabelFor(c).setForeground(getForeground());
263     }
264     
265     private Color JavaDoc getErrorColor() {
266         Color JavaDoc c=UIManager.getColor("nb.errorForeground");
267         if (c == null) {
268             c = Color.RED;
269         }
270         return c;
271     }
272     
273     private JLabel JavaDoc findLabelFor(JTextField JavaDoc c) {
274         return labelMap.get(c);
275     }
276
277     public void propertyChange(PropertyChangeEvent JavaDoc evt) {
278         if (PropertyEnv.PROP_STATE.equals(evt.getPropertyName()) && evt.getNewValue() == PropertyEnv.STATE_VALID) {
279             editor.setValue(getPropertyValue());
280         }
281     }
282     
283     // Variables declaration - do not modify
284
private javax.swing.JPanel JavaDoc jPanel2;
285     private javax.swing.JLabel JavaDoc xLabel;
286     private javax.swing.JTextField JavaDoc xField;
287     private javax.swing.JLabel JavaDoc yLabel;
288     private javax.swing.JTextField JavaDoc yField;
289     private javax.swing.JLabel JavaDoc widthLabel;
290     private javax.swing.JTextField JavaDoc widthField;
291     private javax.swing.JLabel JavaDoc heightLabel;
292     private javax.swing.JTextField JavaDoc heightField;
293     // End of variables declaration
294

295     private RectangleEditor editor;
296
297 }
298
299
Popular Tags