KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > cheatsheets > registry > CheatSheetCollectionElement


1 /*******************************************************************************
2  * Copyright (c) 2002, 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.internal.cheatsheets.registry;
12
13 import java.util.Iterator JavaDoc;
14
15 import org.eclipse.core.runtime.*;
16 import org.eclipse.ui.IPluginContribution;
17 import org.eclipse.ui.internal.cheatsheets.ICheatSheetResource;
18 import org.eclipse.ui.model.AdaptableList;
19 import org.eclipse.ui.model.IWorkbenchAdapter;
20 /**
21  * Instances of this class are a collection of CheatSheetCollectionElements,
22  * thereby facilitating the definition of tree structures composed of
23  * these elements. Instances also store a list of cheatsheets.
24  */

25 public class CheatSheetCollectionElement extends AdaptableList implements IPluginContribution {
26     private String JavaDoc pluginId;
27     private String JavaDoc id;
28     private String JavaDoc name;
29     private CheatSheetCollectionElement parent;
30     private AdaptableList cheatsheets = new AdaptableList();
31
32     /**
33      * Creates a new <code>CheatSheetCollectionElement</code>. Parent can be null.
34      *
35      * @param name java.lang.String
36      */

37     public CheatSheetCollectionElement(String JavaDoc pluginId, String JavaDoc id, String JavaDoc name, CheatSheetCollectionElement parent) {
38         this.name = name;
39         this.pluginId = pluginId;
40         this.id = id;
41         this.parent = parent;
42     }
43
44     /**
45      * Adds a cheatsheet collection to this collection.
46      */

47     public AdaptableList add(IAdaptable a) {
48         if (a instanceof CheatSheetElement) {
49             cheatsheets.add(a);
50         } else {
51             super.add(a);
52         }
53         return this;
54     }
55
56     /**
57      * Returns the cheatsheet collection child object corresponding to the
58      * passed path (relative to this object), or <code>null</code> if
59      * such an object could not be found.
60      *
61      * @param searchPath org.eclipse.core.runtime.IPath
62      * @return CheatSheetCollectionElement
63      */

64     public CheatSheetCollectionElement findChildCollection(IPath searchPath) {
65         Object JavaDoc[] children = getChildren(null);
66         String JavaDoc searchString = searchPath.segment(0);
67         for (int i = 0; i < children.length; ++i) {
68             CheatSheetCollectionElement currentCategory = (CheatSheetCollectionElement) children[i];
69             if (currentCategory.getLabel(null).equals(searchString)) {
70                 if (searchPath.segmentCount() == 1)
71                     return currentCategory;
72
73                 return currentCategory.findChildCollection(searchPath.removeFirstSegments(1));
74             }
75         }
76
77         return null;
78     }
79
80     /**
81      * Returns this collection's associated cheatsheet object corresponding to the
82      * passed id, or <code>null</code> if such an object could not be found.
83      */

84     public CheatSheetElement findCheatSheet(String JavaDoc searchId, boolean recursive) {
85         Object JavaDoc[] cheatsheets = getCheatSheets();
86         for (int i = 0; i < cheatsheets.length; ++i) {
87             CheatSheetElement currentCheatSheet = (CheatSheetElement) cheatsheets[i];
88             if (currentCheatSheet.getID().equals(searchId))
89                 return currentCheatSheet;
90         }
91         if (!recursive)
92             return null;
93         for (Iterator JavaDoc iterator = children.iterator(); iterator.hasNext();) {
94             CheatSheetCollectionElement child = (CheatSheetCollectionElement) iterator.next();
95             CheatSheetElement result = child.findCheatSheet(searchId, true);
96             if (result != null)
97                 return result;
98         }
99         return null;
100     }
101
102     /**
103      * Returns an object which is an instance of the given class
104      * associated with this object. Returns <code>null</code> if
105      * no such object can be found.
106      */

107     public Object JavaDoc getAdapter(Class JavaDoc adapter) {
108         if (adapter == IWorkbenchAdapter.class) {
109             return this;
110         }
111         return Platform.getAdapterManager().getAdapter(this, adapter);
112     }
113
114     /**
115      * Returns the unique ID of this element.
116      */

117     public String JavaDoc getId() {
118         return id;
119     }
120
121     /**
122      * Returns the label for this collection.
123      */

124     public String JavaDoc getLabel(Object JavaDoc o) {
125         return name;
126     }
127
128     /**
129      * Returns the logical parent of the given object in its tree.
130      */

131     public Object JavaDoc getParent(Object JavaDoc o) {
132         return parent;
133     }
134
135     /**
136      * Returns a path representing this collection's ancestor chain.
137      */

138     public IPath getPath() {
139         if (parent == null)
140             return new Path(ICheatSheetResource.EMPTY_STRING);
141
142         return parent.getPath().append(name);
143     }
144
145     /**
146      * Returns this collection element's associated collection of cheatsheets.
147      */

148     public Object JavaDoc[] getCheatSheets() {
149         return cheatsheets.getChildren();
150     }
151
152     /**
153      * Returns true if this element has no children and no cheatsheets.
154      */

155     public boolean isEmpty() {
156         return size() == 0 && cheatsheets.size() == 0;
157     }
158
159     /**
160      * Sets this collection's unique id.
161      */

162     public void setId(java.lang.String JavaDoc newId) {
163         id = newId;
164     }
165
166     /**
167      * Sets the collection of cheatsheets associated with this collection element.
168      */

169     public void setCheatSheets(AdaptableList value) {
170         cheatsheets = value;
171     }
172
173     /**
174      * For debugging purposes.
175      */

176     public String JavaDoc toString() {
177         StringBuffer JavaDoc buf = new StringBuffer JavaDoc("CheatSheetCollection, "); //$NON-NLS-1$
178
buf.append(children.size());
179         buf.append(" children, "); //$NON-NLS-1$
180
buf.append(cheatsheets.size());
181         buf.append(" cheatsheets"); //$NON-NLS-1$
182
return buf.toString();
183     }
184
185     public String JavaDoc getLocalId() {
186         return getId();
187     }
188
189     public String JavaDoc getPluginId() {
190         return pluginId;
191     }
192 }
193
Popular Tags