1 19 20 package org.netbeans.beaninfo.editors; 21 22 import java.awt.Color ; 23 import java.awt.Component ; 24 import java.awt.Rectangle ; 25 import java.awt.event.KeyListener ; 26 import java.beans.PropertyChangeEvent ; 27 import java.beans.PropertyChangeListener ; 28 import java.util.HashMap ; 29 import java.util.ResourceBundle ; 30 import javax.swing.JLabel ; 31 import javax.swing.JTextField ; 32 import javax.swing.UIManager ; 33 import org.netbeans.core.UIExceptions; 34 import org.openide.awt.Mnemonics; 35 import org.openide.explorer.propertysheet.PropertyEnv; 36 import org.openide.util.NbBundle; 37 38 41 public class RectangleCustomEditor extends javax.swing.JPanel 42 implements KeyListener , PropertyChangeListener { 43 static final long serialVersionUID =-9015667991684634296L; 44 45 private HashMap <JTextField , JLabel > labelMap = new HashMap <JTextField , JLabel >(); 46 private PropertyEnv env; 47 48 public RectangleCustomEditor(RectangleEditor editor, PropertyEnv env) { 49 this.env=env; 50 initComponents (); 51 this.editor = editor; 52 Rectangle rectangle = (Rectangle )editor.getValue (); 53 if (rectangle == null) rectangle = new Rectangle (0, 0, 0, 0); 54 xField.setText (Integer.toString(rectangle.x)); yField.setText (Integer.toString(rectangle.y)); widthField.setText (Integer.toString(rectangle.width)); heightField.setText (Integer.toString(rectangle.height)); 59 ResourceBundle b = NbBundle.getBundle(RectangleCustomEditor.class); 60 setBorder (new javax.swing.border.EmptyBorder (new java.awt.Insets (5, 5, 5, 5))); 61 jPanel2.setBorder (new javax.swing.border.CompoundBorder ( 62 new javax.swing.border.TitledBorder ( 63 new javax.swing.border.EtchedBorder (), 64 " " + b.getString ("CTL_Rectangle") + " "), 65 new javax.swing.border.EmptyBorder (new java.awt.Insets (5, 5, 5, 5)))); 66 67 Mnemonics.setLocalizedText(xLabel, b.getString ("CTL_X")); 68 Mnemonics.setLocalizedText(yLabel, b.getString ("CTL_Y")); 69 Mnemonics.setLocalizedText(widthLabel, b.getString ("CTL_Width")); 70 Mnemonics.setLocalizedText(heightLabel, b.getString ("CTL_Height")); 71 72 xLabel.setLabelFor(xField); 73 yLabel.setLabelFor(yField); 74 widthLabel.setLabelFor(widthField); 75 heightLabel.setLabelFor(heightField); 76 77 xField.getAccessibleContext().setAccessibleDescription(b.getString ("ACSD_CTL_X")); 78 yField.getAccessibleContext().setAccessibleDescription(b.getString ("ACSD_CTL_Y")); 79 widthField.getAccessibleContext().setAccessibleDescription(b.getString ("ACSD_CTL_Width")); 80 heightField.getAccessibleContext().setAccessibleDescription(b.getString ("ACSD_CTL_Height")); 81 82 getAccessibleContext().setAccessibleDescription(b.getString ("ACSD_CustomRectangleEditor")); 83 84 labelMap.put(widthField,widthLabel); 85 labelMap.put(xField,xLabel); 86 labelMap.put(yField,yLabel); 87 labelMap.put(heightField,heightLabel); 88 90 env.setState(PropertyEnv.STATE_NEEDS_VALIDATION); 91 env.addPropertyChangeListener(this); 92 } 93 94 public java.awt.Dimension getPreferredSize () { 95 return new java.awt.Dimension (280, 160); 96 } 97 98 private Object getPropertyValue () throws IllegalStateException { 99 try { 100 int x = Integer.parseInt (xField.getText ()); 101 int y = Integer.parseInt (yField.getText ()); 102 int width = Integer.parseInt (widthField.getText ()); 103 int height = Integer.parseInt (heightField.getText ()); 104 if ((x < 0) || (y < 0) || (width < 0) || (height < 0)) { 105 IllegalStateException ise = new IllegalStateException (); 106 UIExceptions.annotateUser(ise, null, 107 NbBundle.getMessage(RectangleCustomEditor.class, 108 "CTL_NegativeSize"), 109 null, null); 110 throw ise; 111 } 112 return new Rectangle (x, y, width, height); 113 } catch (NumberFormatException e) { 114 IllegalStateException ise = new IllegalStateException (); 115 UIExceptions.annotateUser(ise, null, 116 NbBundle.getMessage(RectangleCustomEditor.class, 117 "CTL_InvalidValue"), 118 null, null); 119 throw ise; 120 } 121 } 122 123 124 private void initComponents () { 125 setLayout (new java.awt.BorderLayout ()); 126 127 jPanel2 = new javax.swing.JPanel (); 128 jPanel2.setLayout (new java.awt.GridBagLayout ()); 129 java.awt.GridBagConstraints gridBagConstraints1; 130 131 xLabel = new javax.swing.JLabel (); 132 xLabel.setText (null); 133 134 gridBagConstraints1 = new java.awt.GridBagConstraints (); 135 gridBagConstraints1.anchor = java.awt.GridBagConstraints.WEST; 136 jPanel2.add (xLabel, gridBagConstraints1); 137 138 xField = new javax.swing.JTextField (); 139 xField.addKeyListener (this); 140 141 gridBagConstraints1 = new java.awt.GridBagConstraints (); 142 gridBagConstraints1.gridwidth = 0; 143 gridBagConstraints1.fill = java.awt.GridBagConstraints.HORIZONTAL; 144 gridBagConstraints1.insets = new java.awt.Insets (4, 8, 4, 0); 145 gridBagConstraints1.weightx = 1.0; 146 jPanel2.add (xField, gridBagConstraints1); 147 148 yLabel = new javax.swing.JLabel (); 149 yLabel.setText (null); 150 151 gridBagConstraints1 = new java.awt.GridBagConstraints (); 152 gridBagConstraints1.anchor = java.awt.GridBagConstraints.WEST; 153 jPanel2.add (yLabel, gridBagConstraints1); 154 155 yField = new javax.swing.JTextField (); 156 yField.addKeyListener(this); 157 158 gridBagConstraints1 = new java.awt.GridBagConstraints (); 159 gridBagConstraints1.gridwidth = 0; 160 gridBagConstraints1.fill = java.awt.GridBagConstraints.HORIZONTAL; 161 gridBagConstraints1.insets = new java.awt.Insets (4, 8, 4, 0); 162 gridBagConstraints1.weightx = 1.0; 163 jPanel2.add (yField, gridBagConstraints1); 164 165 widthLabel = new javax.swing.JLabel (); 166 widthLabel.setText (null); 167 168 gridBagConstraints1 = new java.awt.GridBagConstraints (); 169 gridBagConstraints1.anchor = java.awt.GridBagConstraints.WEST; 170 jPanel2.add (widthLabel, gridBagConstraints1); 171 172 widthField = new javax.swing.JTextField (); 173 widthField.addKeyListener(this); 174 175 gridBagConstraints1 = new java.awt.GridBagConstraints (); 176 gridBagConstraints1.gridwidth = 0; 177 gridBagConstraints1.fill = java.awt.GridBagConstraints.HORIZONTAL; 178 gridBagConstraints1.insets = new java.awt.Insets (4, 8, 4, 0); 179 gridBagConstraints1.weightx = 1.0; 180 jPanel2.add (widthField, gridBagConstraints1); 181 182 heightLabel = new javax.swing.JLabel (); 183 heightLabel.setText (null); 184 185 gridBagConstraints1 = new java.awt.GridBagConstraints (); 186 gridBagConstraints1.anchor = java.awt.GridBagConstraints.WEST; 187 jPanel2.add (heightLabel, gridBagConstraints1); 188 189 heightField = new javax.swing.JTextField (); 190 heightField.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 (heightField, gridBagConstraints1); 198 199 200 add (jPanel2, "Center"); 202 } 203 204 205 private void updateRectangle () { 206 try { 207 int x = Integer.parseInt (xField.getText ()); 208 int y = Integer.parseInt (yField.getText ()); 209 int width = Integer.parseInt (widthField.getText ()); 210 int height = Integer.parseInt (heightField.getText ()); 211 editor.setValue (new Rectangle (x, y, width, height)); 212 } catch (NumberFormatException e) { 213 } 215 } 216 217 public void keyPressed(java.awt.event.KeyEvent e) { 218 } 219 220 public void keyReleased(java.awt.event.KeyEvent e) { 221 if (checkValues()) { 222 updateRectangle(); 223 } 224 } 225 226 public void keyTyped(java.awt.event.KeyEvent e) { 227 } 228 229 private boolean checkValues() { 230 Component [] c = jPanel2.getComponents(); 231 boolean valid=true; 232 for (int i=0; i < c.length; i++) { 233 if (c[i] instanceof JTextField ) { 234 valid &= validFor((JTextField ) c[i]); 235 } 236 } 237 if (env != null) { 238 env.setState(valid ? env.STATE_NEEDS_VALIDATION : env.STATE_INVALID); 239 } 240 return valid; 241 } 242 243 private boolean validFor(JTextField c) { 244 String s = c.getText().trim(); 245 try { 246 Integer.parseInt(s); 247 handleValid(c); 248 return true; 249 } catch (NumberFormatException e) { 250 handleInvalid(c); 251 return false; 252 } 253 } 254 255 private void handleInvalid(JTextField c) { 256 c.setForeground(getErrorColor()); 257 findLabelFor(c).setForeground(getErrorColor()); 258 } 259 260 private void handleValid(JTextField c) { 261 c.setForeground(getForeground()); 262 findLabelFor(c).setForeground(getForeground()); 263 } 264 265 private Color getErrorColor() { 266 Color c=UIManager.getColor("nb.errorForeground"); 267 if (c == null) { 268 c = Color.RED; 269 } 270 return c; 271 } 272 273 private JLabel findLabelFor(JTextField c) { 274 return labelMap.get(c); 275 } 276 277 public void propertyChange(PropertyChangeEvent evt) { 278 if (PropertyEnv.PROP_STATE.equals(evt.getPropertyName()) && evt.getNewValue() == PropertyEnv.STATE_VALID) { 279 editor.setValue(getPropertyValue()); 280 } 281 } 282 283 private javax.swing.JPanel jPanel2; 285 private javax.swing.JLabel xLabel; 286 private javax.swing.JTextField xField; 287 private javax.swing.JLabel yLabel; 288 private javax.swing.JTextField yField; 289 private javax.swing.JLabel widthLabel; 290 private javax.swing.JTextField widthField; 291 private javax.swing.JLabel heightLabel; 292 private javax.swing.JTextField heightField; 293 295 private RectangleEditor editor; 296 297 } 298 299 | Popular Tags |