KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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.Collection JavaDoc;
15 import java.util.HashMap JavaDoc;
16 import java.util.Iterator JavaDoc;
17 import java.util.List JavaDoc;
18
19 import org.eclipse.core.runtime.Assert;
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.Platform;
24 import org.eclipse.core.runtime.dynamichelpers.ExtensionTracker;
25 import org.eclipse.core.runtime.dynamichelpers.IExtensionChangeHandler;
26 import org.eclipse.core.runtime.dynamichelpers.IExtensionTracker;
27 import org.eclipse.ui.PlatformUI;
28 import org.eclipse.ui.dialogs.IWorkingSetPage;
29
30 /**
31  * Stores working set descriptors for working set extensions.
32  */

33 public class WorkingSetRegistry implements IExtensionChangeHandler {
34     // used in Workbench plugin.xml for default workingSet extension
35
// @issue this is an IDE specific working set page!
36
private static final String JavaDoc DEFAULT_PAGE_ID = "org.eclipse.ui.resourceWorkingSetPage"; //$NON-NLS-1$
37

38     private HashMap JavaDoc/*<String, WorkingSetDescriptor>*/ workingSetDescriptors = new HashMap JavaDoc();
39
40     /**
41      *
42      */

43     public WorkingSetRegistry() {
44         IExtensionTracker tracker = PlatformUI.getWorkbench()
45                 .getExtensionTracker();
46         tracker.registerHandler(this, ExtensionTracker
47                 .createExtensionPointFilter(getExtensionPointFilter()));
48
49     }
50
51     /**
52      *
53      * @return
54      * @since 3.3
55      */

56     private IExtensionPoint getExtensionPointFilter() {
57         return Platform.getExtensionRegistry().getExtensionPoint(
58                 PlatformUI.PLUGIN_ID,
59                 IWorkbenchRegistryConstants.PL_WORKINGSETS);
60     }
61     
62     /**
63      * Adds a working set descriptor.
64      *
65      * @param descriptor working set descriptor to add. Must not
66      * exist in the registry yet.
67      */

68     public void addWorkingSetDescriptor(WorkingSetDescriptor descriptor) {
69         Assert.isTrue(!workingSetDescriptors.containsValue(descriptor),
70                 "working set descriptor already registered"); //$NON-NLS-1$
71
IExtensionTracker tracker = PlatformUI.getWorkbench()
72                 .getExtensionTracker();
73         tracker.registerObject(descriptor.getConfigurationElement()
74                 .getDeclaringExtension(), descriptor,
75                 IExtensionTracker.REF_WEAK);
76         workingSetDescriptors.put(descriptor.getId(), descriptor);
77     }
78
79     /**
80      * Returns the default, resource based, working set page
81      *
82      * @return the default working set page.
83      */

84     public IWorkingSetPage getDefaultWorkingSetPage() {
85         // @issue this will return the IDE resource working set page... not good for generic workbench
86
WorkingSetDescriptor descriptor = (WorkingSetDescriptor) workingSetDescriptors
87                 .get(DEFAULT_PAGE_ID);
88
89         if (descriptor != null) {
90             return descriptor.createWorkingSetPage();
91         }
92         return null;
93     }
94
95     /**
96      * Returns the working set descriptor with the given id.
97      *
98      * @param pageId working set page id
99      * @return the working set descriptor with the given id.
100      */

101     public WorkingSetDescriptor getWorkingSetDescriptor(String JavaDoc pageId) {
102         return (WorkingSetDescriptor) workingSetDescriptors.get(pageId);
103     }
104
105     /**
106      * Returns an array of all working set descriptors.
107      *
108      * @return an array of all working set descriptors.
109      */

110     public WorkingSetDescriptor[] getWorkingSetDescriptors() {
111         return (WorkingSetDescriptor[]) workingSetDescriptors.values().toArray(
112                 new WorkingSetDescriptor[workingSetDescriptors.size()]);
113     }
114     
115     /**
116      * Returns an array of all working set descriptors having
117      * a page class attribute
118      *
119      * @return an array of all working set descriptors having a
120      * page class attribute
121      */

122     public WorkingSetDescriptor[] getNewPageWorkingSetDescriptors() {
123         Collection JavaDoc descriptors= workingSetDescriptors.values();
124         List JavaDoc result= new ArrayList JavaDoc(descriptors.size());
125         for (Iterator JavaDoc iter= descriptors.iterator(); iter.hasNext();) {
126             WorkingSetDescriptor descriptor= (WorkingSetDescriptor)iter.next();
127             if (descriptor.getPageClassName() != null) {
128                 result.add(descriptor);
129             }
130         }
131         return (WorkingSetDescriptor[])result.toArray(new WorkingSetDescriptor[result.size()]);
132     }
133     
134     /**
135      * Returns <code>true</code> if there is a working set descriptor with
136      * a page class attribute. Otherwise <code>false</code> is returned.
137      *
138      * @return whether a descriptor with a page class attribute exists
139      */

140     public boolean hasNewPageWorkingSetDescriptor() {
141         Collection JavaDoc descriptors= workingSetDescriptors.values();
142         for (Iterator JavaDoc iter= descriptors.iterator(); iter.hasNext();) {
143             WorkingSetDescriptor descriptor= (WorkingSetDescriptor)iter.next();
144             if (descriptor.getPageClassName() != null) {
145                 return true;
146             }
147         }
148         return false;
149     }
150     
151     public WorkingSetDescriptor[] getUpdaterDescriptorsForNamespace(
152             String JavaDoc namespace) {
153         Collection JavaDoc descriptors = workingSetDescriptors.values();
154         List JavaDoc result = new ArrayList JavaDoc();
155         for (Iterator JavaDoc iter = descriptors.iterator(); iter.hasNext();) {
156             WorkingSetDescriptor descriptor = (WorkingSetDescriptor) iter
157                     .next();
158             if (namespace.equals(descriptor.getUpdaterNamespace())) {
159                 result.add(descriptor);
160             }
161         }
162         return (WorkingSetDescriptor[]) result
163                 .toArray(new WorkingSetDescriptor[result.size()]);
164     }
165     
166     public WorkingSetDescriptor[] getElementAdapterDescriptorsForNamespace(
167             String JavaDoc namespace) {
168         Collection JavaDoc descriptors = workingSetDescriptors.values();
169         List JavaDoc result = new ArrayList JavaDoc();
170         for (Iterator JavaDoc iter = descriptors.iterator(); iter.hasNext();) {
171             WorkingSetDescriptor descriptor = (WorkingSetDescriptor) iter
172                     .next();
173             if (namespace.equals(descriptor.getDeclaringNamespace())) {
174                 result.add(descriptor);
175             }
176         }
177         return (WorkingSetDescriptor[]) result
178                 .toArray(new WorkingSetDescriptor[result.size()]);
179     }
180
181     /**
182      * Returns the working set page with the given id.
183      *
184      * @param pageId working set page id
185      * @return the working set page with the given id.
186      */

187     public IWorkingSetPage getWorkingSetPage(String JavaDoc pageId) {
188         WorkingSetDescriptor descriptor = (WorkingSetDescriptor) workingSetDescriptors
189                 .get(pageId);
190
191         if (descriptor == null) {
192             return null;
193         }
194         return descriptor.createWorkingSetPage();
195     }
196
197     /**
198      * Loads the working set registry.
199      */

200     public void load() {
201         WorkingSetRegistryReader reader = new WorkingSetRegistryReader();
202         reader.readWorkingSets(Platform.getExtensionRegistry(), this);
203     }
204
205     /* (non-Javadoc)
206      * @see org.eclipse.core.runtime.dynamichelpers.IExtensionChangeHandler#addExtension(org.eclipse.core.runtime.dynamichelpers.IExtensionTracker, org.eclipse.core.runtime.IExtension)
207      */

208     public void addExtension(IExtensionTracker tracker, IExtension extension) {
209         WorkingSetRegistryReader reader = new WorkingSetRegistryReader(this);
210         IConfigurationElement[] elements = extension.getConfigurationElements();
211         for (int i = 0; i < elements.length; i++) {
212             reader.readElement(elements[i]);
213         }
214     }
215
216     /* (non-Javadoc)
217      * @see org.eclipse.core.runtime.dynamichelpers.IExtensionChangeHandler#removeExtension(org.eclipse.core.runtime.IExtension, java.lang.Object[])
218      */

219     public void removeExtension(IExtension extension, Object JavaDoc[] objects) {
220         for (int i = 0; i < objects.length; i++) {
221             if (objects[i] instanceof WorkingSetDescriptor) {
222                 WorkingSetDescriptor desc = (WorkingSetDescriptor) objects[i];
223                 workingSetDescriptors.remove(desc.getId());
224             }
225         }
226     }
227 }
228
Popular Tags