/** * Set the object to be edited. * * @param value The object to be edited. */ public void setObject(Object value) { if (!(_type.isInstance(value))) { throw new IllegalArgumentException(value.getClass() + " is not of type " + _type); } _value = value; // Disable event generation. _squelchChangeEvents = true; // Iterate over each property, doing a lookup on the associated editor // and setting the editor's value to the value of the property. Iterator it = _prop2Editor.keySet().iterator(); while (it.hasNext()) { PropertyDescriptor desc = (PropertyDescriptor) it.next(); PropertyEditor editor = (PropertyEditor) _prop2Editor.get(desc); Method reader = desc.getReadMethod(); if (reader != null) { try { Object val = reader.invoke(_value, null); editor.setValue(val); } catch (IllegalAccessException ex) { ex.printStackTrace(); } catch (InvocationTargetException ex) { ex.getTargetException().printStackTrace(); } } } // Enable event generation. _squelchChangeEvents = false; }
@Override public void actionPerformed(java.awt.event.ActionEvent e) { try { PropertyEditor propEd = property.getPropertyEditor(); propEd.setValue(property.getValue()); final Component custEditor = propEd.getCustomEditor(); Object[] options = buttons(); DialogDescriptor descriptor = new DialogDescriptor( custEditor, (String) getValue(Action.NAME), true, options, DialogDescriptor.CANCEL_OPTION, DialogDescriptor.DEFAULT_ALIGN, HelpCtx.DEFAULT_HELP, (ActionEvent e1) -> { try { String action = e1.getActionCommand(); switch (action) { case OK_COMMAND: Object value = property.getPropertyEditor().getValue(); property.setValue(value); break; case RESTORE_COMMAND: property.restoreDefaultValue(); break; } dialog.dispose(); } catch (Exception ex) { NotifyDescriptor descriptor2 = new NotifyDescriptor.Message( NbBundle.getMessage(PropertyAction.class, "MSG_InvalidValue")); // NOI18N DialogDisplayer.getDefault().notify(descriptor2); } }); descriptor.setClosingOptions(new Object[0]); dialog = DialogDisplayer.getDefault().createDialog(descriptor); dialog.setVisible(true); dialog = null; } catch (Exception ex) { ErrorManager.getDefault().notify(ex); } }
public void propertyChange(PropertyChangeEvent e) { if (_squelchChangeEvents) return; PropertyEditor editor = (PropertyEditor) e.getSource(); PropertyDescriptor prop = (PropertyDescriptor) _editor2Prop.get(editor); Method writer = prop.getWriteMethod(); if (writer != null) { try { Object[] params = {editor.getValue()}; writer.invoke(_value, params); setObject(_value); firePropertyChange(_value, prop.getName(), null, editor.getValue()); } catch (IllegalAccessException ex) { ex.printStackTrace(); } catch (InvocationTargetException ex) { ex.getTargetException().printStackTrace(); } } }
public static PropertyAction createIfEditable(RADProperty<?> property) { PropertyEditor propEd = property.getPropertyEditor(); return propEd != null && propEd.supportsCustomEditor() ? new PropertyAction(property) : null; }
/** * Standard constructor. * * @param type Type that you are going to be creating and editor for. * @param readOnly Set to true to create a read-only customizer. */ public DynamicCustomizer(Class type, boolean readOnly) { super(new GridBagLayout()); _readOnly = readOnly; _type = type; LabelFieldGBC gbc = new LabelFieldGBC(); try { BeanInfo info = Introspector.getBeanInfo(type); // Set up pretty display stuff. setBorder(BorderFactory.createTitledBorder(info.getBeanDescriptor().getDisplayName())); setToolTipText(info.getBeanDescriptor().getShortDescription()); // Get the properties and sort them. PropertyDescriptor[] props = info.getPropertyDescriptors(); Arrays.sort(props, new PropertyComparator()); for (int i = 0; i < props.length; i++) { // Ignore the "class" property, if it is provided. if (props[i].getName().equals("class")) continue; // Create a label for the field. JLabel label = new JLabel(props[i].getDisplayName() + ":"); // Lookup the editor. PropertyEditor editor = getEditorForProperty(props[i]); // Add a listener to the editor so we know when to update // the bean's fields. editor.addPropertyChangeListener(_eListener); // XXX What we need to do right here is provide a component // that makes use of the "paintable" capability of the editor. Component comp = editor.getCustomEditor(); if (comp == null) { comp = new JLabel("<No editor available.>"); ((JLabel) comp).setBorder(BorderFactory.createEtchedBorder()); } // See if it is a read-only property. If so, then just // display it. if (_readOnly || props[i].getWriteMethod() == null) { comp.setEnabled(false); } // Setup the accellerator key. label.setLabelFor(comp); label.setDisplayedMnemonic(label.getText().charAt(0)); // Set the tool tip text, if any. String tip = props[i].getShortDescription(); if (tip != null) { label.setToolTipText(tip); if (comp instanceof JComponent) { ((JComponent) comp).setToolTipText(tip); } } // Add the label and fields. add(label, gbc.forLabel()); add(comp, gbc.forField()); // Set the mappings between editor and property, etc. for // quick lookup later. _prop2Editor.put(props[i], editor); _editor2Prop.put(editor, props[i]); } // Filler... add(new JLabel(), gbc.forLastLabel()); } catch (Exception ex) { ex.printStackTrace(); } }