Example usage for java.beans PropertyEditor getCustomEditor

List of usage examples for java.beans PropertyEditor getCustomEditor

Introduction

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

Prototype


java.awt.Component getCustomEditor();

Source Link

Document

A PropertyEditor may choose to make available a full custom Component that edits its property value.

Usage

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

/**
 * Create a customizer for a given test bean type.
 *
 * @param testBeanClass//  ww  w .ja va  2 s .  c om
 *            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();
}