001: /*******************************************************************************
002: * Copyright (c) 2003, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.model;
011:
012: import java.lang.reflect.Array;
013: import java.util.ArrayList;
014: import java.util.Collection;
015: import java.util.List;
016:
017: import org.eclipse.core.runtime.Assert;
018: import org.eclipse.core.runtime.IAdaptable;
019:
020: /**
021: * A modifiable list of <code>IAdaptable</code> objects.
022: * The list is adaptable to <code>IWorkbenchAdapter</code>, and can be used to
023: * display an arbitrary set of adaptable objects in a viewer.
024: * <p>
025: * This class is not intended to be subclassed.
026: * </p>
027: *
028: * @since 3.0
029: * @see org.eclipse.ui.model.IWorkbenchAdapter
030: */
031: public class AdaptableList extends WorkbenchAdapter implements
032: IAdaptable {
033:
034: protected List children = null;
035:
036: /**
037: * Creates a new adaptable list. All of the elements in the list must
038: * implement <code>IAdaptable</code>.
039: */
040: public AdaptableList() {
041: children = new ArrayList();
042: }
043:
044: /**
045: * Creates a new adaptable list with the given initial capacity.
046: * All of the elements in the list must implement <code>IAdaptable</code>.
047: *
048: * @param initialCapacity the initial capacity of the list
049: */
050: public AdaptableList(int initialCapacity) {
051: children = new ArrayList(initialCapacity);
052: }
053:
054: /**
055: * Creates a new adaptable list containing the given children.
056: *
057: * @param newChildren the list of children
058: */
059: public AdaptableList(IAdaptable[] newChildren) {
060: this (newChildren.length);
061: for (int i = 0; i < newChildren.length; i++) {
062: children.add(newChildren[i]);
063: }
064: }
065:
066: /**
067: * Creates a new adaptable list containing the elements of the specified
068: * collection, in the order they are returned by the collection's iterator.
069: * All of the elements in the list must implement <code>IAdaptable</code>.
070: *
071: * @param c the initial elements of this list (element type:
072: * <code>IAdaptable</code>)
073: */
074: public AdaptableList(Collection c) {
075: this (c.size());
076: children.addAll(c);
077: }
078:
079: /**
080: * Adds the given adaptable object to this list.
081: *
082: * @param adaptable the new element
083: * @return this list
084: */
085: public AdaptableList add(IAdaptable adaptable) {
086: Assert.isNotNull(adaptable);
087: children.add(adaptable);
088: return this ;
089: }
090:
091: /**
092: * Removes the given adaptable object from this list.
093: *
094: * @param adaptable the element to remove
095: */
096: public void remove(IAdaptable adaptable) {
097: Assert.isNotNull(adaptable);
098: children.remove(adaptable);
099: }
100:
101: /**
102: * Returns the number of children in this list.
103: *
104: * @return the length of this list
105: */
106: public int size() {
107: return children.size();
108: }
109:
110: /* (non-Javadoc)
111: * @see IAdaptable#getAdapter
112: */
113: public Object getAdapter(Class adapter) {
114: if (adapter == IWorkbenchAdapter.class) {
115: return this ;
116: }
117: return null;
118: }
119:
120: /* (non-Javadoc)
121: * @see IWorkbenchAdapter
122: */
123: public Object[] getChildren(Object o) {
124: // @issue suspicious - does not reference parameter
125: return children.toArray();
126: }
127:
128: /**
129: * Returns the elements in this list.
130: *
131: * @return the elements in this list
132: */
133: public Object[] getChildren() {
134: return children.toArray();
135: }
136:
137: /**
138: * Return the elements in this list in an array of the given type.
139: *
140: * @param type the type of the array to create
141: * @return the elements in the list
142: * @since 3.1
143: */
144: public Object[] getTypedChildren(Class type) {
145: return children.toArray((Object[]) Array.newInstance(type,
146: children.size()));
147: }
148:
149: /* (non-javadoc)
150: * For debugging purposes only.
151: */
152: public String toString() {
153: return children.toString();
154: }
155: }
|