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.ArrayList;
018: import java.util.HashMap;
019: import java.util.List;
020: import java.util.Map;
021: import java.util.Random;
022: import org.araneaframework.example.main.TemplateBaseWidget;
023: import org.araneaframework.example.main.business.util.DataDTO;
024: import org.araneaframework.uilib.form.BeanFormWidget;
025: import org.araneaframework.uilib.form.control.CheckboxControl;
026: import org.araneaframework.uilib.form.formlist.FormRow;
027: import org.araneaframework.uilib.form.formlist.adapter.ValidOnlyIndividualFormRowHandler;
028: import org.araneaframework.uilib.list.EditableBeanListWidget;
029: import org.araneaframework.uilib.list.dataprovider.MemoryBasedListDataProvider;
030:
031: /**
032: * This is an example of component with a single list.
033: */
034: public class DemoCheckboxList extends TemplateBaseWidget {
035: private static final long serialVersionUID = 1L;
036: private EditableBeanListWidget checkList;
037: private Map data = new HashMap();
038:
039: {
040: Random rnd = new Random();
041:
042: for (int i = 0; i < 100; i += 3) {
043: //Just making the initial data
044: //In reality it should have been read from the database
045: data.put(new Long(i + 1), new DataDTO(new Long(i + 1),
046: Boolean.TRUE, new Long(rnd.nextLong() % 100),
047: "Strange"));
048: data.put(new Long(i + 2), new DataDTO(new Long(i + 2),
049: Boolean.FALSE, new Long(rnd.nextLong() % 100),
050: "Peculiar"));
051: data.put(new Long(i + 3), new DataDTO(new Long(i + 3),
052: Boolean.TRUE, new Long(rnd.nextLong() % 100),
053: "Queer"));
054: }
055: }
056:
057: public void init() throws Exception {
058: setViewSelector("demo/demoCheckboxList");
059:
060: checkList = new EditableBeanListWidget(
061: new DemoCheckboxListRowHandler(), DataDTO.class);
062: checkList.setDataProvider(new DemoCheckboxListDataProvider());
063: addWidget("checkList", checkList);
064:
065: checkList.addField("booleanField", "#Boolean");
066: checkList.addField("stringField", "#String");
067: checkList.addField("longField", "#Long");
068:
069: checkList.setInitialOrder("longField", false);
070: }
071:
072: public void handleEventSave(String parameter) throws Exception {
073: checkList.getFormList().saveCurrentRows();
074: }
075:
076: public void handleEventReturn(String parameter) throws Exception {
077: getFlowCtx().cancel();
078: }
079:
080: public class DemoCheckboxListRowHandler 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) {
089: DataDTO rowData = (DataDTO) data.get(editableRow.getKey());
090: rowData.setBooleanField((Boolean) editableRow.getForm()
091: .getValueByFullName("booleanField"));
092: }
093:
094: public void initFormRow(FormRow editableRow, Object row)
095: throws Exception {
096: ((BeanFormWidget) editableRow.getForm()).addBeanElement(
097: "booleanField", "#Boolean field",
098: new CheckboxControl(), true);
099: ((BeanFormWidget) editableRow.getForm()).readFromBean(row);
100: }
101: }
102:
103: public class DemoCheckboxListDataProvider extends
104: MemoryBasedListDataProvider {
105: private static final long serialVersionUID = 1L;
106:
107: public DemoCheckboxListDataProvider() {
108: super (DataDTO.class);
109: }
110:
111: public List loadData() throws Exception {
112: return new ArrayList(data.values());
113: }
114: }
115: }
|