e72f265d9bbbd62e723459559f11ef6d51a63715
[phpeclipse.git] / net.sourceforge.phpeclipse.ui / src / net / sourceforge / phpeclipse / phpeditor / JavaMarkerAnnotation.java
1 /*******************************************************************************
2  * Copyright (c) 2000, 2003 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.phpeclipse.phpeditor;
13
14 import java.util.Iterator;
15
16 import net.sourceforge.phpdt.core.ICompilationUnit;
17 import net.sourceforge.phpdt.core.IJavaElement;
18 import net.sourceforge.phpdt.core.IJavaModelMarker;
19 import net.sourceforge.phpdt.core.JavaCore;
20 import net.sourceforge.phpdt.internal.corext.util.JavaModelUtil;
21
22 import org.eclipse.core.resources.IMarker;
23 import org.eclipse.swt.graphics.Image;
24 import org.eclipse.swt.widgets.Display;
25 import org.eclipse.ui.texteditor.MarkerAnnotation;
26
27 public class JavaMarkerAnnotation extends MarkerAnnotation implements
28                 IJavaAnnotation {
29
30         public static final String JAVA_MARKER_TYPE_PREFIX = "net.sourceforge.phpdt"; //$NON-NLS-1$
31
32         public static final String ERROR_ANNOTATION_TYPE = "net.sourceforge.phpdt.ui.error"; //$NON-NLS-1$
33
34         public static final String WARNING_ANNOTATION_TYPE = "net.sourceforge.phpdt.ui.warning"; //$NON-NLS-1$
35
36         public static final String INFO_ANNOTATION_TYPE = "net.sourceforge.phpdt.ui.info"; //$NON-NLS-1$
37
38         public static final String TASK_ANNOTATION_TYPE = "org.eclipse.ui.workbench.texteditor.task"; //$NON-NLS-1$
39
40         private IJavaAnnotation fOverlay;
41
42         public JavaMarkerAnnotation(IMarker marker) {
43                 super(marker);
44         }
45
46         /*
47          * @see net.sourceforge.phpdt.internal.ui.javaeditor.IJavaAnnotation#getImage(org.eclipse.swt.widgets.Display)
48          */
49         public Image getImage(Display display) {
50                 return super.getImage(display);
51         }
52
53         /*
54          * @see IJavaAnnotation#getArguments()
55          */
56         public String[] getArguments() {
57                 IMarker marker = getMarker();
58                 if (marker != null && marker.exists() && isProblem())
59                         return JavaModelUtil.getProblemArgumentsFromMarker(marker
60                                         .getAttribute(IJavaModelMarker.ARGUMENTS, "")); //$NON-NLS-1$
61                 return null;
62         }
63
64         /*
65          * @see IJavaAnnotation#getId()
66          */
67         public int getId() {
68                 IMarker marker = getMarker();
69                 if (marker == null || !marker.exists())
70                         return -1;
71
72                 if (isProblem())
73                         return marker.getAttribute(IJavaModelMarker.ID, -1);
74
75                 // if (TASK_ANNOTATION_TYPE.equals(getAnnotationType())) {
76                 // try {
77                 // if (marker.isSubtypeOf(IJavaModelMarker.TASK_MARKER)) {
78                 // return IProblem.Task;
79                 // }
80                 // } catch (CoreException e) {
81                 // JavaPlugin.log(e); // should no happen, we test for marker.exists
82                 // }
83                 // }
84
85                 return -1;
86         }
87
88         /*
89          * @see IJavaAnnotation#isProblem()
90          */
91         public boolean isProblem() {
92                 String type = getType();
93                 return WARNING_ANNOTATION_TYPE.equals(type)
94                                 || ERROR_ANNOTATION_TYPE.equals(type);
95         }
96
97         /**
98          * Overlays this annotation with the given javaAnnotation.
99          * 
100          * @param javaAnnotation
101          *            annotation that is overlaid by this annotation
102          */
103         public void setOverlay(IJavaAnnotation javaAnnotation) {
104                 if (fOverlay != null)
105                         fOverlay.removeOverlaid(this);
106
107                 fOverlay = javaAnnotation;
108                 if (!isMarkedDeleted())
109                         markDeleted(fOverlay != null);
110
111                 if (fOverlay != null)
112                         fOverlay.addOverlaid(this);
113         }
114
115         /*
116          * @see IJavaAnnotation#hasOverlay()
117          */
118         public boolean hasOverlay() {
119                 return fOverlay != null;
120         }
121
122         /*
123          * @see net.sourceforge.phpdt.internal.ui.javaeditor.IJavaAnnotation#getOverlay()
124          */
125         public IJavaAnnotation getOverlay() {
126                 return fOverlay;
127         }
128
129         /*
130          * @see IJavaAnnotation#addOverlaid(IJavaAnnotation)
131          */
132         public void addOverlaid(IJavaAnnotation annotation) {
133                 // not supported
134         }
135
136         /*
137          * @see IJavaAnnotation#removeOverlaid(IJavaAnnotation)
138          */
139         public void removeOverlaid(IJavaAnnotation annotation) {
140                 // not supported
141         }
142
143         /*
144          * @see IJavaAnnotation#getOverlaidIterator()
145          */
146         public Iterator getOverlaidIterator() {
147                 // not supported
148                 return null;
149         }
150
151         /*
152          * (non-Javadoc)
153          * 
154          * @see net.sourceforge.phpdt.internal.ui.javaeditor.IJavaAnnotation#getCompilationUnit()
155          */
156         public ICompilationUnit getCompilationUnit() {
157                 IJavaElement element = JavaCore.create(getMarker().getResource());
158                 if (element instanceof ICompilationUnit) {
159                         ICompilationUnit cu = (ICompilationUnit) element;
160                         ICompilationUnit workingCopy = EditorUtility.getWorkingCopy(cu);
161                         if (workingCopy != null) {
162                                 return workingCopy;
163                         }
164                         return cu;
165                 }
166                 return null;
167         }
168 }