01: package org.jicengine.element;
02:
03: import org.jicengine.operation.SimpleContext;
04: import org.jicengine.operation.OperationException;
05: import org.jicengine.operation.LocalContext;
06: import org.jicengine.operation.Context;
07: import org.jicengine.operation.Operation;
08: import org.jicengine.expression.*;
09: import java.util.List;
10: import java.util.ArrayList;
11:
12: /**
13: * <p>
14: * WrapperActionElement adds an action to a VariableElement.
15: * </p>
16: * <p>
17: * Useful way for example transforming VariableElements as property-setters:
18: * wrap them inside a WrapperActionElement and set the
19: * action-operation to the set-property.
20: * </p>
21: *
22: * <p>
23: * Design-note: currently, there's know way to give the action any parameters
24: * besides the implicit 'parent' and 'this' objects and global objects.
25: * (and we shouldn't create new data that is not available in the JIC-file?)
26: * of course, the action-Operation can always be constructor so that it
27: * won't need other parameters..
28: * </p>
29: * <p>
30: * Copyright (C) 2004 Timo Laitinen
31: * </p>
32: * @author .timo
33: */
34:
35: public class WrapperActionElement extends AbstractElement implements
36: ActionElement {
37:
38: private Operation action;
39: private VariableElement variableElement;
40:
41: public WrapperActionElement(VariableElement variableElement,
42: Location location, Operation action) {
43: super (variableElement.getName(), location);
44: this .action = action;
45: this .variableElement = variableElement;
46: }
47:
48: public boolean isExecuted(Context outerContext,
49: Object parentInstance) throws ElementException {
50: return this .variableElement.isExecuted(outerContext,
51: parentInstance);
52: }
53:
54: public void execute(Context globalContext, Object parentInstance)
55: throws ElementException {
56: // create the context.
57: Context actionParameterContext = new SimpleContext();
58:
59: if (parentInstance != null) {
60: actionParameterContext.addObject(
61: VARIABLE_NAME_PARENT_INSTANCE, parentInstance);
62: }
63:
64: // create the instance here as part of the execution
65: Object instance = this .variableElement.getValue(globalContext,
66: parentInstance);
67:
68: // add the instance to the context, if necessary.
69: if (this .action
70: .needsParameter(Element.VARIABLE_NAME_ELEMENT_INSTANCE)) {
71: actionParameterContext.addObject(
72: Element.VARIABLE_NAME_ELEMENT_INSTANCE, instance);
73: }
74:
75: // NOTE: the 'parent' element should already be in the outerContext.
76:
77: // context-hierarchy: action + global.
78: Context actionContext = new LocalContext(
79: actionParameterContext, globalContext);
80:
81: try {
82: this .action.execute(actionContext);
83: } catch (OperationException e) {
84: // this way the location information is added to the exception.
85: throw new ElementException("Failed to execute action "
86: + this.action, e, getName(), getLocation());
87: }
88: }
89: }
|