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 DemoFormList extends TemplateBaseWidget {
044: private static final long serialVersionUID = 1L;
045: private BeanFormListWidget formList;
046: private Map data = new LinkedMap();
047:
048: //Plays the role of a sequence
049: private Long lastId;
050:
051: {
052: //Just making the initial data
053: //In reality it should have been read from the database
054: data.put(new Long(1), new DataDTO(new Long(1), Boolean.TRUE,
055: new Long(10), "12313"));
056: data.put(new Long(2), new DataDTO(new Long(2), Boolean.FALSE,
057: new Long(123), "werwer"));
058: data.put(new Long(3), new DataDTO(new Long(3), Boolean.TRUE,
059: new Long(10), "adfhadfh"));
060:
061: lastId = new Long(3);
062: }
063:
064: /**
065: * Builds the form with one checkbox, one textbox and a button.
066: */
067: public void init() throws Exception {
068: setViewSelector("demo/demoEditableList");
069:
070: formList = new BeanFormListWidget(new DemoFormRowHandler(),
071: new MapFormListModel(data), DataDTO.class);
072:
073: addWidget("editableList", formList);
074: }
075:
076: public void handleEventReturn(String param) throws Exception {
077: getFlowCtx().cancel();
078: }
079:
080: public class DemoFormRowHandler extends
081: ValidOnlyIndividualFormRowHandler {
082: private static final long serialVersionUID = 1L;
083:
084: public Object getRowKey(Object row) {
085: return ((DataDTO) row).getId();
086: }
087:
088: public void saveValidRow(FormRow editableRow) throws Exception {
089: //Reading data
090: DataDTO rowData = (DataDTO) ((BeanFormWidget) editableRow
091: .getForm()).writeToBean(new DataDTO());
092: rowData.setId((Long) editableRow.getKey());
093:
094: //Saving data
095: data.put(editableRow.getKey(), rowData);
096: }
097:
098: public void deleteRow(Object key) throws Exception {
099: //Deleting data
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: BeanFormWidget rowForm = (BeanFormWidget) editableRow
116: .getForm();
117:
118: addCommonFormFields(rowForm);
119:
120: FormListUtil.addSaveButtonToRowForm("#", formList, rowForm,
121: getRowKey(row));
122: FormListUtil.addDeleteButtonToRowForm("#", formList,
123: rowForm, getRowKey(row));
124:
125: rowForm.readFromBean(row);
126: }
127:
128: public void initAddForm(FormWidget addForm) throws Exception {
129: addCommonFormFields(addForm);
130:
131: FormListUtil.addAddButtonToAddForm("#", formList, addForm);
132: }
133:
134: private void addCommonFormFields(FormWidget form)
135: throws Exception {
136: form.addElement("stringField", "#String field",
137: new TextControl(), new StringData(), true);
138: form.addElement("longField", "#Long field",
139: new NumberControl(), new LongData(), true);
140: form.addElement("booleanField", "#Boolean field",
141: new CheckboxControl(), new BooleanData(), true);
142: }
143: }
144: }
|