KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > browsing > PackagesViewFlatContentProvider


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.internal.ui.browsing;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.List JavaDoc;
15
16 import org.eclipse.swt.widgets.Control;
17 import org.eclipse.swt.widgets.Display;
18
19 import org.eclipse.jface.viewers.IStructuredContentProvider;
20 import org.eclipse.jface.viewers.StructuredViewer;
21 import org.eclipse.jface.viewers.TableViewer;
22
23 import org.eclipse.jdt.core.IJavaElement;
24 import org.eclipse.jdt.core.IJavaElementDelta;
25 import org.eclipse.jdt.core.IJavaProject;
26 import org.eclipse.jdt.core.IPackageFragment;
27 import org.eclipse.jdt.core.IPackageFragmentRoot;
28 import org.eclipse.jdt.core.JavaModelException;
29
30 /**
31  * Table content provider for the hierarchical layout in the packages view.
32  * <p>
33  * XXX: The standard Java browsing part content provider needs and calls
34  * the browsing part/view. This class currently doesn't need to do so
35  * but might be required to later.
36  * </p>
37  */

38 class PackagesViewFlatContentProvider extends LogicalPackagesProvider implements IStructuredContentProvider {
39     PackagesViewFlatContentProvider(StructuredViewer viewer) {
40         super(viewer);
41     }
42
43     /*
44      * @see org.eclipse.jface.viewers.ITreeContentProvider#getChildren(java.lang.Object)
45      */

46     public Object JavaDoc[] getChildren(Object JavaDoc parentElement) {
47
48         if(parentElement instanceof IJavaElement){
49             IJavaElement element= (IJavaElement) parentElement;
50
51             int type= element.getElementType();
52
53             try {
54                 switch (type) {
55                     case IJavaElement.JAVA_PROJECT :
56                         IJavaProject project= (IJavaProject) element;
57                         IPackageFragment[] children= getPackageFragments(project.getPackageFragments());
58                         if(isInCompoundState()) {
59                             fMapToLogicalPackage.clear();
60                             fMapToPackageFragments.clear();
61                             return combineSamePackagesIntoLogialPackages(children);
62                         } else
63                             return children;
64
65                     case IJavaElement.PACKAGE_FRAGMENT_ROOT :
66                         fMapToLogicalPackage.clear();
67                         fMapToPackageFragments.clear();
68                         IPackageFragmentRoot root= (IPackageFragmentRoot) element;
69                         return root.getChildren();
70
71                     case IJavaElement.PACKAGE_FRAGMENT :
72                         //no children in flat view
73
break;
74
75                     default :
76                         //do nothing, empty array returned
77
}
78             } catch (JavaModelException e) {
79                 return NO_CHILDREN;
80             }
81
82         }
83         return NO_CHILDREN;
84     }
85
86     /*
87      * Weeds out packageFragments from external jars
88      */

89     private IPackageFragment[] getPackageFragments(IPackageFragment[] iPackageFragments) {
90         List JavaDoc list= new ArrayList JavaDoc();
91         for (int i= 0; i < iPackageFragments.length; i++) {
92             IPackageFragment fragment= iPackageFragments[i];
93             IJavaElement el= fragment.getParent();
94             if (el instanceof IPackageFragmentRoot) {
95                 IPackageFragmentRoot root= (IPackageFragmentRoot) el;
96                 if(root.isArchive() && root.isExternal())
97                     continue;
98             }
99             list.add(fragment);
100         }
101         return (IPackageFragment[]) list.toArray(new IPackageFragment[list.size()]);
102     }
103
104     /*
105      * @see org.eclipse.jface.viewers.IStructuredContentProvider#getElements(java.lang.Object)
106      */

107     public Object JavaDoc[] getElements(Object JavaDoc inputElement) {
108         return getChildren(inputElement);
109     }
110
111     protected void processDelta(IJavaElementDelta delta) throws JavaModelException {
112
113         int kind= delta.getKind();
114         final IJavaElement element= delta.getElement();
115
116         if (isClassPathChange(delta)) {
117             Object JavaDoc input= fViewer.getInput();
118             if (input != null) {
119                 if (fInputIsProject && input.equals(element.getJavaProject())) {
120                     postRefresh(input);
121                     return;
122                 } else if (!fInputIsProject && input.equals(element)) {
123                     if (element.exists())
124                         postRefresh(input);
125                     else
126                         postRemove(input);
127                     return;
128                 }
129             }
130         }
131
132         if (kind == IJavaElementDelta.REMOVED) {
133             Object JavaDoc input= fViewer.getInput();
134             if (input != null && input.equals(element)) {
135                     postRemove(input);
136                     return;
137             }
138         }
139
140         if (element instanceof IPackageFragment) {
141             final IPackageFragment frag= (IPackageFragment) element;
142
143             if (kind == IJavaElementDelta.REMOVED) {
144                 removeElement(frag);
145
146             } else if (kind == IJavaElementDelta.ADDED) {
147                 addElement(frag);
148
149             } else if (kind == IJavaElementDelta.CHANGED) {
150                 //just refresh
151
Object JavaDoc toBeRefreshed= element;
152
153                 IPackageFragment pkgFragment= (IPackageFragment) element;
154                 LogicalPackage logicalPkg= findLogicalPackage(pkgFragment);
155                 //deal with packages that have been filtered and are now visible
156
if (logicalPkg != null)
157                     toBeRefreshed= findElementToRefresh(logicalPkg);
158                 else
159                     toBeRefreshed= findElementToRefresh(pkgFragment);
160
161                 postRefresh(toBeRefreshed);
162             }
163             //in this view there will be no children of PackageFragment to refresh
164
return;
165         }
166         processAffectedChildren(delta);
167     }
168
169     //test to see if element to be refreshed is being filtered out
170
//and if so refresh the viewers input element (JavaProject or PackageFragmentRoot)
171
private Object JavaDoc findElementToRefresh(IPackageFragment fragment) {
172         if (fViewer.testFindItem(fragment) == null) {
173             if(fInputIsProject)
174                 return fragment.getJavaProject();
175             else return fragment.getParent();
176         }
177         return fragment;
178     }
179
180     //test to see if element to be refreshed is being filtered out
181
//and if so refresh the viewers input element (JavaProject or PackageFragmentRoot)
182
private Object JavaDoc findElementToRefresh(LogicalPackage logicalPackage) {
183         if (fViewer.testFindItem(logicalPackage) == null) {
184             IPackageFragment fragment= logicalPackage.getFragments()[0];
185             return fragment.getJavaProject();
186         }
187         return logicalPackage;
188     }
189
190
191     private void processAffectedChildren(IJavaElementDelta delta) throws JavaModelException {
192         IJavaElementDelta[] children= delta.getAffectedChildren();
193         for (int i= 0; i < children.length; i++) {
194             IJavaElementDelta elementDelta= children[i];
195             processDelta(elementDelta);
196         }
197     }
198
199     private void postAdd(final Object JavaDoc child) {
200         postRunnable(new Runnable JavaDoc() {
201             public void run() {
202                 Control ctrl = fViewer.getControl();
203                 if (ctrl != null && !ctrl.isDisposed()) {
204                     ((TableViewer)fViewer).add(child);
205                 }
206             }
207         });
208     }
209
210
211     private void postRemove(final Object JavaDoc object) {
212         postRunnable(new Runnable JavaDoc() {
213             public void run() {
214                 Control ctrl = fViewer.getControl();
215                 if (ctrl != null && !ctrl.isDisposed()) {
216                     ((TableViewer)fViewer).remove(object);
217                 }
218             }
219         });
220     }
221
222     private void postRunnable(final Runnable JavaDoc r) {
223         Control ctrl= fViewer.getControl();
224         if (ctrl != null && !ctrl.isDisposed()) {
225         // fBrowsingPart.setProcessSelectionEvents(false);
226
try {
227                 Display currentDisplay= Display.getCurrent();
228                 if (currentDisplay != null && currentDisplay.equals(ctrl.getDisplay()))
229                     ctrl.getDisplay().syncExec(r);
230                 else
231                     ctrl.getDisplay().asyncExec(r);
232             } finally {
233         // fBrowsingPart.setProcessSelectionEvents(true);
234
}
235         }
236     }
237
238     private void removeElement(IPackageFragment frag) {
239         String JavaDoc key= getKey(frag);
240         LogicalPackage lp= (LogicalPackage)fMapToLogicalPackage.get(key);
241
242         if(lp != null){
243             lp.remove(frag);
244             //if you need to change the LogicalPackage to a PackageFragment
245
if(lp.getFragments().length == 1){
246                 IPackageFragment fragment= lp.getFragments()[0];
247                 fMapToLogicalPackage.remove(key);
248                 fMapToPackageFragments.put(key,fragment);
249
250                 //@Improve: Should I replace this with a refresh of the parent?
251
postRemove(lp);
252                 postAdd(fragment);
253             } return;
254         } else {
255             fMapToPackageFragments.remove(key);
256             postRemove(frag);
257         }
258     }
259
260
261     private void postRefresh(final Object JavaDoc element) {
262         postRunnable(new Runnable JavaDoc() {
263             public void run() {
264                 Control ctrl= fViewer.getControl();
265                 if (ctrl != null && !ctrl.isDisposed()) {
266                     fViewer.refresh(element);
267                 }
268             }
269         });
270     }
271
272     private void addElement(IPackageFragment frag) {
273         String JavaDoc key= getKey(frag);
274         LogicalPackage lp= (LogicalPackage)fMapToLogicalPackage.get(key);
275
276         if(lp != null && lp.belongs(frag)){
277             lp.add(frag);
278             return;
279         }
280
281         IPackageFragment fragment= (IPackageFragment)fMapToPackageFragments.get(key);
282         if(fragment != null){
283             //must create a new LogicalPackage
284
if(!fragment.equals(frag)){
285                 lp= new LogicalPackage(fragment);
286                 lp.add(frag);
287                 fMapToLogicalPackage.put(key, lp);
288
289                 //@Improve: should I replace this with a refresh?
290
postRemove(fragment);
291                 postAdd(lp);
292
293                 return;
294             }
295         }
296
297         else {
298             fMapToPackageFragments.put(key, frag);
299             postAdd(frag);
300         }
301     }
302 }
303
Popular Tags