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.RepeaterListener;
024: import org.apache.cocoon.forms.util.DomHelper;
025:
026: import org.w3c.dom.Element;
027:
028: /**
029: * Builds {@link RepeaterDefinition}s.
030: *
031: * @version $Id: RepeaterDefinitionBuilder.java 449452 2006-09-24 18:26:03Z simoneg $
032: */
033: public final class RepeaterDefinitionBuilder extends
034: AbstractContainerDefinitionBuilder {
035:
036: public WidgetDefinition buildWidgetDefinition(
037: Element repeaterElement) throws Exception {
038:
039: int initialSize = DomHelper.getAttributeAsInteger(
040: repeaterElement, "initial-size", 0);
041: int minSize = DomHelper.getAttributeAsInteger(repeaterElement,
042: "min-size", 0);
043: int maxSize = DomHelper.getAttributeAsInteger(repeaterElement,
044: "max-size", Integer.MAX_VALUE);
045:
046: // should throw error on negative values ? Just correct them for now.
047: if (minSize < 0) {
048: throw new FormsException("min-size should be positive.",
049: DomHelper.getLocationObject(repeaterElement));
050: }
051:
052: if (maxSize < 0) {
053: throw new FormsException("max-size should be positive.",
054: DomHelper.getLocationObject(repeaterElement));
055: }
056:
057: if (maxSize < minSize) {
058: throw new FormsException(
059: "max-size should be greater than or equal to min-size.",
060: DomHelper.getLocationObject(repeaterElement));
061: }
062:
063: // initial size is at least the min size
064: initialSize = minSize > initialSize ? minSize : initialSize;
065:
066: boolean orderable = DomHelper.getAttributeAsBoolean(
067: repeaterElement, "orderable", false);
068: boolean selectable = DomHelper.getAttributeAsBoolean(
069: repeaterElement, "selectable", false);
070: boolean enhanced = DomHelper.getAttributeAsBoolean(
071: repeaterElement, "enhanced", false);
072:
073: int initialPage = 0;
074: int pageSize = Integer.MAX_VALUE;
075: String customPageId = null;
076:
077: Element pagesElement = DomHelper.getChildElement(
078: repeaterElement, FormsConstants.DEFINITION_NS, "pages");
079: if (pagesElement != null) {
080: enhanced = true;
081: initialPage = DomHelper.getAttributeAsInteger(pagesElement,
082: "initial", 1) - 1;
083: pageSize = DomHelper.getAttributeAsInteger(pagesElement,
084: "size", 0);
085: customPageId = DomHelper.getAttribute(pagesElement,
086: "page-field", null);
087: }
088:
089: RepeaterDefinition repeaterDefinition = new RepeaterDefinition(
090: initialSize, minSize, maxSize, selectable, orderable,
091: enhanced, initialPage, pageSize, customPageId);
092: super .setupDefinition(repeaterElement, repeaterDefinition);
093: setDisplayData(repeaterElement, repeaterDefinition);
094:
095: // parse "on-repeater-modified"
096: Iterator iter = buildEventListeners(repeaterElement,
097: "on-repeater-modified", RepeaterListener.class)
098: .iterator();
099: while (iter.hasNext()) {
100: repeaterDefinition
101: .addRepeaterListener((RepeaterListener) iter.next());
102: }
103:
104: setupContainer(repeaterElement, "widgets", repeaterDefinition);
105:
106: repeaterDefinition.makeImmutable();
107: return repeaterDefinition;
108: }
109: }
|