001: /*
002: * Copyright 2002-2005 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package info.jtrac.wicket;
018:
019: import info.jtrac.domain.Space;
020: import info.jtrac.util.ValidationUtils;
021: import java.util.List;
022: import org.apache.wicket.behavior.HeaderContributor;
023: import org.apache.wicket.markup.html.IHeaderContributor;
024: import org.apache.wicket.markup.html.IHeaderResponse;
025: import org.apache.wicket.markup.html.WebMarkupContainer;
026: import org.apache.wicket.markup.html.WebPage;
027: import org.apache.wicket.markup.html.form.Button;
028: import org.apache.wicket.markup.html.form.CheckBox;
029: import org.apache.wicket.markup.html.form.DropDownChoice;
030: import org.apache.wicket.markup.html.form.Form;
031: import org.apache.wicket.markup.html.form.IChoiceRenderer;
032: import org.apache.wicket.markup.html.form.TextArea;
033: import org.apache.wicket.markup.html.form.TextField;
034: import org.apache.wicket.markup.html.link.Link;
035: import org.apache.wicket.markup.html.panel.FeedbackPanel;
036: import org.apache.wicket.model.BoundCompoundPropertyModel;
037: import org.apache.wicket.validation.IValidatable;
038: import org.apache.wicket.validation.validator.AbstractValidator;
039:
040: /**
041: * space edit form
042: */
043: public class SpaceFormPage extends BasePage {
044:
045: private WebPage previous;
046:
047: public void setPrevious(WebPage previous) {
048: this .previous = previous;
049: }
050:
051: public SpaceFormPage() {
052: Space space = new Space();
053: space.getMetadata().initRoles();
054: add(new SpaceForm("form", space));
055: }
056:
057: public SpaceFormPage(Space space) {
058: add(new SpaceForm("form", space));
059: }
060:
061: /**
062: * wicket form
063: */
064: private class SpaceForm extends Form {
065:
066: private Space space;
067: private Space copyFrom;
068:
069: private JtracFeedbackMessageFilter filter;
070:
071: public Space getSpace() {
072: return space;
073: }
074:
075: public void setSpace(Space space) {
076: this .space = space;
077: }
078:
079: public Space getCopyFrom() {
080: return copyFrom;
081: }
082:
083: public void setCopyFrom(Space copyFrom) {
084: this .copyFrom = copyFrom;
085: }
086:
087: public SpaceForm(String id, final Space space) {
088:
089: super (id);
090:
091: FeedbackPanel feedback = new FeedbackPanel("feedback");
092: filter = new JtracFeedbackMessageFilter();
093: feedback.setFilter(filter);
094: add(feedback);
095:
096: this .space = space;
097: final BoundCompoundPropertyModel model = new BoundCompoundPropertyModel(
098: this );
099: setModel(model);
100:
101: // delete button only if edit ======================================
102: Button delete = new Button("delete") {
103: @Override
104: public void onSubmit() {
105: String heading = localize("space_delete.confirm");
106: String warning = localize("space_delete.line3");
107: String line1 = localize("space_delete.line1");
108: String line2 = localize("space_delete.line2");
109: ConfirmPage confirm = new ConfirmPage(
110: SpaceFormPage.this , heading, warning,
111: new String[] { line1, line2 }) {
112: public void onConfirm() {
113: getJtrac().removeSpace(space);
114: // logged in user may have been allocated to this space
115: SpaceFormPage.this .refreshPrincipal();
116: setResponsePage(new SpaceListPage());
117: }
118: };
119: setResponsePage(confirm);
120: }
121: };
122: delete.setDefaultFormProcessing(false);
123: if (space.getId() <= 0) {
124: delete.setVisible(false);
125: }
126: add(delete);
127: // display name ====================================================
128: final TextField name = new TextField("space.name");
129: name.setRequired(true);
130: name.add(new ErrorHighlighter());
131: name.setOutputMarkupId(true);
132: add(name);
133: add(new HeaderContributor(new IHeaderContributor() {
134: public void renderHead(IHeaderResponse response) {
135: response
136: .renderOnLoadJavascript("document.getElementById('"
137: + name.getMarkupId() + "').focus()");
138: }
139: }));
140: // prefix Code =====================================================
141: TextField prefixCode = new TextField("space.prefixCode");
142: prefixCode.setRequired(true);
143: prefixCode.add(new ErrorHighlighter());
144: // validation: greater than 3 chars?
145: prefixCode.add(new AbstractValidator() {
146: protected void onValidate(IValidatable v) {
147: String s = (String) v.getValue();
148: if (s.length() < 3) {
149: error(v);
150: }
151: }
152:
153: @Override
154: protected String resourceKey() {
155: return "space_form.error.prefixCode.tooShort";
156: }
157: });
158: prefixCode.add(new AbstractValidator() {
159: protected void onValidate(IValidatable v) {
160: String s = (String) v.getValue();
161: if (s.length() > 10) {
162: error(v);
163: }
164: }
165:
166: @Override
167: protected String resourceKey() {
168: return "space_form.error.prefixCode.tooLong";
169: }
170: });
171: // validation: format ok?
172: prefixCode.add(new AbstractValidator() {
173: protected void onValidate(IValidatable v) {
174: String s = (String) v.getValue();
175: if (!ValidationUtils.isAllUpperCase(s)) {
176: error(v);
177: }
178: }
179:
180: @Override
181: protected String resourceKey() {
182: return "space_form.error.prefixCode.invalid";
183: }
184: });
185: // validation: does space already exist with same prefixCode ?
186: prefixCode.add(new AbstractValidator() {
187: protected void onValidate(IValidatable v) {
188: String s = (String) v.getValue();
189: Space temp = getJtrac().loadSpace(s);
190: if (temp != null && temp.getId() != space.getId()) {
191: error(v);
192: }
193: }
194:
195: @Override
196: protected String resourceKey() {
197: return "space_form.error.prefixCode.exists";
198: }
199: });
200: add(prefixCode);
201: // description =====================================================
202: add(new TextArea("space.description"));
203: // guest allowed ===================================================
204: add(new CheckBox("space.guestAllowed"));
205: // hide copy from option if edit ===================================
206: WebMarkupContainer hide = new WebMarkupContainer("hide");
207: if (space.getId() > 0) {
208: hide.setVisible(false);
209: } else {
210: List<Space> spaces = getJtrac().findAllSpaces();
211: DropDownChoice choice = new DropDownChoice("copyFrom",
212: spaces, new IChoiceRenderer() {
213: public Object getDisplayValue(Object o) {
214: return ((Space) o).getName();
215: }
216:
217: public String getIdValue(Object o, int i) {
218: return ((Space) o).getId() + "";
219: }
220: });
221: choice.setNullValid(true);
222: hide.add(choice);
223: }
224: add(hide);
225: // cancel link =====================================================
226: add(new Link("cancel") {
227: public void onClick() {
228: if (previous == null) {
229: setResponsePage(new OptionsPage());
230: } else {
231: if (previous instanceof SpaceListPage) {
232: ((SpaceListPage) previous)
233: .setSelectedSpaceId(space.getId());
234: }
235: setResponsePage(previous);
236: }
237: }
238: });
239: }
240:
241: @Override
242: protected void validate() {
243: filter.reset();
244: super .validate();
245: }
246:
247: @Override
248: protected void onSubmit() {
249: if (copyFrom != null) {
250: Space temp = getJtrac().loadSpace(copyFrom.getId());
251: space.getMetadata().setXmlString(
252: temp.getMetadata().getXmlString());
253: }
254: setResponsePage(new SpaceFieldListPage(space, null,
255: previous));
256: }
257: }
258:
259: }
|