KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > viewsupport > ProblemTreeViewer


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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.viewsupport;
13
14 import java.util.ArrayList JavaDoc;
15 import java.util.List JavaDoc;
16
17 import org.eclipse.core.resources.IResource;
18
19 import org.eclipse.swt.events.DisposeEvent;
20 import org.eclipse.swt.widgets.Composite;
21 import org.eclipse.swt.widgets.Item;
22 import org.eclipse.swt.widgets.Tree;
23 import org.eclipse.swt.widgets.Widget;
24
25 import org.eclipse.jface.viewers.IBaseLabelProvider;
26 import org.eclipse.jface.viewers.LabelProviderChangedEvent;
27 import org.eclipse.jface.viewers.TreeViewer;
28 import org.eclipse.jface.viewers.ViewerFilter;
29
30 import org.eclipse.jdt.core.IMember;
31
32 import org.eclipse.jdt.ui.IWorkingCopyProvider;
33 import org.eclipse.jdt.ui.ProblemsLabelDecorator.ProblemsLabelChangedEvent;
34
35
36 /**
37  * Extends a TreeViewer to allow more performance when showing error ticks.
38  * A <code>ProblemItemMapper</code> is contained that maps all items in
39  * the tree to underlying resource
40  */

41 public class ProblemTreeViewer extends TreeViewer implements ResourceToItemsMapper.IContentViewerAccessor {
42
43     protected ResourceToItemsMapper fResourceToItemsMapper;
44
45     /*
46      * @see TreeViewer#TreeViewer(Composite)
47      */

48     public ProblemTreeViewer(Composite parent) {
49         super(parent);
50         initMapper();
51     }
52
53     /*
54      * @see TreeViewer#TreeViewer(Composite, int)
55      */

56     public ProblemTreeViewer(Composite parent, int style) {
57         super(parent, style);
58         initMapper();
59     }
60
61     /*
62      * @see TreeViewer#TreeViewer(Tree)
63      */

64     public ProblemTreeViewer(Tree tree) {
65         super(tree);
66         initMapper();
67     }
68     
69     /* (non-Javadoc)
70      * @see org.eclipse.jdt.internal.ui.viewsupport.ResourceToItemsMapper.IContentViewerAccessor#doUpdateItem(org.eclipse.swt.widgets.Widget)
71      */

72     public void doUpdateItem(Widget item) {
73         doUpdateItem(item, item.getData(), true);
74     }
75     
76     private void initMapper() {
77         fResourceToItemsMapper= new ResourceToItemsMapper(this);
78     }
79     
80     
81     /*
82      * @see StructuredViewer#mapElement(Object, Widget)
83      */

84     protected void mapElement(Object JavaDoc element, Widget item) {
85         super.mapElement(element, item);
86         if (item instanceof Item) {
87             fResourceToItemsMapper.addToMap(element, (Item) item);
88         }
89     }
90
91     /*
92      * @see StructuredViewer#unmapElement(Object, Widget)
93      */

94     protected void unmapElement(Object JavaDoc element, Widget item) {
95         if (item instanceof Item) {
96             fResourceToItemsMapper.removeFromMap(element, (Item) item);
97         }
98         super.unmapElement(element, item);
99     }
100
101     /*
102      * @see StructuredViewer#unmapAllElements()
103      */

104     protected void unmapAllElements() {
105         fResourceToItemsMapper.clearMap();
106         super.unmapAllElements();
107     }
108     
109     
110     // ---------------- filter sessions ----------------------------
111

112     /* (non-Javadoc)
113      * @see org.eclipse.jface.viewers.StructuredViewer#addFilter(org.eclipse.jface.viewers.ViewerFilter)
114      */

115     public void addFilter(ViewerFilter filter) {
116         if (filter instanceof JavaViewerFilter) {
117             ((JavaViewerFilter) filter).filteringStart();
118         }
119         super.addFilter(filter);
120     }
121     
122     /* (non-Javadoc)
123      * @see org.eclipse.jface.viewers.StructuredViewer#removeFilter(org.eclipse.jface.viewers.ViewerFilter)
124      */

125     public void removeFilter(ViewerFilter filter) {
126         super.removeFilter(filter);
127         if (filter instanceof JavaViewerFilter) {
128             ((JavaViewerFilter) filter).filteringEnd();
129         }
130     }
131     
132     /* (non-Javadoc)
133      * @see org.eclipse.jface.viewers.StructuredViewer#setFilters(org.eclipse.jface.viewers.ViewerFilter[])
134      */

135     public void setFilters(ViewerFilter[] filters) {
136         ViewerFilter[] oldFilters= getFilters();
137         for (int i= 0; i < filters.length; i++) {
138             ViewerFilter curr= filters[i];
139             if (curr instanceof JavaViewerFilter && !findAndRemove(oldFilters, curr)) {
140                 ((JavaViewerFilter) curr).filteringStart();
141             }
142         }
143         endFilterSessions(oldFilters);
144         super.setFilters(filters);
145     }
146     
147     /* (non-Javadoc)
148      * @see org.eclipse.jface.viewers.StructuredViewer#resetFilters()
149      */

150     public void resetFilters() {
151         endFilterSessions(getFilters());
152         super.resetFilters();
153     }
154     
155     private boolean findAndRemove(ViewerFilter[] filters, ViewerFilter filter) {
156         for (int i= 0; i < filters.length; i++) {
157             if (filters[i] == filter) {
158                 filters[i]= null;
159                 return true;
160             }
161         }
162         return false;
163     }
164     
165     private void endFilterSessions(ViewerFilter[] filters) {
166         for (int i= 0; i < filters.length; i++) {
167             ViewerFilter curr= filters[i];
168             if (curr instanceof JavaViewerFilter) {
169                 ((JavaViewerFilter) curr).filteringEnd();
170             }
171         }
172     }
173     
174     /* (non-Javadoc)
175      * @see org.eclipse.jface.viewers.StructuredViewer#handleDispose(org.eclipse.swt.events.DisposeEvent)
176      */

177     protected void handleDispose(DisposeEvent event) {
178         endFilterSessions(getFilters());
179         super.handleDispose(event);
180     }
181     
182     
183     /*
184      * @see ContentViewer#handleLabelProviderChanged(LabelProviderChangedEvent)
185      */

186     protected void handleLabelProviderChanged(LabelProviderChangedEvent event) {
187         if (event instanceof ProblemsLabelChangedEvent) {
188             ProblemsLabelChangedEvent e= (ProblemsLabelChangedEvent) event;
189             if (!e.isMarkerChange() && canIgnoreChangesFromAnnotionModel()) {
190                 return;
191             }
192         }
193         Object JavaDoc[] changed= addAditionalProblemParents(event.getElements());
194         
195         if (changed != null && !fResourceToItemsMapper.isEmpty()) {
196             ArrayList JavaDoc others= new ArrayList JavaDoc();
197             for (int i= 0; i < changed.length; i++) {
198                 Object JavaDoc curr= changed[i];
199                 if (curr instanceof IResource) {
200                     fResourceToItemsMapper.resourceChanged((IResource) curr);
201                 } else {
202                     others.add(curr);
203                 }
204             }
205             if (others.isEmpty()) {
206                 return;
207             }
208             event= new LabelProviderChangedEvent((IBaseLabelProvider) event.getSource(), others.toArray());
209         } else {
210             // we have modified the list of changed elements via add additional parents.
211
if (event.getElements() != changed)
212                 event= new LabelProviderChangedEvent((IBaseLabelProvider) event.getSource(), changed);
213         }
214         super.handleLabelProviderChanged(event);
215     }
216     
217     /**
218      * Answers whether this viewer can ignore label provider changes resulting from
219      * marker changes in annotation models
220      * @return return <code>true</code> if annotation model marker changes can be ignored
221      */

222     private boolean canIgnoreChangesFromAnnotionModel() {
223         Object JavaDoc contentProvider= getContentProvider();
224         return contentProvider instanceof IWorkingCopyProvider && !((IWorkingCopyProvider)contentProvider).providesWorkingCopies();
225     }
226     
227         
228     /**
229      * Decides if {@link #isExpandable(Object)} should also test filters. The default behaviour is to
230      * do this only for IMembers. Implementors can replace this behaviour.
231      * @param parent the given element
232      * @return returns if if {@link #isExpandable(Object)} should also test filters for the given element.
233      */

234     protected boolean evaluateExpandableWithFilters(Object JavaDoc parent) {
235         return parent instanceof IMember;
236     }
237     
238     /* (non-Javadoc)
239      * @see org.eclipse.jface.viewers.AbstractTreeViewer#isExpandable(java.lang.Object)
240      */

241     public boolean isExpandable(Object JavaDoc parent) {
242         if (hasFilters() && evaluateExpandableWithFilters(parent)) {
243             // workaround for 65762
244
return hasFilteredChildren(parent);
245         }
246         return super.isExpandable(parent);
247     }
248     
249     protected final boolean hasFilteredChildren(Object JavaDoc parent) {
250         Object JavaDoc[] rawChildren= getRawChildren(parent);
251         return containsNonFiltered(rawChildren, parent);
252     }
253     
254     /* (non-Javadoc)
255      * @see org.eclipse.jface.viewers.AbstractTreeViewer#getFilteredChildren(java.lang.Object)
256      */

257     protected final Object JavaDoc[] getFilteredChildren(Object JavaDoc parent) {
258         return filter(getRawChildren(parent), parent);
259     }
260     
261     private Object JavaDoc[] filter(Object JavaDoc[] elements, Object JavaDoc parent) {
262         if (!hasFilters() || elements.length == 0) {
263             return elements;
264         }
265         List JavaDoc list= new ArrayList JavaDoc(elements.length);
266         ViewerFilter[] filters = getFilters();
267         for (int i = 0; i < elements.length; i++) {
268             Object JavaDoc object = elements[i];
269             if (!isFiltered(object, parent, filters)) {
270                 list.add(object);
271             }
272         }
273         return list.toArray();
274     }
275     
276     private boolean containsNonFiltered(Object JavaDoc[] elements, Object JavaDoc parent) {
277         if (elements.length == 0) {
278             return false;
279         }
280         if (!hasFilters()) {
281             return true;
282         }
283         ViewerFilter[] filters = getFilters();
284         for (int i = 0; i < elements.length; i++) {
285             Object JavaDoc object = elements[i];
286             if (!isFiltered(object, parent, filters)) {
287                 return true;
288             }
289         }
290         return false;
291     }
292     
293     /**
294      * All element filter tests must go through this method.
295      * Can be overridden by subclasses.
296      *
297      * @param object the object to filter
298      * @param parent the parent
299      * @param filters the filters to apply
300      * @return true if the element is filtered
301      */

302     protected boolean isFiltered(Object JavaDoc object, Object JavaDoc parent, ViewerFilter[] filters) {
303         for (int i = 0; i < filters.length; i++) {
304             ViewerFilter filter = filters[i];
305             if (!filter.select(this, parent, object))
306                 return true;
307         }
308         return false;
309     }
310     
311     /* (non-Javadoc)
312      * @see org.eclipse.jface.viewers.StructuredViewer#filter(java.lang.Object[])
313      */

314     protected final Object JavaDoc[] filter(Object JavaDoc[] elements) {
315         return filter(elements, getRoot());
316     }
317     
318     protected Object JavaDoc[] addAditionalProblemParents(Object JavaDoc[] elements) {
319         return elements;
320     }
321     
322     /**
323      * Public method to test if a element is filtered by the views active filters
324      * @param object the element to test for
325      * @param parent the parent element
326      * @return return <code>true if the element is filtered</code>
327      */

328     public boolean isFiltered(Object JavaDoc object, Object JavaDoc parent) {
329         return isFiltered(object, parent, getFilters());
330     }
331 }
332
333
Popular Tags