001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.wicket.examples.wizard;
018:
019: import java.util.Arrays;
020: import java.util.List;
021:
022: import org.apache.wicket.extensions.wizard.StaticContentStep;
023: import org.apache.wicket.extensions.wizard.Wizard;
024: import org.apache.wicket.extensions.wizard.WizardModel;
025: import org.apache.wicket.extensions.wizard.WizardStep;
026: import org.apache.wicket.extensions.wizard.WizardModel.ICondition;
027: import org.apache.wicket.markup.html.form.CheckBox;
028: import org.apache.wicket.markup.html.form.Form;
029: import org.apache.wicket.markup.html.form.FormComponent;
030: import org.apache.wicket.markup.html.form.ListMultipleChoice;
031: import org.apache.wicket.markup.html.form.RequiredTextField;
032: import org.apache.wicket.markup.html.form.TextField;
033: import org.apache.wicket.markup.html.form.validation.AbstractFormValidator;
034: import org.apache.wicket.model.CompoundPropertyModel;
035: import org.apache.wicket.model.IModel;
036: import org.apache.wicket.model.Model;
037: import org.apache.wicket.model.ResourceModel;
038: import org.apache.wicket.model.StringResourceModel;
039: import org.apache.wicket.validation.IValidationError;
040: import org.apache.wicket.validation.ValidationError;
041: import org.apache.wicket.validation.validator.EmailAddressValidator;
042:
043: /**
044: * This wizard shows some basic form use. It uses custom panels for the form
045: * elements, and a single domain object ({@link User}) as it's subject. Also,
046: * the user roles step}is an optional step, that will only be executed when
047: * assignRoles is true (and that value is edited in the user details step).
048: *
049: * @author Eelco Hillenius
050: */
051: public class NewUserWizard extends Wizard {
052: /**
053: * The confirmation step.
054: */
055: private final class ConfirmationStep extends StaticContentStep {
056: /**
057: * Construct.
058: */
059: public ConfirmationStep() {
060: super (true);
061: IModel userModel = new Model(user);
062: setTitleModel(new ResourceModel("confirmation.title"));
063: setSummaryModel(new StringResourceModel(
064: "confirmation.summary", this , userModel));
065: setContentModel(new StringResourceModel(
066: "confirmation.content", this , userModel));
067: }
068: }
069:
070: /**
071: * The user details step.
072: */
073: private final class UserDetailsStep extends WizardStep {
074: /**
075: * Construct.
076: */
077: public UserDetailsStep() {
078: setTitleModel(new ResourceModel("confirmation.title"));
079: setSummaryModel(new StringResourceModel(
080: "userdetails.summary", this , new Model(user)));
081: add(new RequiredTextField("user.firstName"));
082: add(new RequiredTextField("user.lastName"));
083: add(new TextField("user.department"));
084: add(new CheckBox("assignRoles"));
085: }
086: }
087:
088: /**
089: * The user name step.
090: */
091: private final class UserNameStep extends WizardStep {
092: /**
093: * Construct.
094: */
095: public UserNameStep() {
096: super (new ResourceModel("username.title"),
097: new ResourceModel("username.summary"));
098: add(new RequiredTextField("user.userName"));
099: add(new RequiredTextField("user.email")
100: .add(EmailAddressValidator.getInstance()));
101: }
102: }
103:
104: /**
105: * The user details step.
106: */
107: private final class UserRolesStep extends WizardStep implements
108: ICondition {
109: /**
110: * Construct.
111: */
112: public UserRolesStep() {
113: super (new ResourceModel("userroles.title"), null);
114: setSummaryModel(new StringResourceModel(
115: "userroles.summary", this , new Model(user)));
116: final ListMultipleChoice rolesChoiceField = new ListMultipleChoice(
117: "user.roles", allRoles);
118: add(rolesChoiceField);
119: final TextField rolesSetNameField = new TextField(
120: "user.rolesSetName");
121: add(rolesSetNameField);
122: add(new AbstractFormValidator() {
123: public FormComponent[] getDependentFormComponents() {
124: // name and roles don't have anything to validate,
125: // so might as well just skip them here
126: return null;
127: }
128:
129: public void validate(Form form) {
130: String rolesInput = rolesChoiceField.getInput();
131: if (rolesInput != null && (!"".equals(rolesInput))) {
132: if ("".equals(rolesSetNameField.getInput())) {
133: rolesSetNameField
134: .error((IValidationError) new ValidationError()
135: .addMessageKey("error.noSetNameForRoles"));
136: }
137: }
138: }
139: });
140: }
141:
142: /**
143: * @see org.apache.wicket.extensions.wizard.WizardModel.ICondition#evaluate()
144: */
145: public boolean evaluate() {
146: return assignRoles;
147: }
148: }
149:
150: /** cheap ass roles database. */
151: private static final List<String> allRoles = Arrays
152: .asList(new String[] { "admin", "user", "moderator",
153: "joker", "slacker" });
154:
155: /** Whether the assign roles step should be executed. */
156: private boolean assignRoles = false;
157:
158: /** The user we are editing. */
159: private User user;
160:
161: /**
162: * Construct.
163: *
164: * @param id
165: * The component id
166: */
167: public NewUserWizard(String id) {
168: super (id);
169:
170: // create a blank user
171: user = new User();
172:
173: setModel(new CompoundPropertyModel(this ));
174: WizardModel model = new WizardModel();
175: model.add(new UserNameStep());
176: model.add(new UserDetailsStep());
177: model.add(new UserRolesStep());
178: model.add(new ConfirmationStep());
179:
180: // initialize the wizard with the wizard model we just built
181: init(model);
182: }
183:
184: /**
185: * Gets user.
186: *
187: * @return user
188: */
189: public User getUser() {
190: return user;
191: }
192:
193: /**
194: * Gets assignRoles.
195: *
196: * @return assignRoles
197: */
198: public boolean isAssignRoles() {
199: return assignRoles;
200: }
201:
202: /**
203: * @see org.apache.wicket.extensions.wizard.Wizard#onCancel()
204: */
205: public void onCancel() {
206: setResponsePage(Index.class);
207: }
208:
209: /**
210: * @see org.apache.wicket.extensions.wizard.Wizard#onFinish()
211: */
212: public void onFinish() {
213: setResponsePage(Index.class);
214: }
215:
216: /**
217: * Sets assignRoles.
218: *
219: * @param assignRoles
220: * assignRoles
221: */
222: public void setAssignRoles(boolean assignRoles) {
223: this .assignRoles = assignRoles;
224: }
225:
226: /**
227: * Sets user.
228: *
229: * @param user
230: * user
231: */
232: public void setUser(User user) {
233: this.user = user;
234: }
235: }
|