001: package org.osbl.agent.gui.condition;
002:
003: import java.util.List;
004:
005: import org.concern.ControllerLookup;
006: import org.concern.Controller;
007: import org.concern.model.Process;
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.condition.BinaryOperator;
012: import org.osbl.agent.model.condition.Operator;
013: import org.osbl.agent.model.condition.SubjectCondition;
014: import org.osbl.client.wings.shell.Client;
015: import org.wings.SComboBox;
016: import org.wings.SComponent;
017: import org.wings.SLabel;
018: import org.wings.SListCellRenderer;
019:
020: /**
021: * The class SubjectConditionController models the SubjectCondition.
022: *
023: * It presents the user with a combo-box with all available con:cern Conditions and the
024: * possibility (also through a combo-box) to choose whether the choosen condition should
025: * be met or not.
026: *
027: * @author Sebastian Nozzi.
028: */
029: public class SubjectConditionController extends ConditionController {
030:
031: /** The con:cern process. */
032: private Process process;
033:
034: /** The conditions combo. */
035: private SComboBox conditionsCombo;
036:
037: /** The operator combo. */
038: private SComboBox operatorCombo;
039:
040: /**
041: * Instantiates a new SubjectConditionController.
042: */
043: public SubjectConditionController() {
044: this (null);
045: }
046:
047: /**
048: * Instantiates a new subject condition controller.
049: *
050: * @param process the process
051: */
052: public SubjectConditionController(Process process) {
053: this (process, null);
054: }
055:
056: /**
057: * Instantiates a new subject condition controller.
058: *
059: * @param process the con:cern process
060: * @param condition the con:cern condition
061: */
062: public SubjectConditionController(Process process,
063: org.concern.model.Condition condition) {
064:
065: // Create a combo-box for choosing the Operator
066: operatorCombo = new SComboBox();
067:
068: // There is not much that can happen to a condition...
069: // ... either the condition is met...
070: operatorCombo.addItem(new BinaryOperator(msg("operatorIs"),
071: "=="));
072: // ... or it's not.
073: operatorCombo.addItem(new BinaryOperator(msg("operatorIsNot"),
074: "!="));
075:
076: // Here come all (concern)conditions of the Process
077: conditionsCombo = new SComboBox();
078:
079: this .process = process;
080:
081: // Populate the combo-box with all conditions if the Process
082: // is not null... (it can be null if this Controller was instantiated
083: // when restoring an existing Rule, in which case setCondition will be
084: // called later anyway).
085: if (process != null) {
086: populateConditionsCombo(process, null);
087: }
088:
089: }
090:
091: /**
092: * Populate conditions combo.
093: *
094: * @param process the process
095: * @param selectedConditionName the selected condition
096: */
097: protected void populateConditionsCombo(Process process,
098: String selectedConditionName) {
099:
100: // Iterate over all (concern) conditions in the Process...
101: for (Object condObj : process.getConditions()) {
102:
103: // ... add each (concern) condition into the conditionsCombo...
104: conditionsCombo.addItem(condObj);
105:
106: // ... and if the condition matches the selectedCondition (which we
107: // get as a parameter) make it the selected item of the combo.
108: if (((org.concern.model.Condition) condObj).getName()
109: .equals(selectedConditionName)) {
110: conditionsCombo.setSelectedItem(condObj);
111: }
112: }
113:
114: final Process processForRenderer = process;
115:
116: conditionsCombo.setRenderer(new SListCellRenderer() {
117:
118: SLabel label = new SLabel();
119:
120: public SComponent getListCellRendererComponent(
121: SComponent list, java.lang.Object value,
122: boolean isSelected, int index) {
123:
124: if (value != null && processForRenderer != null) {
125:
126: label
127: .setText(Client
128: .getInstance()
129: .getResourceProvider()
130: .getMessage(
131: "process."
132: + processForRenderer
133: .getName()
134: + "."
135: + ((org.concern.model.Condition) value)
136: .getName()));
137: } else {
138: label.setText("");
139: }
140:
141: return label;
142: }
143:
144: });
145:
146: }
147:
148: /*
149: * (non-Javadoc)
150: *
151: * @see agent.controller.OperationController#canBeReplacedBy(agent.controller.OperationController)
152: */
153: public boolean canBeReplacedBy(
154: OperationController candidateController) {
155:
156: // Since there is only one SubjectConditionController it's enough if the
157: // classes match to be replaced.
158: return (candidateController instanceof SubjectConditionController);
159: }
160:
161: /*
162: * (non-Javadoc)
163: *
164: * @see agent.controller.OperationController#getComponents()
165: */
166: protected void populateComponentList(List<SComponent> componentList) {
167:
168: // The operator-combo (see Operators defined by this class).
169: componentList.add(operatorCombo);
170: // All conditions of the Process
171: componentList.add(conditionsCombo);
172:
173: }
174:
175: /*
176: * (non-Javadoc)
177: *
178: * @see org.osbl.agent.gui.condition.ConditionController#setCondition(org.osbl.agent.model.condition.Condition)
179: */
180: public void setCondition(Condition condition) {
181:
182: // Update our operatorCombo to reflect the operator set in the
183: // SubjectCondition
184: if (((SubjectCondition) condition).getOperator().equals(
185: new BinaryOperator("=="))) {
186: operatorCombo.setSelectedIndex(0);
187: } else
188: operatorCombo.setSelectedIndex(1);
189:
190: String processName = ((SubjectCondition) condition)
191: .getProcessName();
192:
193: Controller controller = ControllerLookup.getInstance()
194: .getController(processName);
195: process = controller.getProcess();
196:
197: // Remove all elements in the conditionsCombo, since...
198: conditionsCombo.removeAllItems();
199:
200: // ... we'll be populating the combo anyway.
201: populateConditionsCombo(process, ((SubjectCondition) condition)
202: .getConcernConditionName());
203: }
204:
205: /*
206: * (non-Javadoc)
207: *
208: * @see java.lang.Object#toString()
209: */
210: public String toString() {
211: // For our string version return the name of the Process
212:
213: return Client.getInstance().getResourceProvider().getMessage(
214: "process." + process.getName());
215: }
216:
217: /*
218: * (non-Javadoc)
219: *
220: * @see org.osbl.agent.gui.ConditionController#populateCondition(org.osbl.agent.model.Condition)
221: */
222: protected void populateCondition(Condition condition) {
223:
224: // In order to evaluate correctly, the SubjectCondition needs some
225: // information...
226:
227: // ... a Process (not really, see explanation below)
228: ((SubjectCondition) condition)
229: .setProcessName(process.getName());
230: // ... a org.concern.model.Condition (which is not the same as a
231: // org.osbl.agent.model.Condition)
232: ((SubjectCondition) condition)
233: .setConcernConditionName(((org.concern.model.Condition) conditionsCombo
234: .getSelectedItem()).getName());
235: // ... and an Operator (which is one of those defined by this class, see
236: // above)
237: ((SubjectCondition) condition)
238: .setOperator((Operator) operatorCombo.getSelectedItem());
239:
240: /*
241: * The Process is not really needed for the evaluation, but is needed
242: * for the inverse process: when a SubjectConditionController is
243: * instantiated from a SubjectCondition.
244: *
245: * The information about the Process is needed in order to later
246: * retrieve the concernConditions and show them in the combo-box. This
247: * is necessary because a org.concern.model.Condition doesn't know about
248: * the Process it belongs to.
249: *
250: */
251: }
252:
253: }
|