The MyEditor class extends the BasePatternPropertyEditor class.
An instance of the MyEditor class is created automatically
when the pattern user opens the pattern instance editor.
- The configureEditor() method is called automatically
after the MyEditor class is created. This method
reads the configuration values string for the user-defined editor
and stores each of the comma-separated values.
- The createControls() method is called to create
the user interface for the editor. The controls are defined in the MyComposite class.
- The isValid() method is called automatically
after the valueChanged() method is called. In this
example, isValid() checks the current value of
the pattern parameter and returns an error message if no value is
selected. The error message is displayed to the pattern user in the
pattern instance editor. If the parameter value is valid, the method
returns
null
.
- The setValue(), getValue(),
and setEnabled() methods are defined in the MyComposite class:
- The setValue() method sets the initial value
in the user-defined editor.
- The getValue() method returns the current value
of the pattern parameter to the pattern instance editor.
- The setEnabled() method is called when the
pattern parameter is enabled or disabled by an XPath expression.
package com.your.company.domain.MyPattern.code;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import com.ibm.broker.config.appdev.patterns.ui.BasePatternPropertyEditor;
import com.ibm.broker.config.appdev.patterns.ui.PatternPropertyEditorSite;
public class MyEditor extends BasePatternPropertyEditor {
private MyComposite composite;
private String configurationValues;
private String[] items;
@Override
public void configureEditor(PatternPropertyEditorSite site, boolean required, String configurationValues) {
super.configureEditor(site, required, configurationValues);
this.configurationValues = configurationValues;
this.items = configurationValues.split("\\,");
if (this.items == null) {
this.items = new String[] { };
}
}
@Override
public void createControls(Object parent) {
Composite parentComposite = (Composite) parent;
PatternPropertyEditorSite site = getSite();
composite = new MyComposite(parentComposite, SWT.NONE, items, site);
}
@Override
public String isValid() {
String selection = getValue();
if (selection != null) {
return null;
}
return "Nothing currently selected..!";
}
@Override
public void setValue(String value) {
composite.setValue(value);
}
@Override
public String getValue() {
return composite.getValue();
}
@Override
public void setEnabled(boolean enabled) {
composite.setEnabled(enabled);
}
}
The MyComposite class creates the user interface
controls for the user-defined editor. The MyComposite class
extends the SWT toolkit Composite class.
- The layout of the controls is set and a new list control is created.
- The valueChanged() method is used in a listener
on the list control. This ensures that when the selected value in
the list is changed, change notifications are sent to any XPath expressions
or editors that use the value of this parameter.
- The setValue(), getValue(),
and setEnabled() methods are defined in the MyComposite class,
but are called from the MyEditor class:
- The setValue() method takes the value of the
pattern parameter and updates the list control to show the selected
value.
- The getValue() method retrieves the currently
selected value from the list control and, if it is not blank, returns
it to MyEditor.
- The setEnabled() method uses the boolean value
passed to it to enable or disable the list control.
package com.your.company.domain.MyPattern.code;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.List;
import com.ibm.broker.config.appdev.patterns.ui.PatternPropertyEditorSite;
public class MyComposite extends Composite {
private List list;
private PatternPropertyEditorSite site;
public MyComposite(Composite parent, int style, String[] items, final PatternPropertyEditorSite site) {
super(parent, SWT.NONE);
this.site = site;
GridLayout gridLayout = new GridLayout(1, false);
gridLayout.marginWidth = 1;
gridLayout.marginHeight = 1;
gridLayout.horizontalSpacing = 0;
gridLayout.verticalSpacing = 0;
setLayout(gridLayout);
setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
list = new List(this, SWT.BORDER);
list.setItems(items);
GridData listLayoutData = new GridData(GridData.FILL_HORIZONTAL);
listLayoutData.horizontalIndent = 0;
list.setLayoutData(listLayoutData);
list.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent event) {
site.valueChanged();
}
});
}
public void setValue(String value) {
if (value != null) {
list.setSelection(new String[] { value });
}
}
public String getValue() {
String[] selection = list.getSelection();
if (selection.length > 0) {
return selection[0];
}
return null;
}
public void setEnabled(boolean enabled) {
list.setEnabled(enabled);
}
}