-
Notifications
You must be signed in to change notification settings - Fork 389
/
WizardStep.java
472 lines (430 loc) · 12.1 KB
/
WizardStep.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
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.wicket.extensions.wizard;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.apache.wicket.Component;
import org.apache.wicket.extensions.wizard.dynamic.DynamicWizardModel;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.FormComponent;
import org.apache.wicket.markup.html.form.validation.IFormValidator;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.AbstractReadOnlyModel;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;
/**
* default implementation of {@link IWizardStep}. It is also a panel, which is used as the view
* component.
*
* <p>
* And example of a custom step with a panel follows.
*
* Java (defined e.g. in class x.NewUserWizard):
*
* <pre>
* private final class UserNameStep extends WizardStep
* {
* public UserNameStep()
* {
* super(new ResourceModel("username.title"), new ResourceModel("username.summary"));
* add(new RequiredTextField("user.userName"));
* add(new RequiredTextField("user.email").add(EmailAddressValidator.getInstance()));
* }
* }
* </pre>
*
* HTML (defined in e.g. file x/NewUserWizard$UserNameStep.html):
*
* <pre>
* <wicket:panel>
* <table>
* <tr>
* <td><wicket:message key="username">Username</wicket:message></td>
* <td><input type="text" wicket:id="user.userName" /></td>
* </tr>
* <tr>
* <td><wicket:message key="email">Email Address</wicket:message></td>
* <td><input type="text" wicket:id="user.email" /></td>
* </tr>
* </table>
* </wicket:panel>
* </pre>
*
* </p>
*
* @author Eelco Hillenius
*/
public class WizardStep extends Panel implements IWizardStep
{
/**
* Wraps form validators for this step such that they are only executed when this step is
* active.
*/
private final class FormValidatorWrapper implements IFormValidator
{
private static final long serialVersionUID = 1L;
private final List<IFormValidator> validators = new ArrayList<>();
/**
* Adds a form validator.
*
* @param validator
* The validator to add
*/
public final void add(final IFormValidator validator)
{
validators.add(validator);
}
/**
* @see org.apache.wicket.markup.html.form.validation.IFormValidator#getDependentFormComponents()
*/
@Override
public FormComponent<?>[] getDependentFormComponents()
{
if (isActiveStep())
{
Set<Component> components = new HashSet<>();
for (IFormValidator v : validators)
{
FormComponent<?>[] dependentComponents = v.getDependentFormComponents();
if (dependentComponents != null)
{
int len = dependentComponents.length;
components.addAll(Arrays.asList(dependentComponents).subList(0, len));
}
}
return components.toArray(new FormComponent[components.size()]);
}
return null;
}
/**
* @see org.apache.wicket.markup.html.form.validation.IFormValidator#validate(org.apache.wicket.markup.html.form.Form)
*/
@Override
public void validate(final Form<?> form)
{
if (isActiveStep())
{
for (IFormValidator v : validators)
{
v.validate(form);
}
}
}
/**
* @return whether the step this wrapper is part of is the current step
*/
private boolean isActiveStep()
{
return (wizardModel.getActiveStep().equals(WizardStep.this));
}
}
/**
* Default header for wizards.
*/
private final class Header extends Panel
{
private static final long serialVersionUID = 1L;
/**
* Construct.
*
* @param id
* The component id
* @param wizard
* The containing wizard
*/
public Header(final String id, final IWizard wizard)
{
super(id);
setDefaultModel(new CompoundPropertyModel<>(wizard));
add(new Label("title", new AbstractReadOnlyModel<String>()
{
private static final long serialVersionUID = 1L;
@Override
public String getObject()
{
return getTitle();
}
}));
add(new Label("summary", new AbstractReadOnlyModel<String>()
{
private static final long serialVersionUID = 1L;
@Override
public String getObject()
{
return getSummary();
}
}));
}
}
private static final long serialVersionUID = 1L;
/**
* Marks this step as being fully configured. Only when this is <tt>true</tt> can the wizard
* progress. True by default as that works best with normal forms. Clients can set this to false
* if some intermediate step, like a file upload, needs to be completed before the wizard may
* progress.
*/
private boolean complete = true;
/**
* A summary of this step, or some usage advice.
*/
private IModel<String> summary;
/**
* The title of this step.
*/
private IModel<String> title;
/**
* The wizard model.
*/
private IWizardModel wizardModel;
/**
* The wrapper of {@link IFormValidator}s for this step.
*/
private FormValidatorWrapper formValidatorWrapper = new FormValidatorWrapper();
/**
* Construct without a title and a summary. Useful for when you provide a custom header by
* overiding {@link #getHeader(String, Component, IWizard)}.
*/
public WizardStep()
{
super(Wizard.VIEW_ID);
}
/**
* Creates a new step with the specified title and summary. The title and summary are displayed
* in the wizard title block while this step is active.
*
* @param title
* the title of this step.
* @param summary
* a brief summary of this step or some usage guidelines.
*/
public WizardStep(final IModel<String> title, final IModel<String> summary)
{
this(title, summary, null);
}
/**
* Creates a new step with the specified title and summary. The title and summary are displayed
* in the wizard title block while this step is active.
*
* @param title
* the title of this step.
* @param summary
* a brief summary of this step or some usage guidelines.
* @param model
* Any model which is to be used for this step
*/
public WizardStep(final IModel<String> title, final IModel<String> summary,
final IModel<?> model)
{
super(Wizard.VIEW_ID, model);
this.title = wrap(title);
this.summary = wrap(summary);
}
/**
* Creates a new step with the specified title and summary. The title and summary are displayed
* in the wizard title block while this step is active.
*
* @param title
* the title of this step.
* @param summary
* a brief summary of this step or some usage guidelines.
*/
public WizardStep(final String title, final String summary)
{
this(title, summary, null);
}
/**
* Creates a new step with the specified title and summary. The title and summary are displayed
* in the wizard title block while this step is active.
*
* @param title
* the title of this step.
* @param summary
* a brief summary of this step or some usage guidelines.
* @param model
* Any model which is to be used for this step
*/
public WizardStep(final String title, final String summary, final IModel<?> model)
{
this(new Model<String>(title), new Model<>(summary), model);
}
/**
* Adds a form validator.
*
* @param validator
*/
public final void add(final IFormValidator validator)
{
formValidatorWrapper.add(validator);
}
/**
* @see org.apache.wicket.extensions.wizard.IWizardStep#applyState()
*/
@Override
public void applyState()
{
}
/**
* @see org.apache.wicket.extensions.wizard.IWizardStep#getHeader(java.lang.String,
* org.apache.wicket.Component, org.apache.wicket.extensions.wizard.IWizard)
*/
@Override
public Component getHeader(final String id, final Component parent, final IWizard wizard)
{
return new Header(id, wizard);
}
/**
* Gets the summary of this step. This will be displayed in the title of the wizard while this
* step is active. The summary is typically an overview of the step or some usage guidelines for
* the user.
*
* @return the summary of this step.
*/
public String getSummary()
{
return (summary != null) ? summary.getObject() : null;
}
/**
* Gets the title of this step.
*
* @return the title of this step.
*/
public String getTitle()
{
return (title != null) ? title.getObject() : null;
}
/**
* @see org.apache.wicket.extensions.wizard.IWizardStep#getView(java.lang.String,
* org.apache.wicket.Component, org.apache.wicket.extensions.wizard.IWizard)
*/
@Override
public Component getView(final String id, final Component parent, final IWizard wizard)
{
return this;
}
/**
* Called to initialize the step. When this method is called depends on the kind of wizard model
* that is used.
*
* The {@link WizardModel static wizard model} knows all the steps upfront and initializes themm
* when starting up. This method will be called when the wizard is {@link #init(IWizardModel)
* initializing}.
*
* The {@link DynamicWizardModel dynamic wizard model} initializes steps every time they are
* encountered.
*
* This method sets the wizard model and then calls template method
* {@link #onInit(IWizardModel)}
*
* @param wizardModel
* the model to which the step belongs.
*/
@Override
public final void init(final IWizardModel wizardModel)
{
this.wizardModel = wizardModel;
onInit(wizardModel);
}
/**
* Checks if this step is compete. This method should return true if the wizard can proceed to
* the next step. This property is bound and changes can be made at anytime by calling
* {@link #setComplete(boolean)} .
*
* @return <tt>true</tt> if the wizard can proceed from this step, <tt>false</tt> otherwise.
* @see #setComplete
*/
@Override
public boolean isComplete()
{
return complete;
}
/**
* Marks this step as compete. The wizard will not be able to proceed from this step until this
* property is configured to <tt>true</tt>.
*
* @param complete
* <tt>true</tt> to allow the wizard to proceed, <tt>false</tt> otherwise.
* @see #isComplete
*/
public void setComplete(final boolean complete)
{
this.complete = complete;
}
/**
* Sets summary.
*
* @param summary
* summary
*/
public void setSummaryModel(final IModel<String> summary)
{
this.summary = wrap(summary);
}
/**
* Sets title.
*
* @param title
* title
*/
public void setTitleModel(final IModel<String> title)
{
this.title = wrap(title);
}
/**
* @see org.apache.wicket.Component#detachModel()
*/
@Override
protected void detachModel()
{
super.detachModel();
if (title != null)
{
title.detach();
}
if (summary != null)
{
summary.detach();
}
}
@Override
protected void onInitialize()
{
super.onInitialize();
Form<?> form = findParent(Form.class);
form.add(formValidatorWrapper);
}
/**
* Template method that is called when the step is being initialized.
*
* @param wizardModel
* @see #init(IWizardModel)
*/
protected void onInit(final IWizardModel wizardModel)
{
}
/**
* @return wizard model
*/
public IWizardModel getWizardModel()
{
return wizardModel;
}
}