001: package org.osbl.agent.gui.condition;
002:
003: import java.util.ArrayList;
004: import java.util.List;
005:
006: import org.conform.BeanMeta;
007: import org.conform.PropertyMeta;
008: import org.osbl.agent.gui.ConditionController;
009: import org.osbl.agent.gui.OperationController;
010: import org.osbl.agent.model.Condition;
011: import org.osbl.agent.model.action.SetCurrentTimeAction;
012: import org.osbl.agent.model.condition.BinaryOperator;
013: import org.osbl.agent.model.condition.Operator;
014: import org.osbl.agent.model.condition.PropertyCondition;
015: import org.osbl.client.wings.shell.Client;
016: import org.wings.SComboBox;
017: import org.wings.SComponent;
018:
019: /**
020: * The class PropertyConditionController models the PropertyCondition.
021: *
022: * It serves as a superclass for ConditionControllers that want to offer the comparison
023: * of a property against a value.
024: *
025: * It presents the user with an operator combo-box. Anything beyond that element is
026: * provided by the subclasses. Subclasses can also choose not to include the operator
027: * combo at all (as in {@link SetCurrentTimeAction}), although this is rare.
028: *
029: * @author Sebastian Nozzi.
030: */
031: public abstract class PropertyConditionController extends
032: ConditionController {
033:
034: /** The operator combo. */
035: private SComboBox operatorCombo;
036:
037: /** The property meta. */
038: protected PropertyMeta propertyMeta;
039:
040: /**
041: * Instantiates a new PropertyConditionController.
042: */
043: public PropertyConditionController() {
044:
045: // Combo for the Operators that apply to the selected property
046: // and the value entered by the user in the inputField.
047: operatorCombo = new SComboBox();
048:
049: // Get the Operators from getOperators (overriden by subclasses)...
050: for (Operator relationalOperator : getOperators()) {
051: // ...and add each Operator to the operatorCombo.
052: operatorCombo.addItem(relationalOperator);
053: }
054:
055: }
056:
057: /**
058: * Gets the property meta.
059: *
060: * @return the property meta
061: */
062: public PropertyMeta getPropertyMeta() {
063: return propertyMeta;
064: }
065:
066: /**
067: * Sets the property meta.
068: *
069: * @param propertyMeta the new property meta
070: */
071: public void setPropertyMeta(PropertyMeta propertyMeta) {
072: this .propertyMeta = propertyMeta;
073: }
074:
075: /**
076: * Gets the value to compare to.
077: *
078: * @return the value to compare to
079: */
080: public abstract Object getValueToCompareTo();
081:
082: /**
083: * Sets the value to compare to.
084: *
085: * @param condition the new value to compare to
086: */
087: public abstract void setValueToCompareTo(PropertyCondition condition);
088:
089: /**
090: * Sets the operator.
091: *
092: * @param operatorToBeSelected the new operator
093: */
094: public void setOperator(Operator operatorToBeSelected) {
095: // iterate over all operators in the combo-box...
096: for (int i = 0; i < operatorCombo.getItemCount(); i++) {
097: Operator controllerOp = (Operator) operatorCombo
098: .getItemAt(i);
099:
100: // ...until we find the one that matches...
101: if (controllerOp.equals(operatorToBeSelected))
102: // ...and make it the selected item in the combo-box.
103: operatorCombo.setSelectedIndex(i);
104: }
105: }
106:
107: /*
108: * (non-Javadoc)
109: *
110: * @see org.osbl.agent.gui.condition.RelationalConditionController#canBeReplacedBy(agent.controller.OperationController)
111: */
112: public boolean canBeReplacedBy(
113: OperationController candidateController) {
114:
115: // There are many PropertyConditions in the UI, one for each property...
116:
117: // So, a replacement is found only if the class matches ...
118: if (candidateController instanceof PropertyConditionController) {
119: PropertyConditionController otherControlller = (PropertyConditionController) candidateController;
120: // and they represent the same property...
121: return ((PropertyCondition) otherControlller.getCondition())
122: .getPropertyMetaName().equals(
123: propertyMeta.getName());
124: // Otherwise no match is found.
125: } else
126: return false;
127: }
128:
129: /* (non-Javadoc)
130: * @see org.osbl.agent.gui.ConditionController#populateCondition(org.osbl.agent.model.Condition)
131: */
132: protected void populateCondition(Condition condition) {
133:
134: // Set the operation in the condition to whatever is selected in the UI
135: ((PropertyCondition) condition)
136: .setOperator((Operator) operatorCombo.getSelectedItem());
137:
138: ((PropertyCondition) condition)
139: .setBeanMetaClassName(propertyMeta.getBeanMeta()
140: .getName());
141:
142: // Set the propertyMeta to the one set in this class.
143: ((PropertyCondition) condition)
144: .setPropertyMetaName(propertyMeta.getName());
145:
146: // Set the valueToCompareTo to whatever the user entered or selected in the UI.
147: // (subclasses of this controller let the user enter/choose this information in
148: // different ways).
149: ((PropertyCondition) condition)
150: .setValueToCompareTo(getValueToCompareTo());
151: }
152:
153: /*
154: * (non-Javadoc)
155: *
156: * @see org.osbl.agent.gui.condition.RelationalConditionController#getOperators()
157: */
158: /**
159: * Gets the operators.
160: *
161: * @return the operators
162: */
163: public List<Operator> getOperators() {
164:
165: List<Operator> result = new ArrayList<Operator>();
166:
167: // Since we are dealing with fields in a generic way we can
168: // only define operators we can be sure apply to all fields...
169:
170: result.add(new BinaryOperator(msg("operatorIs"), "=="));
171: result.add(new BinaryOperator(msg("operatorIsNot"), "!="));
172:
173: // Subclasses of this class will add more specific Operators
174: // on their own, depending of the type they represent.
175:
176: return result;
177: }
178:
179: /*
180: * (non-Javadoc)
181: *
182: * @see org.osbl.agent.gui.condition.RelationalConditionController#setCondition(org.osbl.agent.model.condition.Condition)
183: */
184: public void setCondition(Condition condition) {
185:
186: // Get the operator that is set in the PropertyCondition given...
187: Operator conditionOp = ((PropertyCondition) condition)
188: .getOperator();
189:
190: setOperator(conditionOp);
191:
192: // Attemp to find and set the propertyMeta according to the data
193: // stored in the Condition.
194:
195: String className = ((PropertyCondition) condition)
196: .getBeanMetaClassName();
197: String propertyName = ((PropertyCondition) condition)
198: .getPropertyMetaName();
199:
200: try {
201: BeanMeta beanMeta = Client.getInstance()
202: .getBeanMetaProvider().getBeanMeta(
203: Class.forName(className));
204: setPropertyMeta(beanMeta.getProperty(propertyName));
205: } catch (ClassNotFoundException e) {
206: // TODO Auto-generated catch block
207: e.printStackTrace();
208: }
209:
210: // Let subclasses modify the input/choice component to reflect the comparison
211: // value stored in the condition.
212: setValueToCompareTo((PropertyCondition) condition);
213: }
214:
215: /* (non-Javadoc)
216: * @see org.osbl.agent.gui.OperationController#populateComponentList(java.util.List)
217: */
218: protected void populateComponentList(List<SComponent> componentList) {
219: componentList.add(operatorCombo);
220: }
221:
222: /*
223: * (non-Javadoc)
224: *
225: * @see java.lang.Object#toString()
226: */
227: public String toString() {
228:
229: return " " + propertyMeta.getLabel() + " ("
230: + propertyMeta.getName() + ")";
231: }
232:
233: }
|