1 19 20 package org.netbeans.beaninfo.editors; 21 22 import java.awt.Dimension ; 23 import java.beans.PropertyChangeEvent ; 24 import java.beans.PropertyChangeListener ; 25 import java.util.ResourceBundle ; 26 import org.netbeans.core.UIExceptions; 27 import org.openide.awt.Mnemonics; 28 29 import org.openide.explorer.propertysheet.PropertyEnv; 30 import org.openide.util.NbBundle; 31 32 36 public class DimensionCustomEditor extends javax.swing.JPanel implements PropertyChangeListener { 37 38 static final long serialVersionUID =3718340148720193844L; 39 40 41 public DimensionCustomEditor(DimensionEditor editor) { 42 initComponents(); 43 this.editor = editor; 44 this.editor.env.setState(PropertyEnv.STATE_NEEDS_VALIDATION); 45 this.editor.env.addPropertyChangeListener(this); 46 47 Dimension dimension = (Dimension )editor.getValue (); 48 49 if (dimension == null) dimension = new Dimension (0, 0); 50 jLabel1.setText(NbBundle.getMessage(DimensionCustomEditor.class, "CTL_Dimension")); 51 Mnemonics.setLocalizedText(widthLabel, NbBundle.getMessage(DimensionCustomEditor.class, "CTL_Width")); 52 widthLabel.setLabelFor(widthField); 53 Mnemonics.setLocalizedText(heightLabel, NbBundle.getMessage(DimensionCustomEditor.class, "CTL_Height")); 54 heightLabel.setLabelFor(heightField); 55 56 widthField.setText (String.valueOf(dimension.width)); 57 heightField.setText (String.valueOf(dimension.height)); 58 60 widthField.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(DimensionCustomEditor.class, "ACSD_CTL_Width")); 61 heightField.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(DimensionCustomEditor.class, "ACSD_CTL_Height")); 62 getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(DimensionCustomEditor.class, "ACSD_DimensionCustomEditor")); 63 } 64 65 public java.awt.Dimension getPreferredSize () { 66 return new java.awt.Dimension (280, 160); 67 } 68 69 private Object getPropertyValue () throws IllegalStateException { 70 try { 71 int width = Integer.parseInt (widthField.getText ()); 72 int height = Integer.parseInt (heightField.getText ()); 73 if ((width < 0) || (height < 0)) { 74 IllegalStateException ise = new IllegalStateException (); 75 UIExceptions.annotateUser(ise, null, 76 NbBundle.getMessage(DimensionCustomEditor.class, "CTL_NegativeSize"), null, null); 77 throw ise; 78 } 79 return new Dimension (width, height); 80 } catch (NumberFormatException e) { 81 IllegalStateException ise = new IllegalStateException (); 82 UIExceptions.annotateUser(ise, null, 83 NbBundle.getMessage(DimensionCustomEditor.class, "CTL_NegativeSize"), null, null); 84 throw ise; 85 } 86 } 87 88 public void propertyChange(PropertyChangeEvent evt) { 89 if (PropertyEnv.PROP_STATE.equals(evt.getPropertyName()) && evt.getNewValue() == PropertyEnv.STATE_VALID) { 90 editor.setValue(getPropertyValue()); 91 } 92 } 93 94 99 private void initComponents() { insidePanel = new javax.swing.JPanel (); 101 jLabel1 = new javax.swing.JLabel (); 102 widthLabel = new javax.swing.JLabel (); 103 widthField = new javax.swing.JTextField (); 104 heightLabel = new javax.swing.JLabel (); 105 heightField = new javax.swing.JTextField (); 106 107 setLayout(new java.awt.GridBagLayout ()); 108 java.awt.GridBagConstraints gridBagConstraints1; 109 110 insidePanel.setLayout(new java.awt.GridBagLayout ()); 111 java.awt.GridBagConstraints gridBagConstraints2; 112 113 jLabel1.setText("jLabel1"); 114 jLabel1.setLabelFor(insidePanel); 115 gridBagConstraints2 = new java.awt.GridBagConstraints (); 116 gridBagConstraints2.gridx = 0; 117 gridBagConstraints2.gridy = 0; 118 gridBagConstraints2.gridwidth = java.awt.GridBagConstraints.REMAINDER; 119 gridBagConstraints2.insets = new java.awt.Insets (12, 12, 0, 0); 120 gridBagConstraints2.anchor = java.awt.GridBagConstraints.NORTHWEST; 121 gridBagConstraints2.weightx = 1.0; 122 insidePanel.add(jLabel1, gridBagConstraints2); 123 124 widthLabel.setText("jLabel2"); 125 gridBagConstraints2 = new java.awt.GridBagConstraints (); 126 gridBagConstraints2.gridx = 0; 127 gridBagConstraints2.gridy = 1; 128 gridBagConstraints2.insets = new java.awt.Insets (12, 17, 0, 0); 129 gridBagConstraints2.anchor = java.awt.GridBagConstraints.NORTHWEST; 130 insidePanel.add(widthLabel, gridBagConstraints2); 131 132 widthField.setColumns(5); 133 widthField.addActionListener(new java.awt.event.ActionListener () { 134 public void actionPerformed(java.awt.event.ActionEvent evt) { 135 updateInsets(evt); 136 } 137 }); 138 139 gridBagConstraints2 = new java.awt.GridBagConstraints (); 140 gridBagConstraints2.gridx = 1; 141 gridBagConstraints2.gridy = 1; 142 gridBagConstraints2.insets = new java.awt.Insets (12, 5, 0, 12); 143 gridBagConstraints2.anchor = java.awt.GridBagConstraints.NORTHWEST; 144 insidePanel.add(widthField, gridBagConstraints2); 145 146 heightLabel.setText("jLabel3"); 147 gridBagConstraints2 = new java.awt.GridBagConstraints (); 148 gridBagConstraints2.gridx = 0; 149 gridBagConstraints2.gridy = 2; 150 gridBagConstraints2.insets = new java.awt.Insets (5, 17, 0, 0); 151 gridBagConstraints2.anchor = java.awt.GridBagConstraints.NORTHWEST; 152 gridBagConstraints2.weighty = 1.0; 153 insidePanel.add(heightLabel, gridBagConstraints2); 154 155 heightField.setColumns(5); 156 heightField.addActionListener(new java.awt.event.ActionListener () { 157 public void actionPerformed(java.awt.event.ActionEvent evt) { 158 updateInsets(evt); 159 } 160 }); 161 162 gridBagConstraints2 = new java.awt.GridBagConstraints (); 163 gridBagConstraints2.gridx = 1; 164 gridBagConstraints2.gridy = 2; 165 gridBagConstraints2.insets = new java.awt.Insets (5, 5, 0, 12); 166 gridBagConstraints2.anchor = java.awt.GridBagConstraints.NORTHWEST; 167 insidePanel.add(heightField, gridBagConstraints2); 168 169 gridBagConstraints1 = new java.awt.GridBagConstraints (); 170 gridBagConstraints1.fill = java.awt.GridBagConstraints.BOTH; 171 gridBagConstraints1.weightx = 1.0; 172 gridBagConstraints1.weighty = 1.0; 173 add(insidePanel, gridBagConstraints1); 174 175 } 177 private void updateInsets(java.awt.event.ActionEvent evt) { try { 180 int width = Integer.parseInt (widthField.getText ()); 181 int height = Integer.parseInt (heightField.getText ()); 182 editor.setValue (new Dimension (width, height)); 183 } catch (NumberFormatException e) { 184 } 186 } 188 private javax.swing.JPanel insidePanel; 190 private javax.swing.JLabel jLabel1; 191 private javax.swing.JLabel widthLabel; 192 private javax.swing.JTextField widthField; 193 private javax.swing.JLabel heightLabel; 194 private javax.swing.JTextField heightField; 195 197 private DimensionEditor editor; 198 } 199 200 | Popular Tags |