1 11 package org.eclipse.ui.internal.ide; 12 13 import java.util.HashMap ; 14 import java.util.Map ; 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 39 public class IDEWorkbenchActivityHelper { 40 41 private static final String NATURE_POINT = "org.eclipse.ui.ide.natures"; 43 47 private IResourceChangeListener listener; 48 49 53 private Map natureMap; 54 55 58 private static IDEWorkbenchActivityHelper singleton; 59 60 65 public static IDEWorkbenchActivityHelper getInstance() { 66 if (singleton == null) { 67 singleton = new IDEWorkbenchActivityHelper(); 68 } 69 return singleton; 70 } 71 72 76 private IDEWorkbenchActivityHelper() { 77 natureMap = new HashMap (); 78 Platform.getExtensionRegistry().addRegistryChangeListener( 80 new IRegistryChangeListener() { 81 public void registryChanged(IRegistryChangeEvent event) { 82 if (event.getExtensionDeltas( 83 "org.eclipse.core.resources", "natures").length > 0) { loadNatures(); 85 } 86 } 87 }, "org.eclipse.core.resources"); loadNatures(); 89 listener = getChangeListener(); 90 ResourcesPlugin.getWorkspace().addResourceChangeListener(listener); 91 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 } 102 } 103 } 104 105 108 public void loadNatures() { 109 natureMap.clear(); 110 IExtensionPoint point = Platform.getExtensionRegistry() 111 .getExtensionPoint("org.eclipse.core.resources.natures"); IExtension[] extensions = point.getExtensions(); 113 for (int i = 0; i < extensions.length; i++) { 114 IExtension extension = extensions[i]; 115 final String localId = extension.getSimpleIdentifier(); 116 final String pluginId = extension.getNamespace(); 117 String natureId = extension.getUniqueIdentifier(); 118 natureMap.put(natureId, new IPluginContribution() { 119 public String getLocalId() { 120 return localId; 121 } 122 123 public String getPluginId() { 124 return pluginId; 125 } 126 }); 127 } 128 } 129 130 135 private IResourceChangeListener getChangeListener() { 136 return new IResourceChangeListener() { 137 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 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 } 171 } 172 } 173 }; 174 } 175 176 182 protected void processProject(IProject project, 183 IWorkbenchActivitySupport workbenchActivitySupport) 184 throws CoreException { 185 if (!project.isOpen()) { 186 return; 187 } 188 String [] 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; } 199 ITriggerPoint triggerPoint = workbenchActivitySupport 200 .getTriggerPointManager().getTriggerPoint(NATURE_POINT); 201 WorkbenchActivityHelper.allowUseOf(triggerPoint, contribution); 203 } 204 } 205 206 209 public void shutdown() { 210 if (listener != null) { 211 ResourcesPlugin.getWorkspace().removeResourceChangeListener( 212 listener); 213 } 214 } 215 216 } 217 | Popular Tags |