1 11 package org.eclipse.jdt.internal.ui.workingsets; 12 13 import java.util.ArrayList ; 14 import java.util.Arrays ; 15 import java.util.HashSet ; 16 import java.util.List ; 17 import java.util.Set ; 18 19 import org.eclipse.core.runtime.Assert; 20 import org.eclipse.core.runtime.IAdaptable; 21 22 import org.eclipse.core.resources.IResource; 23 import org.eclipse.core.resources.IResourceChangeEvent; 24 import org.eclipse.core.resources.IResourceChangeListener; 25 import org.eclipse.core.resources.IResourceDelta; 26 import org.eclipse.core.resources.ResourcesPlugin; 27 28 import org.eclipse.jdt.core.ElementChangedEvent; 29 import org.eclipse.jdt.core.IElementChangedListener; 30 import org.eclipse.jdt.core.IJavaElement; 31 import org.eclipse.jdt.core.IJavaElementDelta; 32 import org.eclipse.jdt.core.IJavaModel; 33 import org.eclipse.jdt.core.IJavaProject; 34 import org.eclipse.jdt.core.JavaCore; 35 import org.eclipse.jdt.core.JavaModelException; 36 37 import org.eclipse.jdt.internal.ui.JavaPlugin; 38 39 import org.eclipse.jface.util.IPropertyChangeListener; 40 import org.eclipse.jface.util.PropertyChangeEvent; 41 42 import org.eclipse.ui.IWorkingSet; 43 import org.eclipse.ui.IWorkingSetManager; 44 import org.eclipse.ui.IWorkingSetUpdater; 45 import org.eclipse.ui.PlatformUI; 46 47 public class OthersWorkingSetUpdater implements IWorkingSetUpdater { 48 49 public static final String ID= "org.eclipse.jdt.internal.ui.OthersWorkingSet"; 51 private IWorkingSet fWorkingSet; 52 private WorkingSetModel fWorkingSetModel; 53 54 private class ResourceChangeListener implements IResourceChangeListener { 55 public void resourceChanged(IResourceChangeEvent event) { 56 if (fWorkingSet == null) 57 return; IResourceDelta delta= event.getDelta(); 59 IResourceDelta[] affectedChildren= delta.getAffectedChildren(IResourceDelta.ADDED | IResourceDelta.REMOVED, IResource.PROJECT); 60 if (affectedChildren.length > 0) { 61 updateElements(); 62 } else { 63 affectedChildren= delta.getAffectedChildren(IResourceDelta.CHANGED, IResource.PROJECT); 64 for (int i= 0; i < affectedChildren.length; i++) { 65 IResourceDelta projectDelta= affectedChildren[i]; 66 if ((projectDelta.getFlags() & IResourceDelta.DESCRIPTION) != 0) { 67 updateElements(); 68 return; 70 } 71 } 72 } 73 } 74 } 75 private IResourceChangeListener fResourceChangeListener; 76 77 private class WorkingSetListener implements IPropertyChangeListener { 78 public void propertyChange(PropertyChangeEvent event) { 79 if (IWorkingSetManager.CHANGE_WORKING_SET_CONTENT_CHANGE.equals(event.getProperty())) { 80 IWorkingSet changedWorkingSet= (IWorkingSet) event.getNewValue(); 81 if (changedWorkingSet != fWorkingSet && fWorkingSetModel.isActiveWorkingSet(changedWorkingSet)) { 82 updateElements(); 83 } 84 } 85 } 86 } 87 private IPropertyChangeListener fWorkingSetListener; 88 89 private class JavaElementChangeListener implements IElementChangedListener { 90 public void elementChanged(ElementChangedEvent event) { 91 if (fWorkingSet == null) 92 return; 94 processJavaDelta(new ArrayList (Arrays.asList(fWorkingSet.getElements())), event.getDelta()); 95 } 96 private void processJavaDelta(List elements, IJavaElementDelta delta) { 97 IJavaElement jElement= delta.getElement(); 98 int type= jElement.getElementType(); 99 if (type == IJavaElement.JAVA_PROJECT) { 100 int index= elements.indexOf(jElement); 101 int kind= delta.getKind(); 102 int flags= delta.getFlags(); 103 if (kind == IJavaElementDelta.CHANGED) { 104 if (index != -1 && (flags & IJavaElementDelta.F_CLOSED) != 0) { 105 elements.set(index, ((IJavaProject)jElement).getProject()); 106 fWorkingSet.setElements((IAdaptable[])elements.toArray(new IAdaptable[elements.size()])); 107 } else if ((flags & IJavaElementDelta.F_OPENED) != 0) { 108 index= elements.indexOf(((IJavaProject)jElement).getProject()); 109 if (index != -1) { 110 elements.set(index, jElement); 111 fWorkingSet.setElements((IAdaptable[])elements.toArray(new IAdaptable[elements.size()])); 112 } 113 } 114 } 115 return; 117 } 118 IJavaElementDelta[] children= delta.getAffectedChildren(); 119 for (int i= 0; i < children.length; i++) { 120 processJavaDelta(elements, children[i]); 121 } 122 } 123 } 124 private IElementChangedListener fJavaElementChangeListener; 125 126 129 public void add(IWorkingSet workingSet) { 130 Assert.isTrue(fWorkingSet == null && fWorkingSetModel != null); 131 fWorkingSet= workingSet; 132 } 133 134 137 public boolean remove(IWorkingSet workingSet) { 138 Assert.isTrue(fWorkingSet == workingSet); 139 fWorkingSet= null; 140 return true; 141 } 142 143 146 public boolean contains(IWorkingSet workingSet) { 147 return fWorkingSet == workingSet; 148 } 149 150 public void init(WorkingSetModel model) { 151 fWorkingSetModel= model; 152 fResourceChangeListener= new ResourceChangeListener(); 153 ResourcesPlugin.getWorkspace().addResourceChangeListener(fResourceChangeListener, IResourceChangeEvent.POST_CHANGE); 154 fWorkingSetListener= new WorkingSetListener(); 155 PlatformUI.getWorkbench().getWorkingSetManager().addPropertyChangeListener(fWorkingSetListener); 156 fJavaElementChangeListener= new JavaElementChangeListener(); 157 JavaCore.addElementChangedListener(fJavaElementChangeListener, ElementChangedEvent.POST_CHANGE); 158 } 159 160 public void dispose() { 161 if (fResourceChangeListener != null) { 162 ResourcesPlugin.getWorkspace().removeResourceChangeListener(fResourceChangeListener); 163 fResourceChangeListener= null; 164 } 165 if (fWorkingSetListener != null) { 166 PlatformUI.getWorkbench().getWorkingSetManager().removePropertyChangeListener(fWorkingSetListener); 167 fWorkingSetListener= null; 168 } 169 if (fJavaElementChangeListener != null) { 170 JavaCore.removeElementChangedListener(fJavaElementChangeListener); 171 } 172 } 173 174 public void updateElements() { 175 Assert.isTrue(fWorkingSet != null && fWorkingSetModel != null); 177 IWorkingSet[] activeWorkingSets= fWorkingSetModel.getActiveWorkingSets(); 178 179 List result= new ArrayList (); 180 Set projects= new HashSet (); 181 for (int i= 0; i < activeWorkingSets.length; i++) { 182 if (activeWorkingSets[i] == fWorkingSet) continue; 183 IAdaptable[] elements= activeWorkingSets[i].getElements(); 184 for (int j= 0; j < elements.length; j++) { 185 IAdaptable element= elements[j]; 186 IResource resource= (IResource)element.getAdapter(IResource.class); 187 if (resource != null && resource.getType() == IResource.PROJECT) { 188 projects.add(resource); 189 } 190 } 191 } 192 IJavaModel model= JavaCore.create(ResourcesPlugin.getWorkspace().getRoot()); 193 try { 194 IJavaProject[] jProjects= model.getJavaProjects(); 195 for (int i= 0; i < jProjects.length; i++) { 196 if (!projects.contains(jProjects[i].getProject())) 197 result.add(jProjects[i]); 198 } 199 Object [] rProjects= model.getNonJavaResources(); 200 for (int i= 0; i < rProjects.length; i++) { 201 if (!projects.contains(rProjects[i])) 202 result.add(rProjects[i]); 203 } 204 } catch (JavaModelException e) { 205 JavaPlugin.log(e); 206 } 207 fWorkingSet.setElements((IAdaptable[])result.toArray(new IAdaptable[result.size()])); 208 } 209 } 210 | Popular Tags |