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