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
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
12 package net.sourceforge.phpdt.internal.ui.viewsupport;
14 import java.util.ArrayList;
16 import net.sourceforge.phpdt.core.IMember;
17 import net.sourceforge.phpdt.ui.IWorkingCopyProvider;
18 import net.sourceforge.phpdt.ui.ProblemsLabelDecorator.ProblemsLabelChangedEvent;
20 import org.eclipse.core.resources.IResource;
21 import org.eclipse.jface.viewers.IBaseLabelProvider;
22 import org.eclipse.jface.viewers.LabelProviderChangedEvent;
23 import org.eclipse.jface.viewers.TreeViewer;
24 import org.eclipse.swt.widgets.Composite;
25 import org.eclipse.swt.widgets.Item;
26 import org.eclipse.swt.widgets.Tree;
27 import org.eclipse.swt.widgets.Widget;
30 * Extends a TreeViewer to allow more performance when showing error ticks. A
31 * <code>ProblemItemMapper</code> is contained that maps all items in the tree
32 * to underlying resource
34 public class ProblemTreeViewer extends TreeViewer {
36 protected ResourceToItemsMapper fResourceToItemsMapper;
39 * @see TreeViewer#TreeViewer(Composite)
41 public ProblemTreeViewer(Composite parent) {
47 * @see TreeViewer#TreeViewer(Composite, int)
49 public ProblemTreeViewer(Composite parent, int style) {
55 * @see TreeViewer#TreeViewer(Tree)
57 public ProblemTreeViewer(Tree tree) {
62 private void initMapper() {
63 fResourceToItemsMapper = new ResourceToItemsMapper(this);
67 * @see StructuredViewer#mapElement(Object, Widget)
69 protected void mapElement(Object element, Widget item) {
70 super.mapElement(element, item);
71 if (item instanceof Item) {
72 fResourceToItemsMapper.addToMap(element, (Item) item);
77 * @see StructuredViewer#unmapElement(Object, Widget)
79 protected void unmapElement(Object element, Widget item) {
80 if (item instanceof Item) {
81 fResourceToItemsMapper.removeFromMap(element, (Item) item);
83 super.unmapElement(element, item);
87 * @see StructuredViewer#unmapAllElements()
89 protected void unmapAllElements() {
90 fResourceToItemsMapper.clearMap();
91 super.unmapAllElements();
95 * @see ContentViewer#handleLabelProviderChanged(LabelProviderChangedEvent)
97 protected void handleLabelProviderChanged(LabelProviderChangedEvent event) {
98 if (event instanceof ProblemsLabelChangedEvent) {
99 ProblemsLabelChangedEvent e = (ProblemsLabelChangedEvent) event;
100 if (!e.isMarkerChange() && canIgnoreChangesFromAnnotionModel()) {
105 Object[] changed = event.getElements();
106 if (changed != null && !fResourceToItemsMapper.isEmpty()) {
107 ArrayList others = new ArrayList();
108 for (int i = 0; i < changed.length; i++) {
109 Object curr = changed[i];
110 if (curr instanceof IResource) {
111 fResourceToItemsMapper.resourceChanged((IResource) curr);
116 if (others.isEmpty()) {
119 event = new LabelProviderChangedEvent((IBaseLabelProvider) event
120 .getSource(), others.toArray());
122 super.handleLabelProviderChanged(event);
126 * Answers whether this viewer can ignore label provider changes resulting
127 * from marker changes in annotation models
129 private boolean canIgnoreChangesFromAnnotionModel() {
130 Object contentProvider = getContentProvider();
131 return contentProvider instanceof IWorkingCopyProvider
132 && !((IWorkingCopyProvider) contentProvider)
133 .providesWorkingCopies();
139 * @see org.eclipse.jface.viewers.AbstractTreeViewer#isExpandable(java.lang.Object)
141 public boolean isExpandable(Object element) {
142 // workaround for 65762
143 if (hasFilters() && element instanceof IMember) {
144 return getFilteredChildren(element).length > 0;
146 return super.isExpandable(element);