BrightSide Workbench Full Report + Source Code
Wizard.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2012 Lluis TurrĂ³ Cutiller <http://www.turro.org/>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Affero General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Affero General Public License for more details.
14  *
15  * You should have received a copy of the GNU Affero General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 package org.turro.zkoss.wizard;
20 
21 import java.util.Iterator;
22 import java.util.LinkedList;
23 import org.turro.i18n.I_;
24 import org.turro.zkoss.util.ComponentIterator;
25 import org.zkoss.zk.ui.event.Event;
26 import org.zkoss.zk.ui.event.Events;
27 import org.zkoss.zk.ui.util.GenericForwardComposer;
28 import org.zkoss.zul.Button;
29 import org.zkoss.zul.Caption;
30 import org.zkoss.zul.Tab;
31 import org.zkoss.zul.Tabbox;
32 
37 public class Wizard extends GenericForwardComposer {
38 
39  private Button wbprior, wbnext, wbfinish, wbcancel;
40  private LinkedList<WizardPanel> list = new LinkedList<WizardPanel>();
41  private Tabbox wizardbox;
42 
43  public void onOK() {
44  WizardPanel wp = (WizardPanel) wizardbox.getSelectedPanel();
45  if(wp.isAllowNext()) {
46  onClick$wbnext();
47  } else if(wp.isAllowFinish()) {
48  onClick$wbfinish();
49  }
50  }
51 
52  public void onCancel() {
53  WizardPanel wp = (WizardPanel) wizardbox.getSelectedPanel();
54  if(wp.isAllowCancel()) {
55  onClick$wbcancel();
56  }
57  }
58 
59  public void onClick$wbprior() {
60  priorPanel();
61  }
62 
63  public void onClick$wbnext() {
64  nextPanel();
65  }
66 
67  public void onClick$wbfinish() {
68  WizardPanel wp = (WizardPanel) wizardbox.getSelectedPanel();
69  if(wp.doLeave(true)) {
70  if(wp.doFinish()) {
71  Events.postEvent(new Event(Events.ON_CLOSE, self));
72  //self.detach();
73  }
74  }
75  }
76 
77  public void onClick$wbcancel() {
78  WizardPanel wp = (WizardPanel) wizardbox.getSelectedPanel();
79  if(wp.doCancel()) {
80  Events.postEvent(new Event(Events.ON_CLOSE, self));
81  //self.detach();
82  }
83  }
84 
85  protected void priorPanel() {
86  int current = wizardbox.getSelectedIndex();
87  if(current < 1) return;
88  WizardPanel wpcurrent = list.get(current);
89  if(wpcurrent.doLeave(false)) {
90  current--;
91  for(int i = current; i >= 0; i--) {
92  WizardPanel wp = list.get(i);
93  if(!wp.isShouldSkip()) {
94  if(wp.doEnter()) {
95  wizardbox.setSelectedIndex(i);
96  updateWizard();
97  }
98  break;
99  }
100  }
101  }
102  }
103 
104  public void nextPanel() {
105  int current = wizardbox.getSelectedIndex();
106  if(current > list.size() - 2) return;
107  WizardPanel wpcurrent = list.get(current);
108  if(wpcurrent.doLeave(true)) {
109  current++;
110  for(int i = current; i < list.size(); i++) {
111  WizardPanel wp = list.get(i);
112  if(!wp.isShouldSkip() && !wp.checkSkipFirst()) {
113  if(wp.doEnter()) {
114  wizardbox.setSelectedIndex(i);
115  updateWizard();
116  }
117  break;
118  }
119  }
120  }
121  }
122 
123  public void setPriorStatus(boolean enabled) {
124  wbprior.setDisabled(!enabled);
125  }
126 
127  public void setNextStatus(boolean enabled) {
128  wbnext.setDisabled(!enabled);
129  }
130 
131  public void setFinishStatus(boolean enabled) {
132  wbfinish.setDisabled(!enabled);
133  }
134 
135  public void setCancelStatus(boolean enabled) {
136  wbcancel.setDisabled(!enabled);
137  }
138 
139  @Override
140  public void doFinally() throws Exception {
141  super.doFinally();
142  Caption caption = new Caption(null, "/_zul/images/wizard.png");
143  self.appendChild(caption);
144  wizardbox.setSclass("wizard");
145  wizardbox.setHflex("true");
146  wizardbox.setVflex("true");
147  wizardbox.getTabs().setSclass("wtabs");
148  for(Iterator it = wizardbox.getTabs().getChildren().iterator(); it.hasNext();) {
149  Tab tab = (Tab) it.next();
150  tab.setDisabled(true);
151  tab.setSclass("wizardtab");
152  }
154  @Override
155  protected void doComponent(WizardPanel component) {
156  list.add(component);
157  }
158  };
159  list.getFirst().setAllowPrior(false);
160  list.getLast().setAllowNext(false);
161  updateWizard();
162  wbprior.setLabel(I_.get("<< Previous"));
163  wbnext.setLabel(I_.get("Next >>"));
164  wbfinish.setLabel(I_.get("Finish"));
165  wbcancel.setLabel(I_.get("Cancel"));
166  for(WizardPanel wp : list) {
167  if(!wp.shouldSkip && !wp.checkSkipFirst()) {
168  if(wp.doEnter()) {
169  wizardbox.setSelectedPanel(wp);
170  updateWizard();
171  break;
172  }
173  }
174  }
175  }
176 
177  protected void updateWizard() {
178  WizardPanel wp = (WizardPanel) wizardbox.getSelectedPanel();
179  for(Iterator it = wizardbox.getTabs().getChildren().iterator(); it.hasNext();) {
180  Tab tab = (Tab) it.next();
181  if(tab.getIndex() < wp.getIndex()) {
182  tab.setSclass("wpdone");
183  } else if(tab.getIndex() == wp.getIndex()) {
184  tab.setSclass("wpcurrent");
185  } else {
186  tab.setSclass("wppending");
187  }
188  }
189  if(wp.isFinalSplash()) {
190  wbfinish.setLabel(I_.get("Close"));
191  }
192  updateButtons();
193  }
194 
195  protected void updateButtons() {
196  WizardPanel wp = (WizardPanel) wizardbox.getSelectedPanel();
201  }
202 
203 }
static String get(String msg)
Definition: I_.java:41
boolean doLeave(boolean forwards)
void onClick $wbcancel()
Definition: Wizard.java:77
void setNextStatus(boolean enabled)
Definition: Wizard.java:127
void onClick $wbprior()
Definition: Wizard.java:59
void setPriorStatus(boolean enabled)
Definition: Wizard.java:123
void setCancelStatus(boolean enabled)
Definition: Wizard.java:135
void onClick $wbnext()
Definition: Wizard.java:63
void onClick $wbfinish()
Definition: Wizard.java:67
void setFinishStatus(boolean enabled)
Definition: Wizard.java:131