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.Item;
021: import info.jtrac.domain.Space;
022: import info.jtrac.domain.User;
023: import info.jtrac.wicket.yui.YuiCalendar;
024: import java.util.ArrayList;
025: import java.util.List;
026: import java.util.Map;
027: import org.apache.wicket.markup.html.WebMarkupContainer;
028: import org.apache.wicket.markup.html.basic.Label;
029: import org.apache.wicket.markup.html.form.DropDownChoice;
030: import org.apache.wicket.markup.html.form.IChoiceRenderer;
031: import org.apache.wicket.markup.html.form.TextField;
032: import org.apache.wicket.markup.html.list.ListItem;
033: import org.apache.wicket.markup.html.list.ListView;
034: import org.apache.wicket.markup.html.panel.Fragment;
035: import org.apache.wicket.model.BoundCompoundPropertyModel;
036: import org.apache.wicket.model.Model;
037: import org.apache.wicket.model.PropertyModel;
038:
039: /**
040: * panel for custom fields that can be reused in the ite-create / item-view forms
041: */
042: public class CustomFieldsFormPanel extends BasePanel {
043:
044: public CustomFieldsFormPanel(String id,
045: BoundCompoundPropertyModel model, Space space) {
046: super (id);
047: List<Field> fields = space.getMetadata().getFieldList();
048: addComponents(model, fields);
049: }
050:
051: public CustomFieldsFormPanel(String id,
052: BoundCompoundPropertyModel model, Item item, User user) {
053: super (id);
054: List<Field> fields = item.getEditableFieldList(user);
055: addComponents(model, fields);
056: }
057:
058: private void addComponents(final BoundCompoundPropertyModel model,
059: List<Field> fields) {
060: ListView listView = new ListView("fields", fields) {
061: protected void populateItem(ListItem listItem) {
062: final Field field = (Field) listItem.getModelObject();
063: listItem.add(new Label("label", field.getLabel()));
064: listItem.add(new Label("star",
065: field.isOptional() ? " " : "*")
066: .setEscapeModelStrings(false));
067: if (field.getName().getType() < 4) { // drop down list
068: Fragment f = new Fragment("field", "dropDown");
069: final Map<String, String> options = field
070: .getOptions();
071: List<String> keys = new ArrayList(options.keySet()); // bound value
072: DropDownChoice choice = new DropDownChoice("field",
073: keys, new IChoiceRenderer() {
074: public Object getDisplayValue(Object o) {
075: return options.get(o);
076: };
077:
078: public String getIdValue(Object o, int i) {
079: return o.toString();
080: };
081: });
082: choice.setNullValid(true);
083: choice.setLabel(new Model(field.getLabel()));
084: if (!field.isOptional()) {
085: choice.setRequired(true);
086: }
087: WebMarkupContainer border = new WebMarkupContainer(
088: "border");
089: f.add(border);
090: border.add(new ErrorHighlighter(choice));
091: border.add(model.bind(choice, field.getName()
092: .getText()));
093: listItem.add(f);
094: } else if (field.getName().getType() == 6) { // date picker
095: YuiCalendar calendar = new YuiCalendar("field",
096: new PropertyModel(model, field.getName()
097: .getText()), !field.isOptional());
098: listItem.add(calendar);
099: calendar.setLabel(new Model(field.getLabel()));
100: } else {
101: Fragment f = new Fragment("field", "textField");
102: TextField textField = new TextField("field");
103: if (field.getName().getType() == 4) {
104: textField.setType(Double.class);
105: }
106: textField.add(new ErrorHighlighter());
107: if (!field.isOptional()) {
108: textField.setRequired(true);
109: }
110: textField.setLabel(new Model(field.getLabel()));
111: f.add(model.bind(textField, field.getName()
112: .getText()));
113: listItem.add(f);
114: }
115: }
116: };
117: listView.setReuseItems(true);
118: add(listView);
119: }
120:
121: }
|