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 java.util.Arrays;
019: import java.util.Iterator;
020: import java.util.LinkedList;
021: import java.util.List;
022:
023: import org.springframework.core.style.StylerUtils;
024: import org.springframework.webflow.execution.Action;
025: import org.springframework.webflow.execution.RequestContext;
026:
027: /**
028: * An ordered, typed list of actions, mainly for use internally by flow artifacts
029: * that can execute groups of actions.
030: *
031: * @see Flow#getStartActionList()
032: * @see Flow#getEndActionList()
033: * @see State#getEntryActionList()
034: * @see ActionState#getActionList()
035: * @see TransitionableState#getExitActionList()
036: * @see ViewState#getRenderActionList()
037: *
038: * @author Keith Donald
039: */
040: public class ActionList {
041:
042: /**
043: * The lists of actions.
044: */
045: private List actions = new LinkedList();
046:
047: /**
048: * Add an action to this list.
049: * @param action the action to add
050: * @return true if this list's contents changed as a result of the add
051: * operation
052: */
053: public boolean add(Action action) {
054: return actions.add(action);
055: }
056:
057: /**
058: * Add a collection of actions to this list.
059: * @param actions the actions to add
060: * @return true if this list's contents changed as a result of the add
061: * operation
062: */
063: public boolean addAll(Action[] actions) {
064: if (actions == null) {
065: return false;
066: }
067: return this .actions.addAll(Arrays.asList(actions));
068: }
069:
070: /**
071: * Tests if the action is in this list.
072: * @param action the action
073: * @return true if the action is contained in this list, false otherwise
074: */
075: public boolean contains(Action action) {
076: return actions.contains(action);
077: }
078:
079: /**
080: * Remove the action instance from this list.
081: * @param action the action to add
082: * @return true if this list's contents changed as a result of the remove
083: * operation
084: */
085: public boolean remove(Action action) {
086: return actions.remove(action);
087: }
088:
089: /**
090: * Returns the size of this action list.
091: * @return the action list size.
092: */
093: public int size() {
094: return actions.size();
095: }
096:
097: /**
098: * Returns the action in this list at the provided index.
099: * @param index the action index
100: * @return the action the action
101: */
102: public Action get(int index) throws IndexOutOfBoundsException {
103: return (Action) actions.get(index);
104: }
105:
106: /**
107: * Returns the action in this list at the provided index, exposing it as an
108: * annotated action. This allows clients to access specific properties about
109: * a target action instance if they exist.
110: * @return the action, as an annotated action
111: */
112: public AnnotatedAction getAnnotated(int index)
113: throws IndexOutOfBoundsException {
114: Action action = get(index);
115: if (action instanceof AnnotatedAction) {
116: return (AnnotatedAction) action;
117: } else {
118: // wrap the action; no annotations will be available
119: return new AnnotatedAction(action);
120: }
121: }
122:
123: /**
124: * Returns an iterator over this action list.
125: */
126: public Iterator iterator() {
127: return actions.iterator();
128: }
129:
130: /**
131: * Convert this list to a typed action array.
132: * @return the action list, as a typed array
133: */
134: public Action[] toArray() {
135: return (Action[]) actions.toArray(new Action[actions.size()]);
136: }
137:
138: /**
139: * Returns the list of actions in this list as a typed annotated action
140: * array. This is a convenience method allowing clients to access properties
141: * about an action if they exist.
142: * @return the annotated action list, as a typed array
143: */
144: public AnnotatedAction[] toAnnotatedArray() {
145: AnnotatedAction[] annotatedActions = new AnnotatedAction[actions
146: .size()];
147: for (int i = 0; i < size(); i++) {
148: annotatedActions[i] = getAnnotated(i);
149: }
150: return annotatedActions;
151: }
152:
153: /**
154: * Executes the actions contained within this action list. Simply iterates
155: * over each action and calls execute. Action result events are ignored.
156: * @param context the action execution request context
157: */
158: public void execute(RequestContext context) {
159: Iterator it = actions.iterator();
160: while (it.hasNext()) {
161: ActionExecutor.execute((Action) it.next(), context);
162: }
163: }
164:
165: public String toString() {
166: return StylerUtils.style(actions);
167: }
168: }
|