]>
Commit | Line | Data |
---|---|---|
1b2798f6 EK |
1 | /******************************************************************************* |
2 | * Copyright (c) 2000, 2011 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.runtime.CoreException; | |
16 | ||
17 | import org.eclipse.core.resources.IMarker; | |
18 | import org.eclipse.core.resources.IMarkerDelta; | |
19 | import org.eclipse.core.resources.IResource; | |
20 | import org.eclipse.core.resources.IResourceChangeEvent; | |
21 | import org.eclipse.core.resources.IResourceChangeListener; | |
22 | import org.eclipse.core.resources.IResourceDelta; | |
23 | import org.eclipse.core.resources.IWorkspace; | |
24 | ||
25 | import org.eclipse.ui.texteditor.AbstractMarkerAnnotationModel; | |
26 | import org.eclipse.ui.texteditor.MarkerAnnotation; | |
27 | ||
28 | import org.eclipse.jdt.core.IClassFile; | |
29 | import org.eclipse.jdt.core.JavaCore; | |
30 | ||
31 | ||
32 | /** | |
33 | * | |
34 | */ | |
35 | public class ClassFileMarkerAnnotationModel extends AbstractMarkerAnnotationModel implements IResourceChangeListener { | |
36 | ||
37 | protected IClassFile fClassFile; | |
38 | protected IWorkspace fWorkspace; | |
39 | protected IResource fMarkerResource; | |
40 | protected boolean fChangesApplied; | |
41 | ||
42 | ||
43 | public ClassFileMarkerAnnotationModel(IResource markerResource) { | |
44 | super(); | |
45 | fMarkerResource= markerResource; | |
46 | fWorkspace= fMarkerResource.getWorkspace(); | |
47 | } | |
48 | ||
49 | public void setClassFile(IClassFile classFile) { | |
50 | fClassFile= classFile; | |
51 | } | |
52 | ||
53 | /** | |
54 | * @see AbstractMarkerAnnotationModel#isAcceptable | |
55 | */ | |
56 | @Override | |
57 | protected boolean isAcceptable(IMarker marker) { | |
58 | try { | |
59 | return JavaCore.isReferencedBy(fClassFile, marker); | |
60 | } catch (CoreException x) { | |
61 | handleCoreException(x, JavaEditorMessages.ClassFileMarkerAnnotationModel_error_isAcceptable); | |
62 | return false; | |
63 | } | |
64 | } | |
65 | ||
66 | protected boolean isAffected(IMarkerDelta markerDelta) { | |
67 | try { | |
68 | return JavaCore.isReferencedBy(fClassFile, markerDelta); | |
69 | } catch (CoreException x) { | |
70 | handleCoreException(x, JavaEditorMessages.ClassFileMarkerAnnotationModel_error_isAffected); | |
71 | return false; | |
72 | } | |
73 | } | |
74 | ||
75 | /** | |
76 | * @see AbstractMarkerAnnotationModel#createMarkerAnnotation(IMarker) | |
77 | */ | |
78 | @Override | |
79 | protected MarkerAnnotation createMarkerAnnotation(IMarker marker) { | |
80 | return new JavaMarkerAnnotation(marker); | |
81 | } | |
82 | ||
83 | /** | |
84 | * @see AbstractMarkerAnnotationModel#listenToMarkerChanges(boolean) | |
85 | */ | |
86 | @Override | |
87 | protected void listenToMarkerChanges(boolean listen) { | |
88 | if (listen) | |
89 | fWorkspace.addResourceChangeListener(this); | |
90 | else | |
91 | fWorkspace.removeResourceChangeListener(this); | |
92 | } | |
93 | ||
94 | /** | |
95 | * @see AbstractMarkerAnnotationModel#deleteMarkers(IMarker[]) | |
96 | */ | |
97 | @Override | |
98 | protected void deleteMarkers(IMarker[] markers) throws CoreException { | |
99 | // empty as class files are read only | |
100 | } | |
101 | ||
102 | /** | |
103 | * @see AbstractMarkerAnnotationModel#retrieveMarkers() | |
104 | */ | |
105 | @Override | |
106 | protected IMarker[] retrieveMarkers() throws CoreException { | |
107 | if (fMarkerResource != null) { | |
108 | IMarker[] workspaceMarkers= fWorkspace.getRoot().findMarkers(IMarker.MARKER, true, IResource.DEPTH_ZERO); | |
109 | IMarker[] resourceMarkers= fMarkerResource.findMarkers(IMarker.MARKER, true, IResource.DEPTH_ZERO); | |
110 | int workspaceMarkersLength= workspaceMarkers.length; | |
111 | if (workspaceMarkersLength == 0) | |
112 | return resourceMarkers; | |
113 | ||
114 | int resourceMarkersLength= resourceMarkers.length; | |
115 | if (resourceMarkersLength == 0) | |
116 | return workspaceMarkers; | |
117 | ||
118 | IMarker[] result= new IMarker[resourceMarkersLength + workspaceMarkersLength]; | |
119 | System.arraycopy(resourceMarkers, 0, result, 0, resourceMarkersLength); | |
120 | System.arraycopy(workspaceMarkers, 0, result, resourceMarkersLength, workspaceMarkersLength); | |
121 | return result; | |
122 | } | |
123 | return null; | |
124 | } | |
125 | ||
126 | private void checkDeltas(IMarkerDelta[] markerDeltas) throws CoreException { | |
127 | for (int i= 0; i < markerDeltas.length; i++) { | |
128 | if (isAffected(markerDeltas[i])) { | |
129 | IMarker marker= markerDeltas[i].getMarker(); | |
130 | switch (markerDeltas[i].getKind()) { | |
131 | case IResourceDelta.ADDED : | |
132 | addMarkerAnnotation(marker); | |
133 | fChangesApplied= true; | |
134 | break; | |
135 | case IResourceDelta.REMOVED : | |
136 | removeMarkerAnnotation(marker); | |
137 | fChangesApplied= true; | |
138 | break; | |
139 | case IResourceDelta.CHANGED: | |
140 | modifyMarkerAnnotation(marker); | |
141 | fChangesApplied= true; | |
142 | break; | |
143 | } | |
144 | } | |
145 | } | |
146 | } | |
147 | ||
148 | /** | |
149 | * @see IResourceChangeListener#resourceChanged | |
150 | */ | |
151 | public void resourceChanged(IResourceChangeEvent e) { | |
152 | try { | |
153 | IMarkerDelta[] deltas= e.findMarkerDeltas(null, true); | |
154 | if (deltas != null) { | |
155 | fChangesApplied= false; | |
156 | checkDeltas(deltas); | |
157 | if (fChangesApplied) | |
158 | fireModelChanged(); | |
159 | } | |
160 | } catch (CoreException x) { | |
161 | handleCoreException(x, JavaEditorMessages.ClassFileMarkerAnnotationModel_error_resourceChanged); | |
162 | } | |
163 | } | |
164 | } | |
165 |