1 11 package org.eclipse.search.internal.ui.text; 12 13 import java.util.HashMap ; 14 import java.util.HashSet ; 15 import java.util.Map ; 16 import java.util.Set ; 17 18 import org.eclipse.core.resources.IProject; 19 import org.eclipse.core.resources.IResource; 20 21 import org.eclipse.jface.viewers.AbstractTreeViewer; 22 import org.eclipse.jface.viewers.ITreeContentProvider; 23 import org.eclipse.jface.viewers.Viewer; 24 25 import org.eclipse.search.ui.text.AbstractTextSearchResult; 26 27 public class FileTreeContentProvider implements ITreeContentProvider, IFileSearchContentProvider { 28 29 private final Object [] EMPTY_ARR= new Object [0]; 30 31 private AbstractTextSearchResult fResult; 32 private FileSearchPage fPage; 33 private AbstractTreeViewer fTreeViewer; 34 private Map fChildrenMap; 35 36 FileTreeContentProvider(FileSearchPage page, AbstractTreeViewer viewer) { 37 fPage= page; 38 fTreeViewer= viewer; 39 } 40 41 public Object [] getElements(Object inputElement) { 42 Object [] children= getChildren(inputElement); 43 int elementLimit= getElementLimit(); 44 if (elementLimit != -1 && elementLimit < children.length) { 45 Object [] limitedChildren= new Object [elementLimit]; 46 System.arraycopy(children, 0, limitedChildren, 0, elementLimit); 47 return limitedChildren; 48 } 49 return children; 50 } 51 52 private int getElementLimit() { 53 return fPage.getElementLimit().intValue(); 54 } 55 56 public void dispose() { 57 } 59 60 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) { 61 if (newInput instanceof FileSearchResult) { 62 initialize((FileSearchResult) newInput); 63 } 64 } 65 66 67 protected synchronized void initialize(AbstractTextSearchResult result) { 68 fResult= result; 69 fChildrenMap= new HashMap (); 70 if (result != null) { 71 Object [] elements= result.getElements(); 72 for (int i= 0; i < elements.length; i++) { 73 insert(elements[i], false); 74 } 75 } 76 } 77 78 protected void insert(Object child, boolean refreshViewer) { 79 80 Object parent= getParent(child); 81 while (parent != null) { 82 if (insertChild(parent, child)) { 83 if (refreshViewer) 84 fTreeViewer.add(parent, child); 85 } else { 86 if (refreshViewer) 87 fTreeViewer.refresh(parent); 88 return; 89 } 90 child= parent; 91 parent= getParent(child); 92 } 93 if (insertChild(fResult, child)) { 94 if (refreshViewer) 95 fTreeViewer.add(fResult, child); 96 } 97 } 98 99 106 private boolean insertChild(Object parent, Object child) { 107 Set children= (Set ) fChildrenMap.get(parent); 108 if (children == null) { 109 children= new HashSet (); 110 fChildrenMap.put(parent, children); 111 } 112 return children.add(child); 113 } 114 115 protected void remove(Object element, boolean refreshViewer) { 116 118 if (hasChildren(element)) { 119 if (refreshViewer) 120 fTreeViewer.refresh(element); 121 } else { 122 if (fResult.getMatchCount(element) == 0) { 123 fChildrenMap.remove(element); 124 Object parent= getParent(element); 125 if (parent != null) { 126 removeFromSiblings(element, parent); 127 remove(parent, refreshViewer); 128 } else { 129 removeFromSiblings(element, fResult); 130 if (refreshViewer) 131 fTreeViewer.refresh(); 132 } 133 } else { 134 if (refreshViewer) { 135 fTreeViewer.refresh(element); 136 } 137 } 138 } 139 } 140 141 private void removeFromSiblings(Object element, Object parent) { 142 Set siblings= (Set ) fChildrenMap.get(parent); 143 if (siblings != null) { 144 siblings.remove(element); 145 } 146 } 147 148 public Object [] getChildren(Object parentElement) { 149 Set children= (Set ) fChildrenMap.get(parentElement); 150 if (children == null) 151 return EMPTY_ARR; 152 return children.toArray(); 153 } 154 155 public boolean hasChildren(Object element) { 156 return getChildren(element).length > 0; 157 } 158 159 public synchronized void elementsChanged(Object [] updatedElements) { 160 for (int i= 0; i < updatedElements.length; i++) { 161 if (fResult.getMatchCount(updatedElements[i]) > 0) 162 insert(updatedElements[i], true); 163 else 164 remove(updatedElements[i], true); 165 } 166 } 167 168 public void clear() { 169 initialize(fResult); 170 fTreeViewer.refresh(); 171 } 172 173 public Object getParent(Object element) { 174 if (element instanceof IProject) 175 return null; 176 if (element instanceof IResource) { 177 IResource resource = (IResource) element; 178 return resource.getParent(); 179 } 180 return null; 181 } 182 } 183 | Popular Tags |