1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.myfaces.tobago.model;
21
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24
25 import javax.faces.event.ActionEvent;
26 import java.util.ArrayList;
27 import java.util.List;
28
29 public class Wizard {
30
31 private static final Logger LOG = LoggerFactory.getLogger(Wizard.class);
32
33 private int index;
34
35 private List<WizardStep> course;
36
37 public Wizard() {
38 reset();
39 }
40
41 public void next(ActionEvent event) {
42 LOG.debug("next: " + event);
43
44 index++;
45 }
46
47 public void gotoStep(ActionEvent event) {
48 Object step = (event.getComponent().getAttributes().get("step"));
49 if (step instanceof Integer) {
50 index = (Integer) step;
51 } else {
52 index = Integer.parseInt((String) step);
53 }
54
55 LOG.debug("gotoStep: " + index);
56 }
57
58 public String previous() {
59 String outcome = getPreviousStep().getOutcome();
60 if (index > 0) {
61 index--;
62 } else {
63 LOG.error("Previous not available!");
64 }
65
66 LOG.debug("gotoStep: " + index);
67 return outcome;
68 }
69
70 public final boolean isPreviousAvailable() {
71 return getIndex() > 0;
72 }
73
74 public final void finish(ActionEvent event) {
75 if (LOG.isDebugEnabled()) {
76 LOG.debug("finish");
77 }
78
79 reset();
80 }
81
82 public final void cancel(ActionEvent event) {
83 if (LOG.isDebugEnabled()) {
84 LOG.debug("cancel");
85 }
86 reset();
87 }
88
89 public final int getIndex() {
90 return index;
91 }
92
93
94
95
96 public void reset() {
97 index = 0;
98 course = new ArrayList<WizardStep>();
99 }
100
101 public List<WizardStep> getCourse() {
102 return course;
103 }
104
105 public int getSize() {
106 return course.size();
107 }
108
109 public void register() {
110
111 if (index == course.size()) {
112 course.add(new WizardStep(index));
113 } else if (index < course.size()) {
114 course.set(index, new WizardStep(index));
115 } else {
116 throw new IllegalStateException("Index too large for course: index="
117 + index + " course.size()=" + course.size());
118 }
119 if (LOG.isInfoEnabled()) {
120 LOG.info("course: " + course);
121 }
122 }
123
124 public WizardStep getPreviousStep() {
125 if (index > 0) {
126 return course.get(index - 1);
127 } else {
128 return null;
129 }
130 }
131 public WizardStep getCurrentStep() {
132 return course.get(index);
133 }
134
135 public void removeForwardSteps() {
136
137 LOG.error("Not implemented yet");
138 }
139 }