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.Field;
020: import info.jtrac.domain.Space;
021: import java.io.Serializable;
022: import org.apache.wicket.markup.html.WebPage;
023: import org.apache.wicket.markup.html.basic.Label;
024: import org.apache.wicket.markup.html.form.Button;
025: import org.apache.wicket.markup.html.form.Form;
026: import org.apache.wicket.markup.html.form.TextField;
027: import org.apache.wicket.markup.html.link.Link;
028: import org.apache.wicket.markup.html.panel.FeedbackPanel;
029: import org.apache.wicket.model.BoundCompoundPropertyModel;
030: import org.apache.wicket.model.PropertyModel;
031: import org.apache.wicket.validation.IValidatable;
032: import org.apache.wicket.validation.validator.AbstractValidator;
033:
034: /**
035: * space field option edit form
036: */
037: public class SpaceFieldOptionPage extends BasePage {
038:
039: private WebPage previous;
040: private Space space;
041:
042: private void addComponents(Field field, String key) {
043: add(new SpaceFieldOptionForm("form", field, key));
044: }
045:
046: public SpaceFieldOptionPage(Space space, Field field, String key,
047: WebPage previous) {
048: this .space = space;
049: this .previous = previous;
050: addComponents(field, key);
051: }
052:
053: /**
054: * wicket form
055: */
056: private class SpaceFieldOptionForm extends Form {
057:
058: private Field field;
059: private String key;
060:
061: public SpaceFieldOptionForm(String id, final Field field,
062: final String key) {
063:
064: super (id);
065: add(new FeedbackPanel("feedback"));
066:
067: this .field = field;
068: this .key = key;
069:
070: SpaceFieldOptionModel modelObject = new SpaceFieldOptionModel();
071: modelObject.setOption(field.getCustomValue(key));
072: final BoundCompoundPropertyModel model = new BoundCompoundPropertyModel(
073: modelObject);
074: setModel(model);
075:
076: // delete ==========================================================
077: Button delete = new Button("delete") {
078: @Override
079: public void onSubmit() {
080: int affectedCount = getJtrac()
081: .loadCountOfRecordsHavingFieldWithValue(
082: space, field, Integer.parseInt(key));
083: if (affectedCount > 0) {
084: String heading = localize("space_field_option_delete.confirm")
085: + " : "
086: + field.getCustomValue(key)
087: + " [" + field.getLabel() + "]";
088: String warning = localize("space_field_option_delete.line3");
089: String line1 = localize("space_field_option_delete.line1");
090: String line2 = localize(
091: "space_field_option_delete.line2",
092: affectedCount + "");
093: ConfirmPage confirm = new ConfirmPage(
094: SpaceFieldOptionPage.this , heading,
095: warning, new String[] { line1, line2 }) {
096: public void onConfirm() {
097: field.getOptions().remove(key);
098: getJtrac()
099: .bulkUpdateFieldToNullForValue(
100: space, field,
101: Integer.parseInt(key));
102: // database has been updated, if we don't do this
103: // user may leave without committing metadata change
104: getJtrac().storeSpace(space);
105: // synchronize metadata else when we save again we get Stale Object Exception
106: space.setMetadata(getJtrac()
107: .loadMetadata(
108: space.getMetadata()
109: .getId()));
110: setResponsePage(new SpaceFieldFormPage(
111: space, field, previous));
112: }
113: };
114: setResponsePage(confirm);
115: } else {
116: // this is an unsaved space / field or there are no impacted items
117: field.getOptions().remove(key);
118: setResponsePage(new SpaceFieldFormPage(space,
119: field, previous));
120: }
121: }
122: };
123: delete.setDefaultFormProcessing(false);
124: add(delete);
125: // heading label ===================================================
126: add(new Label("label", new PropertyModel(field, "label")));
127: // option ===========================================================
128: final TextField option = new TextField("option");
129: option.setRequired(true);
130: option.add(new ErrorHighlighter());
131: option.add(new AbstractValidator() {
132: protected void onValidate(IValidatable v) {
133: String s = (String) v.getValue();
134: if (field.hasOption(s)) {
135: error(v);
136: }
137: }
138:
139: @Override
140: protected String resourceKey() {
141: return "space_field_option_edit.error.exists";
142: }
143: });
144: add(option);
145: // cancel ==========================================================
146: add(new Link("cancel") {
147: public void onClick() {
148: setResponsePage(new SpaceFieldFormPage(space,
149: field, previous));
150: }
151: });
152: }
153:
154: @Override
155: protected void onSubmit() {
156: SpaceFieldOptionModel model = (SpaceFieldOptionModel) getModelObject();
157: field.addOption(key, model.getOption());
158: setResponsePage(new SpaceFieldFormPage(space, field,
159: previous));
160: }
161:
162: }
163:
164: /**
165: * custom form backing object that wraps Field
166: * required for the create / edit use case
167: */
168: private class SpaceFieldOptionModel implements Serializable {
169:
170: private String option;
171:
172: public String getOption() {
173: return option;
174: }
175:
176: public void setOption(String option) {
177: this.option = option;
178: }
179:
180: }
181:
182: }
|