001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.cocoon.forms.formmodel;
018:
019: import java.util.Iterator;
020:
021: import org.apache.cocoon.forms.FormsConstants;
022: import org.apache.cocoon.forms.FormsException;
023: import org.apache.cocoon.forms.event.ActionListener;
024: import org.apache.cocoon.forms.util.DomHelper;
025: import org.apache.cocoon.util.Deprecation;
026:
027: import org.w3c.dom.Element;
028:
029: /**
030: * Builds a <code><fd:repeater-action/></code>
031: *
032: * <p>Three actions are defined:
033: * <ul>
034: * <li>
035: * <code><fd:repeater-action id="add" command="add-row"
036: * repeater="repeater-id"/></code>: when activated, adds a row to the
037: * sibling repeater named "repeater-id".
038: * </li>
039: * <li>
040: * <code><fd:repeater-action id="rm" command="delete-rows"
041: * repeater="repeater-id" select="select-id"/></code>: removes the
042: * selected rows from the sibling repeater named "repeater-id". The
043: * selected rows are identified by the boolean field "select-id" present
044: * in each row.
045: * </li>
046: * <li>
047: * <code><fd:repeater-action id="insert" command="insert-rows"
048: * repeater="repeater-id" select="select-id"/></code>: inserts rows before
049: * the selected rows from the sibling repeater named "repeater-id". The
050: * selected rows are identified by the boolean field "select-id" present
051: * in each row.
052: * </li>
053: * </ul>
054: *
055: * @version $Id: RepeaterActionDefinitionBuilder.java 484663 2006-12-08 17:09:09Z simoneg $
056: */
057: public class RepeaterActionDefinitionBuilder extends
058: AbstractWidgetDefinitionBuilder {
059:
060: public WidgetDefinition buildWidgetDefinition(Element widgetElement)
061: throws Exception {
062: // Get the "command" attribute
063: String actionCommand = DomHelper.getAttribute(widgetElement,
064: "command", null);
065:
066: // If unspecified, check the deprecated "action-command" deprecated attribute
067: if (actionCommand == null) {
068: actionCommand = DomHelper.getAttribute(widgetElement,
069: "action-command", null);
070: if (actionCommand != null) {
071: Deprecation.logger
072: .info("The 'action-command' attribute is deprecated and replaced by 'command', at "
073: + DomHelper.getLocation(widgetElement));
074: }
075: }
076:
077: if (actionCommand == null) {
078: throw new FormsException(
079: "Required attribute 'command' is missing.",
080: DomHelper.getLocationObject(widgetElement));
081: }
082:
083: RepeaterActionDefinition definition = createDefinition(
084: widgetElement, actionCommand);
085: super .setupDefinition(widgetElement, definition);
086: setDisplayData(widgetElement, definition);
087:
088: definition.setActionCommand(actionCommand);
089:
090: // Warn of the mis-named 'on-action' that existed initially
091: Element buggyOnActivate = DomHelper.getChildElement(
092: widgetElement, FormsConstants.DEFINITION_NS,
093: "on-activate", false);
094: if (buggyOnActivate != null) {
095: throw new FormsException(
096: "Use 'on-action' instead of 'on-activate' on row-action.",
097: DomHelper.getLocationObject(buggyOnActivate));
098: }
099:
100: Iterator i = buildEventListeners(widgetElement, "on-action",
101: ActionListener.class).iterator();
102: while (i.hasNext()) {
103: definition.addActionListener((ActionListener) i.next());
104: }
105:
106: definition.makeImmutable();
107: return definition;
108: }
109:
110: protected RepeaterActionDefinition createDefinition(
111: Element element, String actionCommand) throws Exception {
112:
113: String repeater = DomHelper.getAttribute(element, "repeater");
114: if ("delete-rows".equals(actionCommand)) {
115: String select = DomHelper.getAttribute(element, "select");
116: return new RepeaterActionDefinition.DeleteRowsActionDefinition(
117: repeater, select);
118:
119: } else if ("add-row".equals(actionCommand)) {
120: int insertRows = DomHelper.getAttributeAsInteger(element,
121: "number-of-rows", 1);
122: return new RepeaterActionDefinition.AddRowActionDefinition(
123: repeater, insertRows);
124:
125: } else if ("insert-rows".equals(actionCommand)) {
126: String select = DomHelper.getAttribute(element, "select");
127: return new RepeaterActionDefinition.InsertRowsActionDefinition(
128: repeater, select);
129:
130: } else if ("sort-by".equals(actionCommand)) {
131: String field = DomHelper.getAttribute(element, "field",
132: null);
133: return new RepeaterActionDefinition.SortActionDefinition(
134: repeater, field);
135:
136: } else if ("page-first".equals(actionCommand)) {
137: return new RepeaterActionDefinition.ChangePageActionDefinition(
138: repeater,
139: RepeaterActionDefinition.ChangePageActionDefinition.FIRST);
140:
141: } else if ("page-prev".equals(actionCommand)) {
142: return new RepeaterActionDefinition.ChangePageActionDefinition(
143: repeater,
144: RepeaterActionDefinition.ChangePageActionDefinition.PREV);
145:
146: } else if ("page-next".equals(actionCommand)) {
147: return new RepeaterActionDefinition.ChangePageActionDefinition(
148: repeater,
149: RepeaterActionDefinition.ChangePageActionDefinition.NEXT);
150:
151: } else if ("page-last".equals(actionCommand)) {
152: return new RepeaterActionDefinition.ChangePageActionDefinition(
153: repeater,
154: RepeaterActionDefinition.ChangePageActionDefinition.LAST);
155:
156: } else if ("page-custom".equals(actionCommand)) {
157: return new RepeaterActionDefinition.ChangePageActionDefinition(
158: repeater,
159: RepeaterActionDefinition.ChangePageActionDefinition.CUSTOM);
160:
161: } else {
162: throw new FormsException("Unknown repeater action '"
163: + actionCommand + "'.", DomHelper
164: .getLocationObject(element));
165: }
166: }
167: }
|