KickJava   Java API By Example, From Geeks To Geeks.

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


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.Insets 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 javax.swing.JLabel JavaDoc;
30 import javax.swing.JTextField JavaDoc;
31 import javax.swing.UIManager JavaDoc;
32 import org.netbeans.core.UIExceptions;
33 import org.openide.awt.Mnemonics;
34 import org.openide.explorer.propertysheet.PropertyEnv;
35 import org.openide.util.NbBundle;
36
37 /**
38 *
39 * @author Ian Formanek
40 * @version 1.00, 01 Sep 1998
41 */

42 public class InsetsCustomEditor extends javax.swing.JPanel JavaDoc implements PropertyChangeListener JavaDoc, KeyListener JavaDoc {
43     static final long serialVersionUID =-1472891501739636852L;
44    
45     //XXX this is just a copy of RectangleEditor with the fields and value
46
//type changed. A proper solution would be have an equivalent of
47
//ArrayOfIntSupport for custom editors, which would just produce the
48
//required number of fields
49

50     private HashMap JavaDoc<JTextField JavaDoc,JLabel JavaDoc> labelMap = new HashMap JavaDoc<JTextField JavaDoc,JLabel JavaDoc>();
51     private PropertyEnv env;
52     /** Initializes the Form */
53     public InsetsCustomEditor(InsetsEditor editor, PropertyEnv env) {
54         this.env=env;
55         this.env.setState(PropertyEnv.STATE_NEEDS_VALIDATION);
56         this.env.addPropertyChangeListener(this);
57
58         initComponents ();
59         this.editor = editor;
60         Insets JavaDoc insets = (Insets JavaDoc)editor.getValue ();
61         if (insets == null) insets = new Insets JavaDoc (0, 0, 0, 0);
62         xField.setText (Integer.toString(insets.top)); // NOI18N
63
yField.setText (Integer.toString(insets.left)); // NOI18N
64
widthField.setText (Integer.toString(insets.bottom)); // NOI18N
65
heightField.setText (Integer.toString(insets.right)); // NOI18N
66

67         setBorder (new javax.swing.border.EmptyBorder JavaDoc (new Insets JavaDoc(5, 5, 5, 5)));
68         jPanel2.setBorder (new javax.swing.border.CompoundBorder JavaDoc (
69                                new javax.swing.border.TitledBorder JavaDoc (
70                                    new javax.swing.border.EtchedBorder JavaDoc (),
71                                    " " + NbBundle.getMessage (InsetsCustomEditor.class, "CTL_Insets") + " "),
72                                new javax.swing.border.EmptyBorder JavaDoc (new java.awt.Insets JavaDoc(5, 5, 5, 5))));
73
74         Mnemonics.setLocalizedText(xLabel, NbBundle.getMessage (InsetsCustomEditor.class, "CTL_Top"));
75         Mnemonics.setLocalizedText(yLabel, NbBundle.getMessage (InsetsCustomEditor.class, "CTL_Left"));
76         Mnemonics.setLocalizedText(widthLabel, NbBundle.getMessage (InsetsCustomEditor.class, "CTL_Bottom"));
77         Mnemonics.setLocalizedText(heightLabel, NbBundle.getMessage (InsetsCustomEditor.class, "CTL_Right"));
78
79         xLabel.setLabelFor(xField);
80         yLabel.setLabelFor(yField);
81         widthLabel.setLabelFor(widthField);
82         heightLabel.setLabelFor(heightField);
83
84
85         xField.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage (InsetsCustomEditor.class, "ACSD_CTL_Top"));
86         yField.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage (InsetsCustomEditor.class, "ACSD_CTL_Left"));
87         widthField.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage (InsetsCustomEditor.class, "ACSD_CTL_Bottom"));
88         heightField.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage (InsetsCustomEditor.class, "ACSD_CTL_Right"));
89         
90         getAccessibleContext().setAccessibleDescription(NbBundle.getMessage (InsetsCustomEditor.class, "ACSD_CustomRectangleEditor"));
91
92         labelMap.put(widthField,widthLabel);
93         labelMap.put(xField,xLabel);
94         labelMap.put(yField,yLabel);
95         labelMap.put(heightField,heightLabel);
96     }
97
98     public java.awt.Dimension JavaDoc getPreferredSize () {
99         return new java.awt.Dimension JavaDoc (280, 160);
100     }
101
102     private Object JavaDoc getPropertyValue () throws IllegalStateException JavaDoc {
103         try {
104             int x = Integer.parseInt (xField.getText ());
105             int y = Integer.parseInt (yField.getText ());
106             int width = Integer.parseInt (widthField.getText ());
107             int height = Integer.parseInt (heightField.getText ());
108             if ((x < 0) || (y < 0) || (width < 0) || (height < 0)) {
109                 IllegalStateException JavaDoc ise = new IllegalStateException JavaDoc();
110                 UIExceptions.annotateUser(ise, null,
111                                          NbBundle.getMessage(InsetsCustomEditor.class,
112                                                              "CTL_NegativeSize"),
113                                          null, null);
114                 throw ise;
115             }
116             return new Insets JavaDoc (x, y, width, height);
117         } catch (NumberFormatException JavaDoc e) {
118             IllegalStateException JavaDoc ise = new IllegalStateException JavaDoc();
119             UIExceptions.annotateUser(ise, null,
120                                      NbBundle.getMessage(InsetsCustomEditor.class,
121                                                          "CTL_InvalidValue"),
122                                      null, null);
123             throw ise;
124         }
125     }
126
127     public void propertyChange(PropertyChangeEvent JavaDoc evt) {
128         if (PropertyEnv.PROP_STATE.equals(evt.getPropertyName()) && evt.getNewValue() == PropertyEnv.STATE_VALID) {
129             editor.setValue(getPropertyValue());
130         }
131     }
132     
133
134     private void initComponents () {
135         setLayout (new java.awt.BorderLayout JavaDoc ());
136
137         jPanel2 = new javax.swing.JPanel JavaDoc ();
138         jPanel2.setLayout (new java.awt.GridBagLayout JavaDoc ());
139         java.awt.GridBagConstraints JavaDoc gridBagConstraints1;
140
141         xLabel = new javax.swing.JLabel JavaDoc ();
142         xLabel.setText (null);
143
144         gridBagConstraints1 = new java.awt.GridBagConstraints JavaDoc ();
145         gridBagConstraints1.anchor = java.awt.GridBagConstraints.WEST;
146         jPanel2.add (xLabel, gridBagConstraints1);
147
148         xField = new javax.swing.JTextField JavaDoc ();
149         xField.addKeyListener (this);
150         
151         gridBagConstraints1 = new java.awt.GridBagConstraints JavaDoc ();
152         gridBagConstraints1.gridwidth = 0;
153         gridBagConstraints1.fill = java.awt.GridBagConstraints.HORIZONTAL;
154         gridBagConstraints1.insets = new java.awt.Insets JavaDoc (4, 8, 4, 0);
155         gridBagConstraints1.weightx = 1.0;
156         jPanel2.add (xField, gridBagConstraints1);
157
158         yLabel = new javax.swing.JLabel JavaDoc ();
159         yLabel.setText (null);
160
161         gridBagConstraints1 = new java.awt.GridBagConstraints JavaDoc ();
162         gridBagConstraints1.anchor = java.awt.GridBagConstraints.WEST;
163         jPanel2.add (yLabel, gridBagConstraints1);
164
165         yField = new javax.swing.JTextField JavaDoc ();
166         yField.addKeyListener(this);
167
168         gridBagConstraints1 = new java.awt.GridBagConstraints JavaDoc ();
169         gridBagConstraints1.gridwidth = 0;
170         gridBagConstraints1.fill = java.awt.GridBagConstraints.HORIZONTAL;
171         gridBagConstraints1.insets = new java.awt.Insets JavaDoc (4, 8, 4, 0);
172         gridBagConstraints1.weightx = 1.0;
173         jPanel2.add (yField, gridBagConstraints1);
174
175         widthLabel = new javax.swing.JLabel JavaDoc ();
176         widthLabel.setText (null);
177
178         gridBagConstraints1 = new java.awt.GridBagConstraints JavaDoc ();
179         gridBagConstraints1.anchor = java.awt.GridBagConstraints.WEST;
180         jPanel2.add (widthLabel, gridBagConstraints1);
181
182         widthField = new javax.swing.JTextField JavaDoc ();
183         widthField.addKeyListener(this);
184         
185         gridBagConstraints1 = new java.awt.GridBagConstraints JavaDoc ();
186         gridBagConstraints1.gridwidth = 0;
187         gridBagConstraints1.fill = java.awt.GridBagConstraints.HORIZONTAL;
188         gridBagConstraints1.insets = new java.awt.Insets JavaDoc (4, 8, 4, 0);
189         gridBagConstraints1.weightx = 1.0;
190         jPanel2.add (widthField, gridBagConstraints1);
191
192         heightLabel = new javax.swing.JLabel JavaDoc ();
193         heightLabel.setText (null);
194
195         gridBagConstraints1 = new java.awt.GridBagConstraints JavaDoc ();
196         gridBagConstraints1.anchor = java.awt.GridBagConstraints.WEST;
197         jPanel2.add (heightLabel, gridBagConstraints1);
198
199         heightField = new javax.swing.JTextField JavaDoc ();
200         heightField.addKeyListener(this);
201
202         gridBagConstraints1 = new java.awt.GridBagConstraints JavaDoc ();
203         gridBagConstraints1.gridwidth = 0;
204         gridBagConstraints1.fill = java.awt.GridBagConstraints.HORIZONTAL;
205         gridBagConstraints1.insets = new java.awt.Insets JavaDoc (4, 8, 4, 0);
206         gridBagConstraints1.weightx = 1.0;
207         jPanel2.add (heightField, gridBagConstraints1);
208
209
210         add (jPanel2, "Center"); // NOI18N
211

212     }
213
214
215     private void updateRectangle () {
216         try {
217             int x = Integer.parseInt (xField.getText ());
218             int y = Integer.parseInt (yField.getText ());
219             int width = Integer.parseInt (widthField.getText ());
220             int height = Integer.parseInt (heightField.getText ());
221             editor.setValue (new Insets JavaDoc (x, y, width, height));
222         } catch (NumberFormatException JavaDoc e) {
223             // [PENDING beep]
224
}
225     }
226
227     public void keyPressed(java.awt.event.KeyEvent JavaDoc e) {
228     }
229
230     public void keyReleased(java.awt.event.KeyEvent JavaDoc e) {
231         if (checkValues()) {
232             updateRectangle();
233         }
234     }
235     
236     public void keyTyped(java.awt.event.KeyEvent JavaDoc e) {
237     }
238     
239     private boolean checkValues() {
240         Component JavaDoc[] c = jPanel2.getComponents();
241         boolean valid=true;
242         for (int i=0; i < c.length; i++) {
243             if (c[i] instanceof JTextField JavaDoc) {
244                 valid &= validFor((JTextField JavaDoc) c[i]);
245             }
246         }
247         if (env != null) {
248            env.setState(valid ? env.STATE_VALID : env.STATE_INVALID);
249         }
250         return valid;
251     }
252     
253     private boolean validFor(JTextField JavaDoc c) {
254         String JavaDoc s = c.getText().trim();
255         try {
256             Integer.parseInt(s);
257             handleValid(c);
258             return true;
259         } catch (NumberFormatException JavaDoc e) {
260             handleInvalid(c);
261             return false;
262         }
263     }
264     
265     private void handleInvalid(JTextField JavaDoc c) {
266         c.setForeground(getErrorColor());
267         labelMap.get(c).setForeground(getErrorColor());
268     }
269     
270     private void handleValid(JTextField JavaDoc c) {
271         c.setForeground(getForeground());
272         labelMap.get(c).setForeground(getForeground());
273     }
274     
275     private Color JavaDoc getErrorColor() {
276         Color JavaDoc c=UIManager.getColor("nb.errorForeground");
277         if (c == null) {
278             c = Color.RED;
279         }
280         return c;
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 InsetsEditor editor;
296
297 }
298
299
Popular Tags