KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > search > LevelTreeContentProvider


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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
12 package org.eclipse.jdt.internal.ui.search;
13
14 import java.util.HashMap JavaDoc;
15 import java.util.HashSet JavaDoc;
16 import java.util.Iterator JavaDoc;
17 import java.util.Map JavaDoc;
18 import java.util.Set JavaDoc;
19
20 import org.eclipse.core.resources.IResource;
21
22 import org.eclipse.jface.viewers.AbstractTreeViewer;
23 import org.eclipse.jface.viewers.ITreeContentProvider;
24
25 import org.eclipse.jdt.core.IClassFile;
26 import org.eclipse.jdt.core.ICompilationUnit;
27 import org.eclipse.jdt.core.IJavaElement;
28 import org.eclipse.jdt.core.IType;
29
30 import org.eclipse.jdt.ui.StandardJavaElementContentProvider;
31
32 public class LevelTreeContentProvider extends JavaSearchContentProvider implements ITreeContentProvider {
33     private Map JavaDoc fChildrenMap;
34     private StandardJavaElementContentProvider fContentProvider;
35     
36     public static final int LEVEL_TYPE= 1;
37     public static final int LEVEL_FILE= 2;
38     public static final int LEVEL_PACKAGE= 3;
39     public static final int LEVEL_PROJECT= 4;
40     
41     private static final int[][] JAVA_ELEMENT_TYPES= {{IJavaElement.TYPE},
42             {IJavaElement.CLASS_FILE, IJavaElement.COMPILATION_UNIT},
43             {IJavaElement.PACKAGE_FRAGMENT},
44             {IJavaElement.JAVA_PROJECT, IJavaElement.PACKAGE_FRAGMENT_ROOT},
45             {IJavaElement.JAVA_MODEL}};
46     private static final int[][] RESOURCE_TYPES= {
47             {},
48             {IResource.FILE},
49             {IResource.FOLDER},
50             {IResource.PROJECT},
51             {IResource.ROOT}};
52     
53     private static final int MAX_LEVEL= JAVA_ELEMENT_TYPES.length - 1;
54     private int fCurrentLevel;
55     static class FastJavaElementProvider extends StandardJavaElementContentProvider {
56         public Object JavaDoc getParent(Object JavaDoc element) {
57             return internalGetParent(element);
58         }
59     }
60
61     public LevelTreeContentProvider(JavaSearchResultPage page, int level) {
62         super(page);
63         fCurrentLevel= level;
64         fContentProvider= new FastJavaElementProvider();
65     }
66
67     public Object JavaDoc getParent(Object JavaDoc child) {
68         Object JavaDoc possibleParent= internalGetParent(child);
69         if (possibleParent instanceof IJavaElement) {
70             IJavaElement javaElement= (IJavaElement) possibleParent;
71             for (int j= fCurrentLevel; j < MAX_LEVEL + 1; j++) {
72                 for (int i= 0; i < JAVA_ELEMENT_TYPES[j].length; i++) {
73                     if (javaElement.getElementType() == JAVA_ELEMENT_TYPES[j][i]) {
74                         return null;
75                     }
76                 }
77             }
78         } else if (possibleParent instanceof IResource) {
79             IResource resource= (IResource) possibleParent;
80             for (int j= fCurrentLevel; j < MAX_LEVEL + 1; j++) {
81                 for (int i= 0; i < RESOURCE_TYPES[j].length; i++) {
82                     if (resource.getType() == RESOURCE_TYPES[j][i]) {
83                         return null;
84                     }
85                 }
86             }
87         }
88         if (fCurrentLevel != LEVEL_FILE && child instanceof IType) {
89             IType type= (IType) child;
90             if (possibleParent instanceof ICompilationUnit
91                     || possibleParent instanceof IClassFile)
92                 possibleParent= type.getPackageFragment();
93         }
94         return possibleParent;
95     }
96
97     private Object JavaDoc internalGetParent(Object JavaDoc child) {
98         return fContentProvider.getParent(child);
99     }
100
101     public Object JavaDoc[] getElements(Object JavaDoc inputElement) {
102         return getChildren(inputElement);
103     }
104
105     protected synchronized void initialize(JavaSearchResult result) {
106         super.initialize(result);
107         fChildrenMap= new HashMap JavaDoc();
108         if (result != null) {
109             Object JavaDoc[] elements= result.getElements();
110             for (int i= 0; i < elements.length; i++) {
111                 if (getPage().getDisplayedMatchCount(elements[i]) > 0) {
112                     insert(null, null, elements[i]);
113                 }
114             }
115         }
116     }
117
118     protected void insert(Map JavaDoc toAdd, Set JavaDoc toUpdate, Object JavaDoc child) {
119         Object JavaDoc parent= getParent(child);
120         while (parent != null) {
121             if (insertChild(parent, child)) {
122                 if (toAdd != null)
123                     insertInto(parent, child, toAdd);
124             } else {
125                 if (toUpdate != null)
126                     toUpdate.add(parent);
127                 return;
128             }
129             child= parent;
130             parent= getParent(child);
131         }
132         if (insertChild(getSearchResult(), child)) {
133             if (toAdd != null)
134                 insertInto(getSearchResult(), child, toAdd);
135         }
136     }
137
138     private boolean insertChild(Object JavaDoc parent, Object JavaDoc child) {
139         return insertInto(parent, child, fChildrenMap);
140     }
141
142     private boolean insertInto(Object JavaDoc parent, Object JavaDoc child, Map JavaDoc map) {
143         Set JavaDoc children= (Set JavaDoc) map.get(parent);
144         if (children == null) {
145             children= new HashSet JavaDoc();
146             map.put(parent, children);
147         }
148         return children.add(child);
149     }
150
151     protected void remove(Set JavaDoc toRemove, Set JavaDoc toUpdate, Object JavaDoc element) {
152         // precondition here: fResult.getMatchCount(child) <= 0
153

154         if (hasChildren(element)) {
155             if (toUpdate != null)
156                 toUpdate.add(element);
157         } else {
158             if (getPage().getDisplayedMatchCount(element) == 0) {
159                 fChildrenMap.remove(element);
160                 Object JavaDoc parent= getParent(element);
161                 if (parent != null) {
162                     if (removeFromSiblings(element, parent)) {
163                         remove(toRemove, toUpdate, parent);
164                     }
165                 } else {
166                     if (removeFromSiblings(element, getSearchResult())) {
167                         if (toRemove != null)
168                             toRemove.add(element);
169                     }
170                 }
171             } else {
172                 if (toUpdate != null) {
173                     toUpdate.add(element);
174                 }
175             }
176         }
177     }
178
179     /**
180      * @param element
181      * @param parent
182      * @return returns true if it really was a remove (i.e. element was a child of parent).
183      */

184     private boolean removeFromSiblings(Object JavaDoc element, Object JavaDoc parent) {
185         Set JavaDoc siblings= (Set JavaDoc) fChildrenMap.get(parent);
186         if (siblings != null) {
187             return siblings.remove(element);
188         } else {
189             return false;
190         }
191     }
192
193     public Object JavaDoc[] getChildren(Object JavaDoc parentElement) {
194         Set JavaDoc children= (Set JavaDoc) fChildrenMap.get(parentElement);
195         if (children == null)
196             return EMPTY_ARR;
197         int limit= getPage().getElementLimit().intValue();
198         if (limit != -1 && limit < children.size()) {
199             Object JavaDoc[] limitedArray= new Object JavaDoc[limit];
200             Iterator JavaDoc iterator= children.iterator();
201             for (int i= 0; i < limit; i++) {
202                 limitedArray[i]= iterator.next();
203             }
204             return limitedArray;
205         }
206         
207         return children.toArray();
208     }
209
210     public boolean hasChildren(Object JavaDoc element) {
211         Set JavaDoc children= (Set JavaDoc) fChildrenMap.get(element);
212         return children != null && !children.isEmpty();
213     }
214
215     public synchronized void elementsChanged(Object JavaDoc[] updatedElements) {
216         if (getSearchResult() == null)
217             return;
218         
219         AbstractTreeViewer viewer= (AbstractTreeViewer) getPage().getViewer();
220
221         Set JavaDoc toRemove= new HashSet JavaDoc();
222         Set JavaDoc toUpdate= new HashSet JavaDoc();
223         Map JavaDoc toAdd= new HashMap JavaDoc();
224         for (int i= 0; i < updatedElements.length; i++) {
225             if (getPage().getDisplayedMatchCount(updatedElements[i]) > 0)
226                 insert(toAdd, toUpdate, updatedElements[i]);
227             else
228                 remove(toRemove, toUpdate, updatedElements[i]);
229         }
230         
231         viewer.remove(toRemove.toArray());
232         for (Iterator JavaDoc iter= toAdd.keySet().iterator(); iter.hasNext();) {
233             Object JavaDoc parent= iter.next();
234             HashSet JavaDoc children= (HashSet JavaDoc) toAdd.get(parent);
235             viewer.add(parent, children.toArray());
236         }
237         for (Iterator JavaDoc elementsToUpdate= toUpdate.iterator(); elementsToUpdate.hasNext();) {
238             viewer.refresh(elementsToUpdate.next());
239         }
240         
241     }
242     
243     public void clear() {
244         initialize(getSearchResult());
245         getPage().getViewer().refresh();
246     }
247
248     public void setLevel(int level) {
249         fCurrentLevel= level;
250         initialize(getSearchResult());
251         getPage().getViewer().refresh();
252     }
253
254 }
255
Popular Tags