KickJava   Java API By Example, From Geeks To Geeks.

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


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.Point JavaDoc;
23 import java.awt.Dimension JavaDoc;
24 import java.beans.PropertyChangeEvent JavaDoc;
25 import java.beans.PropertyChangeListener JavaDoc;
26 import org.netbeans.core.UIExceptions;
27 import org.openide.awt.Mnemonics;
28 import org.openide.explorer.propertysheet.PropertyEnv;
29 import org.openide.util.NbBundle;
30
31 /** Custom property editor for Point and Dimension
32 *
33 * @author Ian Formanek
34 */

35 public class PointCustomEditor extends javax.swing.JPanel JavaDoc
36 implements PropertyChangeListener JavaDoc {
37
38     static final long serialVersionUID =-4067033871196801978L;
39
40     private boolean dimensionMode = false;
41
42     private PropertyEnv env;
43     
44     /** Initializes the Form */
45     public PointCustomEditor(PointEditor editor, PropertyEnv env) {
46         initComponents ();
47         this.editor = editor;
48         Point JavaDoc point = (Point JavaDoc)editor.getValue ();
49         if (point == null) point = new Point JavaDoc (0, 0);
50         xField.setText (Integer.toString(point.x)); // NOI18N
51
yField.setText (Integer.toString(point.y)); // NOI18N
52

53         xField.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(PointCustomEditor.class, "ACSD_CTL_X"));
54         yField.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(PointCustomEditor.class, "ACSD_CTL_Y"));
55         
56         commonInit( NbBundle.getMessage(PointCustomEditor.class, "CTL_Point"), env );
57     }
58     
59     public PointCustomEditor(DimensionEditor editor, PropertyEnv env) {
60         dimensionMode = true;
61
62         initComponents();
63         this.editor = editor;
64         Dimension JavaDoc dimension = (Dimension JavaDoc)editor.getValue ();
65         if (dimension == null) dimension = new Dimension JavaDoc (0, 0);
66         xField.setText (Integer.toString(dimension.width)); // NOI18N
67
yField.setText (Integer.toString(dimension.height)); // NOI18N
68

69         Mnemonics.setLocalizedText(xLabel, NbBundle.getMessage(PointCustomEditor.class, "CTL_Width"));
70         xLabel.setLabelFor(xField);
71         Mnemonics.setLocalizedText(yLabel, NbBundle.getMessage(PointCustomEditor.class, "CTL_Height"));
72         yLabel.setLabelFor(yField);
73
74         xField.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(PointCustomEditor.class, "ACSD_CTL_Width"));
75         yField.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(PointCustomEditor.class, "ACSD_CTL_Height"));
76         
77         commonInit( NbBundle.getMessage(PointCustomEditor.class, "CTL_Dimension"), env );
78     }
79     
80     private void commonInit( String JavaDoc panelTitle, PropertyEnv env ) {
81         getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(PointCustomEditor.class, "ACSD_PointCustomEditor"));
82         
83         setBorder (new javax.swing.border.EmptyBorder JavaDoc(12, 12, 0, 11));
84         insidePanel.setBorder (new javax.swing.border.CompoundBorder JavaDoc (
85                                    new javax.swing.border.TitledBorder JavaDoc (
86                                        new javax.swing.border.EtchedBorder JavaDoc (),
87                                        " " + panelTitle + " "
88                                    ),
89                                    new javax.swing.border.EmptyBorder JavaDoc (new java.awt.Insets JavaDoc(5, 5, 5, 5))));
90
91
92         this.env = env;
93         env.setState(PropertyEnv.STATE_NEEDS_VALIDATION);
94         env.addPropertyChangeListener(this);
95     }
96
97     public java.awt.Dimension JavaDoc getPreferredSize () {
98         return new java.awt.Dimension JavaDoc (280, 160);
99     }
100
101     private Object JavaDoc getPropertyValue () throws IllegalStateException JavaDoc {
102         try {
103             int x = Integer.parseInt (xField.getText ());
104             int y = Integer.parseInt (yField.getText ());
105             if ( dimensionMode ) {
106                 if ((x < 0) || (y < 0)) {
107                     IllegalStateException JavaDoc ise = new IllegalStateException JavaDoc();
108                     UIExceptions.annotateUser(ise, null,
109                                              NbBundle.getMessage(PointCustomEditor.class,
110                                                                  "CTL_NegativeSize"),
111                                              null, null);
112                     throw ise;
113                 }
114                 return new Dimension JavaDoc (x, y);
115             } else {
116                 return new Point JavaDoc (x, y);
117             }
118         } catch (NumberFormatException JavaDoc e) {
119             IllegalStateException JavaDoc ise = new IllegalStateException JavaDoc();
120             UIExceptions.annotateUser(ise, null,
121                                      NbBundle.getMessage(PointCustomEditor.class,
122                                                          "CTL_InvalidValue"),
123                                      null, null);
124             throw ise;
125         }
126     }
127
128
129     public void propertyChange(PropertyChangeEvent JavaDoc evt) {
130         if (
131             PropertyEnv.PROP_STATE.equals(evt.getPropertyName())
132             &&
133             PropertyEnv.STATE_VALID.equals(evt.getNewValue())
134         ) {
135             editor.setValue(getPropertyValue());
136         }
137     }
138
139
140     /** This method is called from within the constructor to
141      * initialize the form.
142      * WARNING: Do NOT modify this code. The content of this method is
143      * always regenerated by the FormEditor.
144      */

