/
Wizard.java
588 lines (517 loc) · 19.3 KB
/
Wizard.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
package org.vaadin.teemu.wizards;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.vaadin.teemu.wizards.event.WizardCancelledEvent;
import org.vaadin.teemu.wizards.event.WizardCompletedEvent;
import org.vaadin.teemu.wizards.event.WizardProgressListener;
import org.vaadin.teemu.wizards.event.WizardStepActivationEvent;
import org.vaadin.teemu.wizards.event.WizardStepSetChangedEvent;
import com.vaadin.server.Page;
import com.vaadin.server.Page.UriFragmentChangedEvent;
import com.vaadin.server.Page.UriFragmentChangedListener;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Component;
import com.vaadin.ui.CustomComponent;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Panel;
import com.vaadin.ui.VerticalLayout;
/**
* Component for displaying multi-step wizard style user interface.
*
* <p>
* The steps of the wizard must be implementations of the {@link WizardStep}
* interface. Use the {@link #addStep(WizardStep)} method to add these steps in
* the same order they are supposed to be displayed.
* </p>
*
* <p>
* The wizard also supports navigation through URI fragments. This feature is
* disabled by default, but you can enable it using
* {@link #setUriFragmentEnabled(boolean)} method. Each step will get a
* generated identifier that is used as the URI fragment. If you wish to
* override these with your own identifiers, you can add the steps using the
* overloaded {@link #addStep(WizardStep, String)} method.
* </p>
*
* <p>
* To react on the progress, cancellation or completion of this {@code Wizard}
* you should add one or more listeners that implement the
* {@link WizardProgressListener} interface. These listeners are added using the
* {@link #addListener(WizardProgressListener)} method and removed with the
* {@link #removeListener(WizardProgressListener)}.
* </p>
*
* @author Teemu Pöntelin / Vaadin Ltd
*/
@SuppressWarnings("serial")
public class Wizard extends CustomComponent implements
UriFragmentChangedListener {
protected final List<WizardStep> steps = new ArrayList<WizardStep>();
protected final Map<String, WizardStep> idMap = new HashMap<String, WizardStep>();
private final Map<WizardStep, ScrollPosition> scrollPositions = new HashMap<WizardStep, ScrollPosition>();
protected WizardStep currentStep;
protected WizardStep lastCompletedStep;
private int stepIndex = 1;
protected VerticalLayout mainLayout;
protected HorizontalLayout footer;
private Panel contentPanel;
private Button nextButton;
private Button backButton;
private Button finishButton;
private Button cancelButton;
private Component header;
private boolean uriFragmentEnabled;
private static final Method WIZARD_ACTIVE_STEP_CHANGED_METHOD;
private static final Method WIZARD_STEP_SET_CHANGED_METHOD;
private static final Method WIZARD_COMPLETED_METHOD;
private static final Method WIZARD_CANCELLED_METHOD;
static {
try {
WIZARD_COMPLETED_METHOD = WizardProgressListener.class
.getDeclaredMethod("wizardCompleted",
new Class[] { WizardCompletedEvent.class });
WIZARD_STEP_SET_CHANGED_METHOD = WizardProgressListener.class
.getDeclaredMethod("stepSetChanged",
new Class[] { WizardStepSetChangedEvent.class });
WIZARD_ACTIVE_STEP_CHANGED_METHOD = WizardProgressListener.class
.getDeclaredMethod("activeStepChanged",
new Class[] { WizardStepActivationEvent.class });
WIZARD_CANCELLED_METHOD = WizardProgressListener.class
.getDeclaredMethod("wizardCancelled",
new Class[] { WizardCancelledEvent.class });
} catch (final java.lang.NoSuchMethodException e) {
// This should never happen
throw new java.lang.RuntimeException(
"Internal error finding methods in Wizard", e);
}
}
private static final class ScrollPosition {
int scrollTop;
int scrollLeft;
public ScrollPosition(int scrollTop, int scrollLeft) {
this.scrollTop = scrollTop;
this.scrollLeft = scrollLeft;
}
}
public Wizard() {
setStyleName("wizard");
init();
}
private void init() {
mainLayout = new VerticalLayout();
setCompositionRoot(mainLayout);
setSizeFull();
contentPanel = new Panel();
contentPanel.setSizeFull();
initControlButtons();
footer = new HorizontalLayout();
footer.setSpacing(true);
footer.addComponent(cancelButton);
footer.addComponent(backButton);
footer.addComponent(nextButton);
footer.addComponent(finishButton);
mainLayout.addComponent(contentPanel);
mainLayout.addComponent(footer);
mainLayout.setComponentAlignment(footer, Alignment.BOTTOM_RIGHT);
mainLayout.setExpandRatio(contentPanel, 1.0f);
mainLayout.setSizeFull();
initDefaultHeader();
}
private void initControlButtons() {
nextButton = new Button("Next");
nextButton.addClickListener(new Button.ClickListener() {
public void buttonClick(ClickEvent event) {
next();
}
});
backButton = new Button("Back");
backButton.addClickListener(new Button.ClickListener() {
public void buttonClick(ClickEvent event) {
back();
}
});
finishButton = new Button("Finish");
finishButton.addClickListener(new Button.ClickListener() {
public void buttonClick(ClickEvent event) {
finish();
}
});
finishButton.setEnabled(false);
cancelButton = new Button("Cancel");
cancelButton.addClickListener(new Button.ClickListener() {
public void buttonClick(ClickEvent event) {
cancel();
}
});
}
private void initDefaultHeader() {
WizardProgressBar progressBar = new WizardProgressBar(this);
addListener(progressBar);
setHeader(progressBar);
}
public void setUriFragmentEnabled(boolean enabled) {
if (enabled) {
Page.getCurrent().addUriFragmentChangedListener(this);
} else {
Page.getCurrent().removeUriFragmentChangedListener(this);
}
uriFragmentEnabled = enabled;
}
public boolean isUriFragmentEnabled() {
return uriFragmentEnabled;
}
/**
* Sets a {@link Component} that is displayed on top of the actual content.
* Set to {@code null} to remove the header altogether.
*
* @param newHeader
* {@link Component} to be displayed on top of the actual content
* or {@code null} to remove the header.
*/
public void setHeader(Component newHeader) {
if (header != null) {
if (newHeader == null) {
mainLayout.removeComponent(header);
} else {
mainLayout.replaceComponent(header, newHeader);
}
} else {
if (newHeader != null) {
mainLayout.addComponentAsFirst(newHeader);
}
}
this.header = newHeader;
}
/**
* Returns a {@link Component} that is displayed on top of the actual
* content or {@code null} if no header is specified.
*
* <p>
* By default the header is a {@link WizardProgressBar} component that is
* also registered as a {@link WizardProgressListener} to this Wizard.
* </p>
*
* @return {@link Component} that is displayed on top of the actual content
* or {@code null}.
*/
public Component getHeader() {
return header;
}
/**
* Adds a step to this Wizard with the given identifier. The used {@code id}
* must be unique or an {@link IllegalArgumentException} is thrown. If you
* don't wish to explicitly provide an identifier, you can use the
* {@link #addStep(WizardStep)} method.
*
* @param step
* @param id
* @throws IllegalStateException
* if the given {@code id} already exists.
*/
public void addStep(WizardStep step, String id) {
if (idMap.containsKey(id)) {
throw new IllegalArgumentException(
String.format(
"A step with given id %s already exists. You must use unique identifiers for the steps.",
id));
}
steps.add(step);
idMap.put(id, step);
updateButtons();
// notify listeners
fireEvent(new WizardStepSetChangedEvent(this));
// activate the first step immediately
if (currentStep == null) {
activateStep(step);
}
}
/**
* Adds a step to this Wizard. The WizardStep will be assigned an identifier
* automatically. If you wish to provide an explicit identifier for your
* WizardStep, you can use the {@link #addStep(WizardStep, String)} method
* instead.
*
* @param step
*/
public void addStep(WizardStep step) {
addStep(step, "wizard-step-" + stepIndex++);
}
public void addListener(WizardProgressListener listener) {
addListener(WizardCompletedEvent.class, listener,
WIZARD_COMPLETED_METHOD);
addListener(WizardStepActivationEvent.class, listener,
WIZARD_ACTIVE_STEP_CHANGED_METHOD);
addListener(WizardStepSetChangedEvent.class, listener,
WIZARD_STEP_SET_CHANGED_METHOD);
addListener(WizardCancelledEvent.class, listener,
WIZARD_CANCELLED_METHOD);
}
public void removeListener(WizardProgressListener listener) {
removeListener(WizardCompletedEvent.class, listener,
WIZARD_COMPLETED_METHOD);
removeListener(WizardStepActivationEvent.class, listener,
WIZARD_ACTIVE_STEP_CHANGED_METHOD);
removeListener(WizardStepSetChangedEvent.class, listener,
WIZARD_STEP_SET_CHANGED_METHOD);
removeListener(WizardCancelledEvent.class, listener,
WIZARD_CANCELLED_METHOD);
}
public List<WizardStep> getSteps() {
return Collections.unmodifiableList(steps);
}
/**
* Returns {@code true} if the given step is already completed by the user.
*
* @param step
* step to check for completion.
* @return {@code true} if the given step is already completed.
*/
public boolean isCompleted(WizardStep step) {
return steps.indexOf(step) < steps.indexOf(currentStep);
}
/**
* Returns {@code true} if the given step is the currently active step.
*
* @param step
* step to check for.
* @return {@code true} if the given step is the currently active step.
*/
public boolean isActive(WizardStep step) {
return (step == currentStep);
}
private void updateButtons() {
if (isLastStep(currentStep)) {
finishButton.setEnabled(true);
nextButton.setEnabled(false);
} else {
finishButton.setEnabled(false);
nextButton.setEnabled(true);
}
backButton.setEnabled(!isFirstStep(currentStep));
}
public Button getNextButton() {
return nextButton;
}
public Button getBackButton() {
return backButton;
}
public Button getFinishButton() {
return finishButton;
}
public Button getCancelButton() {
return cancelButton;
}
protected void activateStep(WizardStep step) {
if (step == null) {
return;
}
if (currentStep != null) {
if (currentStep.equals(step)) {
// already active
return;
}
// ask if we're allowed to move
boolean advancing = steps.indexOf(step) > steps
.indexOf(currentStep);
if (advancing) {
if (!currentStep.onAdvance()) {
// not allowed to advance
return;
}
} else {
if (!currentStep.onBack()) {
// not allowed to go back
return;
}
}
// keep track of the last step that was completed
int currentIndex = steps.indexOf(currentStep);
if (lastCompletedStep == null
|| steps.indexOf(lastCompletedStep) < currentIndex) {
lastCompletedStep = currentStep;
}
saveScrollPosition(currentStep);
}
contentPanel.setContent(step.getContent());
currentStep = step;
restoreScrollPosition(currentStep);
updateUriFragment();
updateButtons();
fireEvent(new WizardStepActivationEvent(this, step));
}
private void restoreScrollPosition(WizardStep step) {
ScrollPosition scrollPosition = scrollPositions.get(step);
if (scrollPosition != null) {
contentPanel.setScrollTop(scrollPosition.scrollTop);
contentPanel.setScrollLeft(scrollPosition.scrollLeft);
} else {
// scroll to top
contentPanel.setScrollTop(0);
contentPanel.setScrollLeft(0);
}
}
private void saveScrollPosition(WizardStep step) {
// remove possible old value
scrollPositions.remove(step);
int scrollTop = contentPanel.getScrollTop();
int scrollLeft = contentPanel.getScrollLeft();
if (scrollTop > 0 || scrollLeft > 0) {
// save only if not at the default value (both 0)
scrollPositions
.put(step, new ScrollPosition(scrollTop, scrollLeft));
}
}
protected void activateStep(String id) {
WizardStep step = idMap.get(id);
if (step != null) {
// check that we don't go past the lastCompletedStep by using the id
int lastCompletedIndex = lastCompletedStep == null ? -1 : steps
.indexOf(lastCompletedStep);
int stepIndex = steps.indexOf(step);
if (lastCompletedIndex < stepIndex) {
activateStep(lastCompletedStep);
} else {
activateStep(step);
}
}
}
protected String getId(WizardStep step) {
for (Map.Entry<String, WizardStep> entry : idMap.entrySet()) {
if (entry.getValue().equals(step)) {
return entry.getKey();
}
}
return null;
}
private void updateUriFragment() {
if (isUriFragmentEnabled()) {
String currentStepId = getId(currentStep);
if (currentStepId != null && currentStepId.length() > 0) {
Page.getCurrent().setUriFragment(currentStepId, false);
} else {
Page.getCurrent().setUriFragment(null, false);
}
}
}
protected boolean isFirstStep(WizardStep step) {
if (step != null) {
return steps.indexOf(step) == 0;
}
return false;
}
protected boolean isLastStep(WizardStep step) {
if (step != null && !steps.isEmpty()) {
return steps.indexOf(step) == (steps.size() - 1);
}
return false;
}
/**
* Cancels this Wizard triggering a {@link WizardCancelledEvent}. This
* method is called when user clicks the cancel button.
*/
public void cancel() {
fireEvent(new WizardCancelledEvent(this));
}
/**
* Triggers a {@link WizardCompletedEvent} if the current step is the last
* step and it allows advancing (see {@link WizardStep#onAdvance()}). This
* method is called when user clicks the finish button.
*/
public void finish() {
if (isLastStep(currentStep) && currentStep.onAdvance()) {
// next (finish) allowed -> fire complete event
fireEvent(new WizardCompletedEvent(this));
}
}
/**
* Activates the next {@link WizardStep} if the current step allows
* advancing (see {@link WizardStep#onAdvance()}) or calls the
* {@link #finish()} method the current step is the last step. This method
* is called when user clicks the next button.
*/
public void next() {
if (isLastStep(currentStep)) {
finish();
} else {
int currentIndex = steps.indexOf(currentStep);
activateStep(steps.get(currentIndex + 1));
}
}
/**
* Activates the previous {@link WizardStep} if the current step allows
* going back (see {@link WizardStep#onBack()}) and the current step is not
* the first step. This method is called when user clicks the back button.
*/
public void back() {
int currentIndex = steps.indexOf(currentStep);
if (currentIndex > 0) {
activateStep(steps.get(currentIndex - 1));
}
}
@Override
public void uriFragmentChanged(UriFragmentChangedEvent event) {
if (isUriFragmentEnabled()) {
String fragment = event.getUriFragment();
if ((fragment == null || fragment.equals("")) && !steps.isEmpty()) {
// empty fragment -> set the fragment of first step
Page.getCurrent().setUriFragment(getId(steps.get(0)));
} else {
activateStep(fragment);
}
}
}
/**
* Removes the given step from this Wizard. An {@link IllegalStateException}
* is thrown if the given step is already completed or is the currently
* active step.
*
* @param stepToRemove
* the step to remove.
* @see #isCompleted(WizardStep)
* @see #isActive(WizardStep)
*/
public void removeStep(WizardStep stepToRemove) {
if (idMap.containsValue(stepToRemove)) {
for (Map.Entry<String, WizardStep> entry : idMap.entrySet()) {
if (entry.getValue().equals(stepToRemove)) {
// delegate the actual removal to the overloaded method
removeStep(entry.getKey());
return;
}
}
}
}
/**
* Removes the step with given id from this Wizard. An
* {@link IllegalStateException} is thrown if the given step is already
* completed or is the currently active step.
*
* @param id
* identifier of the step to remove.
* @see #isCompleted(WizardStep)
* @see #isActive(WizardStep)
*/
public void removeStep(String id) {
if (idMap.containsKey(id)) {
WizardStep stepToRemove = idMap.get(id);
if (isCompleted(stepToRemove)) {
throw new IllegalStateException(
"Already completed step cannot be removed.");
}
if (isActive(stepToRemove)) {
throw new IllegalStateException(
"Currently active step cannot be removed.");
}
idMap.remove(id);
steps.remove(stepToRemove);
// notify listeners
fireEvent(new WizardStepSetChangedEvent(this));
}
}
}