KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > navigator > JavaNavigatorContentProvider


1 /*******************************************************************************
2  * Copyright (c) 2003, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.ui.navigator;
12
13 import java.util.Arrays JavaDoc;
14 import java.util.Collection JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.LinkedHashSet JavaDoc;
17 import java.util.List JavaDoc;
18 import java.util.Set JavaDoc;
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 JavaDoc JDT_EXTENSION_ID = "org.eclipse.jdt.ui.javaContent"; //$NON-NLS-1$
63

64     private IExtensionStateModel fStateModel;
65
66     private Object JavaDoc 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 JavaDoc) 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 JavaDoc oldInput, Object JavaDoc newInput) {
103         fRealInput = newInput;
104         super.inputChanged(viewer, oldInput, findInputElement(newInput));
105     }
106     
107     public Object JavaDoc getParent(Object JavaDoc element) {
108         Object JavaDoc parent= super.getParent(element);
109         if (parent instanceof IJavaModel) {
110             return getViewerInput() != null ? fRealInput : parent;
111         }
112         return parent;
113     }
114
115     public Object JavaDoc[] getElements(Object JavaDoc 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 JavaDoc[] getChildren(Object JavaDoc 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 JavaDoc findInputElement(Object JavaDoc 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 JavaDoc parent, Set JavaDoc currentChildren) {
155         customize(getChildren(parent), currentChildren);
156     }
157
158     public void getPipelinedElements(Object JavaDoc input, Set JavaDoc currentElements) {
159         customize(getElements(input), currentElements);
160     }
161
162     public Object JavaDoc getPipelinedParent(Object JavaDoc object, Object JavaDoc 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 JavaDoc convertedChildren = new LinkedHashSet JavaDoc();
190         for (Iterator JavaDoc iterator = modification.getChildren().iterator(); iterator.hasNext();) {
191             Object JavaDoc 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     /**
201      * Converts the shape modification to use Java elements.
202      *
203      *
204      * @param modification
205      * the shape modification to convert
206      * @return returns true if the conversion took place
207      */

208     private boolean convertToJavaElements(PipelinedShapeModification modification) {
209         Object JavaDoc parent = modification.getParent();
210         // As of 3.3, we no longer re-parent additions to IProject.
211
if (parent instanceof IContainer) {
212             IJavaElement element = JavaCore.create((IContainer) parent);
213             if (element != null && element.exists()) {
214                 // we don't convert the root
215
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     /**
225      * Converts the shape modification to use Java elements.
226      *
227      *
228      * @param currentChildren
229      * The set of current children that would be contributed or refreshed in the viewer.
230      * @return returns true if the conversion took place
231      */

232     private boolean convertToJavaElements(Set JavaDoc currentChildren) {
233
234         LinkedHashSet JavaDoc convertedChildren = new LinkedHashSet JavaDoc();
235         IJavaElement newChild;
236         for (Iterator JavaDoc childrenItr = currentChildren.iterator(); childrenItr
237                 .hasNext();) {
238             Object JavaDoc child = childrenItr.next();
239             // only convert IFolders and IFiles
240
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     /**
260      * Adapted from the C Navigator Content Provider
261      * @param javaElements
262      * @param proposedChildren
263      */

264     private void customize(Object JavaDoc[] javaElements, Set JavaDoc proposedChildren) {
265         List JavaDoc elementList= Arrays.asList(javaElements);
266         for (Iterator JavaDoc iter= proposedChildren.iterator(); iter.hasNext();) {
267             Object JavaDoc 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 JavaDoc 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 JavaDoc parent, final Object JavaDoc element, Collection JavaDoc 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 JavaDoc toRefresh, final boolean updateLabels, Collection JavaDoc runnables) {
316         for (Iterator JavaDoc iter = toRefresh.iterator(); iter.hasNext();) {
317             Object JavaDoc 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