KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > javaeditor > ClassFileMarkerAnnotationModel


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11
12 package org.eclipse.jdt.internal.ui.javaeditor;
13
14
15 import org.eclipse.core.resources.IMarker;
16 import org.eclipse.core.resources.IMarkerDelta;
17 import org.eclipse.core.resources.IResource;
18 import org.eclipse.core.resources.IResourceChangeEvent;
19 import org.eclipse.core.resources.IResourceChangeListener;
20 import org.eclipse.core.resources.IResourceDelta;
21 import org.eclipse.core.resources.IWorkspace;
22 import org.eclipse.core.runtime.CoreException;
23
24 import org.eclipse.ui.texteditor.AbstractMarkerAnnotationModel;
25 import org.eclipse.ui.texteditor.MarkerAnnotation;
26
27 import org.eclipse.jdt.core.IClassFile;
28 import org.eclipse.jdt.core.JavaCore;
29
30
31 /**
32  *
33  */

34 public class ClassFileMarkerAnnotationModel extends AbstractMarkerAnnotationModel implements IResourceChangeListener {
35
36     protected IClassFile fClassFile;
37     protected IWorkspace fWorkspace;
38     protected IResource fMarkerResource;
39     protected boolean fChangesApplied;
40
41
42     public ClassFileMarkerAnnotationModel(IResource markerResource) {
43         super();
44         fMarkerResource= markerResource;
45         fWorkspace= fMarkerResource.getWorkspace();
46     }
47
48     public void setClassFile(IClassFile classFile) {
49         fClassFile= classFile;
50     }
51
52     /**
53      * @see AbstractMarkerAnnotationModel#isAcceptable
54      */

55     protected boolean isAcceptable(IMarker marker) {
56         try {
57             return JavaCore.isReferencedBy(fClassFile, marker);
58         } catch (CoreException x) {
59             handleCoreException(x, JavaEditorMessages.ClassFileMarkerAnnotationModel_error_isAcceptable);
60             return false;
61         }
62     }
63
64     protected boolean isAffected(IMarkerDelta markerDelta) {
65         try {
66             return JavaCore.isReferencedBy(fClassFile, markerDelta);
67         } catch (CoreException x) {
68             handleCoreException(x, JavaEditorMessages.ClassFileMarkerAnnotationModel_error_isAffected);
69             return false;
70         }
71     }
72
73     /**
74      * @see AbstractMarkerAnnotationModel#createMarkerAnnotation(IMarker)
75      */

76     protected MarkerAnnotation createMarkerAnnotation(IMarker marker) {
77         return new JavaMarkerAnnotation(marker);
78     }
79
80     /**
81      * @see AbstractMarkerAnnotationModel#listenToMarkerChanges(boolean)
82      */

83     protected void listenToMarkerChanges(boolean listen) {
84         if (listen)
85             fWorkspace.addResourceChangeListener(this);
86         else
87             fWorkspace.removeResourceChangeListener(this);
88     }
89
90     /**
91      * @see AbstractMarkerAnnotationModel#deleteMarkers(IMarker[])
92      */

93     protected void deleteMarkers(IMarker[] markers) throws CoreException {
94         // empty as class files are read only
95
}
96
97     /**
98      * @see AbstractMarkerAnnotationModel#retrieveMarkers()
99      */

100     protected IMarker[] retrieveMarkers() throws CoreException {
101         if (fMarkerResource != null) {
102             IMarker[] workspaceMarkers= fWorkspace.getRoot().findMarkers(IMarker.MARKER, true, IResource.DEPTH_ZERO);
103             IMarker[] resourceMarkers= fMarkerResource.findMarkers(IMarker.MARKER, true, IResource.DEPTH_ZERO);
104             int workspaceMarkersLength= workspaceMarkers.length;
105             if (workspaceMarkersLength == 0)
106                 return resourceMarkers;
107             
108             int resourceMarkersLength= resourceMarkers.length;
109             if (resourceMarkersLength == 0)
110                 return workspaceMarkers;
111             
112             IMarker[] result= new IMarker[resourceMarkersLength + workspaceMarkersLength];
113             System.arraycopy(resourceMarkers, 0, result, 0, resourceMarkersLength);
114             System.arraycopy(workspaceMarkers, 0, result, resourceMarkersLength, workspaceMarkersLength);
115             return result;
116         }
117         return null;
118     }
119
120     private void checkDeltas(IMarkerDelta[] markerDeltas) throws CoreException {
121         for (int i= 0; i < markerDeltas.length; i++) {
122             if (isAffected(markerDeltas[i])) {
123                 IMarker marker= markerDeltas[i].getMarker();
124                 switch (markerDeltas[i].getKind()) {
125                     case IResourceDelta.ADDED :
126                         addMarkerAnnotation(marker);
127                         fChangesApplied= true;
128                         break;
129                     case IResourceDelta.REMOVED :
130                         removeMarkerAnnotation(marker);
131                         fChangesApplied= true;
132                         break;
133                     case IResourceDelta.CHANGED:
134                         modifyMarkerAnnotation(marker);
135                         fChangesApplied= true;
136                         break;
137                 }
138             }
139         }
140     }
141
142     /**
143      * @see IResourceChangeListener#resourceChanged
144      */

145     public void resourceChanged(IResourceChangeEvent e) {
146         try {
147             IMarkerDelta[] deltas= e.findMarkerDeltas(null, true);
148             if (deltas != null) {
149                 fChangesApplied= false;
150                 checkDeltas(deltas);
151                 if (fChangesApplied)
152                     fireModelChanged();
153             }
154         } catch (CoreException x) {
155             handleCoreException(x, JavaEditorMessages.ClassFileMarkerAnnotationModel_error_resourceChanged);
156         }
157     }
158 }
159
160
Popular Tags