001: /*
002: * Copyright 2001-2006 C:1 Financial Services GmbH
003: *
004: * This software is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License Version 2.1, as published by the Free Software Foundation.
007: *
008: * This software is distributed in the hope that it will be useful,
009: * but WITHOUT ANY WARRANTY; without even the implied warranty of
010: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
011: * Lesser General Public License for more details.
012: *
013: * You should have received a copy of the GNU Lesser General Public
014: * License along with this library; if not, write to the Free Software
015: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
016: */
017:
018: package de.finix.contelligent.components;
019:
020: import java.util.Collections;
021: import java.util.Iterator;
022: import java.util.LinkedList;
023: import java.util.List;
024:
025: import de.finix.contelligent.CallData;
026: import de.finix.contelligent.Component;
027: import de.finix.contelligent.ModificationVetoException;
028: import de.finix.contelligent.ObservingContainer;
029: import de.finix.contelligent.logging.LoggingService;
030: import de.finix.contelligent.util.StringUtils;
031:
032: /**
033: * A <code>SortedFolder</code> holds a comma separated list of names.
034: */
035: public class SortedFolder extends Folder implements ObservingContainer {
036: final static org.apache.log4j.Logger log = LoggingService
037: .getLogger(SortedFolder.class);
038:
039: // if property 'nameList' equals this token (which should be the types
040: // default value)
041: // the name-list gets build up from scratch in postCreate().
042: final static public String CREATE_NAMELIST_TOKEN = "[RECREATE-NAMELIST]";
043:
044: // holds the sorted names of the subcomponents: the top element is at index
045: // 0
046: private List sortedNameList = null;
047:
048: /** contains a comma separated list of names */
049: private String nameList = "";
050:
051: public void setNameList(String nameList) {
052: this .nameList = nameList;
053: }
054:
055: public String getNameList() {
056: return nameList;
057: }
058:
059: public void postCreate() throws Exception {
060: super .postCreate();
061: if (CREATE_NAMELIST_TOKEN.equals(nameList)) {
062: if (log.isDebugEnabled()) {
063: log
064: .debug("'"
065: + this
066: + "':postCreate() - initially filling nameList ...");
067: }
068: boolean isFirst = true;
069: StringBuffer nameBuf = new StringBuffer();
070: for (Iterator it = super .getSubcomponentNames(); it
071: .hasNext();) {
072: String name = (String) it.next();
073: if (!isFirst)
074: nameBuf.append(',');
075: if (log.isDebugEnabled()) {
076: log.debug("'" + this + "':postCreate() - adding"
077: + name);
078: }
079: nameBuf.append(name);
080: isFirst = false;
081: }
082: nameList = nameBuf.toString();
083: }
084: if (nameList == null || nameList.trim().length() == 0) {
085: sortedNameList = new LinkedList();
086: } else {
087: sortedNameList = StringUtils.tokenizeCSV(nameList.trim(),
088: ',');
089: // tokenizeCSV() returns list with one empty string if string is " "
090: }
091: }
092:
093: public boolean moveUp(String toMove) {
094: int oldIndex = sortedNameList.indexOf(toMove);
095: if (oldIndex > 0) {
096: Object o = sortedNameList.remove(oldIndex);
097: sortedNameList.add(oldIndex - 1, o);
098: setNameList(StringUtils.createCSV(sortedNameList, ','));
099: return true;
100: }
101: return false;
102: }
103:
104: public boolean moveDown(String toMove) {
105: int oldIndex = sortedNameList.indexOf(toMove);
106: if (oldIndex != -1 && (oldIndex < sortedNameList.size() - 1)) {
107: Object o = sortedNameList.remove(oldIndex);
108: sortedNameList.add(oldIndex + 1, o);
109: setNameList(StringUtils.createCSV(sortedNameList, ','));
110: return true;
111: }
112: return false;
113: }
114:
115: public boolean moveToTop(String toMove) {
116: int oldIndex = sortedNameList.indexOf(toMove);
117: if (oldIndex > 0) {
118: Object o = sortedNameList.remove(oldIndex);
119: sortedNameList.add(0, o);
120: setNameList(StringUtils.createCSV(sortedNameList, ','));
121: return true;
122: }
123: return false;
124: }
125:
126: public boolean moveToBottom(String toMove) {
127: int oldIndex = sortedNameList.indexOf(toMove);
128: if (oldIndex != -1 && (oldIndex < sortedNameList.size() - 1)) {
129: Object o = sortedNameList.remove(oldIndex);
130: sortedNameList.add(sortedNameList.size(), o);
131: setNameList(StringUtils.createCSV(sortedNameList, ','));
132: return true;
133: }
134: return false;
135: }
136:
137: public int getPosition(String element) {
138: return sortedNameList.indexOf(element);
139: }
140:
141: public boolean setPosition(String toMove, int index) {
142: int oldIndex = sortedNameList.indexOf(toMove);
143: if (index < sortedNameList.size()) {
144: Object o = sortedNameList.remove(oldIndex);
145: sortedNameList.add(index, o);
146: setNameList(StringUtils.createCSV(sortedNameList, ','));
147: return true;
148: }
149: return false;
150: }
151:
152: public Iterator getSortedNames() {
153: return sortedNameList.iterator();
154: }
155:
156: public Iterator getSubcomponentNames() {
157: return sortedNameList.listIterator(0);
158: }
159:
160: /**
161: * overwrites the method of the Folder to allow sorted eval
162: */
163: public Iterator getSubcomponentNames(int start, int amount) {
164: if (amount <= -1) {
165: if (start < sortedNameList.size()) {
166: return sortedNameList.listIterator(start);
167: } else {
168:
169: }
170: } else if (start < sortedNameList.size()) {
171: if (start + amount <= sortedNameList.size()) {
172: return sortedNameList.subList(start, start + amount)
173: .listIterator();
174: } else {
175: return sortedNameList.listIterator(start);
176: }
177: }
178: return Collections.EMPTY_LIST.iterator();
179: }
180:
181: /** appends the name of the given child to the name-list */
182: public boolean subcomponentAdded(CallData callData, Component child) {
183: String childName = child.getComponentContext().getPath()
184: .getName();
185: if (log.isDebugEnabled()) {
186: log.debug("subcomponentAdded()- invoked with child '"
187: + childName + "' in callManager='"
188: + ctx.getCallManager() + "', persistenceManager='"
189: + ctx.getPersistenceManager() + "'");
190: }
191: if (!sortedNameList.contains(childName)) {
192: sortedNameList.add(childName);
193: setNameList(StringUtils.createCSV(sortedNameList, ','));
194: return true;
195: }
196: return false;
197: }
198:
199: public boolean subcomponentRemoved(CallData callData,
200: String childName) {
201: if (log.isDebugEnabled())
202: log
203: .debug("'"
204: + this
205: + "':subcomponentRemoved()- invoked with childName '"
206: + childName + "' ...");
207:
208: if (sortedNameList.remove(childName)) {
209: setNameList(StringUtils.createCSV(sortedNameList, ','));
210: if (log.isDebugEnabled())
211: log
212: .debug("'"
213: + this
214: + "':subcomponentRemoved()- removed childName '"
215: + childName + "'.");
216: return true;
217: }
218: return false;
219: }
220:
221: public boolean subcomponentRemoved(CallData callData,
222: Component child) {
223: String childName = child.getComponentContext().getPath()
224: .getName();
225:
226: if (log.isDebugEnabled())
227: log
228: .debug("'"
229: + this
230: + "':subcomponentRemoved()- invoked with childName '"
231: + childName + "' ...");
232:
233: if (sortedNameList.remove(childName)) {
234: setNameList(StringUtils.createCSV(sortedNameList, ','));
235: if (log.isDebugEnabled())
236: log
237: .debug("'"
238: + this
239: + "':subcomponentRemoved()- removed childName '"
240: + childName + "'.");
241: return true;
242: }
243: return false;
244: }
245:
246: public boolean subcomponentChanged(CallData callData,
247: Component subComponent) throws ModificationVetoException {
248: return false;
249: }
250: }
|