Example usage for java.beans PropertyEditor supportsCustomEditor

List of usage examples for java.beans PropertyEditor supportsCustomEditor

Introduction

In this page you can find the example usage for java.beans PropertyEditor supportsCustomEditor.

Prototype

boolean supportsCustomEditor();

Source Link

Document

Determines whether this property editor supports a custom editor.

Usage

From source file:org.apache.jmeter.testbeans.gui.GenericTestBeanCustomizer.java

/**
 * Create a customizer for a given test bean type.
 *
 * @param testBeanClass//from ww  w  .  j ava 2 s .  c o m
 *            a subclass of TestBean
 * @see org.apache.jmeter.testbeans.TestBean
 */
GenericTestBeanCustomizer(BeanInfo beanInfo) {
    super();

    this.beanInfo = beanInfo;

    // Get and sort the property descriptors:
    descriptors = beanInfo.getPropertyDescriptors();
    Arrays.sort(descriptors, new PropertyComparator(beanInfo));

    // Obtain the propertyEditors:
    editors = new PropertyEditor[descriptors.length];
    int scriptLanguageIndex = 0;
    int textAreaEditorIndex = 0;
    for (int i = 0; i < descriptors.length; i++) { // Index is also used for accessing editors array
        PropertyDescriptor descriptor = descriptors[i];
        String name = descriptor.getName();

        // Don't get editors for hidden or non-read-write properties:
        if (TestBeanHelper.isDescriptorIgnored(descriptor)) {
            log.debug("Skipping editor for property " + name);
            editors[i] = null;
            continue;
        }

        PropertyEditor propertyEditor;
        Object guiType = descriptor.getValue(GUITYPE);
        if (guiType instanceof TypeEditor) {
            propertyEditor = ((TypeEditor) guiType).getInstance(descriptor);
        } else if (guiType instanceof Class && Enum.class.isAssignableFrom((Class<?>) guiType)) {
            @SuppressWarnings("unchecked") // we check the class type above
            final Class<? extends Enum<?>> enumClass = (Class<? extends Enum<?>>) guiType;
            propertyEditor = new EnumEditor(descriptor, enumClass,
                    (ResourceBundle) descriptor.getValue(GenericTestBeanCustomizer.RESOURCE_BUNDLE));
        } else {
            Class<?> editorClass = descriptor.getPropertyEditorClass();
            if (log.isDebugEnabled()) {
                log.debug("Property " + name + " has editor class " + editorClass);
            }

            if (editorClass != null) {
                try {
                    propertyEditor = (PropertyEditor) editorClass.newInstance();
                } catch (InstantiationException | IllegalAccessException e) {
                    log.error("Can't create property editor.", e);
                    throw new Error(e.toString());
                }
            } else {
                Class<?> c = descriptor.getPropertyType();
                propertyEditor = PropertyEditorManager.findEditor(c);
            }
        }

        if (propertyEditor == null) {
            log.warn("No editor for property: " + name + " type: " + descriptor.getPropertyType() + " in bean: "
                    + beanInfo.getBeanDescriptor().getDisplayName());
            editors[i] = null;
            continue;
        }

        if (log.isDebugEnabled()) {
            log.debug("Property " + name + " has property editor " + propertyEditor);
        }

        validateAttributes(descriptor, propertyEditor);

        if (!propertyEditor.supportsCustomEditor()) {
            propertyEditor = createWrapperEditor(propertyEditor, descriptor);

            if (log.isDebugEnabled()) {
                log.debug("Editor for property " + name + " is wrapped in " + propertyEditor);
            }
        }
        if (propertyEditor instanceof TestBeanPropertyEditor) {
            ((TestBeanPropertyEditor) propertyEditor).setDescriptor(descriptor);
        }

        if (propertyEditor instanceof TextAreaEditor) {
            textAreaEditorIndex = i;
        }
        if (propertyEditor.getCustomEditor() instanceof JScrollPane) {
            scrollerCount++;
        }

        editors[i] = propertyEditor;

        // Initialize the editor with the provided default value or null:
        setEditorValue(i, descriptor.getValue(DEFAULT));

        if (name.equals("scriptLanguage")) {
            scriptLanguageIndex = i;
        }

    }
    // In case of BSF and JSR elements i want to add textAreaEditor as a listener to scriptLanguage ComboBox.
    String beanName = this.beanInfo.getBeanDescriptor().getName();
    if (beanName.startsWith("BSF") || beanName.startsWith("JSR223")) { // $NON-NLS-1$ $NON-NLS-2$
        WrapperEditor we = (WrapperEditor) editors[scriptLanguageIndex];
        TextAreaEditor tae = (TextAreaEditor) editors[textAreaEditorIndex];
        we.addChangeListener(tae);
    }

    // Obtain message formats:
    propertyFieldLabelMessage = new MessageFormat(JMeterUtils.getResString("property_as_field_label")); //$NON-NLS-1$
    propertyToolTipMessage = new MessageFormat(JMeterUtils.getResString("property_tool_tip")); //$NON-NLS-1$

    // Initialize the GUI:
    init();
}

