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.util.ArrayList;
022: import java.util.Collections;
023: import java.util.List;
024: import java.util.Map;
025: import org.apache.wicket.behavior.SimpleAttributeModifier;
026: import org.apache.wicket.markup.html.WebPage;
027: import org.apache.wicket.markup.html.basic.Label;
028: import org.apache.wicket.markup.html.form.Button;
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.link.Link;
033: import org.apache.wicket.markup.html.list.ListItem;
034: import org.apache.wicket.markup.html.list.ListView;
035: import org.apache.wicket.model.BoundCompoundPropertyModel;
036: import org.apache.wicket.model.PropertyModel;
037:
038: /**
039: * space fields add / re-order page
040: */
041: public class SpaceFieldListPage extends BasePage {
042:
043: private Space space;
044: private WebPage previous;
045:
046: public SpaceFieldListPage(Space space, String selectedFieldName,
047: WebPage previous) {
048: this .previous = previous;
049: this .space = space;
050: add(new SpaceFieldsForm("form", space, selectedFieldName));
051: }
052:
053: /**
054: * wicket form
055: */
056: private class SpaceFieldsForm extends Form {
057:
058: private String type;
059:
060: public String getType() {
061: return type;
062: }
063:
064: public void setType(String type) {
065: this .type = type;
066: }
067:
068: public SpaceFieldsForm(String id, final Space space,
069: final String selectedFieldName) {
070: super (id);
071:
072: final BoundCompoundPropertyModel model = new BoundCompoundPropertyModel(
073: this );
074: setModel(model);
075:
076: add(new Label("name", new PropertyModel(space, "name")));
077: add(new Label("prefixCode", new PropertyModel(space,
078: "prefixCode")));
079:
080: final SimpleAttributeModifier sam = new SimpleAttributeModifier(
081: "class", "alt");
082:
083: ListView listView = new ListView("fields", space
084: .getMetadata().getFieldList()) {
085: protected void populateItem(ListItem listItem) {
086: final Field field = (Field) listItem
087: .getModelObject();
088:
089: if (field.getName().getText().equals(
090: selectedFieldName)) {
091: listItem.add(new SimpleAttributeModifier(
092: "class", "selected"));
093: } else if (listItem.getIndex() % 2 == 1) {
094: listItem.add(sam);
095: }
096:
097: listItem.add(new Button("up") {
098: @Override
099: public void onSubmit() {
100: List<Field.Name> fieldOrder = space
101: .getMetadata().getFieldOrder();
102: int index = fieldOrder.indexOf(field
103: .getName());
104: int swapIndex = index - 1;
105: if (swapIndex < 0) {
106: if (fieldOrder.size() > 1) {
107: swapIndex = fieldOrder.size() - 1;
108: } else {
109: swapIndex = 0;
110: }
111: }
112: if (index != swapIndex) {
113: Collections.swap(fieldOrder, index,
114: swapIndex);
115: setResponsePage(new SpaceFieldListPage(
116: space, field.getName()
117: .getText(), previous));
118: }
119: }
120: });
121:
122: listItem.add(new Button("down") {
123: @Override
124: public void onSubmit() {
125: List<Field.Name> fieldOrder = space
126: .getMetadata().getFieldOrder();
127: int index = fieldOrder.indexOf(field
128: .getName());
129: int swapIndex = index + 1;
130: if (swapIndex == fieldOrder.size()) {
131: swapIndex = 0;
132: }
133: if (index != swapIndex) {
134: Collections.swap(fieldOrder, index,
135: swapIndex);
136: setResponsePage(new SpaceFieldListPage(
137: space, field.getName()
138: .getText(), previous));
139: }
140: }
141: });
142:
143: listItem.add(new Label("name", new PropertyModel(
144: field, "name.text")));
145: listItem.add(new Label("type", new PropertyModel(
146: field, "name.description")));
147: listItem.add(new Label("label", new PropertyModel(
148: field, "label")));
149: List<String> optionsList;
150: if (field.getOptions() != null) {
151: optionsList = new ArrayList(field.getOptions()
152: .values());
153: } else {
154: optionsList = new ArrayList(0);
155: }
156: ListView options = new ListView("options",
157: optionsList) {
158: protected void populateItem(ListItem item) {
159: item.add(new Label("option", item
160: .getModelObject()
161: + ""));
162: }
163: };
164: listItem.add(options);
165: listItem.add(new Button("edit") {
166: @Override
167: public void onSubmit() {
168: Field f = field.getClone();
169: setResponsePage(new SpaceFieldFormPage(
170: space, f, previous));
171: }
172: });
173: }
174: };
175: add(listView);
176:
177: final Map<String, String> types = space.getMetadata()
178: .getAvailableFieldTypes();
179: List<String> typesList = new ArrayList(types.keySet());
180: // pre-select the drop down for convenience
181: if (typesList.size() > 0) {
182: type = typesList.get(0);
183: }
184:
185: DropDownChoice choice = new DropDownChoice("type",
186: typesList, new IChoiceRenderer() {
187: public Object getDisplayValue(Object o) {
188: return localize("space_fields.type_" + o)
189: + " - "
190: + localize(
191: "space_fields.typeRemaining",
192: types.get(o));
193: }
194:
195: public String getIdValue(Object o, int i) {
196: return o.toString();
197: }
198: });
199:
200: add(choice);
201:
202: add(new Button("add") {
203: @Override
204: public void onSubmit() {
205: if (type == null) {
206: return;
207: }
208: Field field = space.getMetadata()
209: .getNextAvailableField(
210: Integer.parseInt(type));
211: field.initOptions();
212: setResponsePage(new SpaceFieldFormPage(space,
213: field, previous));
214: }
215: });
216:
217: add(new Button("back") {
218: @Override
219: public void onSubmit() {
220: SpaceFormPage page = new SpaceFormPage(space);
221: page.setPrevious(previous);
222: setResponsePage(page);
223: }
224: });
225:
226: add(new Button("next") {
227: @Override
228: public void onSubmit() {
229: setResponsePage(new SpacePermissionsPage(space,
230: previous));
231: }
232: });
233:
234: add(new Link("cancel") {
235: public void onClick() {
236: if (previous == null) {
237: setResponsePage(new OptionsPage());
238: } else {
239: if (previous instanceof SpaceListPage) {
240: ((SpaceListPage) previous)
241: .setSelectedSpaceId(space.getId());
242: }
243: setResponsePage(previous);
244: }
245: }
246: });
247:
248: }
249:
250: }
251:
252: }
|