001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.editor.options;
043:
044: import java.awt.Insets;
045: import javax.swing.event.DocumentEvent;
046: import javax.swing.event.DocumentListener;
047:
048: import org.openide.NotifyDescriptor;
049: import org.openide.explorer.propertysheet.PropertyEnv;
050: import org.openide.util.NbBundle;
051:
052: /** Custom editor for java.awt.Insets allowing to set per cent values
053: * as negative numbers.
054: *
055: * @author Petr Nejedly
056: * @author Ian Formanek
057: */
058: public class ScrollInsetsCustomEditor extends javax.swing.JPanel
059: implements DocumentListener {
060:
061: static final long serialVersionUID = -1472891501739636852L;
062:
063: private ScrollInsetsEditor editor;
064: private PropertyEnv env;
065:
066: /** Initializes the Form */
067: public ScrollInsetsCustomEditor(ScrollInsetsEditor editor,
068: PropertyEnv env) {
069: initComponents();
070:
071: this .editor = editor;
072: this .env = env;
073:
074: Insets insets = (Insets) editor.getValue();
075:
076: if (insets == null)
077: insets = new Insets(0, 0, 0, 0);
078:
079: getAccessibleContext().setAccessibleDescription(
080: getBundleString("ACSD_SICE")); // NOI18N
081: topField.setText(int2percent(insets.top));
082: leftField.setText(int2percent(insets.left));
083: bottomField.setText(int2percent(insets.bottom));
084: rightField.setText(int2percent(insets.right));
085: topField.getAccessibleContext().setAccessibleDescription(
086: getBundleString("ACSD_SICE_Top")); // NOI18N
087: leftField.getAccessibleContext().setAccessibleDescription(
088: getBundleString("ACSD_SICE_Left")); // NOI18N
089: bottomField.getAccessibleContext().setAccessibleDescription(
090: getBundleString("ACSD_SICE_Bottom")); // NOI18N
091: rightField.getAccessibleContext().setAccessibleDescription(
092: getBundleString("ACSD_SICE_Right")); // NOI18N
093:
094: /*
095: jPanel2.setBorder (new javax.swing.border.CompoundBorder (
096: new javax.swing.border.TitledBorder (
097: new javax.swing.border.EtchedBorder (),
098: getBundleString().getString ("SICE_Insets")), // NOI18N
099: new javax.swing.border.EmptyBorder (new java.awt.Insets(12, 12, 11, 11))));
100: */
101:
102: setPreferredSize(new java.awt.Dimension(320,
103: getPreferredSize().height));
104: this .env.setState(PropertyEnv.STATE_VALID);
105: topField.getDocument().addDocumentListener(this );
106: leftField.getDocument().addDocumentListener(this );
107: bottomField.getDocument().addDocumentListener(this );
108: rightField.getDocument().addDocumentListener(this );
109: }
110:
111: private String getBundleString(String s) {
112: return NbBundle.getMessage(ScrollInsetsCustomEditor.class, s);
113: }
114:
115: // public Object getPropertyValue () throws IllegalStateException {
116: // try {
117: // return getValue();
118: // } catch (NumberFormatException e) {
119: // org.openide.DialogDisplayer.getDefault().notify( new NotifyDescriptor.Message(
120: // getBundleString("SIC_InvalidValue"), // NOI18N
121: // NotifyDescriptor.ERROR_MESSAGE
122: // ) );
123: // throw new IllegalStateException();
124: // }
125: // }
126:
127: public static String int2percent(int i) {
128: if (i < 0)
129: return ("" + (-i) + "%"); // NOI18N
130: else
131: return ("" + i);
132: }
133:
134: private int percent2int(String val) throws NumberFormatException {
135: val = val.trim();
136: if (val.endsWith("%")) { // NOI18N
137: return -Math.abs(Integer.parseInt(val.substring(0, val
138: .length() - 1)));
139: } else {
140: return Integer.parseInt(val);
141: }
142: }
143:
144: Insets getValue() throws NumberFormatException {
145: int top = percent2int(topField.getText());
146: int left = percent2int(leftField.getText());
147: int bottom = percent2int(bottomField.getText());
148: int right = percent2int(rightField.getText());
149: return new Insets(top, left, bottom, right);
150: }
151:
152: /** This method is called from within the constructor to
153: * initialize the form.
154: * WARNING: Do NOT modify this code. The content of this method is
155: * always regenerated by the FormEditor.
156: */
157: // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
158: private void initComponents() {
159: java.awt.GridBagConstraints gridBagConstraints;
160:
161: jPanel2 = new javax.swing.JPanel();
162: topLabel = new javax.swing.JLabel();
163: topField = new javax.swing.JTextField();
164: leftLabel = new javax.swing.JLabel();
165: leftField = new javax.swing.JTextField();
166: bottomLabel = new javax.swing.JLabel();
167: bottomField = new javax.swing.JTextField();
168: rightLabel = new javax.swing.JLabel();
169: rightField = new javax.swing.JTextField();
170:
171: setBorder(new javax.swing.border.EmptyBorder(
172: new java.awt.Insets(12, 12, 11, 11)));
173: setLayout(new javax.swing.BoxLayout(this ,
174: javax.swing.BoxLayout.LINE_AXIS));
175:
176: jPanel2.setLayout(new java.awt.GridBagLayout());
177:
178: topLabel.setLabelFor(topField);
179: org.openide.awt.Mnemonics.setLocalizedText(topLabel,
180: getBundleString("SICE_Top"));
181: gridBagConstraints = new java.awt.GridBagConstraints();
182: gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
183: gridBagConstraints.insets = new java.awt.Insets(5, 0, 0, 12);
184: jPanel2.add(topLabel, gridBagConstraints);
185: gridBagConstraints = new java.awt.GridBagConstraints();
186: gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
187: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
188: gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
189: gridBagConstraints.weightx = 1.0;
190: jPanel2.add(topField, gridBagConstraints);
191:
192: leftLabel.setLabelFor(leftField);
193: org.openide.awt.Mnemonics.setLocalizedText(leftLabel,
194: getBundleString("SICE_Left"));
195: gridBagConstraints = new java.awt.GridBagConstraints();
196: gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
197: gridBagConstraints.insets = new java.awt.Insets(5, 0, 0, 12);
198: jPanel2.add(leftLabel, gridBagConstraints);
199: gridBagConstraints = new java.awt.GridBagConstraints();
200: gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
201: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
202: gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
203: gridBagConstraints.weightx = 1.0;
204: gridBagConstraints.insets = new java.awt.Insets(5, 0, 0, 0);
205: jPanel2.add(leftField, gridBagConstraints);
206:
207: bottomLabel.setLabelFor(bottomField);
208: org.openide.awt.Mnemonics.setLocalizedText(bottomLabel,
209: getBundleString("SICE_Bottom"));
210: gridBagConstraints = new java.awt.GridBagConstraints();
211: gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
212: gridBagConstraints.insets = new java.awt.Insets(5, 0, 0, 12);
213: jPanel2.add(bottomLabel, gridBagConstraints);
214: gridBagConstraints = new java.awt.GridBagConstraints();
215: gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
216: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
217: gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
218: gridBagConstraints.weightx = 1.0;
219: gridBagConstraints.insets = new java.awt.Insets(5, 0, 0, 0);
220: jPanel2.add(bottomField, gridBagConstraints);
221:
222: rightLabel.setLabelFor(rightField);
223: org.openide.awt.Mnemonics.setLocalizedText(rightLabel,
224: getBundleString("SICE_Right"));
225: gridBagConstraints = new java.awt.GridBagConstraints();
226: gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
227: gridBagConstraints.insets = new java.awt.Insets(7, 0, 0, 12);
228: jPanel2.add(rightLabel, gridBagConstraints);
229: gridBagConstraints = new java.awt.GridBagConstraints();
230: gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
231: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
232: gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
233: gridBagConstraints.weightx = 1.0;
234: gridBagConstraints.weighty = 1.0;
235: gridBagConstraints.insets = new java.awt.Insets(5, 0, 0, 0);
236: jPanel2.add(rightField, gridBagConstraints);
237:
238: add(jPanel2);
239: }// </editor-fold>//GEN-END:initComponents
240:
241: public void insertUpdate(DocumentEvent e) {
242: updateInsets();
243: }
244:
245: public void removeUpdate(DocumentEvent e) {
246: updateInsets();
247: }
248:
249: public void changedUpdate(DocumentEvent e) {
250: updateInsets();
251: }
252:
253: private void updateInsets() {
254: try {
255: editor.setValue(getValue());
256: env.setState(PropertyEnv.STATE_VALID);
257: } catch (NumberFormatException e) {
258: env.setState(PropertyEnv.STATE_INVALID);
259: }
260: }
261:
262: // Variables declaration - do not modify//GEN-BEGIN:variables
263: private javax.swing.JTextField bottomField;
264: private javax.swing.JLabel bottomLabel;
265: private javax.swing.JPanel jPanel2;
266: private javax.swing.JTextField leftField;
267: private javax.swing.JLabel leftLabel;
268: private javax.swing.JTextField rightField;
269: private javax.swing.JLabel rightLabel;
270: private javax.swing.JTextField topField;
271: private javax.swing.JLabel topLabel;
272: // End of variables declaration//GEN-END:variables
273:
274: }
|