KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > forms > formmodel > RepeaterActionDefinitionBuilder


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * 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 org.apache.cocoon.forms.formmodel;
17
18 import java.util.Iterator JavaDoc;
19
20 import org.apache.cocoon.forms.FormsConstants;
21 import org.apache.cocoon.forms.event.ActionListener;
22 import org.apache.cocoon.forms.util.DomHelper;
23 import org.apache.cocoon.util.Deprecation;
24 import org.w3c.dom.Element JavaDoc;
25
26 /**
27  * Builds a <code>&lt;fd:repeater-action/&gt;</code>
28  *
29  * <p>Three actions are defined:
30  * <ul>
31  * <li>
32  * <code>&lt;fd:repeater-action id="add" command="add-row"
33  * repeater="repeater-id"/&gt;</code>: when activated, adds a row to the
34  * sibling repeater named "repeater-id".
35  * </li>
36  * <li>
37  * <code>&lt;fd:repeater-action id="rm" command="delete-rows"
38  * repeater="repeater-id" select="select-id"/&gt;</code>: removes the
39  * selected rows from the sibling repeater named "repeater-id". The
40  * selected rows are identified by the boolean field "select-id" present
41  * in each row.
42  * </li>
43  * <li>
44  * <code>&lt;fd:repeater-action id="insert" command="insert-rows"
45  * repeater="repeater-id" select="select-id"/&gt;</code>: inserts rows before
46  * the selected rows from the sibling repeater named "repeater-id". The
47  * selected rows are identified by the boolean field "select-id" present
48  * in each row.
49  * </li>
50  * </ul>
51  *
52  * @author <a HREF="http://www.apache.org/~sylvain/">Sylvain Wallez</a>
53  * @version $Id: RepeaterActionDefinitionBuilder.java 326838 2005-10-20 06:26:53Z sylvain $
54  */

55 public class RepeaterActionDefinitionBuilder extends AbstractWidgetDefinitionBuilder {
56
57     public WidgetDefinition buildWidgetDefinition(Element JavaDoc widgetElement) throws Exception JavaDoc {
58         // Get the "command" attribute
59
String JavaDoc actionCommand = DomHelper.getAttribute(widgetElement, "command", null);
60         
61         // If unspecified, check the deprecated "action-command" deprecated attribute
62
if (actionCommand == null) {
63             actionCommand = DomHelper.getAttribute(widgetElement, "action-command", null);
64             if (actionCommand != null) {
65                 Deprecation.logger.info("The 'action-command' attribute is deprecated and replaced by 'command', at " +
66                     DomHelper.getLocation(widgetElement));
67             }
68         }
69         if (actionCommand == null) {
70             throw new Exception JavaDoc("Missing attribute 'command' at " + DomHelper.getLocation(widgetElement));
71         }
72
73
74         RepeaterActionDefinition definition = createDefinition(widgetElement, actionCommand);
75         super.setupDefinition(widgetElement, definition);
76         setDisplayData(widgetElement, definition);
77
78         definition.setActionCommand(actionCommand);
79
80         // Warn of the mis-named 'on-action' that existed initially
81
Element JavaDoc buggyOnActivate = DomHelper.getChildElement(widgetElement, FormsConstants.DEFINITION_NS, "on-activate", false);
82         if (buggyOnActivate != null) {
83             throw new Exception JavaDoc("Use 'on-action' instead of 'on-activate' on row-action at " +
84                 DomHelper.getLocation(buggyOnActivate));
85         }
86
87         Iterator JavaDoc iter = buildEventListeners(widgetElement, "on-action", ActionListener.class).iterator();
88         while (iter.hasNext()) {
89             definition.addActionListener((ActionListener)iter.next());
90         }
91
92         definition.makeImmutable();
93         return definition;
94     }
95
96     protected RepeaterActionDefinition createDefinition(Element JavaDoc element, String JavaDoc actionCommand) throws Exception JavaDoc {
97
98         String JavaDoc repeater = DomHelper.getAttribute(element, "repeater");
99         if ("delete-rows".equals(actionCommand)) {
100             String JavaDoc select = DomHelper.getAttribute(element, "select");
101             return new RepeaterActionDefinition.DeleteRowsActionDefinition(repeater, select);
102
103         } else if ("add-row".equals(actionCommand)) {
104             return new RepeaterActionDefinition.AddRowActionDefinition(repeater);
105
106         } else if ("insert-rows".equals(actionCommand)) {
107             String JavaDoc select = DomHelper.getAttribute(element, "select");
108             return new RepeaterActionDefinition.InsertRowsActionDefinition(repeater, select);
109
110         } else {
111             throw new Exception JavaDoc("Unknown repeater action '" + actionCommand + "' at " + DomHelper.getLineLocation(element));
112         }
113     }
114 }
115
Popular Tags