KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > registry > ViewRegistry


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.ui.internal.registry;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Comparator JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.List JavaDoc;
17 import java.util.SortedSet JavaDoc;
18 import java.util.TreeSet JavaDoc;
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 /**
41  * The central manager for view descriptors.
42  */

43 public class ViewRegistry implements IViewRegistry, IExtensionChangeHandler {
44     
45     /**
46      * Proxies a Category implementation.
47      *
48      * @since 3.1
49      */

50     private static class ViewCategoryProxy implements IViewCategory, IPluginContribution {
51
52         private Category rawCategory;
53
54         /**
55          * Create a new instance of this class
56          *
57          * @param rawCategory the category
58          */

59         public ViewCategoryProxy(Category rawCategory) {
60             this.rawCategory = rawCategory;
61         }
62         
63         /* (non-Javadoc)
64          * @see org.eclipse.ui.views.IViewCategory#getViews()
65          */

66         public IViewDescriptor[] getViews() {
67             ArrayList JavaDoc 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         /* (non-Javadoc)
77          * @see org.eclipse.ui.views.IViewCategory#getId()
78          */

79         public String JavaDoc getId() {
80             return rawCategory.getId();
81         }
82
83         /* (non-Javadoc)
84          * @see org.eclipse.ui.views.IViewCategory#getPath()
85          */

86         public IPath getPath() {
87             String JavaDoc rawParentPath = rawCategory.getRawParentPath();
88             if (rawParentPath == null) {
89                 return new Path(""); //$NON-NLS-1$
90
}
91             return new Path(rawParentPath);
92         }
93
94         /* (non-Javadoc)
95          * @see org.eclipse.ui.views.IViewCategory#getLabel()
96          */

97         public String JavaDoc getLabel() {
98             return rawCategory.getLabel();
99         }
100
101         /* (non-Javadoc)
102          * @see org.eclipse.ui.IPluginContribution#getLocalId()
103          */

104         public String JavaDoc getLocalId() {
105             return getId();
106         }
107
108         /* (non-Javadoc)
109          * @see org.eclipse.ui.IPluginContribution#getPluginId()
110          */

111         public String JavaDoc getPluginId() {
112             return rawCategory.getPluginId();
113         }
114         
115         /* (non-Javadoc)
116          * @see java.lang.Object#equals(java.lang.Object)
117          */

118         public boolean equals(Object JavaDoc o) {
119             if (o instanceof IViewCategory) {
120                 return getId().equals(((IViewCategory)o).getId());
121             }
122             return false;
123         }
124         
125         /* (non-Javadoc)
126          * @see java.lang.Object#hashCode()
127          */

128         public int hashCode() {
129             return getId().hashCode();
130         }
131     }
132     
133     private static String JavaDoc EXTENSIONPOINT_UNIQUE_ID = WorkbenchPlugin.PI_WORKBENCH + "." + IWorkbenchRegistryConstants.PL_VIEWS; //$NON-NLS-1$
134

135     /**
136      * A set that will only ever contain ViewDescriptors.
137      */

138     private SortedSet JavaDoc views = new TreeSet JavaDoc(new Comparator JavaDoc() {
139         public int compare(Object JavaDoc o1, Object JavaDoc o2) {
140             String JavaDoc id1 = ((ViewDescriptor) o1).getId();
141             String JavaDoc id2 = ((ViewDescriptor) o2).getId();
142             
143             return id1.compareTo(id2);
144         }});
145
146     private List JavaDoc categories;
147
148     private List JavaDoc sticky;
149
150     private Category miscCategory;
151
152     protected static final String JavaDoc TAG_DESCRIPTION = "description"; //$NON-NLS-1$
153

154     private ViewRegistryReader reader = new ViewRegistryReader();
155
156     private boolean dirtyViewCategoryMappings = true;
157
158     /**
159      * Create a new ViewRegistry.
160      */

161     public ViewRegistry() {
162         super();
163         categories = new ArrayList JavaDoc();
164         sticky = new ArrayList JavaDoc();
165         PlatformUI.getWorkbench().getExtensionTracker().registerHandler(this, ExtensionTracker.createExtensionPointFilter(getExtensionPointFilter()));
166         reader.readViews(Platform.getExtensionRegistry(), this);
167     }
168
169     /**
170      * Add a category to the registry.
171      *
172      * @param desc the descriptor to add
173      */

174     public void add(Category desc) {
175         /* fix for 1877 */
176         if (internalFindCategory(desc.getId()) == null) {
177             dirtyViewCategoryMappings = true;
178             // Mark categories list as dirty
179
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     /**
193      * Add a descriptor to the registry.
194      *
195      * @param desc the descriptor to add
196      */

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     /**
208      * Add a sticky descriptor to the registry.
209      *
210      * @param desc the descriptor to add
211      */

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 // /**
224
// * Return the sticky view descriptor.
225
// *
226
// * @param id the id to searc for
227
// * @return the sticky view descriptor
228
// */
229
// private IStickyViewDescriptor findSticky(String id) {
230
// for (Iterator i = sticky.iterator(); i.hasNext();) {
231
// IStickyViewDescriptor desc = (IStickyViewDescriptor) i.next();
232
// if (id.equals(desc.getId()))
233
// return desc;
234
// }
235
// return null;
236
// }
237

238     /**
239      * Find a descriptor in the registry.
240      */

241     public IViewDescriptor find(String JavaDoc id) {
242         Iterator JavaDoc 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     /**
253      * Find a category with a given name.
254      *
255      * @param id the id to search for
256      * @return the category or <code>null</code>
257      */

258     public IViewCategory findCategory(String JavaDoc id) {
259         mapViewsToCategories();
260         Category category = internalFindCategory(id);
261         if (category == null) {
262             return null;
263         }
264         return new ViewCategoryProxy(category);
265     }
266
267     /**
268      * Returns the category with no updating of the view/category mappings.
269      *
270      * @param id the category id
271      * @return the Category
272      * @since 3.1
273      */

274     private Category internalFindCategory(String JavaDoc id) {
275         Iterator JavaDoc 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     /**
286      * Get the list of view categories.
287      */

288     public IViewCategory[] getCategories() {
289         mapViewsToCategories();
290         int nSize = categories.size();
291         IViewCategory[] retArray = new IViewCategory[nSize];
292         int i = 0;
293         for (Iterator JavaDoc itr = categories.iterator(); itr.hasNext();) {
294             retArray[i++] = new ViewCategoryProxy((Category) itr.next());
295         }
296         return retArray;
297     }
298
299     /**
300      * Get the list of sticky views.
301      */

302     public IStickyViewDescriptor[] getStickyViews() {
303         return (IStickyViewDescriptor[]) sticky
304                 .toArray(new IStickyViewDescriptor[sticky.size()]);
305     }
306
307     /**
308      * Returns the Misc category. This may be <code>null</code> if there are
309      * no miscellaneous views.
310      *
311      * @return the misc category or <code>null</code>
312      */

313     public Category getMiscCategory() {
314         return miscCategory;
315     }
316
317     /**
318      * Get an enumeration of view descriptors.
319      */

320     public IViewDescriptor[] getViews() {
321         return (IViewDescriptor []) views.toArray(new IViewDescriptor [views.size()]);
322     }
323
324     /**
325      * Adds each view in the registry to a particular category.
326      * The view category may be defined in xml. If not, the view is
327      * added to the "misc" category.
328      */

329     public void mapViewsToCategories() {
330         if (dirtyViewCategoryMappings) {
331             dirtyViewCategoryMappings = false;
332             // clear all category mappings
333
for (Iterator JavaDoc i = categories.iterator(); i.hasNext(); ) {
334                 Category category = (Category) i.next();
335                 category.clear(); // this is bad
336
}
337             
338             if (miscCategory != null) {
339                 miscCategory.clear();
340             }
341             
342             for (Iterator JavaDoc i = views.iterator(); i.hasNext(); ) {
343                 IViewDescriptor desc = (IViewDescriptor) i.next();
344                 Category cat = null;
345                 String JavaDoc[] catPath = desc.getCategoryPath();
346                 if (catPath != null) {
347                     String JavaDoc 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                         // If we get here, this view specified a category which
361
// does not exist. Add this view to the 'Other' category
362
// but give out a message (to the log only) indicating
363
// this has been done.
364
String JavaDoc fmt = "Category {0} not found for view {1}. This view added to ''{2}'' category."; //$NON-NLS-1$
365
WorkbenchPlugin.log(MessageFormat
366                                 .format(fmt, new Object JavaDoc[] { catPath[0],
367                                         desc.getId(), miscCategory.getLabel() }));
368                     }
369                     miscCategory.addElement(desc);
370                 }
371             }
372         }
373     }
374
375     /**
376      * Dispose of this registry.
377      */

378     public void dispose() {
379         PlatformUI.getWorkbench().getExtensionTracker().unregisterHandler(this);
380     }
381
382     /* (non-Javadoc)
383      * @see org.eclipse.core.runtime.dynamicHelpers.IExtensionChangeHandler#removeExtension(org.eclipse.core.runtime.IExtension, java.lang.Object[])
384      */

385     public void removeExtension(IExtension extension,Object JavaDoc[] 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     /* (non-Javadoc)
408      * @see org.eclipse.core.runtime.dynamicHelpers.IExtensionChangeHandler#addExtension(org.eclipse.core.runtime.dynamicHelpers.IExtensionTracker, org.eclipse.core.runtime.IExtension)
409      */

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