001: /**
002: * Copyright 2006 Webmedia Group Ltd.
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: **/package org.araneaframework.example.main.web.demo;
016:
017: import java.util.Map;
018: import org.apache.commons.collections.map.LinkedMap;
019: import org.araneaframework.example.main.TemplateBaseWidget;
020: import org.araneaframework.example.main.business.util.DataDTO;
021: import org.araneaframework.uilib.form.BeanFormWidget;
022: import org.araneaframework.uilib.form.FormWidget;
023: import org.araneaframework.uilib.form.control.CheckboxControl;
024: import org.araneaframework.uilib.form.control.NumberControl;
025: import org.araneaframework.uilib.form.control.TextControl;
026: import org.araneaframework.uilib.form.data.BooleanData;
027: import org.araneaframework.uilib.form.data.LongData;
028: import org.araneaframework.uilib.form.data.StringData;
029: import org.araneaframework.uilib.form.formlist.BeanFormListWidget;
030: import org.araneaframework.uilib.form.formlist.FormListUtil;
031: import org.araneaframework.uilib.form.formlist.FormRow;
032: import org.araneaframework.uilib.form.formlist.adapter.ValidOnlyIndividualFormRowHandler;
033: import org.araneaframework.uilib.form.formlist.model.MapFormListModel;
034:
035: /**
036: * Editable list component.
037: *
038: * Seperate forms are used for individual rows, so that client-side validation
039: * would work on the same separate rows.
040: *
041: * @author Jevgeni Kabanov (ekabanov <i>at</i> araneaframework <i>dot</i> org)
042: */
043: public class DemoEmbeddedDisplayableEditableList extends
044: TemplateBaseWidget {
045: private static final long serialVersionUID = 1L;
046:
047: public DemoEmbeddedDisplayableEditableList() {
048: }
049:
050: private BeanFormListWidget editableRows;
051: private Map data = new LinkedMap();
052:
053: //Plays the role of a sequence
054: private Long lastId;
055:
056: {
057: //Just making the initial data
058: //In reality it should have been read from the database
059: data.put(new Long(1), new DataDTO(new Long(1), Boolean.TRUE,
060: new Long(10), "12313"));
061: data.put(new Long(2), new DataDTO(new Long(2), Boolean.FALSE,
062: new Long(123), "werwer"));
063: data.put(new Long(3), new DataDTO(new Long(3), Boolean.TRUE,
064: new Long(10), "adfhadfh"));
065:
066: lastId = new Long(3);
067: }
068:
069: /**
070: * Builds the form with one checkbox, one textbox and a button.
071: */
072: public void init() throws Exception {
073: setViewSelector("demo/demoEmbeddedDisplayableEditableList");
074:
075: editableRows = new BeanFormListWidget(
076: new DemoEditableRowHandler(),
077: new MapFormListModel(data), DataDTO.class);
078:
079: addWidget("editableList", editableRows);
080: }
081:
082: public class DemoEditableRowHandler extends
083: ValidOnlyIndividualFormRowHandler {
084: private static final long serialVersionUID = 1L;
085:
086: public Object getRowKey(Object row) {
087: return ((DataDTO) row).getId();
088: }
089:
090: public void saveValidRow(FormRow editableRow) throws Exception {
091: DataDTO rowData = (DataDTO) ((BeanFormWidget) editableRow
092: .getForm()).writeToBean(new DataDTO());
093: rowData.setId((Long) editableRow.getKey());
094: data.put(editableRow.getKey(), rowData);
095:
096: editableRow.close();
097: }
098:
099: public void deleteRow(Object key) throws Exception {
100: data.remove(key);
101: }
102:
103: public void addValidRow(FormWidget addForm) throws Exception {
104: lastId = new Long(lastId.longValue() + 1);
105:
106: DataDTO rowData = (DataDTO) ((BeanFormWidget) addForm)
107: .writeToBean(new DataDTO());
108: rowData.setId(lastId);
109:
110: data.put(lastId, rowData);
111: }
112:
113: public void initFormRow(FormRow editableRow, Object row)
114: throws Exception {
115: editableRow.close();
116:
117: BeanFormWidget rowForm = (BeanFormWidget) editableRow
118: .getForm();
119:
120: addCommonFormFields(rowForm);
121:
122: FormListUtil.addEditSaveButtonToRowForm("#", editableRows,
123: rowForm, getRowKey(row));
124: FormListUtil.addDeleteButtonToRowForm("#", editableRows,
125: rowForm, getRowKey(row));
126:
127: rowForm.readFromBean(row);
128:
129: rowForm.addWidget("embeddedFormList",
130: new DemoDisplayableEditableList());
131: }
132:
133: public void initAddForm(FormWidget addForm) throws Exception {
134: addCommonFormFields(addForm);
135: FormListUtil.addAddButtonToAddForm("#", editableRows,
136: addForm);
137: }
138:
139: private void addCommonFormFields(FormWidget form)
140: throws Exception {
141: form.addElement("stringField", "#String field",
142: new TextControl(), new StringData(), true);
143: form.addElement("longField", "#Long field",
144: new NumberControl(), new LongData(), true);
145: form.addElement("booleanField", "#Boolean field",
146: new CheckboxControl(), new BooleanData(), true);
147: }
148: }
149: }
|