0670c04faf23ddb9c59682f89ae860ad96642f97
[phpeclipse.git] / net.sourceforge.phpeclipse.ui / src / net / sourceforge / phpdt / internal / ui / viewsupport / ProblemTableViewer.java
1 /*******************************************************************************
2  * Copyright (c) 2000, 2004 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials 
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  * 
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11
12 package net.sourceforge.phpdt.internal.ui.viewsupport;
13
14 import java.util.ArrayList;
15
16 import net.sourceforge.phpdt.ui.IWorkingCopyProvider;
17 import net.sourceforge.phpdt.ui.ProblemsLabelDecorator.ProblemsLabelChangedEvent;
18
19 import org.eclipse.core.resources.IResource;
20 import org.eclipse.jface.viewers.IBaseLabelProvider;
21 import org.eclipse.jface.viewers.LabelProviderChangedEvent;
22 import org.eclipse.jface.viewers.TableViewer;
23 //import org.eclipse.swt.widgets.Composite;
24 import org.eclipse.swt.widgets.Item;
25 import org.eclipse.swt.widgets.Table;
26 import org.eclipse.swt.widgets.Widget;
27
28 /**
29  * Extends a TableViewer to allow more performance when showing error ticks. A
30  * <code>ProblemItemMapper</code> is contained that maps all items in the tree
31  * to underlying resource
32  */
33 public class ProblemTableViewer extends TableViewer {
34
35         protected ResourceToItemsMapper fResourceToItemsMapper;
36
37         /**
38          * Constructor for ProblemTableViewer.
39          * 
40          * @param parent
41          */
42 //      public ProblemTableViewer(Composite parent) {
43 //              super(parent);
44 //              initMapper();
45 //      }
46
47         /**
48          * Constructor for ProblemTableViewer.
49          * 
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          * 
61          * @param table
62          */
63         public ProblemTableViewer(Table table) {
64                 super(table);
65                 initMapper();
66         }
67
68         private void initMapper() {
69                 fResourceToItemsMapper = new ResourceToItemsMapper(this);
70         }
71
72         /*
73          * @see StructuredViewer#mapElement(Object, Widget)
74          */
75         protected void mapElement(Object element, Widget item) {
76                 super.mapElement(element, item);
77                 if (item instanceof Item) {
78                         fResourceToItemsMapper.addToMap(element, (Item) item);
79                 }
80         }
81
82         /*
83          * @see StructuredViewer#unmapElement(Object, Widget)
84          */
85         protected void unmapElement(Object element, Widget item) {
86                 if (item instanceof Item) {
87                         fResourceToItemsMapper.removeFromMap(element, (Item) item);
88                 }
89                 super.unmapElement(element, item);
90         }
91
92         /*
93          * @see StructuredViewer#unmapAllElements()
94          */
95         protected void unmapAllElements() {
96                 fResourceToItemsMapper.clearMap();
97                 super.unmapAllElements();
98         }
99
100         /*
101          * @see ContentViewer#handleLabelProviderChanged(LabelProviderChangedEvent)
102          */
103         protected void handleLabelProviderChanged(LabelProviderChangedEvent event) {
104                 if (event instanceof ProblemsLabelChangedEvent) {
105                         ProblemsLabelChangedEvent e = (ProblemsLabelChangedEvent) event;
106                         if (!e.isMarkerChange() && canIgnoreChangesFromAnnotionModel()) {
107                                 return;
108                         }
109                 }
110
111                 Object[] changed = event.getElements();
112                 if (changed != null && !fResourceToItemsMapper.isEmpty()) {
113                         ArrayList others = new ArrayList(changed.length);
114                         for (int i = 0; i < changed.length; i++) {
115                                 Object curr = changed[i];
116                                 if (curr instanceof IResource) {
117                                         fResourceToItemsMapper.resourceChanged((IResource) curr);
118                                 } else {
119                                         others.add(curr);
120                                 }
121                         }
122                         if (others.isEmpty()) {
123                                 return;
124                         }
125                         event = new LabelProviderChangedEvent((IBaseLabelProvider) event
126                                         .getSource(), others.toArray());
127                 }
128                 super.handleLabelProviderChanged(event);
129         }
130
131         /**
132          * Answers whether this viewer can ignore label provider changes resulting
133          * from marker changes in annotation models
134          */
135         private boolean canIgnoreChangesFromAnnotionModel() {
136                 Object contentProvider = getContentProvider();
137                 return contentProvider instanceof IWorkingCopyProvider
138                                 && !((IWorkingCopyProvider) contentProvider)
139                                                 .providesWorkingCopies();
140         }
141 }