1
2
3
4
5 package net.sf.wfnm;
6
7 import java.io.Serializable;
8
9 import java.util.HashSet;
10 import java.util.Map;
11 import java.util.Set;
12 import java.util.Stack;
13
14
15 /***
16 * Defines a webflow with a name and a default ownership.<br>
17 * It contains a stack of visited pages and a set of owned objects.
18 *
19 * @author <a href="mailto:malbari@users.sourceforge.net">Maurizio Albari</a>
20 * @version 1.0.6
21 */
22 public class Webflow implements ObjectSetOwner, Serializable {
23 /***
24 * The page changed listener
25 */
26 private PageChangedListener pageChangedListener;
27
28 /***
29 * The object set owned by this webflow.
30 */
31 private Set ownedObjectSet;
32
33 /***
34 * The stack of pages visited during this webflow.
35 */
36 private Stack pageStack = new Stack();
37
38 /***
39 * The name of this webflow.
40 */
41 private String name;
42
43 /***
44 * The default ownership.
45 */
46 private int defaultOwnership;
47
48 /***
49 * Creates a new Webflow object.
50 *
51 * @param name the name of this webflow
52 * @param defaultOwnership the default ownership
53 * @param pageChangedListener the page changed listener
54 */
55 public Webflow(String name, int defaultOwnership, PageChangedListener pageChangedListener) {
56 this.name = name;
57 this.defaultOwnership = defaultOwnership;
58 this.pageChangedListener = pageChangedListener;
59 }
60
61 /***
62 * Gets the default ownership.
63 *
64 * @return the default ownership
65 */
66 public int getDefaultOwnership() {
67 return defaultOwnership;
68 }
69
70 /***
71 * Gets the name of this webflow.
72 *
73 * @return the name of this webflow
74 */
75 public String getName() {
76 return name;
77 }
78
79 /***
80 * Gets the object set owned by this webflow.
81 *
82 * @return the object set owned by this webflow
83 */
84 public Set getOwnedObjectSet() {
85 if (ownedObjectSet == null) {
86 ownedObjectSet = new HashSet();
87 }
88
89 return ownedObjectSet;
90 }
91
92 /***
93 * Gets the page stack.
94 *
95 * @return the page stack
96 */
97 public Stack getPageStack() {
98 return pageStack;
99 }
100
101 /***
102 * Gets the previous page.
103 *
104 * @return the previous page (null=no prevoius page)
105 */
106 public Page getPreviousPage() {
107 Page page = null;
108
109 if (pageStack.size() >= 2) {
110 page = (Page) pageStack.elementAt(pageStack.size() - 2);
111 }
112
113 return page;
114 }
115
116 /***
117 * Gets the top page of this webflow (null if the webflow is empty).
118 *
119 * @return the top page of this webflow (null if the webflow is empty)
120 */
121 public Page getTopPage() {
122 Page topPage = null;
123
124 if (pageStack.size() > 0) {
125 topPage = (Page) pageStack.lastElement();
126 }
127
128 return topPage;
129 }
130
131 /***
132 * Forward to a page inside this webflow.
133 *
134 * @param page the url of the page
135 * @param container the attribute container
136 */
137 public void addPage(Page page, AttributeContainer container) {
138 pageStack.push(page);
139
140 if (pageChangedListener != null) {
141 pageChangedListener.pageOpened(page.getUrl(), container);
142 }
143 }
144
145 /***
146 * Back to a page inside this webflow.
147 *
148 * @param url the url of the page
149 * @param objectSetToRemove the object set to remove for adding objects to remove
150 * @param urlMapToClear the url map to clear
151 * @param container the attribute container
152 */
153 public void backToPage(String url, Set objectSetToRemove, Map urlMapToClear, AttributeContainer container) {
154 Page page = getTopPage();
155
156 while (!page.getUrl().equals(url)) {
157 objectSetToRemove.addAll(page.getOwnedObjectSet());
158
159 Page removedPage = (Page) pageStack.pop();
160
161 if (pageChangedListener != null) {
162 pageChangedListener.pageClosed(removedPage.getUrl(), container);
163 }
164
165 urlMapToClear.remove(removedPage.getUrl());
166 page = getTopPage();
167 }
168 }
169
170 /***
171 * Closes this webflow.
172 *
173 * @param objectSetToRemove adds to this set all object that has to be removed
174 * @param urlMapToClear the url map to clear
175 * @param container the attribute container
176 */
177 public void closeWebflow(Set objectSetToRemove, Map urlMapToClear, AttributeContainer container) {
178 while (!pageStack.isEmpty()) {
179 Page page = (Page) pageStack.pop();
180
181 if (pageChangedListener != null) {
182 pageChangedListener.pageClosed(page.getUrl(), container);
183 }
184
185 urlMapToClear.remove(page.getUrl());
186 objectSetToRemove.addAll(page.getOwnedObjectSet());
187 }
188
189 objectSetToRemove.addAll(getOwnedObjectSet());
190 }
191 }