001: /*
002: * Copyright 2004-2007 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.springframework.webflow.engine;
017:
018: import org.springframework.core.style.ToStringCreator;
019: import org.springframework.util.Assert;
020: import org.springframework.webflow.execution.FlowExecutionException;
021: import org.springframework.webflow.execution.RequestContext;
022: import org.springframework.webflow.execution.ViewSelection;
023:
024: /**
025: * A view state is a state that issues a response to the user, for
026: * example, for soliciting form input.
027: * <p>
028: * To accomplish this, a <code>ViewState</code> makes a {@link ViewSelection},
029: * which contains the necessary information to issue a suitable response.
030: *
031: * @see org.springframework.webflow.engine.ViewSelector
032: *
033: * @author Keith Donald
034: * @author Erwin Vervaet
035: */
036: public class ViewState extends TransitionableState {
037:
038: /**
039: * The list of actions to be executed when this state is entered.
040: */
041: private ActionList renderActionList = new ActionList();
042:
043: /**
044: * The factory for the view selection to return when this state is entered.
045: */
046: private ViewSelector viewSelector = NullViewSelector.INSTANCE;
047:
048: /**
049: * Create a new view state.
050: * @param flow the owning flow
051: * @param id the state identifier (must be unique to the flow)
052: * @throws IllegalArgumentException when this state cannot be added to given
053: * flow, e.g. because the id is not unique
054: */
055: public ViewState(Flow flow, String id)
056: throws IllegalArgumentException {
057: super (flow, id);
058: }
059:
060: /**
061: * Returns the strategy used to select the view to render in this view
062: * state.
063: */
064: public ViewSelector getViewSelector() {
065: return viewSelector;
066: }
067:
068: /**
069: * Sets the strategy used to select the view to render in this view state.
070: */
071: public void setViewSelector(ViewSelector viewSelector) {
072: Assert
073: .notNull(viewSelector,
074: "The view selector to make view selections is required");
075: this .viewSelector = viewSelector;
076: }
077:
078: /**
079: * Returns the list of actions executable by this view state on entry and on
080: * refresh. The returned list is mutable.
081: * @return the state action list
082: */
083: public ActionList getRenderActionList() {
084: return renderActionList;
085: }
086:
087: /**
088: * Specialization of State's <code>doEnter</code> template method that
089: * executes behavior specific to this state type in polymorphic fashion.
090: * <p>
091: * Returns a view selection indicating a response to issue. The view
092: * selection typically contains all the data necessary to issue the
093: * response.
094: * @param context the control context for the currently executing flow, used
095: * by this state to manipulate the flow execution
096: * @return a view selection serving as a response instruction
097: * @throws FlowExecutionException if an exception occurs in this state
098: */
099: protected ViewSelection doEnter(RequestControlContext context)
100: throws FlowExecutionException {
101: if (viewSelector.isEntrySelectionRenderable(context)) {
102: // the entry selection will be rendered!
103: renderActionList.execute(context);
104: }
105: return viewSelector.makeEntrySelection(context);
106: }
107:
108: /**
109: * Request that the current view selection be reconstituted to support
110: * reissuing the response. This is an idempotent operation that may be
111: * safely called any number of times on a paused execution, used primarily
112: * to support a flow execution redirect.
113: * @param context the request context
114: * @return the view selection
115: * @throws FlowExecutionException if an exception occurs in this state
116: */
117: public ViewSelection refresh(RequestContext context)
118: throws FlowExecutionException {
119: renderActionList.execute(context);
120: return viewSelector.makeRefreshSelection(context);
121: }
122:
123: protected void appendToString(ToStringCreator creator) {
124: creator.append("viewSelector", viewSelector);
125: super.appendToString(creator);
126: }
127: }
|