001: package org.araneaframework.example.main.release.features;
002:
003: import java.util.ArrayList;
004: import java.util.Arrays;
005: import java.util.Iterator;
006: import java.util.List;
007: import java.util.Locale;
008: import java.util.Random;
009: import org.apache.commons.beanutils.BeanPropertyValueEqualsPredicate;
010: import org.apache.commons.collections.CollectionUtils;
011: import org.araneaframework.example.main.TemplateBaseWidget;
012: import org.araneaframework.uilib.form.BeanFormWidget;
013: import org.araneaframework.uilib.form.FormElement;
014: import org.araneaframework.uilib.form.FormWidget;
015: import org.araneaframework.uilib.form.constraint.BaseFieldConstraint;
016: import org.araneaframework.uilib.form.control.TextControl;
017: import org.araneaframework.uilib.form.data.StringData;
018: import org.araneaframework.uilib.form.formlist.BeanFormListWidget;
019: import org.araneaframework.uilib.form.formlist.FormListUtil;
020: import org.araneaframework.uilib.form.formlist.FormRow;
021: import org.araneaframework.uilib.form.formlist.adapter.ValidOnlyIndividualFormRowHandler;
022: import org.araneaframework.uilib.list.EditableBeanListWidget;
023: import org.araneaframework.uilib.list.dataprovider.MemoryBasedListDataProvider;
024: import org.araneaframework.uilib.list.util.FormUtil;
025: import org.araneaframework.uilib.util.MessageUtil;
026:
027: public class SimpleInMemoryEditableList extends TemplateBaseWidget {
028: private static final long serialVersionUID = 1L;
029: protected List friends = new ArrayList();
030:
031: private MemoryBasedListDataProvider dataProvider = new DataProvider();
032:
033: private BeanFormListWidget editableRows;
034:
035: //Plays the role of a sequence
036: private Long lastId = new Long(0);
037:
038: {
039: Random rn = new Random();
040: List allSuggestions = new ArrayList();
041:
042: for (Iterator i = Arrays.asList(Locale.getISOCountries())
043: .iterator(); i.hasNext();) {
044: allSuggestions.add(new Locale("en", (String) i.next())
045: .getDisplayCountry(Locale.ENGLISH));
046: }
047:
048: for (int i = 0; i < ExampleData.males.length; i++) {
049: ExampleData.Client friend = new ExampleData.Client();
050: friend.setForename(ExampleData.males[i]);
051: friend.setId(lastId);
052: friend.setSex("M");
053: friend.setSurname(ExampleData.fungi[rn
054: .nextInt(ExampleData.fungi.length)]);
055: friend.setCountry((String) allSuggestions.get(rn
056: .nextInt(allSuggestions.size())));
057: friends.add(friend);
058: lastId = new Long(lastId.longValue() + 1);
059: }
060:
061: for (int i = 0; i < ExampleData.females.length; i++) {
062: ExampleData.Client friend = new ExampleData.Client();
063: friend.setForename(ExampleData.females[i]);
064: friend.setSex("F");
065: friend.setSurname(ExampleData.fungi[rn
066: .nextInt(ExampleData.fungi.length)]);
067: friend.setCountry((String) allSuggestions.get(rn
068: .nextInt(allSuggestions.size())));
069: friends.add(friend);
070: lastId = new Long(lastId.longValue() + 1);
071: }
072: }
073:
074: /* Editable list. */
075: private EditableBeanListWidget list;
076: /* Actual holder of editable list rows (resides inside EditableBeanListWidget).
077: Look inside init() method to see where it comes from. */
078: private BeanFormListWidget formList;
079:
080: protected void init() throws Exception {
081: setViewSelector("release/features/simpleEditableList/simpleInmemoryEditableList");
082:
083: /* PersonMO class is already familiar from form examples.
084: FormRowHandler class that will handle the different row operations. */
085: list = new EditableBeanListWidget(
086: new FriendEditableRowHandler(),
087: ExampleData.Client.class);
088: this .formList = list.getFormList();
089: addWidget("list", list);
090: list.setOrderableByDefault(true);
091: //list.addField("id", "#Id", false);
092: /* Filtering by fields other than ID is enabled. */
093: list.addField("sex", "sed.Sex").like();
094: list.addField("forename", "sed.Forename").like();
095: list.addField("surname", "sed.Surname").like();
096: list.addField("country", "common.Country").like();
097: list.addField("dummy", null, false);
098:
099: list.setDataProvider(dataProvider);
100: }
101:
102: private class DataProvider extends MemoryBasedListDataProvider {
103: private static final long serialVersionUID = 1L;
104:
105: protected DataProvider() {
106: super (ExampleData.Client.class);
107: }
108:
109: public List loadData() throws Exception {
110: return friends;
111: }
112: }
113:
114: private class FriendEditableRowHandler extends
115: ValidOnlyIndividualFormRowHandler {
116: private static final long serialVersionUID = 1L;
117:
118: public FriendEditableRowHandler() {
119: }
120:
121: public Object getRowKey(Object rowData) {
122: return ((ExampleData.Client) rowData).getId();
123: }
124:
125: public void saveValidRow(FormRow editableRow) throws Exception {
126: ExampleData.Client rowData = (ExampleData.Client) ((BeanFormWidget) editableRow
127: .getForm()).writeToBean(new ExampleData.Client());
128: rowData.setId((Long) editableRow.getKey());
129:
130: ExampleData.Client toModify = (ExampleData.Client) CollectionUtils
131: .find(friends,
132: new BeanPropertyValueEqualsPredicate("id",
133: editableRow.getKey()));
134: toModify.edit(rowData);
135:
136: editableRow.close();
137: }
138:
139: public void deleteRow(Object key) throws Exception {
140: Long id = (Long) key;
141: ExampleData.Client toRemove = (ExampleData.Client) CollectionUtils
142: .find(friends,
143: new BeanPropertyValueEqualsPredicate("id",
144: id));
145: friends.remove(toRemove);
146:
147: list.getDataProvider().refreshData();
148: }
149:
150: public void addValidRow(FormWidget addForm) throws Exception {
151: ExampleData.Client newFriend = (ExampleData.Client) (((BeanFormWidget) addForm)
152: .writeToBean(new ExampleData.Client()));
153: friends.add(newFriend);
154: list.getDataProvider().refreshData();
155: formList.resetAddForm();
156: }
157:
158: // Called to initialize each row in editable list.
159: public void initFormRow(FormRow editableRow, Object rowData)
160: throws Exception {
161: editableRow.close();
162:
163: BeanFormWidget rowForm = (BeanFormWidget) editableRow
164: .getForm();
165: addCommonFormFields(rowForm);
166: FormListUtil.addEditSaveButtonToRowForm("#", formList,
167: rowForm, getRowKey(rowData));
168: FormListUtil.addDeleteButtonToRowForm("#", formList,
169: rowForm, getRowKey(rowData));
170:
171: rowForm.readFromBean(rowData);
172: }
173:
174: public void initAddForm(FormWidget addForm) throws Exception {
175: addCommonFormFields((BeanFormWidget) addForm);
176: FormListUtil.addAddButtonToAddForm("#", formList, addForm);
177: }
178:
179: private void addCommonFormFields(BeanFormWidget form)
180: throws Exception {
181: form.addElement("sex", buildSexElement());
182: form.addBeanElement("forename", "sed.Forename",
183: new TextControl(), true);
184: form.addBeanElement("surname", "sed.Surname",
185: new TextControl(), true);
186: form.addBeanElement("country", "common.Country",
187: new TextControl(), true);
188: }
189: }
190:
191: private FormElement buildSexElement() {
192: FormElement result = FormUtil.createElement("sed.Sex",
193: new TextControl(), new StringData(), true);
194: result.setConstraint(new SexConstraint());
195:
196: return result;
197: }
198:
199: private class SexConstraint extends BaseFieldConstraint {
200: public SexConstraint() {
201: }
202:
203: protected void validateConstraint() throws Exception {
204: String value = (String) getValue();
205: if (!("m".equals(value.toLowerCase()) || "f".equals(value
206: .toLowerCase()))) {
207: addError(MessageUtil.localizeAndFormat(
208: "sed.sexcon.ermsg", t(getLabel()),
209: getEnvironment()));
210: }
211: }
212: }
213: }
|