Example: Implementing a color value editor in Rule Designer

As an example of implementing a value editor, you can define a color editor as a custom editor with a user interface, using the AWT toolkit in Rule Designer.

About this task

In this example, you define a value editor for a BOM class sample.Color. This color editor is a custom editor with a user interface. You define it using the AWT toolkit in Rule Designer.

Procedure

To define a value editor for a BOM class sample.Color:

  1. Extend the abstract class IlrAWTValueEditor:
    public class ValueColorEditor extends IlrAWTValueEditor implements 
    ChangeListener {
    
     private AbstractColorChooserPanel chooser;
     
     public IlrValueDateEditor(IlrValueDescriptor valueDescriptor) {
        super(valueDescriptor); 
        chooser = ColorChooserComponentFactory.getDefaultChooserPanels()[0];
    
        // Gets the default chooser color panel
        // Registers this as a color model listener
        chooser.getColorSelectionModel().addChangeListener(this); 
    
        // Register a key listener to cancel editing when ESC is pressed
        editor.registerKeyboardAction(new ActionListener() { 
            public void actionPerformed(ActionEvent e) { 
             fireEditionCanceled(); 
            }
            }, KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0),
               JComponent.WHEN_IN_FOCUSED_WINDOW);
     }
     
    // Returns the edited value
     public Object getValue() {
         return chooser.getColorSelectionModel().getSelectedColor();
     }
    
     // Sets the value to edit
     public void setValue(Object value) {
        chooser.getColorSelectionModel().setSelectedColor((Color) value);
     }
    
     // When the state has changed, stops the value editing
     // to validate the new selection
     public void stateChanged(ChangeEvent e) { 
        if (chooser.isShowing())
          fireEditionStopped();
     }
    
     // Returns the user interface for editing the value
     public Object getEditorComponent(IlrBRLRuleEditingContext context) {    
         Locale locale = context.getVocabulary().getLocale();
         if (!locale.equals(editor.getLocale()))
           chooser.setLocale(locale);
         return chooser;
     }
    }
  2. Attach the value editor to the BOM class sample.Color by defining a custom property valueEditor on this class:
    valueEditor = sample.valueEditor.Color
  3. Integrate the color value editor into Rule Designer by declaring an extension point ilog.rules.studio.model.brl.valueEditors:

    ID: sample.valueEditor.Color

    Key: sample.valueEditor.Color

    Class: sample.ValueColorEditor

Results

You can now use the color value editor in Rule Designer.