1 11 package org.eclipse.ui.internal.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.ISharedImages; 21 import org.eclipse.ui.WorkbenchException; 22 import org.eclipse.ui.internal.WorkbenchImages; 23 import org.eclipse.ui.internal.WorkbenchMessages; 24 import org.eclipse.ui.model.IWorkbenchAdapter; 25 26 35 public class Category implements IWorkbenchAdapter, IPluginContribution, IAdaptable { 36 39 public final static String MISC_NAME = WorkbenchMessages.ICategory_other; 40 41 44 public final static String MISC_ID = "org.eclipse.ui.internal.otherCategory"; 46 private String id; 47 48 private String name; 49 50 private String [] parentPath; 51 52 private ArrayList elements; 53 54 private IConfigurationElement configurationElement; 55 56 private String pluginId; 57 58 62 public Category() { 63 this.id = MISC_ID; 64 this.name = MISC_NAME; 65 this.pluginId = MISC_ID; } 67 68 75 public Category(String id, String label) { 76 this.id = id; 77 this.name = label; 78 } 79 80 88 public Category(IConfigurationElement configElement) 89 throws WorkbenchException { 90 id = configElement.getAttribute(IWorkbenchRegistryConstants.ATT_ID); 91 92 configurationElement = configElement; 93 if (id == null || getLabel() == null) { 94 throw new WorkbenchException("Invalid category: " + id); } 96 } 97 98 99 104 public void addElement(Object element) { 105 if (elements == null) { 106 elements = new ArrayList (5); 107 } 108 elements.add(element); 109 } 110 111 114 public Object getAdapter(Class adapter) { 115 if (adapter == IWorkbenchAdapter.class) { 116 return this; 117 } else if (adapter == IConfigurationElement.class) { 118 return configurationElement; 119 } else { 120 return null; 121 } 122 } 123 124 127 public Object [] getChildren(Object o) { 128 return getElements().toArray(); 129 } 130 131 134 public ImageDescriptor getImageDescriptor(Object object) { 135 return WorkbenchImages.getImageDescriptor(ISharedImages.IMG_OBJ_FOLDER); 136 } 137 138 141 public String getLabel(Object o) { 142 return getLabel(); 143 } 144 145 149 public String getId() { 150 return id; 151 } 152 153 158 public String getLabel() { 159 return configurationElement == null ? name : configurationElement 160 .getAttribute(IWorkbenchRegistryConstants.ATT_NAME); 161 } 162 163 168 public String [] getParentPath() { 169 if (parentPath != null) { 170 return parentPath; 171 } 172 173 String unparsedPath = getRawParentPath(); 174 if (unparsedPath != null) { 175 StringTokenizer stok = new StringTokenizer (unparsedPath, "/"); parentPath = new String [stok.countTokens()]; 177 for (int i = 0; stok.hasMoreTokens(); i++) { 178 parentPath[i] = stok.nextToken(); 179 } 180 } 181 182 return parentPath; 183 } 184 185 190 public String getRawParentPath() { 191 return configurationElement == null ? null 192 : configurationElement.getAttribute(IWorkbenchRegistryConstants.ATT_PARENT_CATEGORY); 193 } 194 195 200 public String getRootPath() { 201 String [] path = getParentPath(); 202 if (path != null && path.length > 0) { 203 return path[0]; 204 } 205 206 return id; 207 } 208 209 214 public ArrayList getElements() { 215 return elements; 216 } 217 218 224 public boolean hasElement(Object o) { 225 if (elements == null) { 226 return false; 227 } 228 if (elements.isEmpty()) { 229 return false; 230 } 231 return elements.contains(o); 232 } 233 234 239 public boolean hasElements() { 240 if (elements != null) { 241 return !elements.isEmpty(); 242 } 243 244 return false; 245 } 246 247 250 public Object getParent(Object o) { 251 return null; 252 } 253 254 257 public String getLocalId() { 258 return id; 259 } 260 261 264 public String getPluginId() { 265 return configurationElement == null ? pluginId : configurationElement 266 .getNamespace(); 267 } 268 269 274 public void clear() { 275 if (elements != null) { 276 elements.clear(); 277 } 278 } 279 } 280 | Popular Tags |