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.domain.State;
021: import info.jtrac.util.ValidationUtils;
022: import java.io.Serializable;
023: import org.apache.wicket.markup.html.WebPage;
024: import org.apache.wicket.markup.html.basic.Label;
025: import org.apache.wicket.markup.html.form.Button;
026: import org.apache.wicket.markup.html.form.Form;
027: import org.apache.wicket.markup.html.form.TextField;
028: import org.apache.wicket.markup.html.link.Link;
029: import org.apache.wicket.markup.html.panel.FeedbackPanel;
030: import org.apache.wicket.model.BoundCompoundPropertyModel;
031: import org.apache.wicket.validation.IValidatable;
032: import org.apache.wicket.validation.validator.AbstractValidator;
033:
034: /**
035: * space state add / edit form
036: */
037: public class SpaceStatePage extends BasePage {
038:
039: private WebPage previous;
040: private Space space;
041:
042: public SpaceStatePage(Space space, int stateKey, WebPage previous) {
043: this .space = space;
044: this .previous = previous;
045: add(new SpaceStateForm("form", stateKey));
046: }
047:
048: /**
049: * wicket form
050: */
051: private class SpaceStateForm extends Form {
052:
053: private int stateKey;
054:
055: public SpaceStateForm(String id, final int stateKey) {
056:
057: super (id);
058: add(new FeedbackPanel("feedback"));
059: this .stateKey = stateKey;
060:
061: SpaceStateModel modelObject = new SpaceStateModel();
062: // stateKey is -1 if add new state
063: final String stateName = space.getMetadata().getStates()
064: .get(stateKey);
065: modelObject.setStateName(stateName);
066: final BoundCompoundPropertyModel model = new BoundCompoundPropertyModel(
067: modelObject);
068: setModel(model);
069:
070: add(new Label("label", stateName));
071:
072: // delete ==========================================================
073: Button delete = new Button("delete") {
074: @Override
075: public void onSubmit() {
076: int affectedCount = getJtrac()
077: .loadCountOfRecordsHavingStatus(space,
078: stateKey);
079: if (affectedCount > 0) {
080: String heading = localize("space_state_delete.confirm")
081: + " : " + stateName;
082: String warning = localize("space_state_delete.line3");
083: String line1 = localize("space_state_delete.line1");
084: String line2 = localize(
085: "space_state_delete.line2",
086: affectedCount + "");
087: ConfirmPage confirm = new ConfirmPage(
088: SpaceStatePage.this , heading, warning,
089: new String[] { line1, line2 }) {
090: public void onConfirm() {
091: getJtrac().bulkUpdateStatusToOpen(
092: space, stateKey);
093: space.getMetadata().removeState(
094: stateKey);
095: getJtrac().storeSpace(space);
096: // synchronize metadata else when we save again we get Stale Object Exception
097: space.setMetadata(getJtrac()
098: .loadMetadata(
099: space.getMetadata()
100: .getId()));
101: setResponsePage(new SpacePermissionsPage(
102: space, previous));
103: }
104: };
105: setResponsePage(confirm);
106: } else {
107: // this is an unsaved space / field or there are no impacted items
108: space.getMetadata().removeState(stateKey);
109: setResponsePage(new SpacePermissionsPage(space,
110: previous));
111: }
112: }
113: };
114: delete.setDefaultFormProcessing(false);
115: if (stateKey == State.OPEN || stateKey == -1) {
116: delete.setEnabled(false);
117: }
118: add(delete);
119: // option ===========================================================
120: final TextField field = new TextField("stateName");
121: field.setRequired(true);
122: field.add(new ErrorHighlighter());
123: // validation: format ok?
124: field.add(new AbstractValidator() {
125: protected void onValidate(IValidatable v) {
126: String s = (String) v.getValue();
127: if (!ValidationUtils.isCamelDashCase(s)) {
128: error(v);
129: }
130: }
131:
132: @Override
133: protected String resourceKey() {
134: return "space_state_form.error.state.invalid";
135: }
136: });
137: // validation: already exists?
138: field.add(new AbstractValidator() {
139: protected void onValidate(IValidatable v) {
140: String s = (String) v.getValue();
141: if (space.getMetadata().getStates()
142: .containsValue(s)) {
143: error(v);
144: }
145: }
146:
147: @Override
148: protected String resourceKey() {
149: return "space_state_form.error.state.exists";
150: }
151: });
152: add(field);
153: // cancel ==========================================================
154: add(new Link("cancel") {
155: public void onClick() {
156: setResponsePage(new SpacePermissionsPage(space,
157: previous));
158: }
159: });
160: }
161:
162: @Override
163: protected void onSubmit() {
164: SpaceStateModel model = (SpaceStateModel) getModelObject();
165: if (stateKey == -1) {
166: space.getMetadata().addState(model.getStateName());
167: } else {
168: space.getMetadata().getStates().put(stateKey,
169: model.getStateName());
170: }
171: setResponsePage(new SpacePermissionsPage(space, previous));
172: }
173:
174: }
175:
176: /**
177: * custom form backing object that wraps state key
178: * required for the create / edit use case
179: */
180: private class SpaceStateModel implements Serializable {
181:
182: private String stateName;
183:
184: public String getStateName() {
185: return stateName;
186: }
187:
188: public void setStateName(String stateName) {
189: this.stateName = stateName;
190: }
191:
192: }
193:
194: }
|