KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > search > internal > ui > text > FileTreeContentProvider


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 package org.eclipse.search.internal.ui.text;
12
13 import java.util.HashMap JavaDoc;
14 import java.util.HashSet JavaDoc;
15 import java.util.Map JavaDoc;
16 import java.util.Set JavaDoc;
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 JavaDoc[] EMPTY_ARR= new Object JavaDoc[0];
30
31     private AbstractTextSearchResult fResult;
32     private FileSearchPage fPage;
33     private AbstractTreeViewer fTreeViewer;
34     private Map JavaDoc fChildrenMap;
35     
36     FileTreeContentProvider(FileSearchPage page, AbstractTreeViewer viewer) {
37         fPage= page;
38         fTreeViewer= viewer;
39     }
40     
41     public Object JavaDoc[] getElements(Object JavaDoc inputElement) {
42         Object JavaDoc[] children= getChildren(inputElement);
43         int elementLimit= getElementLimit();
44         if (elementLimit != -1 && elementLimit < children.length) {
45             Object JavaDoc[] limitedChildren= new Object JavaDoc[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         // nothing to do
58
}
59     
60     public void inputChanged(Viewer viewer, Object JavaDoc oldInput, Object JavaDoc 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 JavaDoc();
70         if (result != null) {
71             Object JavaDoc[] 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 JavaDoc child, boolean refreshViewer) {
79
80         Object JavaDoc 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     /**
100      * returns true if the child already was a child of parent.
101      *
102      * @param parent
103      * @param child
104      * @return Returns <code>trye</code> if the child was added
105      */

106     private boolean insertChild(Object JavaDoc parent, Object JavaDoc child) {
107         Set JavaDoc children= (Set JavaDoc) fChildrenMap.get(parent);
108         if (children == null) {
109             children= new HashSet JavaDoc();
110             fChildrenMap.put(parent, children);
111         }
112         return children.add(child);
113     }
114
115     protected void remove(Object JavaDoc element, boolean refreshViewer) {
116         // precondition here: fResult.getMatchCount(child) <= 0
117

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 JavaDoc 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 JavaDoc element, Object JavaDoc parent) {
142         Set JavaDoc siblings= (Set JavaDoc) fChildrenMap.get(parent);
143         if (siblings != null) {
144             siblings.remove(element);
145         }
146     }
147
148     public Object JavaDoc[] getChildren(Object JavaDoc parentElement) {
149         Set JavaDoc children= (Set JavaDoc) fChildrenMap.get(parentElement);
150         if (children == null)
151             return EMPTY_ARR;
152         return children.toArray();
153     }
154
155     public boolean hasChildren(Object JavaDoc element) {
156         return getChildren(element).length > 0;
157     }
158
159     public synchronized void elementsChanged(Object JavaDoc[] 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 JavaDoc getParent(Object JavaDoc 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