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: import org.w3c.dom.Element;
027:
028: /**
029: *
030: * @version $Id: RowActionDefinitionBuilder.java 449149 2006-09-23 03:58:05Z crossley $
031: */
032: public class RowActionDefinitionBuilder extends
033: AbstractWidgetDefinitionBuilder {
034:
035: public WidgetDefinition buildWidgetDefinition(Element widgetElement)
036: throws Exception {
037: // Get the "command" attribute
038: String actionCommand = DomHelper.getAttribute(widgetElement,
039: "command", null);
040:
041: // If unspecified, check the deprecated "action-command" deprecated attribute
042: if (actionCommand == null) {
043: actionCommand = DomHelper.getAttribute(widgetElement,
044: "action-command", null);
045: if (actionCommand != null) {
046: Deprecation.logger
047: .info("The 'action-command' attribute is deprecated and replaced by 'command', at "
048: + DomHelper.getLocation(widgetElement));
049: }
050: }
051: if (actionCommand == null) {
052: throw new FormsException(
053: "Required attribute 'command' is missing.",
054: DomHelper.getLocationObject(widgetElement));
055: }
056:
057: RowActionDefinition definition = createDefinition(
058: widgetElement, actionCommand);
059: super .setupDefinition(widgetElement, definition);
060: setDisplayData(widgetElement, definition);
061:
062: definition.setActionCommand(actionCommand);
063:
064: // Warn of the mis-named 'on-action' that existed initially
065: Element buggyOnActivate = DomHelper.getChildElement(
066: widgetElement, FormsConstants.DEFINITION_NS,
067: "on-activate", false);
068: if (buggyOnActivate != null) {
069: throw new FormsException(
070: "Use 'on-action' instead of 'on-activate' on row-action.",
071: DomHelper.getLocationObject(buggyOnActivate));
072: }
073:
074: Iterator iter = buildEventListeners(widgetElement, "on-action",
075: ActionListener.class).iterator();
076: while (iter.hasNext()) {
077: definition.addActionListener((ActionListener) iter.next());
078: }
079:
080: definition.makeImmutable();
081: return definition;
082: }
083:
084: protected RowActionDefinition createDefinition(Element element,
085: String actionCommand) throws Exception {
086:
087: if ("delete".equals(actionCommand)) {
088: return new RowActionDefinition.DeleteRowDefinition();
089:
090: } else if ("add-after".equals(actionCommand)) {
091: return new RowActionDefinition.AddAfterDefinition();
092:
093: } else if ("move-up".equals(actionCommand)) {
094: return new RowActionDefinition.MoveUpDefinition();
095:
096: } else if ("move-down".equals(actionCommand)) {
097: return new RowActionDefinition.MoveDownDefinition();
098:
099: } else {
100: throw new FormsException("Unknown repeater row action '"
101: + actionCommand + "'.", DomHelper
102: .getLocationObject(element));
103: }
104: }
105: }
|