145     // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
146
private void initComponents() {
147         java.awt.GridBagConstraints JavaDoc gridBagConstraints;
148
149         insidePanel = new javax.swing.JPanel JavaDoc();
150         xLabel = new javax.swing.JLabel JavaDoc();
151         xField = new javax.swing.JTextField JavaDoc();
152         yLabel = new javax.swing.JLabel JavaDoc();
153         yField = new javax.swing.JTextField JavaDoc();
154
155         setLayout(new java.awt.BorderLayout JavaDoc());
156
157         insidePanel.setLayout(new java.awt.GridBagLayout JavaDoc());
158
159         xLabel.setLabelFor(xField);
160         java.util.ResourceBundle JavaDoc bundle = java.util.ResourceBundle.getBundle("org/netbeans/beaninfo/editors/Bundle"); // NOI18N
161
org.openide.awt.Mnemonics.setLocalizedText(xLabel, bundle.getString("CTL_X")); // NOI18N
162
gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
163         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
164         insidePanel.add(xLabel, gridBagConstraints);
165
166         xField.addActionListener(new java.awt.event.ActionListener JavaDoc() {
167             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
168                 updateInsets(evt);
169             }
170         });
171         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
172         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
173         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
174         gridBagConstraints.weightx = 1.0;
175         gridBagConstraints.insets = new java.awt.Insets JavaDoc(4, 8, 4, 0);
176         insidePanel.add(xField, gridBagConstraints);
177
178         yLabel.setLabelFor(yField);
179         org.openide.awt.Mnemonics.setLocalizedText(yLabel, bundle.getString("CTL_Y")); // NOI18N
180
gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
181         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
182         insidePanel.add(yLabel, gridBagConstraints);
183
184         yField.addActionListener(new java.awt.event.ActionListener JavaDoc() {
185             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
186                 updateInsets(evt);
187             }
188         });
189         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
190         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
191         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
192         gridBagConstraints.weightx = 1.0;
193         gridBagConstraints.insets = new java.awt.Insets JavaDoc(4, 8, 4, 0);
194         insidePanel.add(yField, gridBagConstraints);
195
196         add(insidePanel, java.awt.BorderLayout.CENTER);
197     }// </editor-fold>//GEN-END:initComponents
198

199
200     private void updateInsets (java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_updateInsets
201
try {
202             int x = Integer.parseInt (xField.getText ());
203             int y = Integer.parseInt (yField.getText ());
204             if ( dimensionMode )
205                 editor.setValue (new Dimension JavaDoc (x, y));
206             else
207                 editor.setValue (new Point JavaDoc (x, y));
208         } catch (NumberFormatException JavaDoc e) {
209             // [PENDING beep]
210
}
211     }//GEN-LAST:event_updateInsets
212

213
214     // Variables declaration - do not modify//GEN-BEGIN:variables
215
private javax.swing.JPanel JavaDoc insidePanel;
216     private javax.swing.JTextField JavaDoc xField;
217     private javax.swing.JLabel JavaDoc xLabel;
218     private javax.swing.JTextField JavaDoc yField;
219     private javax.swing.JLabel JavaDoc yLabel;
220     // End of variables declaration//GEN-END:variables
221

222     private ArrayOfIntSupport editor;
223
224 }
225
226
Popular Tags