001: package org.osbl.agent.gui;
002:
003: import java.util.ArrayList;
004: import java.util.List;
005:
006: import org.osbl.agent.logic.AgentSystem;
007: import org.osbl.agent.logic.RuntimeContext;
008: import org.osbl.agent.model.Rule;
009: import org.osbl.agent.model.RuleContext;
010: import org.osbl.client.wings.form.*;
011: import org.osbl.client.wings.shell.Client;
012: import org.osbl.client.wings.shell.Window;
013: import org.wings.session.SessionManager;
014:
015: /**
016: * The DesignContext is one of the most important classes in the implementation of the Agent System.
017: * Many classes present context-dependent interface and/or behaviour. Instead of having to subclass
018: * these classes for each Context, this presentation and/or behaviour is this class.
019: * Classes that delegate to DesignContext are:
020: * <ul>
021: * <li>{@link SidePanel}, which implements the Side Panel</li>
022: * <li>{@link RuleEditorPanel}, which implements the Rule Editor</li>
023: * <li>{@link PreviewController}, which implements the Preview Mode</li>
024: * </ul>
025: *
026: * @author Sebastian Nozzi.
027: */
028: public abstract class DesignContext {
029:
030: /** The object editor. */
031: protected ObjectEditor objectEditor;
032:
033: /** The current rules. */
034: protected List<Rule> currentRules;
035:
036: /** The preview controller. */
037: protected PreviewController previewController;
038:
039: /** The agent system. */
040: protected AgentSystem agentSystem;
041:
042: /** The rule editor panel. */
043: protected RuleEditorPanel ruleEditorPanel;
044:
045: /** The side panel. */
046: protected SidePanel sidePanel;
047:
048: /**
049: * Instantiates a new design context.
050: * It has to be passed an ObjectEditor, used to display modified objects and persist those
051: * changes (primarily used in the Preview Mode).
052: *
053: * @param objectEditor an OSBL object editor instance
054: */
055: public DesignContext(ObjectEditor objectEditor) {
056:
057: this .objectEditor = objectEditor;
058:
059: sidePanel = null;
060: ruleEditorPanel = null;
061: previewController = null;
062:
063: currentRules = new ArrayList<Rule>();
064:
065: // We need THE AgentSystem instance for accessing the Rules repository
066: agentSystem = AgentSystem.getSingletonInstance();
067: }
068:
069: /**
070: * Returns the user-id of the currently logged-in user.
071: *
072: * @return the current user's id.
073: */
074: static public String getCurrentUser() {
075:
076: String user = SessionManager.getSession().getServletRequest()
077: .getUserPrincipal().getName();
078:
079: return user;
080: }
081:
082: /**
083: * Convenience method to retrieve a localized version of a String. The String might contain
084: * format indicators, in which case the data corresponding to those are provided as
085: * additional arguments.
086: *
087: * @param code the code of the localizable String.
088: * @param args additional arguments applicable to the (format) String.
089: *
090: * @return the localized String
091: */
092: static protected String getMsg(String code, Object... args) {
093:
094: Client client = Client.getInstance();
095:
096: if (client != null)
097: return client.getResourceProvider().getMessage(
098: "org.osbl.agents.gui." + code, args);
099: else
100: return "<<" + code + ">>";
101:
102: }
103:
104: /**
105: * Convenience method to retrieve a localized version of a String. The String might contain
106: * format indicators, in which case the data corresponding to those are provided as
107: * additional arguments.
108: *
109: * @param code the code of the localizable String.
110: * @param args additional arguments applicable to the (format) String.
111: *
112: * @return the localized String
113: */
114: public String msg(String code, Object... args) {
115: return DesignContext.getMsg(code, args);
116: }
117:
118: /**
119: * Obtains a context-dependent PreviewController instance. The instance will be
120: * created only once. Successive calls to this method will return the same instance.
121: *
122: * @return the context-specific PreviewController instance.
123: */
124: public PreviewController getPreviewController() {
125:
126: if (previewController == null) {
127: previewController = getPreviewControllerInstance();
128: previewController.setRuntimeContext(getRuntimeContext());
129: }
130:
131: return previewController;
132: }
133:
134: /**
135: * Obtains the context-specific RuleEditorPanel instance. Successive calls to this
136: * method will yield the same instance.
137: *
138: * @return the context-specific RuleEditorPanel instance.
139: */
140: public RuleEditorPanel getRuleEditorPanel() {
141:
142: if (ruleEditorPanel == null)
143: ruleEditorPanel = new RuleEditorPanel(this );
144:
145: return ruleEditorPanel;
146: }
147:
148: /**
149: * Obtains the context-specific SidePanel instance. Successive calls to this methods
150: * yields the same instance.
151: *
152: * @return the context-specific SidePanel instance.
153: */
154: public SidePanel getAgentSidePanel() {
155:
156: if (sidePanel == null)
157: sidePanel = new SidePanel(this );
158:
159: return sidePanel;
160: }
161:
162: /**
163: * Populates a SidePanel with context-specific filters. Called by the SidePanel when
164: * it is time to include the filters during its UI construction.
165: *
166: * @param targetPanel the target panel where the filter components are to be added.
167: */
168: public abstract void addSideFilters(SidePanel targetPanel);
169:
170: /**
171: * Answers whether a given Rule belongs to this DesignContext.
172: *
173: * @param rule the Rule to be analyzed.
174: *
175: * @return true, if it does belong to this context.
176: */
177: public boolean belongsToDesignContext(Rule rule) {
178: return rule.getMetaInformation().get(
179: RuleContext.RUNTIME_OBJECT_CLASS).equals(
180: getRuntimeObjectClass().toString());
181: }
182:
183: /**
184: * Gets the parent window. Used for opening new panels in the central window.
185: *
186: * @return the parent window
187: */
188: protected Window getParentWindow() {
189: return Window.getWindow(objectEditor.getEnvironment());
190: }
191:
192: /**
193: * Returns the context-specific OSBL ObjectEditor passed upon construction.
194: * Requested principally by the PreviewMode to show the modified object,
195: * allow further edition to it, and persist it.
196: *
197: * @return the ObjectEditor.
198: */
199: public ObjectEditor getObjectEditor() {
200: return objectEditor;
201: }
202:
203: /**
204: * Recompute the current Rules to be shown in the UI, based on the logged user-id,
205: * the filters applied (if any) and the public status of the other Rules.
206: *
207: * Called whenever the filters have changed.
208: *
209: * Subclasses implement a context-specific way of recomputing the Rules, usually using
210: * the filter settings.
211: */
212: public abstract void recomputeCurrentRules();
213:
214: /**
215: * Retrieves the list of the current Rules. This call does not recompute the Rules.
216: *
217: * @return the current Rules, as a List.
218: */
219: public List<Rule> getCurrentRules() {
220: return currentRules;
221: }
222:
223: /**
224: * Adds the Specifier components to a RuleEditorPanel. Called by the latter when building
225: * its UI. The DesignContext can put context-specific components in it.
226: *
227: * @param targetPanel the target RuleEditorPanel instance.
228: */
229: public abstract void addSpecifierComponents(
230: RuleEditorPanel targetPanel);
231:
232: /**
233: * Enable/disable Specifier UI-components. Permits/avoids edition or change of the parameters.
234: * Used when the user is not editable.
235: *
236: * @param enabled whether to enable the components or not.
237: */
238: public abstract void enableSpecifierComponents(boolean enabled);
239:
240: /**
241: * Gets the ActionControllers available on this context.
242: * Requested by the RuleEditorPanel.
243: *
244: * @return the ActionControllers for this context.
245: */
246: public abstract List<OperationController> getActionControllers();
247:
248: /**
249: * Gets the ConditionControllers available on this context.
250: * Requested by the RuleEditorPanel.
251: *
252: * @return the ConditionControllers for this context.
253: */
254: public abstract List<OperationController> getConditionControllers();
255:
256: /**
257: * Populate rule.
258: * Requested by the RuleEditorPanel.
259: *
260: * @param editedRule the edited rule
261: */
262: public void populateRule(Rule editedRule) {
263: editedRule.getMetaInformation().put(
264: RuleContext.RUNTIME_OBJECT_CLASS,
265: getRuntimeObjectClass().toString());
266: }
267:
268: /**
269: * Change the Specifier Parameters on-screen to reflect the ones set in the Rule.
270: *
271: * @param rule the Rule which contains Specifier Parameters whose state should be reflected in the UI.
272: */
273: abstract public void changeSpecifiers(Rule rule);
274:
275: /**
276: * Gets the class of the runtime target object.
277: *
278: * @return the class of the runtime target object.
279: */
280: abstract public Class getRuntimeObjectClass();
281:
282: /**
283: * Gets the RuntimeContext that corresponds to this (Design)Context.
284: * Usually invoked by the PreviewController.
285: *
286: * @return the context-specific RuntimeContext.
287: */
288: abstract public RuntimeContext getRuntimeContext();
289:
290: /**
291: * Factory method that instantiates and returns a context-specific PreviewController.
292: * Usually invoked by the SidePanel.
293: *
294: * @return the context-specific PreviewController instance.
295: */
296: abstract protected PreviewController getPreviewControllerInstance();
297:
298: }
|