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.Point;
045: import java.awt.Dimension;
046: import java.beans.PropertyChangeEvent;
047: import java.beans.PropertyChangeListener;
048: import org.netbeans.core.UIExceptions;
049: import org.openide.awt.Mnemonics;
050: import org.openide.explorer.propertysheet.PropertyEnv;
051: import org.openide.util.NbBundle;
052:
053: /** Custom property editor for Point and Dimension
054: *
055: * @author Ian Formanek
056: */
057: public class PointCustomEditor extends javax.swing.JPanel implements
058: PropertyChangeListener {
059:
060: static final long serialVersionUID = -4067033871196801978L;
061:
062: private boolean dimensionMode = false;
063:
064: private PropertyEnv env;
065:
066: /** Initializes the Form */
067: public PointCustomEditor(PointEditor editor, PropertyEnv env) {
068: initComponents();
069: this .editor = editor;
070: Point point = (Point) editor.getValue();
071: if (point == null)
072: point = new Point(0, 0);
073: xField.setText(Integer.toString(point.x)); // NOI18N
074: yField.setText(Integer.toString(point.y)); // NOI18N
075:
076: xField.getAccessibleContext().setAccessibleDescription(
077: NbBundle.getMessage(PointCustomEditor.class,
078: "ACSD_CTL_X"));
079: yField.getAccessibleContext().setAccessibleDescription(
080: NbBundle.getMessage(PointCustomEditor.class,
081: "ACSD_CTL_Y"));
082:
083: commonInit(NbBundle.getMessage(PointCustomEditor.class,
084: "CTL_Point"), env);
085: }
086:
087: public PointCustomEditor(DimensionEditor editor, PropertyEnv env) {
088: dimensionMode = true;
089:
090: initComponents();
091: this .editor = editor;
092: Dimension dimension = (Dimension) editor.getValue();
093: if (dimension == null)
094: dimension = new Dimension(0, 0);
095: xField.setText(Integer.toString(dimension.width)); // NOI18N
096: yField.setText(Integer.toString(dimension.height)); // NOI18N
097:
098: Mnemonics.setLocalizedText(xLabel, NbBundle.getMessage(
099: PointCustomEditor.class, "CTL_Width"));
100: xLabel.setLabelFor(xField);
101: Mnemonics.setLocalizedText(yLabel, NbBundle.getMessage(
102: PointCustomEditor.class, "CTL_Height"));
103: yLabel.setLabelFor(yField);
104:
105: xField.getAccessibleContext().setAccessibleDescription(
106: NbBundle.getMessage(PointCustomEditor.class,
107: "ACSD_CTL_Width"));
108: yField.getAccessibleContext().setAccessibleDescription(
109: NbBundle.getMessage(PointCustomEditor.class,
110: "ACSD_CTL_Height"));
111:
112: commonInit(NbBundle.getMessage(PointCustomEditor.class,
113: "CTL_Dimension"), env);
114: }
115:
116: private void commonInit(String panelTitle, PropertyEnv env) {
117: getAccessibleContext().setAccessibleDescription(
118: NbBundle.getMessage(PointCustomEditor.class,
119: "ACSD_PointCustomEditor"));
120:
121: setBorder(new javax.swing.border.EmptyBorder(12, 12, 0, 11));
122: insidePanel.setBorder(new javax.swing.border.CompoundBorder(
123: new javax.swing.border.TitledBorder(
124: new javax.swing.border.EtchedBorder(), " "
125: + panelTitle + " "),
126: new javax.swing.border.EmptyBorder(new java.awt.Insets(
127: 5, 5, 5, 5))));
128:
129: this .env = env;
130: env.setState(PropertyEnv.STATE_NEEDS_VALIDATION);
131: env.addPropertyChangeListener(this );
132: }
133:
134: public java.awt.Dimension getPreferredSize() {
135: return new java.awt.Dimension(280, 160);
136: }
137:
138: private Object getPropertyValue() throws IllegalStateException {
139: try {
140: int x = Integer.parseInt(xField.getText());
141: int y = Integer.parseInt(yField.getText());
142: if (dimensionMode) {
143: if ((x < 0) || (y < 0)) {
144: IllegalStateException ise = new IllegalStateException();
145: UIExceptions.annotateUser(ise, null, NbBundle
146: .getMessage(PointCustomEditor.class,
147: "CTL_NegativeSize"), null, null);
148: throw ise;
149: }
150: return new Dimension(x, y);
151: } else {
152: return new Point(x, y);
153: }
154: } catch (NumberFormatException e) {
155: IllegalStateException ise = new IllegalStateException();
156: UIExceptions.annotateUser(ise, null, NbBundle.getMessage(
157: PointCustomEditor.class, "CTL_InvalidValue"), null,
158: null);
159: throw ise;
160: }
161: }
162:
163: public void propertyChange(PropertyChangeEvent evt) {
164: if (PropertyEnv.PROP_STATE.equals(evt.getPropertyName())
165: && PropertyEnv.STATE_VALID.equals(evt.getNewValue())) {
166: editor.setValue(getPropertyValue());
167: }
168: }
169:
170: /** This method is called from within the constructor to
171: * initialize the form.
172: * WARNING: Do NOT modify this code. The content of this method is
173: * always regenerated by the FormEditor.
174: */
175: // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
176: private void initComponents() {
177: java.awt.GridBagConstraints gridBagConstraints;
178:
179: insidePanel = new javax.swing.JPanel();
180: xLabel = new javax.swing.JLabel();
181: xField = new javax.swing.JTextField();
182: yLabel = new javax.swing.JLabel();
183: yField = new javax.swing.JTextField();
184:
185: setLayout(new java.awt.BorderLayout());
186:
187: insidePanel.setLayout(new java.awt.GridBagLayout());
188:
189: xLabel.setLabelFor(xField);
190: java.util.ResourceBundle bundle = java.util.ResourceBundle
191: .getBundle("org/netbeans/beaninfo/editors/Bundle"); // NOI18N
192: org.openide.awt.Mnemonics.setLocalizedText(xLabel, bundle
193: .getString("CTL_X")); // NOI18N
194: gridBagConstraints = new java.awt.GridBagConstraints();
195: gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
196: insidePanel.add(xLabel, gridBagConstraints);
197:
198: xField.addActionListener(new java.awt.event.ActionListener() {
199: public void actionPerformed(java.awt.event.ActionEvent evt) {
200: updateInsets(evt);
201: }
202: });
203: gridBagConstraints = new java.awt.GridBagConstraints();
204: gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
205: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
206: gridBagConstraints.weightx = 1.0;
207: gridBagConstraints.insets = new java.awt.Insets(4, 8, 4, 0);
208: insidePanel.add(xField, gridBagConstraints);
209:
210: yLabel.setLabelFor(yField);
211: org.openide.awt.Mnemonics.setLocalizedText(yLabel, bundle
212: .getString("CTL_Y")); // NOI18N
213: gridBagConstraints = new java.awt.GridBagConstraints();
214: gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
215: insidePanel.add(yLabel, gridBagConstraints);
216:
217: yField.addActionListener(new java.awt.event.ActionListener() {
218: public void actionPerformed(java.awt.event.ActionEvent evt) {
219: updateInsets(evt);
220: }
221: });
222: gridBagConstraints = new java.awt.GridBagConstraints();
223: gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
224: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
225: gridBagConstraints.weightx = 1.0;
226: gridBagConstraints.insets = new java.awt.Insets(4, 8, 4, 0);
227: insidePanel.add(yField, gridBagConstraints);
228:
229: add(insidePanel, java.awt.BorderLayout.CENTER);
230: }// </editor-fold>//GEN-END:initComponents
231:
232: private void updateInsets(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_updateInsets
233: try {
234: int x = Integer.parseInt(xField.getText());
235: int y = Integer.parseInt(yField.getText());
236: if (dimensionMode)
237: editor.setValue(new Dimension(x, y));
238: else
239: editor.setValue(new Point(x, y));
240: } catch (NumberFormatException e) {
241: // [PENDING beep]
242: }
243: }//GEN-LAST:event_updateInsets
244:
245: // Variables declaration - do not modify//GEN-BEGIN:variables
246: private javax.swing.JPanel insidePanel;
247: private javax.swing.JTextField xField;
248: private javax.swing.JLabel xLabel;
249: private javax.swing.JTextField yField;
250: private javax.swing.JLabel yLabel;
251: // End of variables declaration//GEN-END:variables
252:
253: private ArrayOfIntSupport editor;
254:
255: }
|