1 11 package org.eclipse.jdt.internal.ui.packageview; 12 13 import java.util.ArrayList ; 14 import java.util.Arrays ; 15 import java.util.Collection ; 16 import java.util.Collections ; 17 import java.util.HashSet ; 18 import java.util.Iterator ; 19 import java.util.List ; 20 import java.util.Set ; 21 22 import org.eclipse.core.runtime.Assert; 23 import org.eclipse.core.runtime.IAdaptable; 24 25 import org.eclipse.core.resources.IProject; 26 import org.eclipse.core.resources.IResource; 27 import org.eclipse.core.resources.ResourcesPlugin; 28 29 import org.eclipse.jface.util.IPropertyChangeListener; 30 import org.eclipse.jface.util.PropertyChangeEvent; 31 import org.eclipse.jface.viewers.TreePath; 32 33 import org.eclipse.ui.IWorkingSet; 34 import org.eclipse.ui.IWorkingSetManager; 35 36 import org.eclipse.jdt.core.IJavaElement; 37 import org.eclipse.jdt.core.IJavaModel; 38 import org.eclipse.jdt.core.IJavaProject; 39 import org.eclipse.jdt.core.JavaCore; 40 41 import org.eclipse.jdt.internal.ui.workingsets.WorkingSetModel; 42 43 public class WorkingSetAwareContentProvider extends PackageExplorerContentProvider implements IMultiElementTreeContentProvider { 44 45 private WorkingSetModel fWorkingSetModel; 46 private IPropertyChangeListener fListener; 47 48 public WorkingSetAwareContentProvider(boolean provideMembers, WorkingSetModel model) { 49 super(provideMembers); 50 fWorkingSetModel= model; 51 fListener= new IPropertyChangeListener() { 52 public void propertyChange(PropertyChangeEvent event) { 53 workingSetModelChanged(event); 54 } 55 }; 56 fWorkingSetModel.addPropertyChangeListener(fListener); 57 } 58 59 62 public void dispose() { 63 fWorkingSetModel.removePropertyChangeListener(fListener); 64 super.dispose(); 65 } 66 67 70 public boolean hasChildren(Object element) { 71 if (element instanceof IWorkingSet) 72 return true; 73 return super.hasChildren(element); 74 } 75 76 79 public Object [] getChildren(Object element) { 80 Object [] children; 81 if (element instanceof WorkingSetModel) { 82 Assert.isTrue(fWorkingSetModel == element); 83 return fWorkingSetModel.getActiveWorkingSets(); 84 } else if (element instanceof IWorkingSet) { 85 children= getWorkingSetChildren((IWorkingSet)element); 86 } else { 87 children= super.getChildren(element); 88 } 89 return children; 90 } 91 92 private Object [] getWorkingSetChildren(IWorkingSet set) { 93 IAdaptable[] elements= fWorkingSetModel.getChildren(set); 94 Set result= new HashSet (elements.length); 95 for (int i= 0; i < elements.length; i++) { 96 IAdaptable element= elements[i]; 97 if (element instanceof IProject) { 98 processResource((IProject) element, result); } else if (element instanceof IResource) { 100 IProject project= ((IResource) element).getProject(); 101 if (project.isOpen()) { 102 processResource((IResource) element, result); 103 } 104 } else if (element instanceof IJavaProject) { 105 result.add(element); } else if (element instanceof IJavaElement) { 107 IJavaElement elem= (IJavaElement) element; 108 IProject project= getProject(elem); 109 if (project != null && project.isOpen()) { 110 result.add(elem); 111 } 112 } else { 113 IProject project= (IProject) element.getAdapter(IProject.class); 114 if (project != null) { 115 processResource(project, result); 116 } 117 } 118 } 119 return result.toArray(); 120 } 121 122 private void processResource(IResource resource, Collection result) { 123 IJavaElement elem= JavaCore.create(resource); 124 if (elem != null && elem.exists()) { 125 result.add(elem); 126 } else { 127 result.add(resource); 128 } 129 } 130 131 private IProject getProject(IJavaElement element) { 132 IJavaProject project= element.getJavaProject(); 133 if (project == null) 134 return null; 135 return project.getProject(); 136 } 137 138 141 public TreePath[] getTreePaths(Object element) { 142 if (element instanceof IWorkingSet) { 143 TreePath path= new TreePath(new Object [] {element}); 144 return new TreePath[] {path}; 145 } 146 List modelParents= getModelPath(element); 147 List result= new ArrayList (); 148 for (int i= 0; i < modelParents.size(); i++) { 149 result.addAll(getTreePaths(modelParents, i)); 150 } 151 return (TreePath[])result.toArray(new TreePath[result.size()]); 152 } 153 154 private List getModelPath(Object element) { 155 List result= new ArrayList (); 156 result.add(element); 157 Object parent= super.getParent(element); 158 Object input= getViewerInput(); 159 while (parent != null && !parent.equals(input) && !(parent instanceof IJavaModel)) { 161 result.add(parent); 162 parent= super.getParent(parent); 163 } 164 Collections.reverse(result); 165 return result; 166 } 167 168 private List getTreePaths(List modelParents, int index) { 169 List result= new ArrayList (); 170 Object input= getViewerInput(); 171 Object element= modelParents.get(index); 172 Object [] parents= fWorkingSetModel.getAllParents(element); 173 for (int i= 0; i < parents.length; i++) { 174 List chain= new ArrayList (); 175 if (!parents[i].equals(input)) 176 chain.add(parents[i]); 177 for (int m= index; m < modelParents.size(); m++) { 178 chain.add(modelParents.get(m)); 179 } 180 result.add(new TreePath(chain.toArray())); 181 } 182 return result; 183 } 184 185 188 public Object getParent(Object child) { 189 Object [] parents= fWorkingSetModel.getAllParents(child); 190 if(parents.length == 0) 191 return super.getParent(child); 192 Object first= parents[0]; 193 return first; 194 } 195 196 protected void augmentElementToRefresh(List toRefresh, int relation, Object affectedElement) { 197 if (JavaCore.create(ResourcesPlugin.getWorkspace().getRoot()).equals(affectedElement)) { 199 toRefresh.remove(affectedElement); 200 toRefresh.add(fWorkingSetModel); 201 } else if (relation == GRANT_PARENT) { 202 Object parent= internalGetParent(affectedElement); 203 if (parent != null) { 204 toRefresh.addAll(Arrays.asList(fWorkingSetModel.getAllParents(parent))); 205 } 206 } 207 List nonProjetTopLevelElemens= fWorkingSetModel.getNonProjectTopLevelElements(); 208 if (nonProjetTopLevelElemens.isEmpty()) 209 return; 210 List toAdd= new ArrayList (); 211 for (Iterator iter= nonProjetTopLevelElemens.iterator(); iter.hasNext();) { 212 Object element= iter.next(); 213 if (isChildOf(element, toRefresh)) 214 toAdd.add(element); 215 } 216 toRefresh.addAll(toAdd); 217 } 218 219 private void workingSetModelChanged(PropertyChangeEvent event) { 220 String property= event.getProperty(); 221 Object newValue= event.getNewValue(); 222 List toRefresh= new ArrayList (1); 223 if (WorkingSetModel.CHANGE_WORKING_SET_MODEL_CONTENT.equals(property)) { 224 toRefresh.add(fWorkingSetModel); 225 } else if (IWorkingSetManager.CHANGE_WORKING_SET_CONTENT_CHANGE.equals(property)) { 226 toRefresh.add(newValue); 227 } else if (IWorkingSetManager.CHANGE_WORKING_SET_NAME_CHANGE.equals(property)) { 228 toRefresh.add(newValue); 229 } 230 ArrayList runnables= new ArrayList (); 231 postRefresh(toRefresh, true, runnables); 232 executeRunnables(runnables); 233 } 234 235 private boolean isChildOf(Object element, List potentialParents) { 236 Object parent= super.getParent(element); 238 if (parent == null) 239 return false; 240 for (Iterator iter= potentialParents.iterator(); iter.hasNext();) { 241 Object potentialParent= iter.next(); 242 while(parent != null) { 243 if (parent.equals(potentialParent)) 244 return true; 245 parent= super.getParent(parent); 246 } 247 248 } 249 return false; 250 } 251 } | Popular Tags |