001: /*******************************************************************************
002: * Copyright (c) 2000, 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.examples.readmetool;
011:
012: import java.util.ArrayList;
013: import java.util.Iterator;
014: import java.util.List;
015:
016: import org.eclipse.core.runtime.IAdaptable;
017: import org.eclipse.jface.resource.ImageDescriptor;
018: import org.eclipse.ui.model.IWorkbenchAdapter;
019:
020: /**
021: * A list of adaptable objects. This is a generic list that can
022: * be used to display an arbitrary set of adaptable objects in the workbench.
023: * Also implements the IWorkbenchAdapter interface for simple display
024: * and navigation.
025: */
026: public class AdaptableList implements IWorkbenchAdapter, IAdaptable {
027: protected List children = new ArrayList();
028:
029: /**
030: * Creates a new adaptable list with the given children.
031: */
032: public AdaptableList() {
033: // do nothing
034: }
035:
036: /**
037: * Creates a new adaptable list with the given children.
038: */
039: public AdaptableList(IAdaptable[] newChildren) {
040: for (int i = 0; i < newChildren.length; i++) {
041: children.add(newChildren[i]);
042: }
043: }
044:
045: /**
046: * Adds all the adaptable objects in the given enumeration to this list.
047: * Returns this list.
048: */
049: public AdaptableList add(Iterator iterator) {
050: while (iterator.hasNext()) {
051: add((IAdaptable) iterator.next());
052: }
053: return this ;
054: }
055:
056: /**
057: * Adds the given adaptable object to this list.
058: * Returns this list.
059: */
060: public AdaptableList add(IAdaptable adaptable) {
061: children.add(adaptable);
062: return this ;
063: }
064:
065: /* (non-Javadoc)
066: * Method declared on IAdaptable
067: */
068: public Object getAdapter(Class adapter) {
069: if (adapter == IWorkbenchAdapter.class)
070: return this ;
071: return null;
072: }
073:
074: /**
075: * Returns the elements in this list.
076: */
077: public Object[] getChildren() {
078: return children.toArray();
079: }
080:
081: /* (non-Javadoc)
082: * Method declared on IWorkbenchAdapter
083: */
084: public Object[] getChildren(Object o) {
085: return children.toArray();
086: }
087:
088: /* (non-Javadoc)
089: * Method declared on IWorkbenchAdapter
090: */
091: public ImageDescriptor getImageDescriptor(Object object) {
092: return null;
093: }
094:
095: /* (non-Javadoc)
096: * Method declared on IWorkbenchAdapter
097: */
098: public String getLabel(Object object) {
099: return object == null ? "" : object.toString(); //$NON-NLS-1$
100: }
101:
102: /* (non-Javadoc)
103: * Method declared on IWorkbenchAdapter
104: */
105: public Object getParent(Object object) {
106: return null;
107: }
108:
109: /**
110: * Removes the given adaptable object from this list.
111: */
112: public void remove(IAdaptable adaptable) {
113: children.remove(adaptable);
114: }
115:
116: /**
117: * Returns the number of items in the list
118: */
119: public int size() {
120: return children.size();
121: }
122: }
|