2009/05/20 - Apache Shale has been retired.
For more information, please explore the Attic.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.shale.dialog.basic;
19
20 import java.io.Serializable;
21 import java.util.Map;
22
23 import javax.faces.context.FacesContext;
24
25 import org.apache.shale.dialog.basic.model.Dialog;
26 import org.apache.shale.dialog.basic.model.State;
27
28 /***
29 * <p>JavaBean that represents the current {@link State} within
30 * a {@link Dialog}.</p>
31 */
32 class Position implements Serializable {
33
34
35
36
37
38 /***
39 * Serial version UID.
40 */
41 private static final long serialVersionUID = -1600336297791202073L;
42
43
44 /***
45 * <p>Construct a new {@link Position} representing the specified
46 * {@link State} for the specified {@link Dialog}, and associated
47 * with the specified <code>data</code> instance.</p>
48 *
49 * @param dialog {@link Dialog} to be recorded
50 * @param state {@link State} to be recorded
51 * @param data Data instance to be recorded
52 */
53 Position(Dialog dialog, State state, java.lang.Object data) {
54 if (dialog == null) {
55 throw new IllegalArgumentException("Dialog cannot be null");
56 }
57 setDialog(dialog);
58 setState(state);
59 setData(data);
60 }
61
62
63
64
65
66 /***
67 * <p>The data instance for the dialog execution represeented by this
68 * {@link Position}.</p>
69 */
70 private Object data = null;
71
72
73 /***
74 * <p>The {@link Dialog} within which this {@link Position} is reported.
75 * This value is transient, and may need to be regenerated.</p>
76 */
77 private transient Dialog dialog = null;
78
79
80 /***
81 * <p>The name of the {@link Dialog} within which this {@link Position}
82 * is reported.</p>
83 */
84 private String dialogName = null;
85
86
87 /***
88 * <p>The {@link State} that represents the current position within the
89 * {@link Dialog} that is being executed. This value is transient, and
90 * may need to be regenerated.</p>
91 */
92 private transient State state = null;
93
94
95 /***
96 * <p>The name of the {@link State} that represents the current position
97 * within the {@link Dialog} that is being executed.</p>
98 */
99 private String stateName = null;
100
101
102
103
104
105 /***
106 * <p>Return the data object associated with this dialog execution.</p>
107 *
108 * @return The associated data object for this dialog instance
109 */
110 Object getData() {
111 return this.data;
112 }
113
114
115 /***
116 * <p>Set the data object associated with this dialog execution.</p>
117 *
118 * @param data The new data object
119 */
120 void setData(Object data) {
121 this.data = data;
122 }
123
124
125 /***
126 * <p>Return the {@link Dialog} whose execution is tracked by this
127 * {@link Position}.</p>
128 *
129 * @return The {@link Dialog} being tracked by this {@link Position}
130 * instance
131 */
132 Dialog getDialog() {
133 if (this.dialog != null) {
134 return this.dialog;
135 }
136 Map map = (Map)
137 FacesContext.getCurrentInstance().getExternalContext().
138 getApplicationMap().get(Globals.DIALOGS);
139 this.dialog = (Dialog) map.get(this.dialogName);
140 return this.dialog;
141 }
142
143
144 /***
145 * <p>Set the {@link Dialog} whose execution is being tracked by this
146 * {@link Position}.</p>
147 *
148 * @param dialog The {@link Dialog} instance being tracked
149 */
150 private void setDialog(Dialog dialog) {
151 this.dialog = dialog;
152 this.dialogName = dialog.getName();
153 }
154
155
156 /***
157 * <p>Return the {@link State} representing the current execution position
158 * within the owning {@link Dialog}.</p>
159 *
160 * @return The current {@link State} within the owning {@link Dialog}
161 */
162 State getState() {
163 if (this.state == null) {
164 Dialog dialog = getDialog();
165 if (dialog == null) {
166 return null;
167 }
168 this.state = dialog.findState(this.stateName);
169 }
170 return this.state;
171 }
172
173
174 /***
175 * <p>Set the {@link State} representing the current execution position
176 * within the owning {@link Dialog}.</p>
177 *
178 * @param state The new {@link State}
179 */
180 void setState(State state) {
181 if (state == null) {
182 this.state = null;
183 this.stateName = null;
184 } else {
185 this.state = state;
186 this.stateName = state.getName();
187 }
188 }
189
190
191
192
193
194 /***
195 * <p>Return a String representation of this object.</p>
196 *
197 * @return The String representation of this {@link Position} instance
198 */
199 public String toString() {
200 if (getState() == null) {
201 return "Position[dialog=" + getDialog().getName()
202 + ",data=" + getData() + "]";
203 } else {
204 return "Position[dialog=" + getDialog().getName()
205 + ",state=" + getState().getName()
206 + ",data=" + getData() + "]";
207 }
208 }
209
210
211 }