01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17: package org.apache.cocoon.woody.formmodel;
18:
19: import java.util.Iterator;
20:
21: import org.apache.cocoon.woody.event.ActionListener;
22: import org.apache.cocoon.woody.util.DomHelper;
23: import org.w3c.dom.Element;
24:
25: /**
26: * Builds a <code><wd:repeater-action/></code>
27: * <p>
28: * Two actions are defined :
29: * <ul>
30: * <li><code><wd:repeater-action id="add" action-command="add-row" repeater="repeater-id"/></code> :
31: * when activated, adds a row to the sibling repeater named "repeater-id".
32: * </li>
33: * <li><code><wd:repeater-action id="rm" action-command="delete-rows" repeater="repeater-id"
34: * select="select-id"/></code> : removes the selected rows from the sibling repeater named "repeater-id".
35: * The selected rows are identified by the boolean field "select-id" present in each row.
36: * </ul>
37: *
38: * @author <a href="http://www.apache.org/~sylvain/">Sylvain Wallez</a>
39: * @version CVS $Id: RepeaterActionDefinitionBuilder.java 433543 2006-08-22 06:22:54Z crossley $
40: */
41: public class RepeaterActionDefinitionBuilder extends
42: AbstractWidgetDefinitionBuilder {
43:
44: public WidgetDefinition buildWidgetDefinition(Element widgetElement)
45: throws Exception {
46: String actionCommand = DomHelper.getAttribute(widgetElement,
47: "action-command");
48: RepeaterActionDefinition definition = createDefinition(
49: widgetElement, actionCommand);
50: setLocation(widgetElement, definition);
51: setId(widgetElement, definition);
52: setDisplayData(widgetElement, definition);
53: setValidators(widgetElement, definition);
54:
55: definition.setActionCommand(actionCommand);
56:
57: Iterator iter = buildEventListeners(widgetElement,
58: "on-activate", ActionListener.class).iterator();
59: while (iter.hasNext()) {
60: definition.addActionListener((ActionListener) iter.next());
61: }
62:
63: return definition;
64: }
65:
66: protected RepeaterActionDefinition createDefinition(
67: Element element, String actionCommand) throws Exception {
68:
69: if ("delete-rows".equals(actionCommand)) {
70: String repeater = DomHelper.getAttribute(element,
71: "repeater");
72: String select = DomHelper.getAttribute(element, "select");
73: return new DeleteRowsActionDefinition(repeater, select);
74:
75: } else if ("add-row".equals(actionCommand)) {
76: String repeater = DomHelper.getAttribute(element,
77: "repeater");
78: return new AddRowActionDefinition(repeater);
79:
80: } else {
81: throw new Exception("Unknown repeater action '"
82: + actionCommand + "' at "
83: + DomHelper.getLineLocation(element));
84: }
85: }
86: }
|