KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > ui > JavaElementContentProvider


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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.ui;
12
13 import org.eclipse.core.resources.IResource;
14 import org.eclipse.core.resources.IResourceDelta;
15
16 import org.eclipse.swt.widgets.Control;
17
18 import org.eclipse.jface.viewers.IBasicPropertyConstants;
19 import org.eclipse.jface.viewers.ITreeContentProvider;
20 import org.eclipse.jface.viewers.TreeViewer;
21 import org.eclipse.jface.viewers.Viewer;
22
23 import org.eclipse.jdt.core.*;
24
25 import org.eclipse.jdt.internal.corext.util.JavaModelUtil;
26 import org.eclipse.jdt.internal.ui.JavaPlugin;
27  
28 /**
29  * A tree content provider for Java elements. It extends the
30  * StandardJavaElementContentProvider with support for listening to changes.
31  * <p>
32  * This class may be instantiated; it is not intended to be subclassed.
33  * </p>
34  * @deprecated use the StandardJavaElementContentProvider instead
35  * @see StandardJavaElementContentProvider
36  */

37 public class JavaElementContentProvider extends StandardJavaElementContentProvider implements ITreeContentProvider, IElementChangedListener {
38     
39     /** The tree viewer */
40     protected TreeViewer fViewer;
41     /** The input object */
42     protected Object JavaDoc fInput;
43     
44     /* (non-Javadoc)
45      * Method declared on IContentProvider.
46      */

47     public void dispose() {
48         super.dispose();
49         JavaCore.removeElementChangedListener(this);
50     }
51
52     /* (non-Javadoc)
53      * Method declared on IContentProvider.
54      */

55     public void inputChanged(Viewer viewer, Object JavaDoc oldInput, Object JavaDoc newInput) {
56         super.inputChanged(viewer, oldInput, newInput);
57         fViewer= (TreeViewer)viewer;
58         if (oldInput == null && newInput != null) {
59             JavaCore.addElementChangedListener(this);
60         } else if (oldInput != null && newInput == null) {
61             JavaCore.removeElementChangedListener(this);
62         }
63         fInput= newInput;
64     }
65     /**
66      * Creates a new content provider for Java elements.
67      */

68     public JavaElementContentProvider() {
69     }
70
71     /**
72      * Creates a new content provider for Java elements.
73      *
74      * @param provideMembers if <code>true</code> members below compilation units
75      * and class files are provided.
76      * @param provideWorkingCopy if <code>true</code> the element provider provides
77      * working copies for members of compilation units which have an associated working
78      * copy. Otherwise only original elements are provided.
79      *
80      * @since 2.0
81      */

82     public JavaElementContentProvider(boolean provideMembers, boolean provideWorkingCopy) {
83         super(provideMembers, provideWorkingCopy);
84     }
85     
86     /* (non-Javadoc)
87      * Method declared on IElementChangedListener.
88      */

89     public void elementChanged(final ElementChangedEvent event) {
90         try {
91             processDelta(event.getDelta());
92         } catch(JavaModelException e) {
93             JavaPlugin.log(e);
94         }
95     }
96     
97     /**
98      * Processes a delta recursively. When more than two children are affected the
99      * tree is fully refreshed starting at this node. The delta is processed in the
100      * current thread but the viewer updates are posted to the UI thread.
101      *
102      * @param delta the delta to be processed
103      *
104      * @throws JavaModelException if an error occurs while processing the delta
105      */

106     protected void processDelta(IJavaElementDelta delta) throws JavaModelException {
107         int kind= delta.getKind();
108         int flags= delta.getFlags();
109         IJavaElement element= delta.getElement();
110
111         if (element instanceof ICompilationUnit) {
112             if (!getProvideWorkingCopy())
113                 return;
114         
115             ICompilationUnit cu= (ICompilationUnit) element;
116             if (!JavaModelUtil.isPrimary(cu) || !isOnClassPath((ICompilationUnit)element)) {
117                 return;
118             }
119         }
120              
121         // handle open and closing of a solution or project
122
if (((flags & IJavaElementDelta.F_CLOSED) != 0) || ((flags & IJavaElementDelta.F_OPENED) != 0)) {
123             postRefresh(element);
124             return;
125         }
126
127         if (kind == IJavaElementDelta.REMOVED) {
128             Object JavaDoc parent= internalGetParent(element);
129             postRemove(element);
130             if (parent instanceof IPackageFragment)
131                 updatePackageIcon((IPackageFragment)parent);
132             // we are filtering out empty subpackages, so we
133
// a package becomes empty we remove it from the viewer.
134
if (isPackageFragmentEmpty(element.getParent())) {
135                 if (fViewer.testFindItem(parent) != null)
136                     postRefresh(internalGetParent(parent));
137             }
138             return;
139         }
140
141         if (kind == IJavaElementDelta.ADDED) {
142             Object JavaDoc parent= internalGetParent(element);
143             // we are filtering out empty subpackages, so we
144
// have to handle additions to them specially.
145
if (parent instanceof IPackageFragment) {
146                 Object JavaDoc grandparent= internalGetParent(parent);
147                 // 1GE8SI6: ITPJUI:WIN98 - Rename is not shown in Packages View
148
// avoid posting a refresh to an invisible parent
149
if (parent.equals(fInput)) {
150                     postRefresh(parent);
151                 } else {
152                     // refresh from grandparent if parent isn't visible yet
153
if (fViewer.testFindItem(parent) == null)
154                         postRefresh(grandparent);
155                     else {
156                         postRefresh(parent);
157                     }
158                 }
159             } else {
160                 postAdd(parent, element);
161             }
162         }
163
164         if (element instanceof ICompilationUnit) {
165             if (kind == IJavaElementDelta.CHANGED) {
166                 postRefresh(element);
167                 return;
168             }
169         }
170         // we don't show the contents of a compilation or IClassFile, so don't go any deeper
171
if ((element instanceof ICompilationUnit) || (element instanceof IClassFile))
172             return;
173             
174         // the contents of an external JAR has changed
175
if (element instanceof IPackageFragmentRoot && ((flags & IJavaElementDelta.F_ARCHIVE_CONTENT_CHANGED) != 0))
176             postRefresh(element);
177             
178         if (isClassPathChange(delta)) {
179              // throw the towel and do a full refresh of the affected java project.
180
postRefresh(element.getJavaProject());
181         }
182         
183         if (delta.getResourceDeltas() != null) {
184             IResourceDelta[] rd= delta.getResourceDeltas();
185             for (int i= 0; i < rd.length; i++) {
186                 processResourceDelta(rd[i], element);
187             }
188         }
189         
190         IJavaElementDelta[] affectedChildren= delta.getAffectedChildren();
191         if (affectedChildren.length > 1) {
192             // a package fragment might become non empty refresh from the parent
193
if (element instanceof IPackageFragment) {
194                 IJavaElement parent= (IJavaElement)internalGetParent(element);
195                 // 1GE8SI6: ITPJUI:WIN98 - Rename is not shown in Packages View
196
// avoid posting a refresh to an invisible parent
197
if (element.equals(fInput)) {
198                     postRefresh(element);
199                 } else {
200                     postRefresh(parent);
201                 }
202                 return;
203             }
204             // more than one child changed, refresh from here downwards
205
if (element instanceof IPackageFragmentRoot)
206                 postRefresh(skipProjectPackageFragmentRoot((IPackageFragmentRoot)element));
207             else
208                 postRefresh(element);
209             return;
210         }
211         for (int i= 0; i < affectedChildren.length; i++) {
212             processDelta(affectedChildren[i]);
213         }
214     }
215
216     private boolean isOnClassPath(ICompilationUnit element) {
217         IJavaProject project= element.getJavaProject();
218         if (project == null || !project.exists())
219             return false;
220         return project.isOnClasspath(element);
221     }
222
223     
224     /*
225      * Updates the package icon
226      */

227      private void updatePackageIcon(final IJavaElement element) {
228         postRunnable(new Runnable JavaDoc() {
229             public void run() {
230                 // 1GF87WR: ITPUI:ALL - SWTEx + NPE closing a workbench window.
231
Control ctrl= fViewer.getControl();
232                 if (ctrl != null && !ctrl.isDisposed())
233                     fViewer.update(element, new String JavaDoc[]{IBasicPropertyConstants.P_IMAGE});
234             }
235         });
236      }
237
238     /*
239      * Process resource deltas
240      */

241     private void processResourceDelta(IResourceDelta delta, Object JavaDoc parent) {
242         int status= delta.getKind();
243         IResource resource= delta.getResource();
244         // filter out changes affecting the output folder
245
if (resource == null)
246             return;
247             
248         // this could be optimized by handling all the added children in the parent
249
if ((status & IResourceDelta.REMOVED) != 0) {
250             if (parent instanceof IPackageFragment)
251                 // refresh one level above to deal with empty package filtering properly
252
postRefresh(internalGetParent(parent));
253             else
254                 postRemove(resource);
255         }
256         if ((status & IResourceDelta.ADDED) != 0) {
257             if (parent instanceof IPackageFragment)
258                 // refresh one level above to deal with empty package filtering properly
259
postRefresh(internalGetParent(parent));
260             else
261                 postAdd(parent, resource);
262         }
263         IResourceDelta[] affectedChildren= delta.getAffectedChildren();
264         
265         if (affectedChildren.length > 1) {
266             // more than one child changed, refresh from here downwards
267
postRefresh(resource);
268             return;
269         }
270
271         for (int i= 0; i < affectedChildren.length; i++)
272             processResourceDelta(affectedChildren[i], resource);
273     }
274     
275     private void postRefresh(final Object JavaDoc root) {
276         postRunnable(new Runnable JavaDoc() {
277             public void run() {
278                 // 1GF87WR: ITPUI:ALL - SWTEx + NPE closing a workbench window.
279
Control ctrl= fViewer.getControl();
280                 if (ctrl != null && !ctrl.isDisposed())
281                     fViewer.refresh(root);
282             }
283         });
284     }
285     
286     private void postAdd(final Object JavaDoc parent, final Object JavaDoc element) {
287         postRunnable(new Runnable JavaDoc() {
288             public void run() {
289                 // 1GF87WR: ITPUI:ALL - SWTEx + NPE closing a workbench window.
290
Control ctrl= fViewer.getControl();
291                 if (ctrl != null && !ctrl.isDisposed())
292                     fViewer.add(parent, element);
293             }
294         });
295     }
296
297     private void postRemove(final Object JavaDoc element) {
298         postRunnable(new Runnable JavaDoc() {
299             public void run() {
300                 // 1GF87WR: ITPUI:ALL - SWTEx + NPE closing a workbench window.
301
Control ctrl= fViewer.getControl();
302                 if (ctrl != null && !ctrl.isDisposed())
303                     fViewer.remove(element);
304             }
305         });
306     }
307
308     private void postRunnable(final Runnable JavaDoc r) {
309         Control ctrl= fViewer.getControl();
310         if (ctrl != null && !ctrl.isDisposed()) {
311             ctrl.getDisplay().asyncExec(r);
312         }
313     }
314 }
315
Popular Tags