001: /*
002: * Copyright 2006 Pentaho Corporation. All rights reserved.
003: * This software was developed by Pentaho Corporation and is provided under the terms
004: * of the Mozilla Public License, Version 1.1, or any later version. You may not use
005: * this file except in compliance with the license. If you need a copy of the license,
006: * please go to http://www.mozilla.org/MPL/MPL-1.1.txt. The Original Code is the Pentaho
007: * BI Platform. The Initial Developer is Pentaho Corporation.
008: *
009: * Software distributed under the Mozilla Public License is distributed on an "AS IS"
010: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. Please refer to
011: * the license for the specific language governing your rights and limitations.
012: *
013: * @created Feb 01, 2006
014: * @author Michael D'Amour
015: */
016: package org.pentaho.jfreereport.wizard;
017:
018: import java.util.LinkedList;
019: import java.util.List;
020: import org.pentaho.jfreereport.wizard.ui.IWizardListener;
021: import org.pentaho.jfreereport.wizard.ui.WizardPanel;
022:
023: public class WizardManager {
024: /**
025: *
026: */
027: List eventListenerList = new LinkedList();
028:
029: /**
030: *
031: */
032: protected List steps = new LinkedList();
033:
034: /**
035: *
036: */
037: protected WizardPanel currentStep = null;
038:
039: /**
040: *
041: */
042: protected WizardPanel previousStep = null;
043:
044: /**
045: *
046: *
047: * @param l
048: */
049: public void addWizardListener(IWizardListener l) {
050: eventListenerList.add(l);
051: }
052:
053: /**
054: *
055: *
056: * @param l
057: */
058: public void removeWizardListener(IWizardListener l) {
059: eventListenerList.remove(l);
060: }
061:
062: /**
063: *
064: */
065: public void removeAllWizardListeners() {
066: eventListenerList.clear();
067: }
068:
069: /**
070: *
071: *
072: * @return $objectType$
073: */
074: public boolean isContinueAllowed() {
075: if ((currentStep == null) || (steps == null)
076: || !currentStep.isNextAllowed()) {
077: return false;
078: }
079: // we must check all steps leading up to this step
080: boolean nextAllowed = true;
081: for (int i = 0; nextAllowed
082: && (i <= steps.indexOf(currentStep)); i++) {
083: WizardPanel step = (WizardPanel) steps.get(i);
084: nextAllowed = step.isContinueAllowed();
085: }
086: return nextAllowed;
087: }
088:
089: /**
090: *
091: *
092: * @return $objectType$
093: */
094: public boolean isNextAllowed() {
095: boolean continueAllowed = isContinueAllowed();
096: if (continueAllowed
097: && (currentStep == steps.get(steps.size() - 1))) {
098: continueAllowed = false;
099: }
100: return continueAllowed;
101: }
102:
103: /**
104: *
105: *
106: * @return $objectType$
107: */
108: public boolean isFinishAllowed() {
109: boolean finishAllowed = true;
110: for (int i = 0; (i < steps.size()) && finishAllowed; i++) {
111: WizardPanel p = (WizardPanel) steps.get(i);
112: finishAllowed = p.isContinueAllowed();
113: }
114: return finishAllowed;
115: }
116:
117: /**
118: *
119: *
120: * @return $objectType$
121: */
122: public boolean isBackAllowed() {
123: if ((currentStep == null) || (steps == null)
124: || (currentStep == steps.get(0))
125: || !currentStep.isBackAllowed()) {
126: return false;
127: }
128: return true;
129: }
130:
131: /**
132: *
133: *
134: * @return $objectType$
135: */
136: public boolean next() {
137: System.out.println("currentStep nextFired begin: "
138: + currentStep.getClass().getName());
139: boolean status = currentStep.nextFired(currentStep);
140: System.out.println("currentStep nextFired end: "
141: + currentStep.getClass().getName());
142: if (status) {
143: previousStep = currentStep;
144: currentStep = (WizardPanel) steps.get(steps
145: .indexOf(currentStep) + 1);
146: List delayedEventList = new LinkedList();
147: for (int i = 0; i < eventListenerList.size(); i++) {
148: IWizardListener l = (IWizardListener) eventListenerList
149: .get(i);
150: if (previousStep == l) {
151: continue;
152: } else if (l instanceof WizardPanel) {
153: l.nextFired(previousStep);
154: } else {
155: delayedEventList.add(l);
156: }
157: }
158: for (int i = 0; i < delayedEventList.size(); i++) {
159: IWizardListener l = (IWizardListener) delayedEventList
160: .get(i);
161: l.nextFired(previousStep);
162: }
163: previousStep.stateChanged = false;
164: }
165: return status;
166: }
167:
168: /**
169: *
170: *
171: * @return $objectType$
172: */
173: public boolean back() {
174: currentStep.backFired(currentStep);
175: previousStep = currentStep;
176: currentStep = (WizardPanel) steps.get(steps
177: .indexOf(currentStep) - 1);
178: List delayedEventList = new LinkedList();
179: for (int i = 0; i < eventListenerList.size(); i++) {
180: IWizardListener l = (IWizardListener) eventListenerList
181: .get(i);
182: if (previousStep == l) {
183: continue;
184: } else if (l instanceof WizardPanel) {
185: l.backFired(previousStep);
186: } else {
187: delayedEventList.add(l);
188: }
189: }
190: for (int i = 0; i < delayedEventList.size(); i++) {
191: IWizardListener l = (IWizardListener) delayedEventList
192: .get(i);
193: l.backFired(previousStep);
194: }
195: return true;
196: }
197:
198: public boolean publish() {
199: currentStep.finishFired(currentStep);
200: List delayedEventList = new LinkedList();
201: for (int i = 0; i < eventListenerList.size(); i++) {
202: IWizardListener l = (IWizardListener) eventListenerList
203: .get(i);
204: if (previousStep == l) {
205: continue;
206: } else if (l instanceof WizardPanel) {
207: l.publishFired(currentStep);
208: } else {
209: delayedEventList.add(l);
210: }
211: }
212: for (int i = 0; i < delayedEventList.size(); i++) {
213: IWizardListener l = (IWizardListener) delayedEventList
214: .get(i);
215: l.publishFired(currentStep);
216: }
217: currentStep.stateChanged = false;
218: return true;
219: }
220:
221: /**
222: *
223: *
224: * @return $objectType$
225: */
226: public boolean finish() {
227: currentStep.finishFired(currentStep);
228: List delayedEventList = new LinkedList();
229: for (int i = 0; i < eventListenerList.size(); i++) {
230: IWizardListener l = (IWizardListener) eventListenerList
231: .get(i);
232: if (previousStep == l) {
233: continue;
234: } else if (l instanceof WizardPanel) {
235: l.finishFired(currentStep);
236: } else {
237: delayedEventList.add(l);
238: }
239: }
240: for (int i = 0; i < delayedEventList.size(); i++) {
241: IWizardListener l = (IWizardListener) delayedEventList
242: .get(i);
243: l.finishFired(currentStep);
244: }
245: currentStep.stateChanged = false;
246: return true;
247: }
248:
249: public boolean preview() {
250: boolean status = currentStep.previewFired(currentStep);
251: List delayedEventList = new LinkedList();
252: for (int i = 0; status && i < eventListenerList.size(); i++) {
253: IWizardListener l = (IWizardListener) eventListenerList
254: .get(i);
255: if (previousStep == l) {
256: continue;
257: } else if (l instanceof WizardPanel && l != currentStep) {
258: status = l.previewFired(currentStep);
259: } else {
260: delayedEventList.add(l);
261: }
262: }
263: for (int i = 0; status && i < delayedEventList.size(); i++) {
264: IWizardListener l = (IWizardListener) delayedEventList
265: .get(i);
266: status = l.previewFired(currentStep);
267: }
268: currentStep.stateChanged = false;
269: return status;
270: }
271:
272: /**
273: *
274: *
275: * @return $objectType$
276: */
277: public boolean cancel() {
278: // any cleanup?
279: boolean status = currentStep.cancelFired(currentStep);
280: if (status) {
281: List delayedEventList = new LinkedList();
282: for (int i = 0; i < eventListenerList.size(); i++) {
283: IWizardListener l = (IWizardListener) eventListenerList
284: .get(i);
285: if (previousStep == l) {
286: continue;
287: } else if (l instanceof WizardPanel) {
288: status = l.cancelFired(currentStep);
289: } else {
290: delayedEventList.add(l);
291: }
292: }
293: for (int i = 0; i < delayedEventList.size() && status; i++) {
294: IWizardListener l = (IWizardListener) delayedEventList
295: .get(i);
296: status = l.cancelFired(currentStep);
297: }
298: }
299: return status;
300: }
301:
302: /**
303: * DOCUMENT ME!
304: *
305: * @return Returns the currentStep.
306: */
307: public WizardPanel getCurrentStep() {
308: return currentStep;
309: }
310:
311: /**
312: *
313: *
314: * @param step
315: */
316: public void setCurrentStep(WizardPanel step) {
317: currentStep = step;
318: }
319:
320: /**
321: *
322: *
323: * @param steps
324: */
325: public void setSteps(List steps) {
326: this .steps = steps;
327: }
328:
329: /**
330: *
331: *
332: * @param step
333: */
334: public void addStep(WizardPanel step) {
335: steps.add(step);
336: }
337:
338: /**
339: *
340: */
341: public void update() {
342: for (int i = 0; i < steps.size(); i++) {
343: WizardPanel p = (WizardPanel) steps.get(i);
344: p.updateState();
345: }
346: }
347:
348: /**
349: * DOCUMENT ME!
350: *
351: * @return Returns the previousStep.
352: */
353: public WizardPanel getPreviousStep() {
354: return previousStep;
355: }
356:
357: /**
358: * DOCUMENT ME!
359: *
360: * @param previousStep
361: * The previousStep to set.
362: */
363: public void setPreviousStep(WizardPanel previousStep) {
364: this .previousStep = previousStep;
365: }
366:
367: /**
368: *
369: *
370: * @return $objectType$
371: */
372: public List getSteps() {
373: return steps;
374: }
375: }
|