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