001: package org.osbl.agent.gui.action;
002:
003: import java.util.List;
004:
005: import org.conform.PropertyMeta;
006: import org.osbl.agent.model.Action;
007: import org.osbl.agent.model.action.SetEnumAction;
008: import org.wings.SComboBox;
009: import org.wings.SComponent;
010: import org.wings.SLabel;
011: import org.wings.SListCellRenderer;
012:
013: /**
014: * The class SetEnumActionController models a SetEnumAction by presenting the
015: * user a combo-box with the possible enum-values to choose from.
016: *
017: * @author Sebastian Nozzi.
018: */
019: public class SetEnumActionController extends
020: SetPropertyActionController {
021:
022: /** The values combo. */
023: private SComboBox valuesCombo;
024:
025: /**
026: * Instantiates a new SetEnumActionController.
027: */
028: public SetEnumActionController() {
029:
030: valuesCombo = new SComboBox();
031:
032: valuesCombo.setRenderer(new SListCellRenderer() {
033:
034: SLabel label = new SLabel();
035:
036: public SComponent getListCellRendererComponent(
037: SComponent list, java.lang.Object value,
038: boolean isSelected, int index) {
039:
040: label.setText(getPropertyMeta().getFormat().format(
041: value));
042:
043: return label;
044: }
045:
046: });
047: }
048:
049: /* (non-Javadoc)
050: * @see org.osbl.agent.gui.action.SetPropertyActionController#populateComponentList(java.util.List)
051: */
052: protected void populateComponentList(List<SComponent> componentList) {
053:
054: // Populate the componentList with the parent's components first.
055: super .populateComponentList(componentList);
056:
057: componentList.add(valuesCombo);
058:
059: }
060:
061: /* (non-Javadoc)
062: * @see org.osbl.agent.gui.action.SetPropertyActionController#setPropertyMeta(org.conform.PropertyMeta)
063: */
064: public void setPropertyMeta(PropertyMeta propertyMeta) {
065: super .setPropertyMeta(propertyMeta);
066:
067: valuesCombo.removeAllItems();
068:
069: for (Object element : propertyMeta.getDomainProvider()
070: .getDomain())
071: valuesCombo.addItem(element);
072: }
073:
074: /* (non-Javadoc)
075: * @see org.osbl.agent.gui.action.SetPropertyActionController#setAction(org.osbl.agent.model.Action)
076: */
077: public void setAction(Action action) {
078:
079: // First let the parent SetProperyActionController set the inherited fields up
080: // acording to the action given (which is, after all, a SetPropertyAction).
081: super .setAction(action);
082:
083: Object valueToSet = ((SetEnumAction) action).getValueToSet();
084:
085: // iterate over all values in the combo-box...
086: for (int i = 0; i < valuesCombo.getItemCount(); i++) {
087: Object candidateValue = valuesCombo.getItemAt(i);
088:
089: // ...until we find the one that matches...
090: if (candidateValue.equals(valueToSet))
091: // ...and make it the selected item in the combo-box.
092: valuesCombo.setSelectedIndex(i);
093: }
094:
095: }
096:
097: /* (non-Javadoc)
098: * @see org.osbl.agent.gui.action.SetPropertyActionController#populateAction(org.osbl.agent.model.Action)
099: */
100: protected void populateAction(Action action) {
101:
102: // Let the parent populate the Action with whatever information it manages.
103: super .populateAction(action);
104:
105: ((SetEnumAction) action).setValueToSet(valuesCombo
106: .getSelectedItem());
107: }
108:
109: }
|