1 19 20 package org.netbeans.modules.form; 21 22 import org.openide.*; 23 import org.openide.nodes.*; 24 import org.openide.explorer.propertysheet.editors.EnhancedCustomPropertyEditor; 25 import org.openide.explorer.propertysheet.PropertyEnv; 26 import org.openide.explorer.propertysheet.ExPropertyEditor; 27 import org.openide.util.HelpCtx; 28 import org.openide.util.Utilities; 29 30 import java.awt.*; 31 import java.beans.PropertyEditor ; 32 import javax.swing.*; 33 34 38 public class FormCustomEditor extends JPanel 39 implements EnhancedCustomPropertyEditor 40 { 41 private static final int DEFAULT_WIDTH = 350; 42 private static final int DEFAULT_HEIGHT = 350; 43 44 47 private FormPropertyEditor editor; 48 private PropertyEditor[] allEditors; 49 private Component[] allCustomEditors; 50 private boolean[] validValues; 51 52 private String preCode; 53 private String postCode; 54 55 56 public FormCustomEditor(FormPropertyEditor editor, 57 Component currentCustomEditor) 58 { 59 initComponents(); 60 61 advancedButton.setText(FormUtils.getBundleString("CTL_Advanced")); if (editor.getProperty() instanceof RADProperty) 65 advancedButton.addActionListener(new java.awt.event.ActionListener () { 66 public void actionPerformed(java.awt.event.ActionEvent evt) { 67 showAdvancedSettings(); 68 } 69 }); 70 else 71 advancedButton.setEnabled(false); 72 73 jLabel1.setText(FormUtils.getBundleString("LAB_SelectMode")); jLabel1.setDisplayedMnemonic((FormUtils.getBundleString( 75 "LAB_SelectMode.mnemonic").charAt(0))); jLabel1.setLabelFor(editorsCombo); 77 78 this.editor = editor; 79 preCode = editor.getProperty().getPreCode(); 80 postCode = editor.getProperty().getPostCode(); 81 allEditors = editor.getAllEditors(); 82 83 PropertyEditor currentEditor = editor.getCurrentEditor(); 84 int currentIndex; 85 86 if (currentEditor != null) { 87 currentIndex = -1; 88 for (int i=0; i < allEditors.length; i++) 89 if (currentEditor.getClass().equals(allEditors[i].getClass())) { 90 currentIndex = i; 91 allEditors[i] = currentEditor; 92 break; 93 } 94 if (currentIndex == -1) { 95 PropertyEditor[] editors = new PropertyEditor[allEditors.length+1]; 97 editors[0] = currentEditor; 98 System.arraycopy(allEditors, 0, editors, 1, allEditors.length); 99 allEditors = editors; 100 currentIndex = 0; 101 } 102 } 103 else currentIndex = 0; 104 105 allCustomEditors = new Component[allEditors.length]; 106 validValues = new boolean[allEditors.length]; 107 108 PropertyEnv env = editor.getPropertyEnv(); 109 Object currentValue = editor.getValue(); 110 111 for (int i=0; i < allEditors.length; i++) { 114 PropertyEditor prEd = allEditors[i]; 115 editor.getPropertyContext().initPropertyEditor(prEd); 116 117 boolean valueSet = false; 118 if (i == currentIndex) { valueSet = true; 120 } 121 else { 122 if (env != null && prEd instanceof ExPropertyEditor) 123 ((ExPropertyEditor)prEd).attachEnv(env); 124 125 if (currentValue != null) { 126 try { 127 if (editor.getPropertyType().isAssignableFrom( 128 currentValue.getClass())) 129 { prEd.setValue(currentValue); 132 valueSet = true; 133 } 134 else if (currentValue instanceof FormDesignValue) { 135 Object realValue = ((FormDesignValue)currentValue).getDesignValue(); 137 if (realValue != FormDesignValue.IGNORED_VALUE) { 138 prEd.setValue(realValue); 140 valueSet = true; 141 } 142 } 143 } 144 catch (IllegalArgumentException ex) {} } 146 148 if (!valueSet) { 149 Object defaultValue = editor.getProperty().getDefaultValue(); 152 if (defaultValue != BeanSupport.NO_VALUE) { 153 prEd.setValue(defaultValue); 154 valueSet = true; 155 } 156 } 160 } 161 validValues[i] = valueSet; 162 163 String editorName = prEd instanceof NamedPropertyEditor ? 164 ((NamedPropertyEditor)prEd).getDisplayName() : 165 Utilities.getShortClassName(prEd.getClass()); 166 167 Component custEd = null; 168 if (i == currentIndex) 169 custEd = currentCustomEditor; 170 else if (prEd.supportsCustomEditor()) 171 custEd = prEd.getCustomEditor(); 172 173 if (custEd == null || custEd instanceof Window) { 174 JPanel p = new JPanel(new GridBagLayout()); 175 JLabel label = new JLabel( 176 FormUtils.getBundleString("CTL_PropertyEditorDoesNot")); p.add(label); 178 p.getAccessibleContext().setAccessibleDescription(label.getText()); 179 custEd = p; 180 } 181 182 allCustomEditors[i] = custEd; 183 cardPanel.add(editorName, custEd); 184 editorsCombo.addItem(editorName); 185 } 186 187 editorsCombo.setSelectedIndex(currentIndex); 188 CardLayout cl = (CardLayout) cardPanel.getLayout(); 189 cl.show(cardPanel, (String ) editorsCombo.getSelectedItem()); 190 191 editorsCombo.addActionListener(new java.awt.event.ActionListener () { 192 public void actionPerformed(java.awt.event.ActionEvent evt) { 193 CardLayout cl2 = (CardLayout) cardPanel.getLayout(); 194 cl2.show(cardPanel, (String ) editorsCombo.getSelectedItem()); 195 196 int i = editorsCombo.getSelectedIndex(); 197 HelpCtx helpCtx = i < 0 ? null : 198 HelpCtx.findHelp(cardPanel.getComponent(i)); 199 String helpID = helpCtx != null ? helpCtx.getHelpID() : ""; HelpCtx.setHelpIDString(FormCustomEditor.this, helpID); 201 202 updateAccessibleDescription(i < 0 ? null : cardPanel.getComponent(i)); 203 } 204 }); 205 206 updateAccessibleDescription(cardPanel.getComponent(currentIndex)); 207 advancedButton.getAccessibleContext().setAccessibleDescription( 208 FormUtils.getBundleString("ACSD_CTL_Advanced")); editorsCombo.getAccessibleContext().setAccessibleDescription( 210 FormUtils.getBundleString("ACSD_BTN_SelectMode")); } 212 213 private void updateAccessibleDescription(Component comp) { 214 if (comp instanceof javax.accessibility.Accessible 215 && comp.getAccessibleContext().getAccessibleDescription() != null) { 216 217 getAccessibleContext().setAccessibleDescription( 218 FormUtils.getFormattedBundleString( 219 "ACSD_FormCustomEditor", new Object [] { 221 comp.getAccessibleContext().getAccessibleDescription() 222 } 223 ) 224 ); 225 } else { 226 getAccessibleContext().setAccessibleDescription(null); 227 } 228 } 229 230 235 private void initComponents() { editorsCombo = new javax.swing.JComboBox (); 237 jLabel1 = new javax.swing.JLabel (); 238 jPanel1 = new javax.swing.JPanel (); 239 cardPanel = new javax.swing.JPanel (); 240 advancedButton = new javax.swing.JButton (); 241 242 setLayout(new java.awt.GridBagLayout ()); 243 java.awt.GridBagConstraints gridBagConstraints1; 244 245 gridBagConstraints1 = new java.awt.GridBagConstraints (); 246 gridBagConstraints1.gridx = 1; 247 gridBagConstraints1.gridy = 0; 248 gridBagConstraints1.gridwidth = java.awt.GridBagConstraints.REMAINDER; 249 gridBagConstraints1.insets = new java.awt.Insets (12, 5, 0, 11); 250 gridBagConstraints1.anchor = java.awt.GridBagConstraints.WEST; 251 add(editorsCombo, gridBagConstraints1); 252 253 jLabel1.setText("jLabel1"); 254 jLabel1.setLabelFor(editorsCombo); 255 gridBagConstraints1 = new java.awt.GridBagConstraints (); 256 gridBagConstraints1.gridx = 0; 257 gridBagConstraints1.gridy = 0; 258 gridBagConstraints1.insets = new java.awt.Insets (12, 12, 0, 0); 259 gridBagConstraints1.anchor = java.awt.GridBagConstraints.WEST; 260 add(jLabel1, gridBagConstraints1); 261 262 jPanel1.setLayout(new java.awt.GridBagLayout ()); 263 java.awt.GridBagConstraints gridBagConstraints2; 264 265 jPanel1.setBorder(new javax.swing.border.EtchedBorder ()); 266 cardPanel.setLayout(new java.awt.CardLayout ()); 267 268 gridBagConstraints2 = new java.awt.GridBagConstraints (); 269 gridBagConstraints2.gridx = 0; 270 gridBagConstraints2.gridy = 0; 271 gridBagConstraints2.fill = java.awt.GridBagConstraints.BOTH; 272 gridBagConstraints2.insets = new java.awt.Insets (12, 12, 11, 11); 273 gridBagConstraints2.anchor = java.awt.GridBagConstraints.NORTHWEST; 274 gridBagConstraints2.weightx = 1.0; 275 gridBagConstraints2.weighty = 1.0; 276 jPanel1.add(cardPanel, gridBagConstraints2); 277 278 gridBagConstraints1 = new java.awt.GridBagConstraints (); 279 gridBagConstraints1.gridwidth = java.awt.GridBagConstraints.REMAINDER; 280 gridBagConstraints1.fill = java.awt.GridBagConstraints.BOTH; 281 gridBagConstraints1.insets = new java.awt.Insets (12, 12, 0, 11); 282 gridBagConstraints1.weightx = 1.0; 283 gridBagConstraints1.weighty = 1.0; 284 add(jPanel1, gridBagConstraints1); 285 286 advancedButton.setText("jButton1"); 287 gridBagConstraints1 = new java.awt.GridBagConstraints (); 288 gridBagConstraints1.gridwidth = java.awt.GridBagConstraints.REMAINDER; 289 gridBagConstraints1.gridheight = java.awt.GridBagConstraints.REMAINDER; 290 gridBagConstraints1.insets = new java.awt.Insets (12, 12, 0, 11); 291 gridBagConstraints1.anchor = java.awt.GridBagConstraints.WEST; 292 add(advancedButton, gridBagConstraints1); 293 294 } 296 297 private javax.swing.JComboBox editorsCombo; 299 private javax.swing.JLabel jLabel1; 300 private javax.swing.JPanel jPanel1; 301 private javax.swing.JPanel cardPanel; 302 private javax.swing.JButton advancedButton; 303 305 public Dimension getPreferredSize() { 306 Dimension inh = super.getPreferredSize(); 307 return new Dimension(Math.max(inh.width, DEFAULT_WIDTH), Math.max(inh.height, DEFAULT_HEIGHT)); 308 } 309 310 private void showAdvancedSettings() { 311 FormCustomEditorAdvanced fcea = new FormCustomEditorAdvanced(preCode, postCode); 312 DialogDescriptor dd = new DialogDescriptor( 313 fcea, 314 FormUtils.getFormattedBundleString( 315 "FMT_CTL_AdvancedInitializationCode", new Object [] { editor.getProperty().getName() })); 317 318 dd.setHelpCtx(new HelpCtx("gui.source.modifying.property")); DialogDisplayer.getDefault().createDialog(dd).show(); 320 321 if (dd.getValue() == DialogDescriptor.OK_OPTION) { 322 preCode = fcea.getPreCode(); 323 postCode = fcea.getPostCode(); 324 } 325 } 326 327 330 335 public Object getPropertyValue() throws IllegalStateException { 336 int currentIndex = editorsCombo.getSelectedIndex(); 337 PropertyEditor currentEditor = currentIndex > -1 ? 338 allEditors[currentIndex] : null; 339 Component currentCustomEditor = currentIndex > -1 ? 340 allCustomEditors[currentIndex] : null; 341 Object value; 342 343 if (currentCustomEditor instanceof EnhancedCustomPropertyEditor) { 344 value = ((EnhancedCustomPropertyEditor) currentCustomEditor) 346 .getPropertyValue(); 347 } 348 else if (currentIndex > -1) { 349 value = validValues[currentIndex] ? currentEditor.getValue() : 350 BeanSupport.NO_VALUE; 351 } 352 else value = editor.getValue(); 353 354 if (currentIndex > -1) { 358 Object [] nodes = editor.getPropertyEnv().getBeans(); 359 if (nodes == null || nodes.length <= 1) { 360 FormProperty prop = editor.getProperty(); 361 362 prop.setPreCode(preCode); 363 prop.setPostCode(postCode); 364 365 value = new FormProperty.ValueWithEditor(value, currentEditor); 366 } 367 else { String propName = editor.getProperty().getName(); 369 370 for (int i=0; i < nodes.length; i++) { 371 if (!(nodes[i] instanceof Node)) 372 break; 374 Node node = (Node) nodes[i]; 375 FormPropertyCookie propCookie = (FormPropertyCookie) 376 node.getCookie(FormPropertyCookie.class); 377 if (propCookie == null) 378 break; 380 FormProperty prop = propCookie.getProperty(propName); 381 if (prop == null) 382 continue; 384 prop.setPreCode(preCode); 385 prop.setPostCode(postCode); 386 } 387 388 value = new FormProperty.ValueWithEditor(value, currentIndex); 389 } 390 } 391 392 return value; 393 } 394 } 395 | Popular Tags |