User-defined editor code example that uses two Java classes

Use the example Java™ code to create a user-defined editor to display a list of values by using two Java classes.

About this task

The following example code shows you how to use two Java classes to display a list of values to a pattern user.

The MyEditor class controls the function of the editor. The MyComposite class controls the appearance of the editor by using the SWT toolkit.

The example assumes that the following steps are completed:
  • The values shown in the list in the user-defined editor are entered as configuration values in the Configure User-Defined Editor window. The values must be entered as a comma-separated string, for example, Apple,Orange,Pear,Lemon. For more information about entering configuration values, see Configuring a user-defined editor.
  • The MyEditor class is entered in the Configure User-Defined Editor window, in the Class name field. For more information about specifying the class name for your user-defined editor code, see Configuring a user-defined editor.
MyEditor class
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.
  1. 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.
  2. The createControls() method is called to create the user interface for the editor. The controls are defined in the MyComposite class.
  3. 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.
  4. The setValue(), getValue(), and setEnabled() methods are defined in the MyComposite class:
    1. The setValue() method sets the initial value in the user-defined editor.
    2. The getValue() method returns the current value of the pattern parameter to the pattern instance editor.
    3. 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);
   }
}
MyComposite class
The MyComposite class creates the user interface controls for the user-defined editor. The MyComposite class extends the SWT toolkit Composite class.
  1. The layout of the controls is set and a new list control is created.
  2. 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.
  3. The setValue(), getValue(), and setEnabled() methods are defined in the MyComposite class, but are called from the MyEditor class:
    1. The setValue() method takes the value of the pattern parameter and updates the list control to show the selected value.
    2. The getValue() method retrieves the currently selected value from the list control and, if it is not blank, returns it to MyEditor.
    3. 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);
   }
}