views:

70

answers:

1

I want to change the values on one ComboFieldEditor depending on another ComboFieldEditor in an eclipse plugin. E.g. if the user changes the package, different classes need to be populated in the second ComboFieldEditor. The ComboFieldEditor class does not seem to to have a way to change the items on the fly.

+1  A: 

I created a SmartComboFieldEditor class to allow me to elegantly get and set the value of a ComboFieldEditor using the backing data store behind the scenes.

package wat.core.plugin;

import org.eclipse.jface.preference.ComboFieldEditor;
import org.eclipse.swt.widgets.Composite;

public class SmartComboFieldEditor extends ComboFieldEditor {

    public SmartComboFieldEditor(String name, String labelText, String[][] entryNamesAndValues, Composite parent) {
        super(name, labelText, entryNamesAndValues, parent);
    }

    public String getSelectedValue()
    {
        doStore();
        return getPreferenceStore().getString(getPreferenceName());
    }

    public void setSelectedValue(String newValue)
    {
        getPreferenceStore().setValue(getPreferenceName(), newValue);
        doLoad();
    }
}

Then you can override the propertyChange and performOK methods like so:

public void propertyChange(PropertyChangeEvent event) {
  super.propertyChange(event);
  if (event.getSource() == combo1)
  {
    if (combo1.getSelectedValue().equals("Some Value"))
    {
      combo2.setSelectedValue("Some Other Value");
    }
  }
}
Chris Jaynes