001: /*******************************************************************************
002: * Copyright (c) 2003, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.internal.ide;
011:
012: import java.util.HashMap;
013: import java.util.Map;
014:
015: import org.eclipse.core.resources.IProject;
016: import org.eclipse.core.resources.IResource;
017: import org.eclipse.core.resources.IResourceChangeEvent;
018: import org.eclipse.core.resources.IResourceChangeListener;
019: import org.eclipse.core.resources.IResourceDelta;
020: import org.eclipse.core.resources.ResourcesPlugin;
021: import org.eclipse.core.runtime.CoreException;
022: import org.eclipse.core.runtime.IExtension;
023: import org.eclipse.core.runtime.IExtensionPoint;
024: import org.eclipse.core.runtime.IRegistryChangeEvent;
025: import org.eclipse.core.runtime.IRegistryChangeListener;
026: import org.eclipse.core.runtime.Platform;
027: import org.eclipse.ui.IPluginContribution;
028: import org.eclipse.ui.PlatformUI;
029: import org.eclipse.ui.activities.ITriggerPoint;
030: import org.eclipse.ui.activities.IWorkbenchActivitySupport;
031: import org.eclipse.ui.activities.WorkbenchActivityHelper;
032:
033: /**
034: * Utility class that manages promotion of activites in response to workspace changes.
035: *
036: * @since 3.0
037: */
038: public class IDEWorkbenchActivityHelper {
039:
040: private static final String NATURE_POINT = "org.eclipse.ui.ide.natures"; //$NON-NLS-1$
041:
042: /**
043: * Resource listener that reacts to new projects (and associated natures)
044: * coming into the workspace.
045: */
046: private IResourceChangeListener listener;
047:
048: /**
049: * Mapping from composite nature ID to IPluginContribution. Used for proper
050: * activity mapping of natures.
051: */
052: private Map natureMap;
053:
054: /**
055: * Singleton instance.
056: */
057: private static IDEWorkbenchActivityHelper singleton;
058:
059: /**
060: * Get the singleton instance of this class.
061: * @return the singleton instance of this class.
062: * @since 3.0
063: */
064: public static IDEWorkbenchActivityHelper getInstance() {
065: if (singleton == null) {
066: singleton = new IDEWorkbenchActivityHelper();
067: }
068: return singleton;
069: }
070:
071: /**
072: * Create a new <code>IDEWorkbenchActivityHelper</code> which will listen
073: * for workspace changes and promote activities accordingly.
074: */
075: private IDEWorkbenchActivityHelper() {
076: natureMap = new HashMap();
077: // for dynamic UI
078: Platform.getExtensionRegistry().addRegistryChangeListener(
079: new IRegistryChangeListener() {
080: public void registryChanged(
081: IRegistryChangeEvent event) {
082: if (event
083: .getExtensionDeltas(
084: "org.eclipse.core.resources", "natures").length > 0) { //$NON-NLS-1$ //$NON-NLS-2$
085: loadNatures();
086: }
087: }
088: }, "org.eclipse.core.resources"); //$NON-NLS-1$
089: loadNatures();
090: listener = getChangeListener();
091: ResourcesPlugin.getWorkspace().addResourceChangeListener(
092: listener);
093: // crawl the initial projects to set up nature bindings
094: IProject[] projects = ResourcesPlugin.getWorkspace().getRoot()
095: .getProjects();
096: IWorkbenchActivitySupport workbenchActivitySupport = PlatformUI
097: .getWorkbench().getActivitySupport();
098: for (int i = 0; i < projects.length; i++) {
099: try {
100: processProject(projects[i], workbenchActivitySupport);
101: } catch (CoreException e) {
102: // do nothing
103: }
104: }
105: }
106:
107: /**
108: * For dynamic UI. Clears the cache of known natures and recreates it.
109: */
110: public void loadNatures() {
111: natureMap.clear();
112: IExtensionPoint point = Platform
113: .getExtensionRegistry()
114: .getExtensionPoint("org.eclipse.core.resources.natures"); //$NON-NLS-1$
115: IExtension[] extensions = point.getExtensions();
116: for (int i = 0; i < extensions.length; i++) {
117: IExtension extension = extensions[i];
118: final String localId = extension.getSimpleIdentifier();
119: final String pluginId = extension.getNamespace();
120: String natureId = extension.getUniqueIdentifier();
121: natureMap.put(natureId, new IPluginContribution() {
122: public String getLocalId() {
123: return localId;
124: }
125:
126: public String getPluginId() {
127: return pluginId;
128: }
129: });
130: }
131: }
132:
133: /**
134: * Get a change listener for listening to resource changes.
135: *
136: * @return the resource change listeners
137: */
138: private IResourceChangeListener getChangeListener() {
139: return new IResourceChangeListener() {
140: /*
141: * (non-Javadoc) @see
142: * org.eclipse.core.resources.IResourceChangeListener#resourceChanged(org.eclipse.core.resources.IResourceChangeEvent)
143: */
144: public void resourceChanged(IResourceChangeEvent event) {
145: if (!WorkbenchActivityHelper.isFiltering()) {
146: return;
147: }
148: IResourceDelta mainDelta = event.getDelta();
149:
150: if (mainDelta == null) {
151: return;
152: }
153: //Has the root changed?
154: if (mainDelta.getKind() == IResourceDelta.CHANGED
155: && mainDelta.getResource().getType() == IResource.ROOT) {
156:
157: try {
158: IResourceDelta[] children = mainDelta
159: .getAffectedChildren();
160: IWorkbenchActivitySupport workbenchActivitySupport = PlatformUI
161: .getWorkbench().getActivitySupport();
162: for (int i = 0; i < children.length; i++) {
163: IResourceDelta delta = children[i];
164: if (delta.getResource().getType() == IResource.PROJECT) {
165: IProject project = (IProject) delta
166: .getResource();
167: processProject(project,
168: workbenchActivitySupport);
169: }
170: }
171: } catch (CoreException exception) {
172: //Do nothing if there is a CoreException
173: }
174: }
175: }
176: };
177: }
178:
179: /**
180: * Handle natures for the given project.
181: *
182: * @param project the project
183: * @param workbenchActivitySupport the activity support
184: */
185: protected void processProject(IProject project,
186: IWorkbenchActivitySupport workbenchActivitySupport)
187: throws CoreException {
188: if (!project.isOpen()) {
189: return;
190: }
191: String[] ids = project.getDescription().getNatureIds();
192: if (ids.length == 0) {
193: return;
194: }
195:
196: for (int j = 0; j < ids.length; j++) {
197: IPluginContribution contribution = (IPluginContribution) natureMap
198: .get(ids[j]);
199: if (contribution == null) {
200: continue; //bad nature ID.
201: }
202: ITriggerPoint triggerPoint = workbenchActivitySupport
203: .getTriggerPointManager().getTriggerPoint(
204: NATURE_POINT);
205: //consult the advisor - if the activities need enabling, they will be
206: WorkbenchActivityHelper.allowUseOf(triggerPoint,
207: contribution);
208: }
209: }
210:
211: /**
212: * Unhooks the <code>IResourceChangeListener</code>.
213: */
214: public void shutdown() {
215: if (listener != null) {
216: ResourcesPlugin.getWorkspace()
217: .removeResourceChangeListener(listener);
218: }
219: }
220:
221: }
|