1 11 12 package org.eclipse.jdt.internal.ui.search; 13 14 import java.util.HashMap ; 15 import java.util.HashSet ; 16 import java.util.Iterator ; 17 import java.util.Map ; 18 import java.util.Set ; 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 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 getParent(Object 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 getParent(Object child) { 68 Object 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 internalGetParent(Object child) { 98 return fContentProvider.getParent(child); 99 } 100 101 public Object [] getElements(Object inputElement) { 102 return getChildren(inputElement); 103 } 104 105 protected synchronized void initialize(JavaSearchResult result) { 106 super.initialize(result); 107 fChildrenMap= new HashMap (); 108 if (result != null) { 109 Object [] 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 toAdd, Set toUpdate, Object child) { 119 Object 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 parent, Object child) { 139 return insertInto(parent, child, fChildrenMap); 140 } 141 142 private boolean insertInto(Object parent, Object child, Map map) { 143 Set children= (Set ) map.get(parent); 144 if (children == null) { 145 children= new HashSet (); 146 map.put(parent, children); 147 } 148 return children.add(child); 149 } 150 151 protected void remove(Set toRemove, Set toUpdate, Object element) { 152 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 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 184 private boolean removeFromSiblings(Object element, Object parent) { 185 Set siblings= (Set ) fChildrenMap.get(parent); 186 if (siblings != null) { 187 return siblings.remove(element); 188 } else { 189 return false; 190 } 191 } 192 193 public Object [] getChildren(Object parentElement) { 194 Set children= (Set ) 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 [] limitedArray= new Object [limit]; 200 Iterator 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 element) { 211 Set children= (Set ) fChildrenMap.get(element); 212 return children != null && !children.isEmpty(); 213 } 214 215 public synchronized void elementsChanged(Object [] updatedElements) { 216 if (getSearchResult() == null) 217 return; 218 219 AbstractTreeViewer viewer= (AbstractTreeViewer) getPage().getViewer(); 220 221 Set toRemove= new HashSet (); 222 Set toUpdate= new HashSet (); 223 Map toAdd= new HashMap (); 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 iter= toAdd.keySet().iterator(); iter.hasNext();) { 233 Object parent= iter.next(); 234 HashSet children= (HashSet ) toAdd.get(parent); 235 viewer.add(parent, children.toArray()); 236 } 237 for (Iterator 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 |