001: /*******************************************************************************
002: * Copyright (c) 2002, 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.internal.cheatsheets.registry;
011:
012: import java.util.Iterator;
013:
014: import org.eclipse.core.runtime.*;
015: import org.eclipse.ui.IPluginContribution;
016: import org.eclipse.ui.internal.cheatsheets.ICheatSheetResource;
017: import org.eclipse.ui.model.AdaptableList;
018: import org.eclipse.ui.model.IWorkbenchAdapter;
019:
020: /**
021: * Instances of this class are a collection of CheatSheetCollectionElements,
022: * thereby facilitating the definition of tree structures composed of
023: * these elements. Instances also store a list of cheatsheets.
024: */
025: public class CheatSheetCollectionElement extends AdaptableList
026: implements IPluginContribution {
027: private String pluginId;
028: private String id;
029: private String name;
030: private CheatSheetCollectionElement parent;
031: private AdaptableList cheatsheets = new AdaptableList();
032:
033: /**
034: * Creates a new <code>CheatSheetCollectionElement</code>. Parent can be null.
035: *
036: * @param name java.lang.String
037: */
038: public CheatSheetCollectionElement(String pluginId, String id,
039: String name, CheatSheetCollectionElement parent) {
040: this .name = name;
041: this .pluginId = pluginId;
042: this .id = id;
043: this .parent = parent;
044: }
045:
046: /**
047: * Adds a cheatsheet collection to this collection.
048: */
049: public AdaptableList add(IAdaptable a) {
050: if (a instanceof CheatSheetElement) {
051: cheatsheets.add(a);
052: } else {
053: super .add(a);
054: }
055: return this ;
056: }
057:
058: /**
059: * Returns the cheatsheet collection child object corresponding to the
060: * passed path (relative to this object), or <code>null</code> if
061: * such an object could not be found.
062: *
063: * @param searchPath org.eclipse.core.runtime.IPath
064: * @return CheatSheetCollectionElement
065: */
066: public CheatSheetCollectionElement findChildCollection(
067: IPath searchPath) {
068: Object[] children = getChildren(null);
069: String searchString = searchPath.segment(0);
070: for (int i = 0; i < children.length; ++i) {
071: CheatSheetCollectionElement currentCategory = (CheatSheetCollectionElement) children[i];
072: if (currentCategory.getLabel(null).equals(searchString)) {
073: if (searchPath.segmentCount() == 1)
074: return currentCategory;
075:
076: return currentCategory.findChildCollection(searchPath
077: .removeFirstSegments(1));
078: }
079: }
080:
081: return null;
082: }
083:
084: /**
085: * Returns this collection's associated cheatsheet object corresponding to the
086: * passed id, or <code>null</code> if such an object could not be found.
087: */
088: public CheatSheetElement findCheatSheet(String searchId,
089: boolean recursive) {
090: Object[] cheatsheets = getCheatSheets();
091: for (int i = 0; i < cheatsheets.length; ++i) {
092: CheatSheetElement currentCheatSheet = (CheatSheetElement) cheatsheets[i];
093: if (currentCheatSheet.getID().equals(searchId))
094: return currentCheatSheet;
095: }
096: if (!recursive)
097: return null;
098: for (Iterator iterator = children.iterator(); iterator
099: .hasNext();) {
100: CheatSheetCollectionElement child = (CheatSheetCollectionElement) iterator
101: .next();
102: CheatSheetElement result = child.findCheatSheet(searchId,
103: true);
104: if (result != null)
105: return result;
106: }
107: return null;
108: }
109:
110: /**
111: * Returns an object which is an instance of the given class
112: * associated with this object. Returns <code>null</code> if
113: * no such object can be found.
114: */
115: public Object getAdapter(Class adapter) {
116: if (adapter == IWorkbenchAdapter.class) {
117: return this ;
118: }
119: return Platform.getAdapterManager().getAdapter(this , adapter);
120: }
121:
122: /**
123: * Returns the unique ID of this element.
124: */
125: public String getId() {
126: return id;
127: }
128:
129: /**
130: * Returns the label for this collection.
131: */
132: public String getLabel(Object o) {
133: return name;
134: }
135:
136: /**
137: * Returns the logical parent of the given object in its tree.
138: */
139: public Object getParent(Object o) {
140: return parent;
141: }
142:
143: /**
144: * Returns a path representing this collection's ancestor chain.
145: */
146: public IPath getPath() {
147: if (parent == null)
148: return new Path(ICheatSheetResource.EMPTY_STRING);
149:
150: return parent.getPath().append(name);
151: }
152:
153: /**
154: * Returns this collection element's associated collection of cheatsheets.
155: */
156: public Object[] getCheatSheets() {
157: return cheatsheets.getChildren();
158: }
159:
160: /**
161: * Returns true if this element has no children and no cheatsheets.
162: */
163: public boolean isEmpty() {
164: return size() == 0 && cheatsheets.size() == 0;
165: }
166:
167: /**
168: * Sets this collection's unique id.
169: */
170: public void setId(java.lang.String newId) {
171: id = newId;
172: }
173:
174: /**
175: * Sets the collection of cheatsheets associated with this collection element.
176: */
177: public void setCheatSheets(AdaptableList value) {
178: cheatsheets = value;
179: }
180:
181: /**
182: * For debugging purposes.
183: */
184: public String toString() {
185: StringBuffer buf = new StringBuffer("CheatSheetCollection, "); //$NON-NLS-1$
186: buf.append(children.size());
187: buf.append(" children, "); //$NON-NLS-1$
188: buf.append(cheatsheets.size());
189: buf.append(" cheatsheets"); //$NON-NLS-1$
190: return buf.toString();
191: }
192:
193: public String getLocalId() {
194: return getId();
195: }
196:
197: public String getPluginId() {
198: return pluginId;
199: }
200: }
|