KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > ide > IDEWorkbenchActivityHelper


1 /*******************************************************************************
2  * Copyright (c) 2003, 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.ide;
12
13 import java.util.HashMap JavaDoc;
14 import java.util.Map JavaDoc;
15
16 import org.eclipse.core.resources.IProject;
17 import org.eclipse.core.resources.IResource;
18 import org.eclipse.core.resources.IResourceChangeEvent;
19 import org.eclipse.core.resources.IResourceChangeListener;
20 import org.eclipse.core.resources.IResourceDelta;
21 import org.eclipse.core.resources.ResourcesPlugin;
22 import org.eclipse.core.runtime.CoreException;
23 import org.eclipse.core.runtime.IExtension;
24 import org.eclipse.core.runtime.IExtensionPoint;
25 import org.eclipse.core.runtime.IRegistryChangeEvent;
26 import org.eclipse.core.runtime.IRegistryChangeListener;
27 import org.eclipse.core.runtime.Platform;
28 import org.eclipse.ui.IPluginContribution;
29 import org.eclipse.ui.PlatformUI;
30 import org.eclipse.ui.activities.ITriggerPoint;
31 import org.eclipse.ui.activities.IWorkbenchActivitySupport;
32 import org.eclipse.ui.activities.WorkbenchActivityHelper;
33
34 /**
35  * Utility class that manages promotion of activites in response to workspace changes.
36  *
37  * @since 3.0
38  */

39 public class IDEWorkbenchActivityHelper {
40
41     private static final String JavaDoc NATURE_POINT = "org.eclipse.ui.ide.natures"; //$NON-NLS-1$
42

43     /**
44      * Resource listener that reacts to new projects (and associated natures)
45      * coming into the workspace.
46      */

47     private IResourceChangeListener listener;
48
49     /**
50      * Mapping from composite nature ID to IPluginContribution. Used for proper
51      * activity mapping of natures.
52      */

53     private Map JavaDoc natureMap;
54
55     /**
56      * Singleton instance.
57      */

58     private static IDEWorkbenchActivityHelper singleton;
59
60     /**
61      * Get the singleton instance of this class.
62      * @return the singleton instance of this class.
63      * @since 3.0
64      */

65     public static IDEWorkbenchActivityHelper getInstance() {
66         if (singleton == null) {
67             singleton = new IDEWorkbenchActivityHelper();
68         }
69         return singleton;
70     }
71
72     /**
73      * Create a new <code>IDEWorkbenchActivityHelper</code> which will listen
74      * for workspace changes and promote activities accordingly.
75      */

76     private IDEWorkbenchActivityHelper() {
77         natureMap = new HashMap JavaDoc();
78         // for dynamic UI
79
Platform.getExtensionRegistry().addRegistryChangeListener(
80                 new IRegistryChangeListener() {
81                     public void registryChanged(IRegistryChangeEvent event) {
82                         if (event.getExtensionDeltas(
83                                 "org.eclipse.core.resources", "natures").length > 0) { //$NON-NLS-1$ //$NON-NLS-2$
84
loadNatures();
85                         }
86                     }
87                 }, "org.eclipse.core.resources"); //$NON-NLS-1$
88
loadNatures();
89         listener = getChangeListener();
90         ResourcesPlugin.getWorkspace().addResourceChangeListener(listener);
91         // crawl the initial projects to set up nature bindings
92
IProject[] projects = ResourcesPlugin.getWorkspace().getRoot()
93                 .getProjects();
94         IWorkbenchActivitySupport workbenchActivitySupport = PlatformUI
95                 .getWorkbench().getActivitySupport();
96         for (int i = 0; i < projects.length; i++) {
97             try {
98                 processProject(projects[i], workbenchActivitySupport);
99             } catch (CoreException e) {
100                 // do nothing
101
}
102         }
103     }
104
105     /**
106      * For dynamic UI. Clears the cache of known natures and recreates it.
107      */

108     public void loadNatures() {
109         natureMap.clear();
110         IExtensionPoint point = Platform.getExtensionRegistry()
111                 .getExtensionPoint("org.eclipse.core.resources.natures"); //$NON-NLS-1$
112
IExtension[] extensions = point.getExtensions();
113         for (int i = 0; i < extensions.length; i++) {
114             IExtension extension = extensions[i];
115             final String JavaDoc localId = extension.getSimpleIdentifier();
116             final String JavaDoc pluginId = extension.getNamespace();
117             String JavaDoc natureId = extension.getUniqueIdentifier();
118             natureMap.put(natureId, new IPluginContribution() {
119                 public String JavaDoc getLocalId() {
120                     return localId;
121                 }
122
123                 public String JavaDoc getPluginId() {
124                     return pluginId;
125                 }
126             });
127         }
128     }
129
130     /**
131      * Get a change listener for listening to resource changes.
132      *
133      * @return the resource change listeners
134      */

135     private IResourceChangeListener getChangeListener() {
136         return new IResourceChangeListener() {
137             /*
138              * (non-Javadoc) @see
139              * org.eclipse.core.resources.IResourceChangeListener#resourceChanged(org.eclipse.core.resources.IResourceChangeEvent)
140              */

141             public void resourceChanged(IResourceChangeEvent event) {
142                 if (!WorkbenchActivityHelper.isFiltering()) {
143                     return;
144                 }
145                 IResourceDelta mainDelta = event.getDelta();
146
147                 if (mainDelta == null) {
148                     return;
149                 }
150                 //Has the root changed?
151
if (mainDelta.getKind() == IResourceDelta.CHANGED
152                         && mainDelta.getResource().getType() == IResource.ROOT) {
153
154                     try {
155                         IResourceDelta[] children = mainDelta
156                                 .getAffectedChildren();
157                         IWorkbenchActivitySupport workbenchActivitySupport = PlatformUI
158                                 .getWorkbench().getActivitySupport();
159                         for (int i = 0; i < children.length; i++) {
160                             IResourceDelta delta = children[i];
161                             if (delta.getResource().getType() == IResource.PROJECT) {
162                                 IProject project = (IProject) delta
163                                         .getResource();
164                                 processProject(project,
165                                         workbenchActivitySupport);
166                             }
167                         }
168                     } catch (CoreException exception) {
169                         //Do nothing if there is a CoreException
170
}
171                 }
172             }
173         };
174     }
175
176     /**
177      * Handle natures for the given project.
178      *
179      * @param project the project
180      * @param workbenchActivitySupport the activity support
181      */

182     protected void processProject(IProject project,
183             IWorkbenchActivitySupport workbenchActivitySupport)
184             throws CoreException {
185         if (!project.isOpen()) {
186             return;
187         }
188         String JavaDoc[] ids = project.getDescription().getNatureIds();
189         if (ids.length == 0) {
190             return;
191         }
192         
193         for (int j = 0; j < ids.length; j++) {
194             IPluginContribution contribution = (IPluginContribution) natureMap
195                     .get(ids[j]);
196             if (contribution == null) {
197                 continue; //bad nature ID.
198
}
199             ITriggerPoint triggerPoint = workbenchActivitySupport
200                     .getTriggerPointManager().getTriggerPoint(NATURE_POINT);
201             //consult the advisor - if the activities need enabling, they will be
202
WorkbenchActivityHelper.allowUseOf(triggerPoint, contribution);
203         }
204     }
205
206     /**
207      * Unhooks the <code>IResourceChangeListener</code>.
208      */

209     public void shutdown() {
210         if (listener != null) {
211             ResourcesPlugin.getWorkspace().removeResourceChangeListener(
212                     listener);
213         }
214     }
215
216 }
217
Popular Tags