KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > webflow > engine > TransitionableState


1 /*
2  * Copyright 2002-2006 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.springframework.webflow.engine;
17
18 import org.springframework.core.style.StylerUtils;
19 import org.springframework.core.style.ToStringCreator;
20 import org.springframework.webflow.definition.TransitionDefinition;
21 import org.springframework.webflow.definition.TransitionableStateDefinition;
22 import org.springframework.webflow.execution.RequestContext;
23 import org.springframework.webflow.execution.ViewSelection;
24
25 /**
26  * Abstract superclass for states that can execute a transition in response to
27  * an event.
28  *
29  * @see org.springframework.webflow.engine.Transition
30  * @see org.springframework.webflow.engine.TransitionCriteria
31  *
32  * @author Keith Donald
33  * @author Erwin Vervaet
34  */

35 public abstract class TransitionableState extends State implements TransitionableStateDefinition {
36
37     /**
38      * The set of possible transitions out of this state.
39      */

40     private TransitionSet transitions = new TransitionSet();
41
42     /**
43      * An actions to execute when exiting this state.
44      */

45     private ActionList exitActionList = new ActionList();
46
47     /**
48      * Create a new transitionable state.
49      * @param flow the owning flow
50      * @param id the state identifier (must be unique to the flow)
51      * @throws IllegalArgumentException when this state cannot be added to given
52      * flow, for instance when the id is not unique
53      * @see State#State(Flow, String)
54      * @see #getTransitionSet()
55      */

56     protected TransitionableState(Flow flow, String JavaDoc id) throws IllegalArgumentException JavaDoc {
57         super(flow, id);
58     }
59
60     // implementing TranstionableStateDefinition
61

62     public TransitionDefinition[] getTransitions() {
63         return getTransitionSet().toArray();
64     }
65
66     /**
67      * Returns the set of transitions. The returned set is mutable.
68      */

69     public TransitionSet getTransitionSet() {
70         return transitions;
71     }
72
73     /**
74      * Get a transition in this state for given flow execution request context.
75      * Throws and exception when there is no corresponding transition.
76      * @throws NoMatchingTransitionException when a matching transition cannot
77      * be found
78      */

79     public Transition getRequiredTransition(RequestContext context) throws NoMatchingTransitionException {
80         Transition transition = getTransitionSet().getTransition(context);
81         if (transition == null) {
82             throw new NoMatchingTransitionException(getFlow().getId(), getId(), context.getLastEvent(),
83                     "No transition found on occurence of event '" + context.getLastEvent() + "' in state '" + getId()
84                             + "' of flow '" + getFlow().getId() + "' -- valid transitional criteria are "
85                             + StylerUtils.style(getTransitionSet().getTransitionCriterias())
86                             + " -- likely programmer error, check the set of TransitionCriteria for this state");
87         }
88         return transition;
89     }
90
91     /**
92      * Returns the list of actions executed by this state when it is exited.
93      * The returned list is mutable.
94      * @return the state exit action list
95      */

96     public ActionList getExitActionList() {
97         return exitActionList;
98     }
99
100     // behavioral methods
101

102     /**
103      * Inform this state definition that an event was signaled in it. The
104      * signaled event is the last event available in given request context
105      * ({@link RequestContext#getLastEvent()}).
106      * @param context the flow execution control context
107      * @return the selected view
108      * @throws NoMatchingTransitionException when a matching transition cannot
109      * be found
110      */

111     public ViewSelection onEvent(RequestControlContext context) throws NoMatchingTransitionException {
112         return getRequiredTransition(context).execute(this, context);
113     }
114
115     /**
116      * Re-enter this state. This is typically called when a transition out of
117      * this state is selected, but transition execution rolls back and as a
118      * result the flow reenters the source state.
119      * <p>
120      * By default, this just calls <code>enter()</code>.
121      * @param context the flow control context in an executing flow (a client
122      * instance of a flow)
123      * @return a view selection containing model and view information needed to
124      * render the results of the state processing
125      */

126     public ViewSelection reenter(RequestControlContext context) {
127         return enter(context);
128     }
129
130     /**
131      * Exit this state. This is typically called when a transition takes the
132      * flow out of this state into another state. By default just executes any
133      * registered exit actions.
134      * @param context the flow control context
135      */

136     public void exit(RequestControlContext context) {
137         exitActionList.execute(context);
138     }
139
140     protected void appendToString(ToStringCreator creator) {
141         creator.append("transitions", transitions).append("exitActionList", exitActionList);
142     }
143 }
Popular Tags