1 11 package org.eclipse.jdt.internal.ui.navigator; 12 13 import java.util.Arrays ; 14 import java.util.Collection ; 15 import java.util.Iterator ; 16 import java.util.LinkedHashSet ; 17 import java.util.List ; 18 import java.util.Set ; 19 20 import org.eclipse.core.runtime.IAdaptable; 21 22 import org.eclipse.core.resources.IContainer; 23 import org.eclipse.core.resources.IFile; 24 import org.eclipse.core.resources.IFolder; 25 import org.eclipse.core.resources.IProject; 26 import org.eclipse.core.resources.IResource; 27 import org.eclipse.core.resources.IWorkspaceRoot; 28 29 import org.eclipse.jface.preference.IPreferenceStore; 30 import org.eclipse.jface.util.IPropertyChangeListener; 31 import org.eclipse.jface.util.PropertyChangeEvent; 32 import org.eclipse.jface.viewers.Viewer; 33 34 import org.eclipse.ui.IMemento; 35 import org.eclipse.ui.navigator.ICommonContentExtensionSite; 36 import org.eclipse.ui.navigator.IExtensionStateModel; 37 import org.eclipse.ui.navigator.IPipelinedTreeContentProvider; 38 import org.eclipse.ui.navigator.PipelinedShapeModification; 39 import org.eclipse.ui.navigator.PipelinedViewerUpdate; 40 41 import org.eclipse.jdt.core.IJavaElement; 42 import org.eclipse.jdt.core.IJavaModel; 43 import org.eclipse.jdt.core.IJavaProject; 44 import org.eclipse.jdt.core.JavaCore; 45 46 import org.eclipse.jdt.ui.PreferenceConstants; 47 48 import org.eclipse.jdt.internal.ui.navigator.IExtensionStateConstants.Values; 49 import org.eclipse.jdt.internal.ui.packageview.PackageExplorerContentProvider; 50 51 public class JavaNavigatorContentProvider extends 52 PackageExplorerContentProvider implements IPipelinedTreeContentProvider { 53 54 public JavaNavigatorContentProvider() { 55 super(false); 56 } 57 58 public JavaNavigatorContentProvider(boolean provideMembers) { 59 super(provideMembers); 60 } 61 62 public static final String JDT_EXTENSION_ID = "org.eclipse.jdt.ui.javaContent"; 64 private IExtensionStateModel fStateModel; 65 66 private Object fRealInput; 67 68 private IPropertyChangeListener fLayoutPropertyListener; 69 70 public void init(ICommonContentExtensionSite commonContentExtensionSite) { 71 IExtensionStateModel stateModel = commonContentExtensionSite 72 .getExtensionStateModel(); 73 IMemento memento = commonContentExtensionSite.getMemento(); 74 75 fStateModel = stateModel; 76 restoreState(memento); 77 fLayoutPropertyListener = new IPropertyChangeListener() { 78 public void propertyChange(PropertyChangeEvent event) { 79 if (Values.IS_LAYOUT_FLAT.equals(event.getProperty())) { 80 if (event.getNewValue() != null) { 81 boolean newValue = ((Boolean ) event.getNewValue()) 82 .booleanValue() ? true : false; 83 setIsFlatLayout(newValue); 84 } 85 } 86 87 } 88 }; 89 fStateModel.addPropertyChangeListener(fLayoutPropertyListener); 90 91 IPreferenceStore store = PreferenceConstants.getPreferenceStore(); 92 boolean showCUChildren = store 93 .getBoolean(PreferenceConstants.SHOW_CU_CHILDREN); 94 setProvideMembers(showCUChildren); 95 } 96 97 public void dispose() { 98 super.dispose(); 99 fStateModel.removePropertyChangeListener(fLayoutPropertyListener); 100 } 101 102 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) { 103 fRealInput = newInput; 104 super.inputChanged(viewer, oldInput, findInputElement(newInput)); 105 } 106 107 public Object getParent(Object element) { 108 Object parent= super.getParent(element); 109 if (parent instanceof IJavaModel) { 110 return getViewerInput() != null ? fRealInput : parent; 111 } 112 return parent; 113 } 114 115 public Object [] getElements(Object inputElement) { 116 if (inputElement instanceof IWorkspaceRoot) { 117 IWorkspaceRoot root = (IWorkspaceRoot) inputElement; 118 return root.getProjects(); 119 } else if (inputElement instanceof IJavaModel) { 120 return ((IJavaModel)inputElement).getWorkspace().getRoot().getProjects(); 121 } 122 if (inputElement instanceof IProject) { 123 return super.getElements(JavaCore.create((IProject)inputElement)); 124 } 125 return super.getElements(inputElement); 126 } 127 128 public Object [] getChildren(Object parentElement) { 129 if (parentElement instanceof IWorkspaceRoot) { 130 IWorkspaceRoot root = (IWorkspaceRoot) parentElement; 131 return root.getProjects(); 132 } 133 if (parentElement instanceof IProject) { 134 return super.getChildren(JavaCore.create((IProject)parentElement)); 135 } 136 return super.getChildren(parentElement); 137 } 138 139 private Object findInputElement(Object newInput) { 140 if (newInput instanceof IWorkspaceRoot) { 141 return JavaCore.create((IWorkspaceRoot) newInput); 142 } 143 return newInput; 144 } 145 146 public void restoreState(IMemento memento) { 147 148 } 149 150 public void saveState(IMemento memento) { 151 152 } 153 154 public void getPipelinedChildren(Object parent, Set currentChildren) { 155 customize(getChildren(parent), currentChildren); 156 } 157 158 public void getPipelinedElements(Object input, Set currentElements) { 159 customize(getElements(input), currentElements); 160 } 161 162 public Object getPipelinedParent(Object object, Object suggestedParent) { 163 return getParent(object); 164 } 165 166 public PipelinedShapeModification interceptAdd( 167 PipelinedShapeModification addModification) { 168 169 if(addModification.getParent() instanceof IJavaProject) { 170 addModification.setParent(((IJavaProject)addModification.getParent()).getProject()); 171 } else if(addModification.getParent() instanceof IWorkspaceRoot || 172 addModification.getParent() instanceof IJavaProject){ 173 174 deconvertJavaProjects(addModification); 175 } 176 177 convertToJavaElements(addModification); 178 return addModification; 179 } 180 181 public PipelinedShapeModification interceptRemove( 182 PipelinedShapeModification removeModification) { 183 deconvertJavaProjects(removeModification); 184 convertToJavaElements(removeModification.getChildren()); 185 return removeModification; 186 } 187 188 private void deconvertJavaProjects(PipelinedShapeModification modification) { 189 Set convertedChildren = new LinkedHashSet (); 190 for (Iterator iterator = modification.getChildren().iterator(); iterator.hasNext();) { 191 Object added = iterator.next(); 192 if(added instanceof IJavaProject) { 193 iterator.remove(); 194 convertedChildren.add(((IJavaProject)added).getProject()); 195 } 196 } 197 modification.getChildren().addAll(convertedChildren); 198 } 199 200 208 private boolean convertToJavaElements(PipelinedShapeModification modification) { 209 Object parent = modification.getParent(); 210 if (parent instanceof IContainer) { 212 IJavaElement element = JavaCore.create((IContainer) parent); 213 if (element != null && element.exists()) { 214 if( !(element instanceof IJavaModel) && !(element instanceof IJavaProject)) 216 modification.setParent(element); 217 return convertToJavaElements(modification.getChildren()); 218 219 } 220 } 221 return false; 222 } 223 224 232 private boolean convertToJavaElements(Set currentChildren) { 233 234 LinkedHashSet convertedChildren = new LinkedHashSet (); 235 IJavaElement newChild; 236 for (Iterator childrenItr = currentChildren.iterator(); childrenItr 237 .hasNext();) { 238 Object child = childrenItr.next(); 239 if (child instanceof IFolder || child instanceof IFile) { 241 if ((newChild = JavaCore.create((IResource) child)) != null 242 && newChild.exists()) { 243 childrenItr.remove(); 244 convertedChildren.add(newChild); 245 } 246 } else if (child instanceof IJavaProject) { 247 childrenItr.remove(); 248 convertedChildren.add( ((IJavaProject)child).getProject()); 249 } 250 } 251 if (!convertedChildren.isEmpty()) { 252 currentChildren.addAll(convertedChildren); 253 return true; 254 } 255 return false; 256 257 } 258 259 264 private void customize(Object [] javaElements, Set proposedChildren) { 265 List elementList= Arrays.asList(javaElements); 266 for (Iterator iter= proposedChildren.iterator(); iter.hasNext();) { 267 Object element= iter.next(); 268 IResource resource= null; 269 if (element instanceof IResource) { 270 resource= (IResource)element; 271 } else if (element instanceof IAdaptable) { 272 resource= (IResource)((IAdaptable)element).getAdapter(IResource.class); 273 } 274 if (resource != null) { 275 int i= elementList.indexOf(resource); 276 if (i >= 0) { 277 javaElements[i]= null; 278 } 279 } 280 } 281 for (int i= 0; i < javaElements.length; i++) { 282 Object element= javaElements[i]; 283 if (element instanceof IJavaElement) { 284 IJavaElement cElement= (IJavaElement)element; 285 IResource resource= cElement.getResource(); 286 if (resource != null) { 287 proposedChildren.remove(resource); 288 } 289 proposedChildren.add(element); 290 } else if (element != null) { 291 proposedChildren.add(element); 292 } 293 } 294 } 295 296 297 298 public boolean interceptRefresh(PipelinedViewerUpdate refreshSynchronization) { 299 return convertToJavaElements(refreshSynchronization.getRefreshTargets()); 300 301 } 302 303 public boolean interceptUpdate(PipelinedViewerUpdate updateSynchronization) { 304 return convertToJavaElements(updateSynchronization.getRefreshTargets()); 305 } 306 307 protected void postAdd(final Object parent, final Object element, Collection runnables) { 308 if (parent instanceof IJavaModel) 309 super.postAdd(((IJavaModel) parent).getWorkspace(), element, runnables); 310 else 311 super.postAdd(parent, element, runnables); 312 } 313 314 315 protected void postRefresh(final List toRefresh, final boolean updateLabels, Collection runnables) { 316 for (Iterator iter = toRefresh.iterator(); iter.hasNext();) { 317 Object element = iter.next(); 318 if(element instanceof IJavaModel) { 319 iter.remove(); 320 toRefresh.add(fRealInput); 321 super.postRefresh(toRefresh, updateLabels, runnables); 322 return; 323 } 324 } 325 super.postRefresh(toRefresh, updateLabels, runnables); 326 } 327 328 } 329 | Popular Tags |