User-defined editor code example that uses one Java class

Use the example Java™ code to create a user-defined editor to display a text box, by using one Java class.

About this task

The following example code shows you how to use one Java class to display a text box user-defined editor to a pattern user. The MyEditor class extends the BasePatternPropertyEditor class and is specified as the user-defined editor class in the user-defined editor configuration; see Configuring a user-defined editor. 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 user-defined editor class is created. This method sets up the editor.
  2. The createControls() method is called to create the user interface for the editor:
    1. The SWT toolkit object, Composite, is used for the user interface.
    2. A text box control is created and the SWT layout information for the text box is defined.
    3. A listener on the text box is defined, which calls the valueChanged() method of the PatternPropertyEditorSite object for the editor. This ensures that when the selected value in the text box is changed, change notifications are sent to any XPath expressions or editors that use the value of this parameter.
  3. The setValue() method sets the initial value for the text box. In this example, the code filters out blank values. The code also filters out values that start with <, to remove watermark values. This assumes watermark values are enclosed in angle brackets.
  4. The getValue() method reads the current value of the text box and returns the value to the pattern instance editor. The pattern instance editor uses this returned value as the value of the pattern parameter.
  5. The setEnabled() method enables or disables the text box control, depending on whether the pattern parameter is enabled or disabled. The parameter can be enabled or disabled by using an XPath expression; see Enabling pattern parameters. When the result of an XPath expression enables or disables the parameter, a boolean value is passed from the pattern instance editor to the setEnabled() method. If the parameter is enabled, the boolean value is true; if the parameter is disabled the boolean value is false.
package com.your.company.domain.MyPattern.code;

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Text;

import com.ibm.broker.config.appdev.patterns.ui.*;

public class MyEditor extends BasePatternPropertyEditor {

   private Text text;

   @Override
   public void configureEditor(PatternPropertyEditorSite site, boolean required, String configurationValues) {
      super.configureEditor(site, required, configurationValues);
	}

   @Override
   public void createControls(Object parent) {
      Composite composite = (Composite) parent;

      text = new Text(composite, SWT.BORDER);

      GridData textLayoutData = new GridData(GridData.FILL_HORIZONTAL);
      textLayoutData.horizontalIndent = 0;
      text.setLayoutData(textLayoutData);

      text.addListener(SWT.Modify, new Listener() {
         @Override
         public void handleEvent(Event event) {
            PatternPropertyEditorSite site = getSite();
            site.valueChanged();
         }
      });
   }

   @Override
   public void setValue(String value) {

      if (value != null) {
         if (value.startsWith("<") == false) {
            text.setText(value);
         }
      }
   }

   @Override
   public String getValue() {
      String value = text.getText();
      if (value.length() > 0) {
         return value;
      }
      return null;
   }

   @Override
   public void setEnabled(boolean enabled) {
      text.setEnabled(enabled);
   }
}