1 19 20 package org.netbeans.modules.editor.options; 21 22 import java.awt.Insets ; 23 import javax.swing.event.DocumentEvent ; 24 import javax.swing.event.DocumentListener ; 25 26 import org.openide.NotifyDescriptor; 27 import org.openide.explorer.propertysheet.PropertyEnv; 28 import org.openide.util.NbBundle; 29 30 36 public class ScrollInsetsCustomEditor extends javax.swing.JPanel implements DocumentListener { 37 38 39 static final long serialVersionUID =-1472891501739636852L; 40 41 private ScrollInsetsEditor editor; 42 private PropertyEnv env; 43 44 45 public ScrollInsetsCustomEditor(ScrollInsetsEditor editor, PropertyEnv env) { 46 initComponents (); 47 48 this.editor = editor; 49 this.env = env; 50 51 Insets insets = (Insets )editor.getValue(); 52 53 if (insets == null) insets = new Insets ( 0, 0, 0, 0 ); 54 55 getAccessibleContext ().setAccessibleDescription (getBundleString("ACSD_SICE")); topLabel.setDisplayedMnemonic (getBundleString("SICE_Top_Mnemonic").charAt(0)); bottomLabel.setDisplayedMnemonic (getBundleString("SICE_Bottom_Mnemonic").charAt(0)); leftLabel.setDisplayedMnemonic (getBundleString("SICE_Left_Mnemonic").charAt(0)); rightLabel.setDisplayedMnemonic (getBundleString("SICE_Right_Mnemonic").charAt(0)); topField.setText (int2percent (insets.top )); 61 leftField.setText (int2percent (insets.left )); 62 bottomField.setText (int2percent (insets.bottom)); 63 rightField.setText (int2percent (insets.right)); 64 topField.getAccessibleContext().setAccessibleDescription(getBundleString("ACSD_SICE_Top")); leftField.getAccessibleContext().setAccessibleDescription(getBundleString("ACSD_SICE_Left")); bottomField.getAccessibleContext().setAccessibleDescription(getBundleString("ACSD_SICE_Bottom")); rightField.getAccessibleContext().setAccessibleDescription(getBundleString("ACSD_SICE_Right")); 69 76 77 setPreferredSize(new java.awt.Dimension (320, getPreferredSize().height)); 78 this.env.setState(PropertyEnv.STATE_VALID); 79 topField.getDocument().addDocumentListener(this); 80 leftField.getDocument().addDocumentListener(this); 81 bottomField.getDocument().addDocumentListener(this); 82 rightField.getDocument().addDocumentListener(this); 83 } 84 85 private String getBundleString(String s) { 86 return NbBundle.getMessage(ScrollInsetsCustomEditor.class, s); 87 } 88 89 90 102 103 104 public static String int2percent( int i ) { 105 if( i < 0 ) return( "" + (-i) + "%" ); else return( "" + i ); 107 } 108 109 private int percent2int( String val ) throws NumberFormatException { 110 val = val.trim(); 111 if( val.endsWith( "%" ) ) { return -Math.abs( Integer.parseInt( val.substring( 0, val.length() - 1 ) ) ); 113 } else { 114 return Integer.parseInt( val ); 115 } 116 } 117 118 Insets getValue() throws NumberFormatException { 119 int top = percent2int( topField.getText() ); 120 int left = percent2int( leftField.getText() ); 121 int bottom = percent2int( bottomField.getText() ); 122 int right = percent2int( rightField.getText() ); 123 return new Insets ( top, left, bottom, right ); 124 } 125 126 131 private void initComponents() { 133 java.awt.GridBagConstraints gridBagConstraints; 134 135 jPanel2 = new javax.swing.JPanel (); 136 topLabel = new javax.swing.JLabel (); 137 topField = new javax.swing.JTextField (); 138 leftLabel = new javax.swing.JLabel (); 139 leftField = new javax.swing.JTextField (); 140 bottomLabel = new javax.swing.JLabel (); 141 bottomField = new javax.swing.JTextField (); 142 rightLabel = new javax.swing.JLabel (); 143 rightField = new javax.swing.JTextField (); 144 145 setLayout(new javax.swing.BoxLayout (this, javax.swing.BoxLayout.X_AXIS)); 146 147 setBorder(new javax.swing.border.EmptyBorder ( new java.awt.Insets ( 12, 12, 11, 11) ) ); 148 jPanel2.setLayout(new java.awt.GridBagLayout ()); 149 150 topLabel.setLabelFor(topField); 151 topLabel.setText(getBundleString("SICE_Top")); 152 gridBagConstraints = new java.awt.GridBagConstraints (); 153 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 154 gridBagConstraints.insets = new java.awt.Insets (5, 0, 0, 12); 155 jPanel2.add(topLabel, gridBagConstraints); 156 157 gridBagConstraints = new java.awt.GridBagConstraints (); 158 gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; 159 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 160 gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST; 161 gridBagConstraints.weightx = 1.0; 162 jPanel2.add(topField, gridBagConstraints); 163 164 leftLabel.setLabelFor(leftField); 165 leftLabel.setText(getBundleString("SICE_Left")); 166 gridBagConstraints = new java.awt.GridBagConstraints (); 167 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 168 gridBagConstraints.insets = new java.awt.Insets (5, 0, 0, 12); 169 jPanel2.add(leftLabel, gridBagConstraints); 170 171 gridBagConstraints = new java.awt.GridBagConstraints (); 172 gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; 173 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 174 gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST; 175 gridBagConstraints.weightx = 1.0; 176 gridBagConstraints.insets = new java.awt.Insets (5, 0, 0, 0); 177 jPanel2.add(leftField, gridBagConstraints); 178 179 bottomLabel.setLabelFor(bottomField); 180 bottomLabel.setText(getBundleString("SICE_Bottom")); 181 gridBagConstraints = new java.awt.GridBagConstraints (); 182 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 183 gridBagConstraints.insets = new java.awt.Insets (5, 0, 0, 12); 184 jPanel2.add(bottomLabel, gridBagConstraints); 185 186 gridBagConstraints = new java.awt.GridBagConstraints (); 187 gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; 188 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 189 gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST; 190 gridBagConstraints.weightx = 1.0; 191 gridBagConstraints.insets = new java.awt.Insets (5, 0, 0, 0); 192 jPanel2.add(bottomField, gridBagConstraints); 193 194 rightLabel.setLabelFor(rightField); 195 rightLabel.setText(getBundleString("SICE_Right")); 196 gridBagConstraints = new java.awt.GridBagConstraints (); 197 gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST; 198 gridBagConstraints.insets = new java.awt.Insets (7, 0, 0, 12); 199 jPanel2.add(rightLabel, gridBagConstraints); 200 201 gridBagConstraints = new java.awt.GridBagConstraints (); 202 gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; 203 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 204 gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST; 205 gridBagConstraints.weightx = 1.0; 206 gridBagConstraints.weighty = 1.0; 207 gridBagConstraints.insets = new java.awt.Insets (5, 0, 0, 0); 208 jPanel2.add(rightField, gridBagConstraints); 209 210 add(jPanel2); 211 212 } 214 public void insertUpdate(DocumentEvent e) { 215 updateInsets(); 216 } 217 218 public void removeUpdate(DocumentEvent e) { 219 updateInsets(); 220 } 221 222 public void changedUpdate(DocumentEvent e) { 223 updateInsets(); 224 } 225 226 private void updateInsets() { 227 try { 228 editor.setValue(getValue()); 229 env.setState(PropertyEnv.STATE_VALID); 230 } catch (NumberFormatException e) { 231 env.setState(PropertyEnv.STATE_INVALID); 232 } 233 } 234 235 private javax.swing.JTextField bottomField; 237 private javax.swing.JLabel bottomLabel; 238 private javax.swing.JPanel jPanel2; 239 private javax.swing.JTextField leftField; 240 private javax.swing.JLabel leftLabel; 241 private javax.swing.JTextField rightField; 242 private javax.swing.JLabel rightLabel; 243 private javax.swing.JTextField topField; 244 private javax.swing.JLabel topLabel; 245 247 } 248 | Popular Tags |