From source file:org.lnicholls.galleon.gui.HMEConfigurationPanel.java

public HMEConfigurationPanel(Object bean) {

    setLayout(new GridLayout(0, 1));

    target = bean;/*from   w ww . jav a  2 s  .  c om*/

    try {

        BeanInfo bi = Introspector.getBeanInfo(target.getClass());

        properties = bi.getPropertyDescriptors();

    } catch (IntrospectionException ex) {

        Tools.logException(HMEConfigurationPanel.class, ex, "PropertySheet: Couldn't introspect");

        return;

    }

    editors = new PropertyEditor[properties.length];

    values = new Object[properties.length];

    views = new Component[properties.length];

    labels = new JLabel[properties.length];

    for (int i = 0; i < properties.length; i++) {

        // Don't display hidden or expert properties.

        if (properties[i].isHidden() || properties[i].isExpert()) {

            continue;

        }

        String name = properties[i].getDisplayName();

        Class type = properties[i].getPropertyType();

        Method getter = properties[i].getReadMethod();

        Method setter = properties[i].getWriteMethod();

        // Only display read/write properties.

        if (getter == null || setter == null) {

            continue;

        }

        Component view = null;

        try {

            Object args[] = {};

            Object value = getter.invoke(target, args);

            values[i] = value;

            PropertyEditor editor = null;

            Class pec = properties[i].getPropertyEditorClass();

            if (pec != null) {

                try {

                    editor = (PropertyEditor) pec.newInstance();

                } catch (Exception ex) {

                    // Drop through.

                }

            }

            if (editor == null) {

                editor = PropertyEditorManager.findEditor(type);

            }

            editors[i] = editor;

            // If we can't edit this component, skip it.

            if (editor == null) {

                // If it's a user-defined property we give a warning.

                String getterClass = properties[i].getReadMethod().getDeclaringClass().getName();

                if (getterClass.indexOf("java.") != 0) {

                    log.error("Warning: Can't find public property editor for property \"" + name

                            + "\".  Skipping.");

                }

                continue;

            }

            // Don't try to set null values:

            if (value == null) {

                // If it's a user-defined property we give a warning.

                String getterClass = properties[i].getReadMethod().getDeclaringClass().getName();

                if (getterClass.indexOf("java.") != 0) {

                    log.error("Warning: Property \"" + name + "\" has null initial value.  Skipping.");

                }

                continue;

            }

            editor.setValue(value);

            // editor.addPropertyChangeListener(adaptor);

            // Now figure out how to display it...

            if (editor.isPaintable() && editor.supportsCustomEditor()) {

                view = new PropertyCanvas(frame, editor);

            } else if (editor.getTags() != null) {

                view = new PropertySelector(editor);

            } else if (editor.getAsText() != null) {

                String init = editor.getAsText();

                view = new PropertyText(editor);

            } else {

                log.error("Warning: Property \"" + name + "\" has non-displayabale editor.  Skipping.");

                continue;

            }

        } catch (InvocationTargetException ex) {

            Tools.logException(HMEConfigurationPanel.class, ex.getTargetException(),
                    "Skipping property " + name);

            continue;

        } catch (Exception ex) {

            Tools.logException(HMEConfigurationPanel.class, ex, "Skipping property " + name);

            continue;

        }

        labels[i] = new JLabel(WordUtils.capitalize(name), JLabel.RIGHT);

        // add(labels[i]);

        views[i] = view;

        // add(views[i]);

    }

    int validCounter = 0;

    for (int i = 0; i < labels.length; i++) {

        if (labels[i] != null)

            validCounter++;

    }

    String rowStrings = ""; // general

    if (validCounter > 0)

        rowStrings = "pref, ";

    else

        rowStrings = "pref";

    int counter = 0;

    for (int i = 0; i < labels.length; i++) {

        if (labels[i] != null) {

            if (++counter == (validCounter))

                rowStrings = rowStrings + "9dlu, " + "pref";

            else

                rowStrings = rowStrings + "9dlu, " + "pref, ";

        }

    }

    FormLayout layout = new FormLayout("right:pref, 3dlu, 50dlu:g, right:pref:grow", rowStrings);

    PanelBuilder builder = new PanelBuilder(layout);

    //DefaultFormBuilder builder = new DefaultFormBuilder(new FormDebugPanel(), layout);

    builder.setDefaultDialogBorder();

    CellConstraints cc = new CellConstraints();

    builder.addSeparator("General", cc.xyw(1, 1, 4));

    counter = 0;

    for (int i = 0; i < labels.length; i++) {

        if (labels[i] != null) {

            counter++;

            builder.add(labels[i], cc.xy(1, counter * 2 + 1));

            builder.add(views[i], cc.xy(3, counter * 2 + 1));

        }

    }

    JPanel panel = builder.getPanel();

    //FormDebugUtils.dumpAll(panel);

    add(panel);

}