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