001: /**********************************************************************************
002: * $URL: https://source.sakaiproject.org/svn/metaobj/tags/sakai_2-4-1/metaobj-api/api/src/java/org/sakaiproject/metaobj/shared/model/ElementListBean.java $
003: * $Id: ElementListBean.java 21204 2007-02-09 19:46:24Z john.ellis@rsmart.com $
004: ***********************************************************************************
005: *
006: * Copyright (c) 2004, 2005, 2006 The Sakai Foundation.
007: *
008: * Licensed under the Educational Community License, Version 1.0 (the "License");
009: * you may not use this file except in compliance with the License.
010: * You may obtain a copy of the License at
011: *
012: * http://www.opensource.org/licenses/ecl1.php
013: *
014: * Unless required by applicable law or agreed to in writing, software
015: * distributed under the License is distributed on an "AS IS" BASIS,
016: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: * See the License for the specific language governing permissions and
018: * limitations under the License.
019: *
020: **********************************************************************************/package org.sakaiproject.metaobj.shared.model;
021:
022: import java.util.ArrayList;
023: import java.util.List;
024: import java.util.Iterator;
025: import java.util.Collection;
026:
027: import org.apache.commons.logging.Log;
028: import org.apache.commons.logging.LogFactory;
029: import org.jdom.Element;
030: import org.sakaiproject.metaobj.utils.xml.SchemaNode;
031:
032: /**
033: * Created by IntelliJ IDEA.
034: * User: John Ellis
035: * Date: Mar 11, 2004
036: * Time: 3:55:06 PM
037: * To change this template use File | Settings | File Templates.
038: */
039: public class ElementListBean extends ArrayList implements LimitedList {
040: protected final Log logger = LogFactory.getLog(getClass());
041:
042: private Element parentElement;
043: private SchemaNode schema;
044: private boolean deferValidation = false;
045:
046: public ElementListBean() {
047:
048: }
049:
050: public ElementListBean(Element parentElement, SchemaNode schema,
051: boolean deferValidation) {
052: this .parentElement = parentElement;
053: this .schema = schema;
054: this .deferValidation = deferValidation;
055: }
056:
057: public ElementListBean(Element parentElement, List elements,
058: SchemaNode schema, boolean deferValidation) {
059: super (elements);
060: this .parentElement = parentElement;
061: this .schema = schema;
062: this .deferValidation = deferValidation;
063: for (Iterator i = iterator(); i.hasNext();) {
064: ElementBean bean = (ElementBean) i.next();
065: bean.setParent(this );
066: }
067: }
068:
069: public ElementListBean(List elements, SchemaNode schema) {
070: super (elements);
071: this .schema = schema;
072: }
073:
074: public Object get(int index) {
075: logger.debug("get called with index " + index);
076: return super .get(index);
077: }
078:
079: public ElementBean createBlank() {
080: return new ElementBean(new Element(schema.getName()), schema,
081: deferValidation);
082: }
083:
084: /**
085: * Removes the element at the specified position in this list.
086: * Shifts any subsequent elements to the left (subtracts one from their
087: * indices).
088: *
089: * @param index the index of the element to removed.
090: * @return the element that was removed from the list.
091: * @throws IndexOutOfBoundsException if index out of range <tt>(index
092: * < 0 || index >= size())</tt>.
093: */
094: public Object remove(int index) {
095: ElementBean bean = (ElementBean) get(index);
096:
097: bean.getBaseElement().getParent().removeContent(
098: bean.getBaseElement());
099:
100: return super .remove(index);
101: }
102:
103: /**
104: * Appends the specified element to the end of this list.
105: *
106: * @param o element to be appended to this list.
107: * @return <tt>true</tt> (as per the general contract of Collection.add).
108: */
109: public boolean add(Object o) {
110: ElementBean bean = (ElementBean) o;
111:
112: parentElement.addContent(bean.getBaseElement());
113:
114: bean.setParent(this );
115:
116: return super .add(o);
117: }
118:
119: /**
120: * Inserts the specified element at the specified position in this
121: * list. Shifts the element currently at that position (if any) and
122: * any subsequent elements to the right (adds one to their indices).
123: *
124: * @param index index at which the specified element is to be inserted.
125: * @param element element to be inserted.
126: * @throws IndexOutOfBoundsException if index is out of range
127: * <tt>(index < 0 || index > size())</tt>.
128: */
129: public void add(int index, Object element) {
130: add(element);
131:
132: super .add(index, element);
133: }
134:
135: public boolean addAll(Collection collection) {
136: for (Iterator i = collection.iterator(); i.hasNext();) {
137: ElementBean bean = createBlank();
138: bean.getBaseElement().addContent((String) i.next());
139: add(bean);
140: }
141:
142: return true;
143: }
144:
145: public void clear() {
146: while (size() > 0) {
147: remove(0);
148: }
149: }
150:
151: public int getUpperLimit() {
152: if (schema.getMaxOccurs() == -1) {
153: return Integer.MAX_VALUE;
154: }
155: return schema.getMaxOccurs();
156: }
157:
158: public int getLowerLimit() {
159: return schema.getMinOccurs();
160: }
161: }
|