KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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
16 import org.eclipse.core.resources.IResource;
17
18 import org.eclipse.swt.widgets.Composite;
19 import org.eclipse.swt.widgets.Item;
20 import org.eclipse.swt.widgets.Table;
21 import org.eclipse.swt.widgets.Widget;
22
23 import org.eclipse.jface.viewers.IBaseLabelProvider;
24 import org.eclipse.jface.viewers.LabelProviderChangedEvent;
25 import org.eclipse.jface.viewers.TableViewer;
26
27 import org.eclipse.jdt.ui.IWorkingCopyProvider;
28 import org.eclipse.jdt.ui.ProblemsLabelDecorator.ProblemsLabelChangedEvent;
29
30 /**
31  * Extends a TableViewer to allow more performance when showing error ticks.
32  * A <code>ProblemItemMapper</code> is contained that maps all items in
33  * the tree to underlying resource
34  */

35 public class ProblemTableViewer extends TableViewer implements ResourceToItemsMapper.IContentViewerAccessor {
36
37     protected ResourceToItemsMapper fResourceToItemsMapper;
38
39     /**
40      * Constructor for ProblemTableViewer.
41      * @param parent
42      */

43     public ProblemTableViewer(Composite parent) {
44         super(parent);
45         initMapper();
46     }
47
48     /**
49      * Constructor for ProblemTableViewer.
50      * @param parent
51      * @param style
52      */

53     public ProblemTableViewer(Composite parent, int style) {
54         super(parent, style);
55         initMapper();
56     }
57
58     /**
59      * Constructor for ProblemTableViewer.
60      * @param table
61      */

62     public ProblemTableViewer(Table table) {
63         super(table);
64         initMapper();
65     }
66
67     private void initMapper() {
68         fResourceToItemsMapper= new ResourceToItemsMapper(this);
69     }
70     
71     /* (non-Javadoc)
72      * @see org.eclipse.jdt.internal.ui.viewsupport.ResourceToItemsMapper.IContentViewerAccessor#doUpdateItem(org.eclipse.swt.widgets.Widget)
73      */

74     public void doUpdateItem(Widget item) {
75         doUpdateItem(item, item.getData(), true);
76     }
77     
78     /*
79      * @see StructuredViewer#mapElement(Object, Widget)
80      */

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

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

101     protected void unmapAllElements() {
102         fResourceToItemsMapper.clearMap();
103         super.unmapAllElements();
104     }
105     
106     /*
107      * @see ContentViewer#handleLabelProviderChanged(LabelProviderChangedEvent)
108      */

109     protected void handleLabelProviderChanged(LabelProviderChangedEvent event) {
110         if (event instanceof ProblemsLabelChangedEvent) {
111             ProblemsLabelChangedEvent e= (ProblemsLabelChangedEvent) event;
112             if (!e.isMarkerChange() && canIgnoreChangesFromAnnotionModel()) {
113                 return;
114             }
115         }
116         
117         Object JavaDoc[] changed= event.getElements();
118         if (changed != null && !fResourceToItemsMapper.isEmpty()) {
119             ArrayList JavaDoc others= new ArrayList JavaDoc(changed.length);
120             for (int i= 0; i < changed.length; i++) {
121                 Object JavaDoc curr= changed[i];
122                 if (curr instanceof IResource) {
123                     fResourceToItemsMapper.resourceChanged((IResource) curr);
124                 } else {
125                     others.add(curr);
126                 }
127             }
128             if (others.isEmpty()) {
129                 return;
130             }
131             event= new LabelProviderChangedEvent((IBaseLabelProvider) event.getSource(), others.toArray());
132         }
133         super.handleLabelProviderChanged(event);
134     }
135     
136     /**
137      * Answers whether this viewer can ignore label provider changes resulting from
138      * marker changes in annotation models
139      */

140     private boolean canIgnoreChangesFromAnnotionModel() {
141         Object JavaDoc contentProvider= getContentProvider();
142         return contentProvider instanceof IWorkingCopyProvider && !((IWorkingCopyProvider)contentProvider).providesWorkingCopies();
143     }
144 }
145
Popular Tags