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.uilib.form.formlist;
016:
017: import java.util.Collections;
018: import java.util.HashMap;
019: import java.util.Iterator;
020: import java.util.List;
021: import java.util.Map;
022: import org.apache.commons.collections.map.LinkedMap;
023: import org.araneaframework.core.AraneaRuntimeException;
024: import org.araneaframework.core.BaseApplicationWidget;
025: import org.araneaframework.core.util.ExceptionUtil;
026: import org.araneaframework.uilib.form.FormWidget;
027: import org.araneaframework.uilib.form.GenericFormElement;
028: import org.araneaframework.uilib.form.visitor.FormElementVisitor;
029:
030: /**
031: * Base class for editable rows widgets that are used to handle simultenous
032: * editing of multiple forms with same structure.
033: *
034: * @author Jevgeni Kabanov (ekabanov <i>at</i> araneaframework <i>dot</i> org)
035: */
036: public abstract class BaseFormListWidget extends GenericFormElement {
037:
038: //*******************************************************************
039: // FIELDS
040: //*******************************************************************
041:
042: protected FormListModel model = null;
043:
044: protected FormRowHandler formRowHandler;
045: protected Map formRows = new LinkedMap();
046:
047: protected int rowFormCounter = 0;
048:
049: public BaseFormListWidget(FormRowHandler formRowHandler) {
050: setFormRowHandler(formRowHandler);
051: }
052:
053: public BaseFormListWidget(FormRowHandler formRowHandler,
054: FormListModel model) {
055: setFormRowHandler(formRowHandler);
056: setModel(model);
057: }
058:
059: //*******************************************************************
060: // PUBLIC METHODS
061: //*******************************************************************
062:
063: /**
064: * Sets form list model.
065: * @param model the form list model
066: */
067: public void setModel(FormListModel model) {
068: this .model = model;
069: }
070:
071: private List getRows() {
072: try {
073: return model.getRows();
074: } catch (Exception e) {
075: throw new AraneaRuntimeException(e);
076: }
077: }
078:
079: /**
080: * Returns <code>Map<Object key, FormRow></code> of initialized editable rows.
081: * @return <code>Map<Object key, FormRow></code> of initialized editable rows.
082: */
083: public Map getFormRows() {
084: for (Iterator i = getRows().iterator(); i.hasNext();) {
085: Object row = i.next();
086:
087: if (formRows.get(formRowHandler.getRowKey(row)) == null)
088: addFormRow(row);
089: else
090: ((FormRow) formRows.get(formRowHandler.getRowKey(row)))
091: .setRow(row);
092: }
093:
094: return formRows;
095: }
096:
097: /**
098: * Returns add form of the widget.
099: * @return Add form of the widget.
100: */
101: public FormWidget getAddForm() {
102: return (FormWidget) getWidget("addForm");
103: }
104:
105: /**
106: * Returns editable row corresponding to the row key.
107: * @param key row key.
108: * @return editable row corresponding to the row key.
109: */
110: public FormRow getFormRow(Object key) {
111: return (FormRow) getFormRows().get(key);
112: }
113:
114: /**
115: * Returns current {@link FormRowHandler}.
116: * @return current {@link FormRowHandler}.
117: */
118: public FormRowHandler getFormRowHandler() {
119: return this .formRowHandler;
120: }
121:
122: /**
123: * Sets new {@link FormRowHandler}.
124: * @param editableRowHandler new {@link FormRowHandler}.
125: */
126: public void setFormRowHandler(FormRowHandler editableRowHandler) {
127: this .formRowHandler = editableRowHandler;
128: }
129:
130: //*******************************************************************
131: // PROTECTED METHODS
132: //*******************************************************************
133:
134: /**
135: * Creates and adds an editable row from a usual row object.
136: */
137: protected void addFormRow(Object newRow) {
138: FormWidget rowForm;
139: try {
140: rowForm = buildAddForm();
141: } catch (Exception e1) {
142: throw ExceptionUtil.uncheckException(e1);
143: }
144: String rowFormId = "rowForm" + rowFormCounter++;
145: FormRow newEditableRow = new FormRow(this , formRowHandler
146: .getRowKey(newRow), newRow, rowFormId, rowForm, true);
147:
148: addWidget(rowFormId, rowForm);
149: try {
150: formRowHandler.initFormRow(newEditableRow, newRow);
151: } catch (Exception e) {
152: throw ExceptionUtil.uncheckException(e);
153: }
154:
155: formRows.put(formRowHandler.getRowKey(newRow), newEditableRow);
156: }
157:
158: protected void init() throws Exception {
159: super .init();
160:
161: resetAddForm();
162: }
163:
164: /** Used to build instance of FormWidget belonging to this list. */
165: protected abstract FormWidget buildAddForm() throws Exception;
166:
167: //*********************************************************************
168: //* ROW HANDLING METHODS
169: //*********************************************************************
170:
171: /**
172: * Saves all editable rows.
173: */
174: public void saveAllRows() {
175: Map rowsToSave = new HashMap();
176:
177: for (Iterator i = getFormRows().values().iterator(); i
178: .hasNext();) {
179: FormRow editableRow = (FormRow) i.next();
180: rowsToSave.put(editableRow.getKey(), editableRow);
181: }
182:
183: try {
184: formRowHandler.saveRows(rowsToSave);
185: } catch (Exception e1) {
186: throw ExceptionUtil.uncheckException(e1);
187: }
188: }
189:
190: /**
191: * Saves all editable rows that correspond to the current usual rows.
192: */
193: public void saveCurrentRows() {
194: Map rowsToSave = new HashMap();
195:
196: for (Iterator i = getRows().iterator(); i.hasNext();) {
197: Object row = i.next();
198:
199: FormRow editableRow = (FormRow) getFormRows().get(
200: formRowHandler.getRowKey(row));
201: rowsToSave.put(editableRow.getKey(), editableRow);
202: }
203:
204: try {
205: formRowHandler.saveRows(rowsToSave);
206: } catch (Exception e1) {
207: throw ExceptionUtil.uncheckException(e1);
208: }
209: }
210:
211: /**
212: * Saves row specified by key.
213: * @param key row key.
214: */
215: public void saveRow(Object key) {
216: FormRow editableRow = (FormRow) getFormRows().get(key);
217:
218: try {
219: formRowHandler.saveRows(Collections.singletonMap(
220: editableRow.getKey(), editableRow));
221: } catch (Exception e1) {
222: throw ExceptionUtil.uncheckException(e1);
223: }
224: }
225:
226: /**
227: * Deletes row specified by key.
228: * @param key row key.
229: */
230: public void deleteRow(Object key) {
231: getFormRows().remove(key);
232:
233: try {
234: formRowHandler.deleteRows(Collections.singleton(key));
235: } catch (Exception e1) {
236: throw ExceptionUtil.uncheckException(e1);
237: }
238: }
239:
240: /**
241: * Adds row from given form.
242: * @param addForm add form.
243: */
244: public void addRow(FormWidget addForm) {
245: try {
246: formRowHandler.addRow(addForm);
247: } catch (Exception e1) {
248: throw ExceptionUtil.uncheckException(e1);
249: }
250: }
251:
252: /**
253: * Opens or closes the row specified by the key.
254: * @param key row key.
255: */
256: public void openCloseRow(Object key) {
257: FormRow currentRow = (FormRow) getFormRows().get(key);
258:
259: try {
260: formRowHandler.openOrCloseRow(currentRow);
261: } catch (Exception e1) {
262: throw ExceptionUtil.uncheckException(e1);
263: }
264: }
265:
266: public void resetAddForm() {
267: try {
268: FormWidget addForm = buildAddForm();
269: addWidget("addForm", addForm);
270: formRowHandler.initAddForm(addForm);
271: } catch (Exception e1) {
272: throw ExceptionUtil.uncheckException(e1);
273: }
274: }
275:
276: public void resetFormRow(Object key) {
277: getFormRows().remove(key);
278: }
279:
280: public void resetFormRows() {
281: getFormRows().clear();
282: }
283:
284: // *********************************************************************
285: //* VIEW MODEL
286: //*********************************************************************
287:
288: public Object getViewModel() throws Exception {
289: return new ViewModel();
290: }
291:
292: public class ViewModel extends BaseApplicationWidget.ViewModel {
293: protected Map editableRows = new HashMap();
294: protected List rows;
295:
296: public ViewModel() {
297: for (Iterator i = BaseFormListWidget.this .getFormRows()
298: .entrySet().iterator(); i.hasNext();) {
299: Map.Entry ent = (Map.Entry) i.next();
300:
301: editableRows.put(ent.getKey(), ((FormRow) ent
302: .getValue()).getViewModel());
303: }
304:
305: this .rows = BaseFormListWidget.this .getRows();
306: }
307:
308: /**
309: *
310: * Returns <code>Map<Object key, EditableRow></code>.
311: * @return <code>Map<Object key, EditableRow></code>.
312: */
313: public Map getFormRows() {
314: return editableRows;
315: }
316:
317: /**
318: * Returns row handler that is used to get row keys.
319: */
320: public FormRowHandler getRowHandler() {
321: return BaseFormListWidget.this .formRowHandler;
322: }
323:
324: /**
325: * Returns rows.
326: * @return rows.
327: */
328: public List getRows() {
329: return rows;
330: }
331: }
332:
333: protected void convertInternal() throws Exception {
334: for (Iterator i = getFormRows().values().iterator(); i
335: .hasNext();) {
336: ((FormRow) i.next()).getForm().convert();
337: }
338: }
339:
340: protected boolean validateInternal() throws Exception {
341: for (Iterator i = getFormRows().values().iterator(); i
342: .hasNext();) {
343: ((FormRow) i.next()).getForm().validate();
344: }
345:
346: return super .validateInternal();
347: }
348:
349: public boolean isStateChanged() {
350: boolean result = false;
351: for (Iterator i = getFormRows().values().iterator(); i
352: .hasNext();)
353: result |= ((FormRow) i.next()).getForm().isStateChanged();
354: return result;
355: }
356:
357: public boolean isDisabled() {
358: boolean result = false;
359: for (Iterator i = getFormRows().values().iterator(); i
360: .hasNext();)
361: result &= ((FormRow) i.next()).getForm().isDisabled();
362: return result;
363: }
364:
365: public void accept(String id, FormElementVisitor visitor) {
366: visitor.visit(id, this );
367:
368: visitor.pushContext(id, this );
369:
370: for (Iterator i = getFormRows().values().iterator(); i
371: .hasNext();) {
372: FormRow entry = (FormRow) i.next();
373:
374: String elementId = (String) entry.getKey();
375: FormWidget element = entry.getForm();
376:
377: element.accept(elementId, visitor);
378: }
379:
380: visitor.popContext();
381: }
382:
383: public void markBaseState() {
384: for (Iterator i = getFormRows().values().iterator(); i
385: .hasNext();) {
386: ((FormRow) i.next()).getForm().markBaseState();
387: }
388: }
389:
390: public void restoreBaseState() {
391: for (Iterator i = getFormRows().values().iterator(); i
392: .hasNext();) {
393: ((FormRow) i.next()).getForm().restoreBaseState();
394: }
395: }
396:
397: public void setDisabled(boolean disabled) {
398: for (Iterator i = getFormRows().values().iterator(); i
399: .hasNext();) {
400: ((FormRow) i.next()).getForm().setDisabled(disabled);
401: }
402: }
403:
404: public void clearErrors() {
405: super .clearErrors();
406: for (Iterator i = getFormRows().values().iterator(); i
407: .hasNext();) {
408: ((FormRow) i.next()).getForm().clearErrors();
409: }
410: }
411:
412: public boolean isValid() {
413: boolean result = super .isValid();
414:
415: for (Iterator i = getFormRows().values().iterator(); i
416: .hasNext();) {
417: if (!result)
418: break;
419: result &= ((FormRow) i.next()).getForm().isValid();
420: }
421:
422: return result;
423: }
424:
425: }
|