A massive organize imports and formatting of the sources using default Eclipse code...
[phpeclipse.git] / net.sourceforge.phpeclipse.ui / src / net / sourceforge / phpeclipse / ui / views / outline / ProblemsLabelDecorator.java
1 /*
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
7  * 
8  * Contributors:
9  *     Christopher Lenz - initial implementation
10  * 
11  * $Id: ProblemsLabelDecorator.java,v 1.3 2006-10-21 23:13:54 pombredanne Exp $
12  */
13
14 package net.sourceforge.phpeclipse.ui.views.outline;
15
16 import java.util.ArrayList;
17 import java.util.Iterator;
18 import java.util.List;
19
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;
25
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;
36
37 /**
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.
41  */
42 public class ProblemsLabelDecorator extends LabelProvider implements
43                 ILabelDecorator {
44
45         // Constants ---------------------------------------------------------------
46
47         private static final String ANNOTATION_TYPE_ERROR = "org.eclipse.ui.workbench.texteditor.error"; //$NON-NLS-1$
48
49         private static final String ANNOTATION_TYPE_WARNING = "org.eclipse.ui.workbench.texteditor.warning"; //$NON-NLS-1$
50
51         // Instance Variables ------------------------------------------------------
52
53         /** The associated text editor if the decorator is used in the outline. */
54         private ITextEditor editor;
55
56         /** Registry of icons and overlay icons. */
57         private ImageDescriptorRegistry registry = new ImageDescriptorRegistry();
58
59         // Constructors ------------------------------------------------------------
60
61         /**
62          * Constructor.
63          * 
64          * @param editor
65          *            the associated text editor
66          */
67         public ProblemsLabelDecorator(ITextEditor editor) {
68                 this.editor = editor;
69         }
70
71         // ILabelDecorator Implementation ------------------------------------------
72
73         /*
74          * @see org.eclipse.jface.viewers.IBaseLabelProvider#dispose()
75          */
76         public void dispose() {
77                 super.dispose();
78                 registry.dispose();
79         }
80
81         /*
82          * @see ILabelDecorator#decorateImage(Image, Object)
83          */
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);
94                         }
95                 }
96                 if (overlay != null) {
97                         ImageDescriptor base = new ImageImageDescriptor(image);
98                         return registry.get(new OverlayImageDescriptor(base,
99                                         new ImageDescriptor[][] { null, null, { overlay }, null },
100                                         null));
101                 } else {
102                 }
103                 return image;
104         }
105
106         /*
107          * @see ILabelDecorator#decorateText(String, Object)
108          */
109         public String decorateText(String text, Object element) {
110                 return text;
111         }
112
113         // Private Methods ---------------------------------------------------------
114
115         /**
116          * Returns all annotations associated with the given model element.
117          * 
118          * @param 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
122          */
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);
135                                         }
136                                 }
137                         }
138                 }
139                 return (Annotation[]) retVal.toArray(new Annotation[retVal.size()]);
140         }
141
142         /**
143          * Determines whether the given annotation is an error.
144          * 
145          * @param annotation
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
149          */
150         private boolean isError(Annotation annotation) {
151                 return ANNOTATION_TYPE_ERROR.equals(annotation.getType());
152         }
153
154         /**
155          * Determines whether the given annotation is a warning.
156          * 
157          * @param annotation
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
161          */
162         private boolean isWarning(Annotation annotation) {
163                 return ANNOTATION_TYPE_WARNING.equals(annotation.getType());
164         }
165
166         /**
167          * Tests if the given position is inside the source region of a model
168          * element.
169          * 
170          * @param pos
171          *            the position to be tested
172          * @param element
173          *            the source element
174          * @return boolean <tt>true</tt> if position is located inside the
175          *         element, otherwise <tt>false</tt>
176          */
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));
182                 }
183                 return false;
184         }
185
186 }