1 19 20 package org.netbeans.beaninfo.editors; 21 22 import java.awt.Point ; 23 import java.awt.Dimension ; 24 import java.beans.PropertyChangeEvent ; 25 import java.beans.PropertyChangeListener ; 26 import org.netbeans.core.UIExceptions; 27 import org.openide.awt.Mnemonics; 28 import org.openide.explorer.propertysheet.PropertyEnv; 29 import org.openide.util.NbBundle; 30 31 35 public class PointCustomEditor extends javax.swing.JPanel 36 implements PropertyChangeListener { 37 38 static final long serialVersionUID =-4067033871196801978L; 39 40 private boolean dimensionMode = false; 41 42 private PropertyEnv env; 43 44 45 public PointCustomEditor(PointEditor editor, PropertyEnv env) { 46 initComponents (); 47 this.editor = editor; 48 Point point = (Point )editor.getValue (); 49 if (point == null) point = new Point (0, 0); 50 xField.setText (Integer.toString(point.x)); yField.setText (Integer.toString(point.y)); 53 xField.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(PointCustomEditor.class, "ACSD_CTL_X")); 54 yField.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(PointCustomEditor.class, "ACSD_CTL_Y")); 55 56 commonInit( NbBundle.getMessage(PointCustomEditor.class, "CTL_Point"), env ); 57 } 58 59 public PointCustomEditor(DimensionEditor editor, PropertyEnv env) { 60 dimensionMode = true; 61 62 initComponents(); 63 this.editor = editor; 64 Dimension dimension = (Dimension )editor.getValue (); 65 if (dimension == null) dimension = new Dimension (0, 0); 66 xField.setText (Integer.toString(dimension.width)); yField.setText (Integer.toString(dimension.height)); 69 Mnemonics.setLocalizedText(xLabel, NbBundle.getMessage(PointCustomEditor.class, "CTL_Width")); 70 xLabel.setLabelFor(xField); 71 Mnemonics.setLocalizedText(yLabel, NbBundle.getMessage(PointCustomEditor.class, "CTL_Height")); 72 yLabel.setLabelFor(yField); 73 74 xField.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(PointCustomEditor.class, "ACSD_CTL_Width")); 75 yField.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(PointCustomEditor.class, "ACSD_CTL_Height")); 76 77 commonInit( NbBundle.getMessage(PointCustomEditor.class, "CTL_Dimension"), env ); 78 } 79 80 private void commonInit( String panelTitle, PropertyEnv env ) { 81 getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(PointCustomEditor.class, "ACSD_PointCustomEditor")); 82 83 setBorder (new javax.swing.border.EmptyBorder (12, 12, 0, 11)); 84 insidePanel.setBorder (new javax.swing.border.CompoundBorder ( 85 new javax.swing.border.TitledBorder ( 86 new javax.swing.border.EtchedBorder (), 87 " " + panelTitle + " " 88 ), 89 new javax.swing.border.EmptyBorder (new java.awt.Insets (5, 5, 5, 5)))); 90 91 92 this.env = env; 93 env.setState(PropertyEnv.STATE_NEEDS_VALIDATION); 94 env.addPropertyChangeListener(this); 95 } 96 97 public java.awt.Dimension getPreferredSize () { 98 return new java.awt.Dimension (280, 160); 99 } 100 101 private Object getPropertyValue () throws IllegalStateException { 102 try { 103 int x = Integer.parseInt (xField.getText ()); 104 int y = Integer.parseInt (yField.getText ()); 105 if ( dimensionMode ) { 106 if ((x < 0) || (y < 0)) { 107 IllegalStateException ise = new IllegalStateException (); 108 UIExceptions.annotateUser(ise, null, 109 NbBundle.getMessage(PointCustomEditor.class, 110 "CTL_NegativeSize"), 111 null, null); 112 throw ise; 113 } 114 return new Dimension (x, y); 115 } else { 116 return new Point (x, y); 117 } 118 } catch (NumberFormatException e) { 119 IllegalStateException ise = new IllegalStateException (); 120 UIExceptions.annotateUser(ise, null, 121 NbBundle.getMessage(PointCustomEditor.class, 122 "CTL_InvalidValue"), 123 null, null); 124 throw ise; 125 } 126 } 127 128 129 public void propertyChange(PropertyChangeEvent evt) { 130 if ( 131 PropertyEnv.PROP_STATE.equals(evt.getPropertyName()) 132 && 133 PropertyEnv.STATE_VALID.equals(evt.getNewValue()) 134 ) { 135 editor.setValue(getPropertyValue()); 136 } 137 } 138 139 140 145 private void initComponents() { 147 java.awt.GridBagConstraints gridBagConstraints; 148 149 insidePanel = new javax.swing.JPanel (); 150 xLabel = new javax.swing.JLabel (); 151 xField = new javax.swing.JTextField (); 152 yLabel = new javax.swing.JLabel (); 153 yField = new javax.swing.JTextField (); 154 155 setLayout(new java.awt.BorderLayout ()); 156 157 insidePanel.setLayout(new java.awt.GridBagLayout ()); 158 159 xLabel.setLabelFor(xField); 160 java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("org/netbeans/beaninfo/editors/Bundle"); org.openide.awt.Mnemonics.setLocalizedText(xLabel, bundle.getString("CTL_X")); gridBagConstraints = new java.awt.GridBagConstraints (); 163 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 164 insidePanel.add(xLabel, gridBagConstraints); 165 166 xField.addActionListener(new java.awt.event.ActionListener () { 167 public void actionPerformed(java.awt.event.ActionEvent evt) { 168 updateInsets(evt); 169 } 170 }); 171 gridBagConstraints = new java.awt.GridBagConstraints (); 172 gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; 173 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 174 gridBagConstraints.weightx = 1.0; 175 gridBagConstraints.insets = new java.awt.Insets (4, 8, 4, 0); 176 insidePanel.add(xField, gridBagConstraints); 177 178 yLabel.setLabelFor(yField); 179 org.openide.awt.Mnemonics.setLocalizedText(yLabel, bundle.getString("CTL_Y")); gridBagConstraints = new java.awt.GridBagConstraints (); 181 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 182 insidePanel.add(yLabel, gridBagConstraints); 183 184 yField.addActionListener(new java.awt.event.ActionListener () { 185 public void actionPerformed(java.awt.event.ActionEvent evt) { 186 updateInsets(evt); 187 } 188 }); 189 gridBagConstraints = new java.awt.GridBagConstraints (); 190 gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; 191 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 192 gridBagConstraints.weightx = 1.0; 193 gridBagConstraints.insets = new java.awt.Insets (4, 8, 4, 0); 194 insidePanel.add(yField, gridBagConstraints); 195 196 add(insidePanel, java.awt.BorderLayout.CENTER); 197 } 199 200 private void updateInsets (java.awt.event.ActionEvent evt) { try { 202 int x = Integer.parseInt (xField.getText ()); 203 int y = Integer.parseInt (yField.getText ()); 204 if ( dimensionMode ) 205 editor.setValue (new Dimension (x, y)); 206 else 207 editor.setValue (new Point (x, y)); 208 } catch (NumberFormatException e) { 209 } 211 } 213 214 private javax.swing.JPanel insidePanel; 216 private javax.swing.JTextField xField; 217 private javax.swing.JLabel xLabel; 218 private javax.swing.JTextField yField; 219 private javax.swing.JLabel yLabel; 220 222 private ArrayOfIntSupport editor; 223 224 } 225 226 | Popular Tags |