KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > model > AdaptableList


1 /*******************************************************************************
2  * Copyright (c) 2003, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.ui.model;
12
13 import java.lang.reflect.Array JavaDoc;
14 import java.util.ArrayList JavaDoc;
15 import java.util.Collection JavaDoc;
16 import java.util.List JavaDoc;
17
18 import org.eclipse.core.runtime.Assert;
19 import org.eclipse.core.runtime.IAdaptable;
20
21 /**
22  * A modifiable list of <code>IAdaptable</code> objects.
23  * The list is adaptable to <code>IWorkbenchAdapter</code>, and can be used to
24  * display an arbitrary set of adaptable objects in a viewer.
25  * <p>
26  * This class is not intended to be subclassed.
27  * </p>
28  *
29  * @since 3.0
30  * @see org.eclipse.ui.model.IWorkbenchAdapter
31  */

32 public class AdaptableList extends WorkbenchAdapter implements IAdaptable {
33
34     protected List JavaDoc children = null;
35
36     /**
37      * Creates a new adaptable list. All of the elements in the list must
38      * implement <code>IAdaptable</code>.
39      */

40     public AdaptableList() {
41         children = new ArrayList JavaDoc();
42     }
43
44     /**
45      * Creates a new adaptable list with the given initial capacity.
46      * All of the elements in the list must implement <code>IAdaptable</code>.
47      *
48      * @param initialCapacity the initial capacity of the list
49      */

50     public AdaptableList(int initialCapacity) {
51         children = new ArrayList JavaDoc(initialCapacity);
52     }
53
54     /**
55      * Creates a new adaptable list containing the given children.
56      *
57      * @param newChildren the list of children
58      */

59     public AdaptableList(IAdaptable[] newChildren) {
60         this(newChildren.length);
61         for (int i = 0; i < newChildren.length; i++) {
62             children.add(newChildren[i]);
63         }
64     }
65
66     /**
67      * Creates a new adaptable list containing the elements of the specified
68      * collection, in the order they are returned by the collection's iterator.
69      * All of the elements in the list must implement <code>IAdaptable</code>.
70      *
71      * @param c the initial elements of this list (element type:
72      * <code>IAdaptable</code>)
73      */

74     public AdaptableList(Collection JavaDoc c) {
75         this(c.size());
76         children.addAll(c);
77     }
78
79     /**
80      * Adds the given adaptable object to this list.
81      *
82      * @param adaptable the new element
83      * @return this list
84      */

85     public AdaptableList add(IAdaptable adaptable) {
86         Assert.isNotNull(adaptable);
87         children.add(adaptable);
88         return this;
89     }
90
91     /**
92      * Removes the given adaptable object from this list.
93      *
94      * @param adaptable the element to remove
95      */

96     public void remove(IAdaptable adaptable) {
97         Assert.isNotNull(adaptable);
98         children.remove(adaptable);
99     }
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 JavaDoc getAdapter(Class JavaDoc 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 JavaDoc[] getChildren(Object JavaDoc 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 JavaDoc[] 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 JavaDoc[] getTypedChildren(Class JavaDoc type) {
145         return children.toArray((Object JavaDoc[]) Array.newInstance(type, children
146                 .size()));
147     }
148
149     /* (non-javadoc)
150      * For debugging purposes only.
151      */

152     public String JavaDoc toString() {
153         return children.toString();
154     }
155 }
156
Popular Tags