1 11 package org.eclipse.ui.internal.cheatsheets.registry; 12 13 import java.util.ArrayList ; 14 import java.util.StringTokenizer ; 15 16 import org.eclipse.core.runtime.IAdaptable; 17 import org.eclipse.core.runtime.IConfigurationElement; 18 import org.eclipse.jface.resource.ImageDescriptor; 19 import org.eclipse.ui.IPluginContribution; 20 import org.eclipse.ui.WorkbenchException; 21 import org.eclipse.ui.internal.cheatsheets.Messages; 22 import org.eclipse.ui.model.IWorkbenchAdapter; 23 24 32 public class Category implements IWorkbenchAdapter, IPluginContribution, 33 IAdaptable { 34 37 public final static String MISC_NAME = Messages.CATEGORY_OTHER; 38 39 41 public final static String MISC_ID = "org.eclipse.ui.cheatsheets.otherCategory"; 43 private String id; 44 45 private String name; 46 47 private String [] parentPath; 48 49 private ArrayList elements; 50 51 private IConfigurationElement configurationElement; 52 53 private String pluginId; 54 55 59 public Category() { 60 this.id = MISC_ID; 61 this.name = MISC_NAME; 62 this.pluginId = MISC_ID; } 64 65 73 public Category(String id, String label) { 74 this.id = id; 75 this.name = label; 76 } 77 78 87 public Category(IConfigurationElement configElement) 88 throws WorkbenchException { 89 id = configElement.getAttribute("id"); 91 configurationElement = configElement; 92 if (id == null || getLabel() == null) 93 throw new WorkbenchException("Invalid category: " + id); } 95 96 102 public void addElement(Object element) { 103 if (elements == null) 104 elements = new ArrayList (5); 105 elements.add(element); 106 } 107 108 111 public Object getAdapter(Class adapter) { 112 if (adapter == IWorkbenchAdapter.class) 113 return this; 114 else if (adapter == IConfigurationElement.class) 115 return configurationElement; 116 else 117 return null; 118 } 119 120 123 public Object [] getChildren(Object o) { 124 return getElements().toArray(); 125 } 126 127 130 public ImageDescriptor getImageDescriptor(Object object) { 131 return null; 132 } 133 134 137 public String getLabel(Object o) { 138 return getLabel(); 139 } 140 141 146 public String getId() { 147 return id; 148 } 149 150 155 public String getLabel() { 156 return configurationElement == null ? name : configurationElement 157 .getAttribute("name"); } 159 160 165 public String [] getParentPath() { 166 if (parentPath != null) 167 return parentPath; 168 169 String unparsedPath = getRawParentPath(); 170 if (unparsedPath != null) { 171 StringTokenizer stok = new StringTokenizer (unparsedPath, "/"); parentPath = new String [stok.countTokens()]; 173 for (int i = 0; stok.hasMoreTokens(); i++) { 174 parentPath[i] = stok.nextToken(); 175 } 176 } 177 178 return parentPath; 179 } 180 181 186 public String getRawParentPath() { 187 return configurationElement == null ? null : configurationElement 188 .getAttribute("parentCategory"); } 190 191 196 public String getRootPath() { 197 String [] path = getParentPath(); 198 if (path != null && path.length > 0) 199 return path[0]; 200 201 return id; 202 } 203 204 209 public ArrayList getElements() { 210 return elements; 211 } 212 213 220 public boolean hasElement(Object o) { 221 if (elements == null) 222 return false; 223 if (elements.isEmpty()) 224 return false; 225 return elements.contains(o); 226 } 227 228 233 public boolean hasElements() { 234 if (elements != null) 235 return !elements.isEmpty(); 236 237 return false; 238 } 239 240 245 public Object getParent(Object o) { 246 return null; 247 } 248 249 254 public String getLocalId() { 255 return id; 256 } 257 258 263 public String getPluginId() { 264 return configurationElement == null ? pluginId : configurationElement 265 .getContributor().getName(); 266 } 267 268 273 public void clear() { 274 if (elements != null) { 275 elements.clear(); 276 } 277 } 278 } 279 | Popular Tags |