KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jicengine > element > WrapperActionElement


1 package org.jicengine.element;
2
3 import org.jicengine.operation.SimpleContext;
4 import org.jicengine.operation.OperationException;
5 import org.jicengine.operation.LocalContext;
6 import org.jicengine.operation.Context;
7 import org.jicengine.operation.Operation;
8 import org.jicengine.expression.*;
9 import java.util.List JavaDoc;
10 import java.util.ArrayList JavaDoc;
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 ActionElement {
36
37     private Operation action;
38     private VariableElement variableElement;
39
40     public WrapperActionElement(VariableElement variableElement, Location location, Operation action)
41     {
42         super(variableElement.getName(), location);
43         this.action = action;
44         this.variableElement = variableElement;
45     }
46
47     public boolean isExecuted(Context outerContext, Object JavaDoc parentInstance) throws ElementException
48     {
49         return this.variableElement.isExecuted(outerContext,parentInstance);
50     }
51
52     public void execute(Context globalContext, Object JavaDoc parentInstance) throws ElementException
53     {
54         // create the context.
55
Context actionParameterContext = new SimpleContext();
56
57         if( parentInstance != null ){
58             actionParameterContext.addObject(VARIABLE_NAME_PARENT_INSTANCE, parentInstance);
59         }
60
61         // create the instance here as part of the execution
62
Object JavaDoc instance = this.variableElement.getValue(globalContext, parentInstance);
63
64         // add the instance to the context, if necessary.
65
if( this.action.needsParameter(Element.VARIABLE_NAME_ELEMENT_INSTANCE) ){
66             actionParameterContext.addObject(Element.VARIABLE_NAME_ELEMENT_INSTANCE, instance);
67         }
68
69         // NOTE: the 'parent' element should already be in the outerContext.
70

71
72         // context-hierarchy: action + global.
73
Context actionContext = new LocalContext(
74             actionParameterContext,
75             globalContext);
76
77         try {
78             this.action.execute(actionContext);
79         } catch (OperationException e){
80             // this way the location information is added to the exception.
81
throw new ElementException("Failed to execute action " + this.action, e, getName(), getLocation());
82         }
83     }
84 }
85
Popular Tags