2 * Copyright (c) 2004 Christopher Lenz 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 * Christopher Lenz - initial implementation
11 * $Id: ProblemsLabelDecorator.java,v 1.3 2006-10-21 23:13:54 pombredanne Exp $
14 package net.sourceforge.phpeclipse.ui.views.outline;
16 import java.util.ArrayList;
17 import java.util.Iterator;
18 import java.util.List;
20 import net.sourceforge.phpeclipse.core.model.ISourceReference;
21 import net.sourceforge.phpeclipse.ui.WebUI;
22 import net.sourceforge.phpeclipse.ui.views.util.ImageDescriptorRegistry;
23 import net.sourceforge.phpeclipse.ui.views.util.ImageImageDescriptor;
24 import net.sourceforge.phpeclipse.ui.views.util.OverlayImageDescriptor;
26 import org.eclipse.jface.resource.ImageDescriptor;
27 import org.eclipse.jface.text.IRegion;
28 import org.eclipse.jface.text.Position;
29 import org.eclipse.jface.text.source.Annotation;
30 import org.eclipse.jface.text.source.IAnnotationModel;
31 import org.eclipse.jface.viewers.ILabelDecorator;
32 import org.eclipse.jface.viewers.LabelProvider;
33 import org.eclipse.swt.graphics.Image;
34 import org.eclipse.ui.IEditorInput;
35 import org.eclipse.ui.texteditor.ITextEditor;
38 * Label decorator for the outline page that adds error and warning overlay
39 * icons to elements in the outline. The information is retrieved from the
40 * annotation model corresponding to the input of the associated text editor.
42 public class ProblemsLabelDecorator extends LabelProvider implements
45 // Constants ---------------------------------------------------------------
47 private static final String ANNOTATION_TYPE_ERROR = "org.eclipse.ui.workbench.texteditor.error"; //$NON-NLS-1$
49 private static final String ANNOTATION_TYPE_WARNING = "org.eclipse.ui.workbench.texteditor.warning"; //$NON-NLS-1$
51 // Instance Variables ------------------------------------------------------
53 /** The associated text editor if the decorator is used in the outline. */
54 private ITextEditor editor;
56 /** Registry of icons and overlay icons. */
57 private ImageDescriptorRegistry registry = new ImageDescriptorRegistry();
59 // Constructors ------------------------------------------------------------
65 * the associated text editor
67 public ProblemsLabelDecorator(ITextEditor editor) {
71 // ILabelDecorator Implementation ------------------------------------------
74 * @see org.eclipse.jface.viewers.IBaseLabelProvider#dispose()
76 public void dispose() {
82 * @see ILabelDecorator#decorateImage(Image, Object)
84 public Image decorateImage(Image image, Object element) {
85 Annotation annotations[] = getAssociatedAnnotations((ISourceReference) element);
86 ImageDescriptor overlay = null;
87 for (int i = 0; i < annotations.length; i++) {
88 if (isError(annotations[i])) {
89 overlay = WebUI.getDefault().getImageRegistry().getDescriptor(
90 WebUI.ICON_OVERLAY_ERROR);
91 } else if (isWarning(annotations[i])) {
92 overlay = WebUI.getDefault().getImageRegistry().getDescriptor(
93 WebUI.ICON_OVERLAY_WARNING);
96 if (overlay != null) {
97 ImageDescriptor base = new ImageImageDescriptor(image);
98 return registry.get(new OverlayImageDescriptor(base,
99 new ImageDescriptor[][] { null, null, { overlay }, null },
107 * @see ILabelDecorator#decorateText(String, Object)
109 public String decorateText(String text, Object element) {
113 // Private Methods ---------------------------------------------------------
116 * Returns all annotations associated with the given model element.
119 * the model element for which annotations should be collected
120 * @return an array containing all annotations for the given element, or an
121 * empty array if no annotations are found
123 private Annotation[] getAssociatedAnnotations(ISourceReference element) {
124 List retVal = new ArrayList();
125 if (editor != null) {
126 IEditorInput input = editor.getEditorInput();
127 IAnnotationModel model = editor.getDocumentProvider()
128 .getAnnotationModel(input);
129 if (model != null) { // bug #1120670
130 for (Iterator i = model.getAnnotationIterator(); i.hasNext();) {
131 Annotation annotation = (Annotation) i.next();
132 Position pos = model.getPosition(annotation);
133 if (pos != null && isInside(pos.getOffset(), element)) {
134 retVal.add(annotation);
139 return (Annotation[]) retVal.toArray(new Annotation[retVal.size()]);
143 * Determines whether the given annotation is an error.
146 * the annotation to check
147 * @return <tt>true</tt> if the annotation is to be displayed as an error,
148 * <tt>false</tt> otherwise
150 private boolean isError(Annotation annotation) {
151 return ANNOTATION_TYPE_ERROR.equals(annotation.getType());
155 * Determines whether the given annotation is a warning.
158 * the annotation to check
159 * @return <tt>true</tt> if the annotation is to be displayed as a
160 * warning, <tt>false</tt> otherwise
162 private boolean isWarning(Annotation annotation) {
163 return ANNOTATION_TYPE_WARNING.equals(annotation.getType());
167 * Tests if the given position is inside the source region of a model
171 * the position to be tested
174 * @return boolean <tt>true</tt> if position is located inside the
175 * element, otherwise <tt>false</tt>
177 private boolean isInside(int pos, ISourceReference element) {
178 IRegion region = element.getSourceRegion();
179 if (region != null) {
180 int offset = region.getOffset();
181 return ((offset <= pos) && (offset + region.getLength() > pos));