Providing a User Interface for Editing Preferences

Although it is not required, you typically will provide a user interface that allows the user to set the preference values. You can provide a user interface that allows the user to set the preference values in your component editor, but you should also make it available in the main Preferences editor for Isight, which is accessed from the View menu in the Gateway.

To provide an interface for editing your component preferences, specify an additional class in your component XML descriptor, similar to the way the editor, executor, and other classes were specified, as follows:

<PreferencesPanel 
type="com.engineous.sdk.preferences.PreferencePanel">com.engineous.com
ponent.excel.ExcelPrefEditor</PreferencesPanel>

The Java class that you write should extend and should implement the methods getPanel(), apply(), toString(), and getIcon(). An example of a Preferences editor is provided below (import statements have been removed for brevity). The esicommon.jar file is required to be in the build path to compile this code (see Configuring the Build Path).

extends AbstractPreferencePanel {

private JLabel techLabel = new JLabel("Default workbook 
close option:");
private JComboBox closeCombo = new JComboBox();
private Preference closePref;
private Preference saveWorkbookPref;
private MetaModel metaModel;
private JCheckBox saveAsParameter = new JCheckBox("Save 
   workbook as an output file parameter after execution");

public ExcelPrefEditor() {
   try {
      jbInit();
   }
   catch (Exception e) {
      e.printStackTrace();
   }
}

private void jbInit()
   throws Exception {

      EtchedBorder border1 = new
      EtchedBorder(EtchedBorder.RAISED, Color.white, new Color(142, 
      142, 142));
      TitledBorder titledBorder1 = new
      TitledBorder(border1, "Excel Preferences");

      setLayout(new GridBagLayout());
      this.setBorder(titledBorder1);
      Insets insets = new Insets(5, 5, 0, 0);
      int west = GridBagConstraints.WEST;
      int none = GridBagConstraints.NONE;

      add(techLabel, new GridBagConstraints(0, 0, 1, 1,
      0.0, 0.0, west, none, insets, 0, 0));
      add(closeCombo, new GridBagConstraints(1, 0, 1,
      1, 1.0, 0.0, west, none, insets, 0, 0));
      add(saveAsParameter,
      new GridBagConstraints(0, 1, 2, 1, 0.0, 0.0, west, none, 
      insets, 0, 0));

      // blank label to take up space for proper spacing in 
      GUI add(new JLabel(), new
      GridBagConstraints(0, 4, 2, 1, 0.0, 1.0, 
      GridBagConstraints.NORTH,GridBagConstraints.VERTICAL, insets, 
      0, 0));

      initialize();

}

public Component getPanel() {
   return this;
}

public void initialize() {

   try {
      closeCombo.addItem("Leave open");
      closeCombo.addItem("Close after job completes");
      closeCombo.addItem("Close after each execution");

      metaModel =
      MetaModelManager.instance().lookupMetaModel("com.engineous.comp
      onent.Excel");
      PreferenceSet prefs = metaModel.getPreferences();

      closePref = prefs.getPreference("Default close option");
      int closeOption = closePref.getAsInt();
      closeCombo.setSelectedIndex(closeOption);

      saveWorkbookPref = prefs.getPreference("Save
      workbook as parameter");
      boolean saveWorkbookOption 
      =saveWorkbookPref.getAsBoolean();
     saveAsParameter.setSelected(saveWorkbookOption);

   }
   catch (Exception e) {
      ExceptionHandler.showSystemError(this, e, "Error
      updating Excel preferences panel");
   }
}

public boolean apply() {

   try {
      closePref.setValue(closeCombo.getSelectedIndex());
      saveWorkbookPref.setValue(saveAsParameter.isSelected());
   }
   catch (Exception e) {
      ExceptionHandler.showSystemError(this, e,
      "Error setting Excel preferences");
      return false;    // Apply failed
   }
   return true;
}

public String toString() {
   return "Excel"
}

public Icon getIcon() {

   try {
      return metaModel.getIcon(16);
   }
   catch (Exception e) {
      return null;
   }
}

}