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.beaninfo.editors;
043:
044: import java.awt.Color;
045: import java.awt.Component;
046: import java.awt.Insets;
047: import java.awt.event.KeyListener;
048: import java.beans.PropertyChangeEvent;
049: import java.beans.PropertyChangeListener;
050: import java.util.HashMap;
051: import javax.swing.JLabel;
052: import javax.swing.JTextField;
053: import javax.swing.UIManager;
054: import org.netbeans.core.UIExceptions;
055: import org.openide.awt.Mnemonics;
056: import org.openide.explorer.propertysheet.PropertyEnv;
057: import org.openide.util.NbBundle;
058:
059: /**
060: *
061: * @author Ian Formanek
062: * @version 1.00, 01 Sep 1998
063: */
064: public class InsetsCustomEditor extends javax.swing.JPanel implements
065: PropertyChangeListener, KeyListener {
066: static final long serialVersionUID = -1472891501739636852L;
067:
068: //XXX this is just a copy of RectangleEditor with the fields and value
069: //type changed. A proper solution would be have an equivalent of
070: //ArrayOfIntSupport for custom editors, which would just produce the
071: //required number of fields
072:
073: private HashMap<JTextField, JLabel> labelMap = new HashMap<JTextField, JLabel>();
074: private PropertyEnv env;
075:
076: /** Initializes the Form */
077: public InsetsCustomEditor(InsetsEditor editor, PropertyEnv env) {
078: this .env = env;
079: this .env.setState(PropertyEnv.STATE_NEEDS_VALIDATION);
080: this .env.addPropertyChangeListener(this );
081:
082: initComponents();
083: this .editor = editor;
084: Insets insets = (Insets) editor.getValue();
085: if (insets == null)
086: insets = new Insets(0, 0, 0, 0);
087: xField.setText(Integer.toString(insets.top)); // NOI18N
088: yField.setText(Integer.toString(insets.left)); // NOI18N
089: widthField.setText(Integer.toString(insets.bottom)); // NOI18N
090: heightField.setText(Integer.toString(insets.right)); // NOI18N
091:
092: setBorder(new javax.swing.border.EmptyBorder(new Insets(5, 5,
093: 5, 5)));
094: jPanel2.setBorder(new javax.swing.border.CompoundBorder(
095: new javax.swing.border.TitledBorder(
096: new javax.swing.border.EtchedBorder(), " "
097: + NbBundle.getMessage(
098: InsetsCustomEditor.class,
099: "CTL_Insets") + " "),
100: new javax.swing.border.EmptyBorder(new java.awt.Insets(
101: 5, 5, 5, 5))));
102:
103: Mnemonics.setLocalizedText(xLabel, NbBundle.getMessage(
104: InsetsCustomEditor.class, "CTL_Top"));
105: Mnemonics.setLocalizedText(yLabel, NbBundle.getMessage(
106: InsetsCustomEditor.class, "CTL_Left"));
107: Mnemonics.setLocalizedText(widthLabel, NbBundle.getMessage(
108: InsetsCustomEditor.class, "CTL_Bottom"));
109: Mnemonics.setLocalizedText(heightLabel, NbBundle.getMessage(
110: InsetsCustomEditor.class, "CTL_Right"));
111:
112: xLabel.setLabelFor(xField);
113: yLabel.setLabelFor(yField);
114: widthLabel.setLabelFor(widthField);
115: heightLabel.setLabelFor(heightField);
116:
117: xField.getAccessibleContext().setAccessibleDescription(
118: NbBundle.getMessage(InsetsCustomEditor.class,
119: "ACSD_CTL_Top"));
120: yField.getAccessibleContext().setAccessibleDescription(
121: NbBundle.getMessage(InsetsCustomEditor.class,
122: "ACSD_CTL_Left"));
123: widthField.getAccessibleContext().setAccessibleDescription(
124: NbBundle.getMessage(InsetsCustomEditor.class,
125: "ACSD_CTL_Bottom"));
126: heightField.getAccessibleContext().setAccessibleDescription(
127: NbBundle.getMessage(InsetsCustomEditor.class,
128: "ACSD_CTL_Right"));
129:
130: getAccessibleContext().setAccessibleDescription(
131: NbBundle.getMessage(InsetsCustomEditor.class,
132: "ACSD_CustomRectangleEditor"));
133:
134: labelMap.put(widthField, widthLabel);
135: labelMap.put(xField, xLabel);
136: labelMap.put(yField, yLabel);
137: labelMap.put(heightField, heightLabel);
138: }
139:
140: public java.awt.Dimension getPreferredSize() {
141: return new java.awt.Dimension(280, 160);
142: }
143:
144: private Object getPropertyValue() throws IllegalStateException {
145: try {
146: int x = Integer.parseInt(xField.getText());
147: int y = Integer.parseInt(yField.getText());
148: int width = Integer.parseInt(widthField.getText());
149: int height = Integer.parseInt(heightField.getText());
150: // if ((x < 0) || (y < 0) || (width < 0) || (height < 0)) {
151: // IllegalStateException ise = new IllegalStateException();
152: // UIExceptions.annotateUser(ise, null,
153: // NbBundle.getMessage(InsetsCustomEditor.class,
154: // "CTL_NegativeSize"),
155: // null, null);
156: // throw ise;
157: // }
158: return new Insets(x, y, width, height);
159: } catch (NumberFormatException e) {
160: IllegalStateException ise = new IllegalStateException();
161: UIExceptions.annotateUser(ise, null, NbBundle.getMessage(
162: InsetsCustomEditor.class, "CTL_InvalidValue"),
163: null, null);
164: throw ise;
165: }
166: }
167:
168: public void propertyChange(PropertyChangeEvent evt) {
169: if (PropertyEnv.PROP_STATE.equals(evt.getPropertyName())
170: && evt.getNewValue() == PropertyEnv.STATE_VALID) {
171: editor.setValue(getPropertyValue());
172: }
173: }
174:
175: private void initComponents() {
176: setLayout(new java.awt.BorderLayout());
177:
178: jPanel2 = new javax.swing.JPanel();
179: jPanel2.setLayout(new java.awt.GridBagLayout());
180: java.awt.GridBagConstraints gridBagConstraints1;
181:
182: xLabel = new javax.swing.JLabel();
183: xLabel.setText(null);
184:
185: gridBagConstraints1 = new java.awt.GridBagConstraints();
186: gridBagConstraints1.anchor = java.awt.GridBagConstraints.WEST;
187: jPanel2.add(xLabel, gridBagConstraints1);
188:
189: xField = new javax.swing.JTextField();
190: xField.addKeyListener(this );
191:
192: gridBagConstraints1 = new java.awt.GridBagConstraints();
193: gridBagConstraints1.gridwidth = 0;
194: gridBagConstraints1.fill = java.awt.GridBagConstraints.HORIZONTAL;
195: gridBagConstraints1.insets = new java.awt.Insets(4, 8, 4, 0);
196: gridBagConstraints1.weightx = 1.0;
197: jPanel2.add(xField, gridBagConstraints1);
198:
199: yLabel = new javax.swing.JLabel();
200: yLabel.setText(null);
201:
202: gridBagConstraints1 = new java.awt.GridBagConstraints();
203: gridBagConstraints1.anchor = java.awt.GridBagConstraints.WEST;
204: jPanel2.add(yLabel, gridBagConstraints1);
205:
206: yField = new javax.swing.JTextField();
207: yField.addKeyListener(this );
208:
209: gridBagConstraints1 = new java.awt.GridBagConstraints();
210: gridBagConstraints1.gridwidth = 0;
211: gridBagConstraints1.fill = java.awt.GridBagConstraints.HORIZONTAL;
212: gridBagConstraints1.insets = new java.awt.Insets(4, 8, 4, 0);
213: gridBagConstraints1.weightx = 1.0;
214: jPanel2.add(yField, gridBagConstraints1);
215:
216: widthLabel = new javax.swing.JLabel();
217: widthLabel.setText(null);
218:
219: gridBagConstraints1 = new java.awt.GridBagConstraints();
220: gridBagConstraints1.anchor = java.awt.GridBagConstraints.WEST;
221: jPanel2.add(widthLabel, gridBagConstraints1);
222:
223: widthField = new javax.swing.JTextField();
224: widthField.addKeyListener(this );
225:
226: gridBagConstraints1 = new java.awt.GridBagConstraints();
227: gridBagConstraints1.gridwidth = 0;
228: gridBagConstraints1.fill = java.awt.GridBagConstraints.HORIZONTAL;
229: gridBagConstraints1.insets = new java.awt.Insets(4, 8, 4, 0);
230: gridBagConstraints1.weightx = 1.0;
231: jPanel2.add(widthField, gridBagConstraints1);
232:
233: heightLabel = new javax.swing.JLabel();
234: heightLabel.setText(null);
235:
236: gridBagConstraints1 = new java.awt.GridBagConstraints();
237: gridBagConstraints1.anchor = java.awt.GridBagConstraints.WEST;
238: jPanel2.add(heightLabel, gridBagConstraints1);
239:
240: heightField = new javax.swing.JTextField();
241: heightField.addKeyListener(this );
242:
243: gridBagConstraints1 = new java.awt.GridBagConstraints();
244: gridBagConstraints1.gridwidth = 0;
245: gridBagConstraints1.fill = java.awt.GridBagConstraints.HORIZONTAL;
246: gridBagConstraints1.insets = new java.awt.Insets(4, 8, 4, 0);
247: gridBagConstraints1.weightx = 1.0;
248: jPanel2.add(heightField, gridBagConstraints1);
249:
250: add(jPanel2, "Center"); // NOI18N
251:
252: }
253:
254: private void updateRectangle() {
255: try {
256: int x = Integer.parseInt(xField.getText());
257: int y = Integer.parseInt(yField.getText());
258: int width = Integer.parseInt(widthField.getText());
259: int height = Integer.parseInt(heightField.getText());
260: editor.setValue(new Insets(x, y, width, height));
261: } catch (NumberFormatException e) {
262: // [PENDING beep]
263: }
264: }
265:
266: public void keyPressed(java.awt.event.KeyEvent e) {
267: }
268:
269: public void keyReleased(java.awt.event.KeyEvent e) {
270: if (checkValues()) {
271: updateRectangle();
272: }
273: }
274:
275: public void keyTyped(java.awt.event.KeyEvent e) {
276: }
277:
278: private boolean checkValues() {
279: Component[] c = jPanel2.getComponents();
280: boolean valid = true;
281: for (int i = 0; i < c.length; i++) {
282: if (c[i] instanceof JTextField) {
283: valid &= validFor((JTextField) c[i]);
284: }
285: }
286: if (env != null) {
287: env.setState(valid ? env.STATE_VALID : env.STATE_INVALID);
288: }
289: return valid;
290: }
291:
292: private boolean validFor(JTextField c) {
293: String s = c.getText().trim();
294: try {
295: Integer.parseInt(s);
296: handleValid(c);
297: return true;
298: } catch (NumberFormatException e) {
299: handleInvalid(c);
300: return false;
301: }
302: }
303:
304: private void handleInvalid(JTextField c) {
305: c.setForeground(getErrorColor());
306: labelMap.get(c).setForeground(getErrorColor());
307: }
308:
309: private void handleValid(JTextField c) {
310: c.setForeground(getForeground());
311: labelMap.get(c).setForeground(getForeground());
312: }
313:
314: private Color getErrorColor() {
315: Color c = UIManager.getColor("nb.errorForeground");
316: if (c == null) {
317: c = Color.RED;
318: }
319: return c;
320: }
321:
322: // Variables declaration - do not modify
323: private javax.swing.JPanel jPanel2;
324: private javax.swing.JLabel xLabel;
325: private javax.swing.JTextField xField;
326: private javax.swing.JLabel yLabel;
327: private javax.swing.JTextField yField;
328: private javax.swing.JLabel widthLabel;
329: private javax.swing.JTextField widthField;
330: private javax.swing.JLabel heightLabel;
331: private javax.swing.JTextField heightField;
332: // End of variables declaration
333:
334: private InsetsEditor editor;
335:
336: }
|