01: /*
02: * Copyright 2007 Hippo
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS"
12: * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
16: package nl.hippo.cocoon.forms;
17:
18: import java.util.ArrayList;
19: import java.util.Iterator;
20: import java.util.List;
21:
22: import org.apache.cocoon.forms.formmodel.ContainerWidget;
23: import org.apache.cocoon.forms.formmodel.Repeater;
24: import org.apache.cocoon.forms.formmodel.Widget;
25: import org.apache.cocoon.forms.formmodel.Repeater.RepeaterRow;
26:
27: /**
28: * Used to perform processing on widgets in the form, after reloading of the form.
29: * One or more filters are applied to every widget found in the widget tree of the form.
30: * The default filter enables / disables repeater actions, depending on the size of the repeater,
31: * the max size of the repeater, and the position of the action widget, e.g. the "move-up"
32: * widget is hidden in the first row of the repeater.
33: *
34: * @author <a href="mailto:d.dam@hippo.nl">Dennis Dam</a>
35: *
36: * @version $Id: HippoFormProcessor.java 8544 2007-10-16 09:48:55Z ddam $
37: */
38: public class HippoFormProcessor {
39:
40: private List filters;
41:
42: public HippoFormProcessor() {
43: filters = new ArrayList();
44: filters.add(new DefaultFilter());
45: }
46:
47: public void process(Widget widget) {
48: process(widget, -1);
49: }
50:
51: private void processChildren(Iterator children, int repeaterIndex) {
52: int this RepeaterIndex = 0;
53: while (children.hasNext()) {
54: Widget w = (Widget) children.next();
55: if (w instanceof RepeaterRow) {
56: process(w, this RepeaterIndex++);
57: } else {
58: process(w, repeaterIndex);
59: }
60: }
61: }
62:
63: private void process(Widget widget, int repeaterIndex) {
64: if (widget instanceof ContainerWidget) {
65: processChildren(((ContainerWidget) widget).getChildren(),
66: repeaterIndex);
67: } else {
68: if (widget instanceof Repeater) {
69: Repeater r = (Repeater) widget;
70: for (int i = 0; i < r.getSize(); i++) {
71: process(r.getRow(i), i);
72: }
73: }
74: }
75:
76: for (Iterator filterIter = filters.iterator(); filterIter
77: .hasNext();) {
78: WidgetFilter filter = (WidgetFilter) filterIter.next();
79: filter.filter(widget, repeaterIndex);
80: }
81: }
82:
83: public List getFilters() {
84: return filters;
85: }
86:
87: public void setFilters(List filters) {
88: this.filters = filters;
89: }
90:
91: }
|