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.portal.layout.impl;
018:
019: import java.util.ArrayList;
020: import java.util.Iterator;
021: import java.util.List;
022:
023: import org.apache.cocoon.portal.layout.AbstractLayout;
024: import org.apache.cocoon.portal.layout.CompositeLayout;
025: import org.apache.cocoon.portal.layout.Item;
026: import org.apache.cocoon.portal.layout.Layout;
027: import org.apache.cocoon.util.ClassUtils;
028:
029: /**
030: * A composite layout is a layout that contains other layouts.
031: *
032: * @author <a href="mailto:cziegeler@s-und-n.de">Carsten Ziegeler</a>
033: * @author <a href="mailto:volker.schmitt@basf-it-services.com">Volker Schmitt</a>
034: *
035: * @version CVS $Id: CompositeLayoutImpl.java 603313 2007-12-11 17:30:12Z cziegeler $
036: */
037: public class CompositeLayoutImpl extends AbstractLayout implements
038: CompositeLayout {
039:
040: protected List items = new ArrayList(3);
041:
042: /** The class name of the items */
043: protected String itemClassName;
044:
045: /**
046: * Constructor
047: */
048: public CompositeLayoutImpl() {
049: // nothing to do
050: }
051:
052: /**
053: * Add indexed item to the itemList.
054: * @param index index for the position inside the list
055: * @param item item to add
056: */
057: public final void addItem(int index, Item item) {
058: this .items.add(index, item);
059: item.setParent(this );
060: }
061:
062: /**
063: * Add Item to the ItemList.
064: * @param item item to add
065: */
066: public final void addItem(Item item) {
067: this .items.add(item);
068: item.setParent(this );
069: }
070:
071: /**
072: * Get Item from the ItemList.
073: * @return Item
074: */
075: public final Item getItem(int index) {
076: return (Item) this .items.get(index);
077: }
078:
079: /**
080: * Get the ItemList.
081: * @return items
082: */
083: public final List getItems() {
084: return this .items;
085: }
086:
087: /**
088: * Get size of ItemList.
089: * @return size
090: */
091: public final int getSize() {
092: return this .items.size();
093: }
094:
095: public final void removeItem(Item item) {
096: this .items.remove(item);
097: item.setParent(null);
098: }
099:
100: /* (non-Javadoc)
101: * @see org.apache.cocoon.portal.layout.CompositeLayout#createNewItem()
102: */
103: public Item createNewItem() {
104: if (this .itemClassName == null) {
105: return new Item();
106: }
107: try {
108: return (Item) ClassUtils.newInstance(this .itemClassName);
109: } catch (Exception ignore) {
110: return new Item();
111: }
112: }
113:
114: /**
115: * @return Returns the item class name.
116: */
117: public String getItemClassName() {
118: return this .itemClassName;
119: }
120:
121: /**
122: * @param value The item class name to set.
123: */
124: public void setItemClassName(String value) {
125: this .itemClassName = value;
126: }
127:
128: /* (non-Javadoc)
129: * @see java.lang.Object#clone()
130: */
131: protected Object clone() throws CloneNotSupportedException {
132: CompositeLayoutImpl clone = (CompositeLayoutImpl) super .clone();
133:
134: // we are not cloning the items
135: clone.items = new ArrayList();
136:
137: return clone;
138: }
139:
140: /* (non-Javadoc)
141: * @see org.apache.cocoon.portal.layout.Layout#copy(java.util.Map)
142: */
143: public Layout copy() {
144: CompositeLayoutImpl clone = (CompositeLayoutImpl) super .copy();
145: final Iterator i = this .items.iterator();
146: while (i.hasNext()) {
147: final Item current = (Item) i.next();
148: final Item clonedItem = current.copy(clone);
149: clone.addItem(clonedItem);
150: }
151: return clone;
152: }
153:
154: }
|