KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > editor > options > ScrollInsetsCustomEditor


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.modules.editor.options;
21
22 import java.awt.Insets JavaDoc;
23 import javax.swing.event.DocumentEvent JavaDoc;
24 import javax.swing.event.DocumentListener JavaDoc;
25
26 import org.openide.NotifyDescriptor;
27 import org.openide.explorer.propertysheet.PropertyEnv;
28 import org.openide.util.NbBundle;
29
30 /** Custom editor for java.awt.Insets allowing to set per cent values
31  * as negative numbers.
32  *
33  * @author Petr Nejedly
34  * @author Ian Formanek
35  */

36 public class ScrollInsetsCustomEditor extends javax.swing.JPanel JavaDoc implements DocumentListener JavaDoc {
37
38
39     static final long serialVersionUID =-1472891501739636852L;
40
41     private ScrollInsetsEditor editor;
42     private PropertyEnv env;
43
44     /** Initializes the Form */
45     public ScrollInsetsCustomEditor(ScrollInsetsEditor editor, PropertyEnv env) {
46         initComponents ();
47         
48         this.editor = editor;
49         this.env = env;
50         
51         Insets JavaDoc insets = (Insets JavaDoc)editor.getValue();
52
53         if (insets == null) insets = new Insets JavaDoc( 0, 0, 0, 0 );
54
55         getAccessibleContext ().setAccessibleDescription (getBundleString("ACSD_SICE")); // NOI18N
56
topLabel.setDisplayedMnemonic (getBundleString("SICE_Top_Mnemonic").charAt(0)); // NOI18N
57
bottomLabel.setDisplayedMnemonic (getBundleString("SICE_Bottom_Mnemonic").charAt(0)); // NOI18N
58
leftLabel.setDisplayedMnemonic (getBundleString("SICE_Left_Mnemonic").charAt(0)); // NOI18N
59
rightLabel.setDisplayedMnemonic (getBundleString("SICE_Right_Mnemonic").charAt(0)); // NOI18N
60
topField.setText (int2percent (insets.top ));
61         leftField.setText (int2percent (insets.left ));
62         bottomField.setText (int2percent (insets.bottom));
63         rightField.setText (int2percent (insets.right));
64         topField.getAccessibleContext().setAccessibleDescription(getBundleString("ACSD_SICE_Top")); // NOI18N
65
leftField.getAccessibleContext().setAccessibleDescription(getBundleString("ACSD_SICE_Left")); // NOI18N
66
bottomField.getAccessibleContext().setAccessibleDescription(getBundleString("ACSD_SICE_Bottom")); // NOI18N
67
rightField.getAccessibleContext().setAccessibleDescription(getBundleString("ACSD_SICE_Right")); // NOI18N
68

69         /*
70         jPanel2.setBorder (new javax.swing.border.CompoundBorder (
71                                new javax.swing.border.TitledBorder (
72                                    new javax.swing.border.EtchedBorder (),
73                                    getBundleString().getString ("SICE_Insets")), // NOI18N
74                                new javax.swing.border.EmptyBorder (new java.awt.Insets(12, 12, 11, 11))));
75                                */

76
77         setPreferredSize(new java.awt.Dimension JavaDoc(320, getPreferredSize().height));
78         this.env.setState(PropertyEnv.STATE_VALID);
79         topField.getDocument().addDocumentListener(this);
80         leftField.getDocument().addDocumentListener(this);
81         bottomField.getDocument().addDocumentListener(this);
82         rightField.getDocument().addDocumentListener(this);
83     }
84
85     private String JavaDoc getBundleString(String JavaDoc s) {
86         return NbBundle.getMessage(ScrollInsetsCustomEditor.class, s);
87     }
88     
89     
90 // public Object getPropertyValue () throws IllegalStateException {
91
// try {
92
// return getValue();
93
// } catch (NumberFormatException e) {
94
// org.openide.DialogDisplayer.getDefault().notify( new NotifyDescriptor.Message(
95
// getBundleString("SIC_InvalidValue"), // NOI18N
96
// NotifyDescriptor.ERROR_MESSAGE
97
// ) );
98
// throw new IllegalStateException();
99
// }
100
// }
101

102
103
104     public static String JavaDoc int2percent( int i ) {
105         if( i < 0 ) return( "" + (-i) + "%" ); // NOI18N
106
else return( "" + i );
107     }
108
109     private int percent2int( String JavaDoc val ) throws NumberFormatException JavaDoc {
110         val = val.trim();
111         if( val.endsWith( "%" ) ) { // NOI18N
112
return -Math.abs( Integer.parseInt( val.substring( 0, val.length() - 1 ) ) );
113         } else {
114             return Integer.parseInt( val );
115         }
116     }
117
118     Insets JavaDoc getValue() throws NumberFormatException JavaDoc {
119         int top = percent2int( topField.getText() );
120         int left = percent2int( leftField.getText() );
121         int bottom = percent2int( bottomField.getText() );
122         int right = percent2int( rightField.getText() );
123         return new Insets JavaDoc( top, left, bottom, right );
124     }
125
126     /** This method is called from within the constructor to
127      * initialize the form.
128      * WARNING: Do NOT modify this code. The content of this method is
129      * always regenerated by the FormEditor.
130      */

131     // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
132
private void initComponents() {
133         java.awt.GridBagConstraints JavaDoc gridBagConstraints;
134
135         jPanel2 = new javax.swing.JPanel JavaDoc();
136         topLabel = new javax.swing.JLabel JavaDoc();
137         topField = new javax.swing.JTextField JavaDoc();
138         leftLabel = new javax.swing.JLabel JavaDoc();
139         leftField = new javax.swing.JTextField JavaDoc();
140         bottomLabel = new javax.swing.JLabel JavaDoc();
141         bottomField = new javax.swing.JTextField JavaDoc();
142         rightLabel = new javax.swing.JLabel JavaDoc();
143         rightField = new javax.swing.JTextField JavaDoc();
144
145         setLayout(new javax.swing.BoxLayout JavaDoc(this, javax.swing.BoxLayout.X_AXIS));
146
147         setBorder(new javax.swing.border.EmptyBorder JavaDoc( new java.awt.Insets JavaDoc( 12, 12, 11, 11) ) );
148         jPanel2.setLayout(new java.awt.GridBagLayout JavaDoc());
149
150         topLabel.setLabelFor(topField);
151         topLabel.setText(getBundleString("SICE_Top"));
152         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
153         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
154         gridBagConstraints.insets = new java.awt.Insets JavaDoc(5, 0, 0, 12);
155         jPanel2.add(topLabel, gridBagConstraints);
156
157         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
158         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
159         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
160         gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
161         gridBagConstraints.weightx = 1.0;
162         jPanel2.add(topField, gridBagConstraints);
163
164         leftLabel.setLabelFor(leftField);
165         leftLabel.setText(getBundleString("SICE_Left"));
166         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
167         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
168         gridBagConstraints.insets = new java.awt.Insets JavaDoc(5, 0, 0, 12);
169         jPanel2.add(leftLabel, gridBagConstraints);
170
171         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
172         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
173         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
174         gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
175         gridBagConstraints.weightx = 1.0;
176         gridBagConstraints.insets = new java.awt.Insets JavaDoc(5, 0, 0, 0);
177         jPanel2.add(leftField, gridBagConstraints);
178
179         bottomLabel.setLabelFor(bottomField);
180         bottomLabel.setText(getBundleString("SICE_Bottom"));
181         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
182         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
183         gridBagConstraints.insets = new java.awt.Insets JavaDoc(5, 0, 0, 12);
184         jPanel2.add(bottomLabel, gridBagConstraints);
185
186         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
187         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
188         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
189         gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
190         gridBagConstraints.weightx = 1.0;
191         gridBagConstraints.insets = new java.awt.Insets JavaDoc(5, 0, 0, 0);
192         jPanel2.add(bottomField, gridBagConstraints);
193
194         rightLabel.setLabelFor(rightField);
195         rightLabel.setText(getBundleString("SICE_Right"));
196         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
197         gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
198         gridBagConstraints.insets = new java.awt.Insets JavaDoc(7, 0, 0, 12);
199         jPanel2.add(rightLabel, gridBagConstraints);
200
201         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
202         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
203         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
204         gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
205         gridBagConstraints.weightx = 1.0;
206         gridBagConstraints.weighty = 1.0;
207         gridBagConstraints.insets = new java.awt.Insets JavaDoc(5, 0, 0, 0);
208         jPanel2.add(rightField, gridBagConstraints);
209
210         add(jPanel2);
211
212     }// </editor-fold>//GEN-END:initComponents
213

214     public void insertUpdate(DocumentEvent JavaDoc e) {
215         updateInsets();
216     }
217
218     public void removeUpdate(DocumentEvent JavaDoc e) {
219         updateInsets();
220     }
221
222     public void changedUpdate(DocumentEvent JavaDoc e) {
223         updateInsets();
224     }
225
226     private void updateInsets() {
227         try {
228             editor.setValue(getValue());
229             env.setState(PropertyEnv.STATE_VALID);
230         } catch (NumberFormatException JavaDoc e) {
231             env.setState(PropertyEnv.STATE_INVALID);
232         }
233     }
234
235     // Variables declaration - do not modify//GEN-BEGIN:variables
236
private javax.swing.JTextField JavaDoc bottomField;
237     private javax.swing.JLabel JavaDoc bottomLabel;
238     private javax.swing.JPanel JavaDoc jPanel2;
239     private javax.swing.JTextField JavaDoc leftField;
240     private javax.swing.JLabel JavaDoc leftLabel;
241     private javax.swing.JTextField JavaDoc rightField;
242     private javax.swing.JLabel JavaDoc rightLabel;
243     private javax.swing.JTextField JavaDoc topField;
244     private javax.swing.JLabel JavaDoc topLabel;
245     // End of variables declaration//GEN-END:variables
246

247 }
248
Popular Tags