avoid NullPointerException in debug
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpeclipse / phpeditor / CompilationUnitAnnotationModelEvent.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 package net.sourceforge.phpeclipse.phpeditor;
12
13
14
15 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
16
17 import org.eclipse.core.resources.IMarker;
18 import org.eclipse.core.resources.IResource;
19 import org.eclipse.core.runtime.CoreException;
20 import org.eclipse.jface.text.Position;
21 import org.eclipse.jface.text.source.Annotation;
22 import org.eclipse.jface.text.source.AnnotationModelEvent;
23 import org.eclipse.jface.text.source.IAnnotationModel;
24 import org.eclipse.ui.texteditor.MarkerAnnotation;
25
26
27 /**
28  * Event sent out by changes of the compilation unit annotation model.
29  */
30 public class CompilationUnitAnnotationModelEvent  extends AnnotationModelEvent {
31         
32         private boolean fIncludesProblemMarkerAnnotations;
33         private IResource fUnderlyingResource;
34         
35         /**
36          * Constructor for CompilationUnitAnnotationModelEvent.
37          * @param model
38          * @param underlyingResource The annotation model's underlying resource 
39          */
40         public CompilationUnitAnnotationModelEvent(IAnnotationModel model, IResource underlyingResource) {
41                 super(model);
42                 fUnderlyingResource= underlyingResource;
43                 fIncludesProblemMarkerAnnotations= false;
44         }
45         
46         private void testIfProblemMarker(Annotation annotation) {
47                 if (fIncludesProblemMarkerAnnotations) {
48                         return;
49                 }
50                 if (annotation instanceof JavaMarkerAnnotation) {
51                         fIncludesProblemMarkerAnnotations= ((JavaMarkerAnnotation) annotation).isProblem();
52                 } else if (annotation instanceof MarkerAnnotation) {
53                         try {
54                                 IMarker marker= ((MarkerAnnotation) annotation).getMarker();
55                                 if (!marker.exists() || marker.isSubtypeOf(IMarker.PROBLEM)) {
56                                         fIncludesProblemMarkerAnnotations= true;
57                                 }
58                         } catch (CoreException e) {
59                                 PHPeclipsePlugin.log(e);
60                         }
61                 }       
62         }
63         
64         /*
65          * @see org.eclipse.jface.text.source.AnnotationModelEvent#annotationAdded(org.eclipse.jface.text.source.Annotation)
66          */
67         public void annotationAdded(Annotation annotation) {
68                 super.annotationAdded(annotation);
69                 testIfProblemMarker(annotation);
70         }
71
72
73         /*
74          * @see org.eclipse.jface.text.source.AnnotationModelEvent#annotationRemoved(org.eclipse.jface.text.source.Annotation)
75          */
76         public void annotationRemoved(Annotation annotation) {
77                 super.annotationRemoved(annotation);
78                 testIfProblemMarker(annotation);
79         }
80
81         /*
82          * @see org.eclipse.jface.text.source.AnnotationModelEvent#annotationRemoved(org.eclipse.jface.text.source.Annotation, org.eclipse.jface.text.Position)
83          */
84         public void annotationRemoved(Annotation annotation, Position position) {
85                 super.annotationRemoved(annotation, position);
86                 testIfProblemMarker(annotation);
87         }
88         
89         /*
90          * @see org.eclipse.jface.text.source.AnnotationModelEvent#annotationChanged(org.eclipse.jface.text.source.Annotation)
91          */
92         public void annotationChanged(Annotation annotation) {
93                 testIfProblemMarker(annotation);
94                 super.annotationChanged(annotation);
95         }
96                 
97         /**
98          * Returns whether the change included problem marker annotations.
99          * 
100          * @return <code>true</code> if the change included marker annotations
101          */
102         public boolean includesProblemMarkerAnnotationChanges() {
103                 return fIncludesProblemMarkerAnnotations;
104         }
105         
106         /**
107          * Returns the annotation model's underlying resource
108          */
109         public IResource getUnderlyingResource() {
110                 return fUnderlyingResource;
111         }
112
113 }