1 11 package org.eclipse.ui.internal.cheatsheets.registry; 12 13 import java.util.Iterator ; 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 25 public class CheatSheetCollectionElement extends AdaptableList implements IPluginContribution { 26 private String pluginId; 27 private String id; 28 private String name; 29 private CheatSheetCollectionElement parent; 30 private AdaptableList cheatsheets = new AdaptableList(); 31 32 37 public CheatSheetCollectionElement(String pluginId, String id, String name, CheatSheetCollectionElement parent) { 38 this.name = name; 39 this.pluginId = pluginId; 40 this.id = id; 41 this.parent = parent; 42 } 43 44 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 64 public CheatSheetCollectionElement findChildCollection(IPath searchPath) { 65 Object [] children = getChildren(null); 66 String 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 84 public CheatSheetElement findCheatSheet(String searchId, boolean recursive) { 85 Object [] 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 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 107 public Object getAdapter(Class adapter) { 108 if (adapter == IWorkbenchAdapter.class) { 109 return this; 110 } 111 return Platform.getAdapterManager().getAdapter(this, adapter); 112 } 113 114 117 public String getId() { 118 return id; 119 } 120 121 124 public String getLabel(Object o) { 125 return name; 126 } 127 128 131 public Object getParent(Object o) { 132 return parent; 133 } 134 135 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 148 public Object [] getCheatSheets() { 149 return cheatsheets.getChildren(); 150 } 151 152 155 public boolean isEmpty() { 156 return size() == 0 && cheatsheets.size() == 0; 157 } 158 159 162 public void setId(java.lang.String newId) { 163 id = newId; 164 } 165 166 169 public void setCheatSheets(AdaptableList value) { 170 cheatsheets = value; 171 } 172 173 176 public String toString() { 177 StringBuffer buf = new StringBuffer ("CheatSheetCollection, "); buf.append(children.size()); 179 buf.append(" children, "); buf.append(cheatsheets.size()); 181 buf.append(" cheatsheets"); return buf.toString(); 183 } 184 185 public String getLocalId() { 186 return getId(); 187 } 188 189 public String getPluginId() { 190 return pluginId; 191 } 192 } 193 | Popular Tags |