1 11 package org.eclipse.ui.internal.registry; 12 13 import java.util.ArrayList ; 14 import java.util.Comparator ; 15 import java.util.Iterator ; 16 import java.util.List ; 17 import java.util.SortedSet ; 18 import java.util.TreeSet ; 19 20 import org.eclipse.core.runtime.IConfigurationElement; 21 import org.eclipse.core.runtime.IExtension; 22 import org.eclipse.core.runtime.IExtensionPoint; 23 import org.eclipse.core.runtime.IPath; 24 import org.eclipse.core.runtime.Path; 25 import org.eclipse.core.runtime.Platform; 26 import org.eclipse.core.runtime.dynamichelpers.ExtensionTracker; 27 import org.eclipse.core.runtime.dynamichelpers.IExtensionChangeHandler; 28 import org.eclipse.core.runtime.dynamichelpers.IExtensionTracker; 29 import org.eclipse.ui.IPluginContribution; 30 import org.eclipse.ui.PlatformUI; 31 import org.eclipse.ui.internal.WorkbenchPlugin; 32 import org.eclipse.ui.internal.util.Util; 33 import org.eclipse.ui.views.IStickyViewDescriptor; 34 import org.eclipse.ui.views.IViewCategory; 35 import org.eclipse.ui.views.IViewDescriptor; 36 import org.eclipse.ui.views.IViewRegistry; 37 38 import com.ibm.icu.text.MessageFormat; 39 40 43 public class ViewRegistry implements IViewRegistry, IExtensionChangeHandler { 44 45 50 private static class ViewCategoryProxy implements IViewCategory, IPluginContribution { 51 52 private Category rawCategory; 53 54 59 public ViewCategoryProxy(Category rawCategory) { 60 this.rawCategory = rawCategory; 61 } 62 63 66 public IViewDescriptor[] getViews() { 67 ArrayList elements = rawCategory.getElements(); 68 if (elements == null) { 69 return new IViewDescriptor[0]; 70 } 71 return (IViewDescriptor[]) elements 72 .toArray( 73 new IViewDescriptor[elements.size()]); 74 } 75 76 79 public String getId() { 80 return rawCategory.getId(); 81 } 82 83 86 public IPath getPath() { 87 String rawParentPath = rawCategory.getRawParentPath(); 88 if (rawParentPath == null) { 89 return new Path(""); } 91 return new Path(rawParentPath); 92 } 93 94 97 public String getLabel() { 98 return rawCategory.getLabel(); 99 } 100 101 104 public String getLocalId() { 105 return getId(); 106 } 107 108 111 public String getPluginId() { 112 return rawCategory.getPluginId(); 113 } 114 115 118 public boolean equals(Object o) { 119 if (o instanceof IViewCategory) { 120 return getId().equals(((IViewCategory)o).getId()); 121 } 122 return false; 123 } 124 125 128 public int hashCode() { 129 return getId().hashCode(); 130 } 131 } 132 133 private static String EXTENSIONPOINT_UNIQUE_ID = WorkbenchPlugin.PI_WORKBENCH + "." + IWorkbenchRegistryConstants.PL_VIEWS; 135 138 private SortedSet views = new TreeSet (new Comparator () { 139 public int compare(Object o1, Object o2) { 140 String id1 = ((ViewDescriptor) o1).getId(); 141 String id2 = ((ViewDescriptor) o2).getId(); 142 143 return id1.compareTo(id2); 144 }}); 145 146 private List categories; 147 148 private List sticky; 149 150 private Category miscCategory; 151 152 protected static final String TAG_DESCRIPTION = "description"; 154 private ViewRegistryReader reader = new ViewRegistryReader(); 155 156 private boolean dirtyViewCategoryMappings = true; 157 158 161 public ViewRegistry() { 162 super(); 163 categories = new ArrayList (); 164 sticky = new ArrayList (); 165 PlatformUI.getWorkbench().getExtensionTracker().registerHandler(this, ExtensionTracker.createExtensionPointFilter(getExtensionPointFilter())); 166 reader.readViews(Platform.getExtensionRegistry(), this); 167 } 168 169 174 public void add(Category desc) { 175 176 if (internalFindCategory(desc.getId()) == null) { 177 dirtyViewCategoryMappings = true; 178 categories.add(desc); 180 IConfigurationElement element = (IConfigurationElement) Util.getAdapter(desc, IConfigurationElement.class); 181 if (element == null) { 182 return; 183 } 184 PlatformUI.getWorkbench().getExtensionTracker() 185 .registerObject( 186 element.getDeclaringExtension(), 187 desc, 188 IExtensionTracker.REF_WEAK); 189 } 190 } 191 192 197 public void add(ViewDescriptor desc) { 198 if (views.add(desc)) { 199 dirtyViewCategoryMappings = true; 200 PlatformUI.getWorkbench().getExtensionTracker().registerObject( 201 desc.getConfigurationElement().getDeclaringExtension(), 202 desc, IExtensionTracker.REF_WEAK); 203 desc.activateHandler(); 204 } 205 } 206 207 212 public void add(StickyViewDescriptor desc) { 213 if (!sticky.contains(desc)) { 214 sticky.add(desc); 215 PlatformUI.getWorkbench().getExtensionTracker() 216 .registerObject( 217 desc.getConfigurationElement().getDeclaringExtension(), 218 desc, 219 IExtensionTracker.REF_WEAK); 220 } 221 } 222 223 238 241 public IViewDescriptor find(String id) { 242 Iterator itr = views.iterator(); 243 while (itr.hasNext()) { 244 IViewDescriptor desc = (IViewDescriptor) itr.next(); 245 if (id.equals(desc.getId())) { 246 return desc; 247 } 248 } 249 return null; 250 } 251 252 258 public IViewCategory findCategory(String id) { 259 mapViewsToCategories(); 260 Category category = internalFindCategory(id); 261 if (category == null) { 262 return null; 263 } 264 return new ViewCategoryProxy(category); 265 } 266 267 274 private Category internalFindCategory(String id) { 275 Iterator itr = categories.iterator(); 276 while (itr.hasNext()) { 277 Category cat = (Category) itr.next(); 278 if (id.equals(cat.getRootPath())) { 279 return cat; 280 } 281 } 282 return null; 283 } 284 285 288 public IViewCategory[] getCategories() { 289 mapViewsToCategories(); 290 int nSize = categories.size(); 291 IViewCategory[] retArray = new IViewCategory[nSize]; 292 int i = 0; 293 for (Iterator itr = categories.iterator(); itr.hasNext();) { 294 retArray[i++] = new ViewCategoryProxy((Category) itr.next()); 295 } 296 return retArray; 297 } 298 299 302 public IStickyViewDescriptor[] getStickyViews() { 303 return (IStickyViewDescriptor[]) sticky 304 .toArray(new IStickyViewDescriptor[sticky.size()]); 305 } 306 307 313 public Category getMiscCategory() { 314 return miscCategory; 315 } 316 317 320 public IViewDescriptor[] getViews() { 321 return (IViewDescriptor []) views.toArray(new IViewDescriptor [views.size()]); 322 } 323 324 329 public void mapViewsToCategories() { 330 if (dirtyViewCategoryMappings) { 331 dirtyViewCategoryMappings = false; 332 for (Iterator i = categories.iterator(); i.hasNext(); ) { 334 Category category = (Category) i.next(); 335 category.clear(); } 337 338 if (miscCategory != null) { 339 miscCategory.clear(); 340 } 341 342 for (Iterator i = views.iterator(); i.hasNext(); ) { 343 IViewDescriptor desc = (IViewDescriptor) i.next(); 344 Category cat = null; 345 String [] catPath = desc.getCategoryPath(); 346 if (catPath != null) { 347 String rootCat = catPath[0]; 348 cat = internalFindCategory(rootCat); 349 } 350 if (cat != null) { 351 if (!cat.hasElement(desc)) { 352 cat.addElement(desc); 353 } 354 } else { 355 if (miscCategory == null) { 356 miscCategory = new Category(); 357 add(miscCategory); 358 } 359 if (catPath != null) { 360 String fmt = "Category {0} not found for view {1}. This view added to ''{2}'' category."; WorkbenchPlugin.log(MessageFormat 366 .format(fmt, new Object [] { catPath[0], 367 desc.getId(), miscCategory.getLabel() })); 368 } 369 miscCategory.addElement(desc); 370 } 371 } 372 } 373 } 374 375 378 public void dispose() { 379 PlatformUI.getWorkbench().getExtensionTracker().unregisterHandler(this); 380 } 381 382 385 public void removeExtension(IExtension extension,Object [] objects) { 386 for (int i = 0; i < objects.length; i++) { 387 if (objects[i] instanceof StickyViewDescriptor) { 388 sticky.remove(objects[i]); 389 } 390 else if (objects[i] instanceof ViewDescriptor) { 391 views.remove(objects[i]); 392 ((ViewDescriptor) objects[i]).deactivateHandler(); 393 dirtyViewCategoryMappings = true; 394 } 395 else if (objects[i] instanceof Category) { 396 categories.remove(objects[i]); 397 dirtyViewCategoryMappings = true; 398 } 399 } 400 401 } 402 403 private IExtensionPoint getExtensionPointFilter() { 404 return Platform.getExtensionRegistry().getExtensionPoint(EXTENSIONPOINT_UNIQUE_ID); 405 } 406 407 410 public void addExtension(IExtensionTracker tracker,IExtension addedExtension){ 411 IConfigurationElement[] addedElements = addedExtension.getConfigurationElements(); 412 for (int i = 0; i < addedElements.length; i++) { 413 IConfigurationElement element = addedElements[i]; 414 if (element.getName().equals(IWorkbenchRegistryConstants.TAG_VIEW)) { 415 reader.readView(element); 416 } else if (element.getName().equals(IWorkbenchRegistryConstants.TAG_CATEGORY)) { 417 reader.readCategory(element); 418 } else if (element.getName().equals(IWorkbenchRegistryConstants.TAG_STICKYVIEW)) { 419 reader.readSticky(element); 420 } 421 } 422 } 423 } 424 | Popular Tags |