KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > texteditor > ResourceMarkerAnnotationModel


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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 package org.eclipse.ui.texteditor;
12
13 import org.eclipse.core.resources.IMarker;
14 import org.eclipse.core.resources.IMarkerDelta;
15 import org.eclipse.core.resources.IResource;
16 import org.eclipse.core.resources.IResourceChangeEvent;
17 import org.eclipse.core.resources.IResourceChangeListener;
18 import org.eclipse.core.resources.IResourceDelta;
19 import org.eclipse.core.resources.IWorkspace;
20 import org.eclipse.core.resources.IWorkspaceRunnable;
21
22 import org.eclipse.core.runtime.Assert;
23 import org.eclipse.core.runtime.CoreException;
24 import org.eclipse.core.runtime.IProgressMonitor;
25
26
27
28 /**
29  * A marker annotation model whose underlying source of markers is
30  * a resource in the workspace.
31  * <p>
32  * This class may be instantiated; it is not intended to be subclassed.</p>
33  */

34 public class ResourceMarkerAnnotationModel extends AbstractMarkerAnnotationModel {
35
36
37     /**
38      * Internal resource change listener.
39      */

40     class ResourceChangeListener implements IResourceChangeListener {
41         /*
42          * @see IResourceChangeListener#resourceChanged(org.eclipse.core.resources.IResourceChangeEvent)
43          */

44         public void resourceChanged(IResourceChangeEvent e) {
45             IResourceDelta delta= e.getDelta();
46             if (delta != null && fResource != null) {
47                 IResourceDelta child= delta.findMember(fResource.getFullPath());
48                 if (child != null)
49                     update(child.getMarkerDeltas());
50             }
51         }
52     }
53
54     /** The workspace. */
55     private IWorkspace fWorkspace;
56     /** The resource. */
57     private IResource fResource;
58     /** The resource change listener. */
59     private IResourceChangeListener fResourceChangeListener= new ResourceChangeListener();
60
61
62     /**
63      * Creates a marker annotation model with the given resource as the source
64      * of the markers.
65      *
66      * @param resource the resource
67      */

68     public ResourceMarkerAnnotationModel(IResource resource) {
69         Assert.isNotNull(resource);
70         fResource= resource;
71         fWorkspace= resource.getWorkspace();
72     }
73
74     /*
75      * @see AbstractMarkerAnnotationModel#isAcceptable(IMarker)
76      */

77     protected boolean isAcceptable(IMarker marker) {
78         return marker != null && fResource.equals(marker.getResource());
79     }
80
81     /**
82      * Updates this model to the given marker deltas.
83      *
84      * @param markerDeltas the array of marker deltas
85      */

86     protected void update(IMarkerDelta[] markerDeltas) {
87
88         if (markerDeltas.length == 0)
89             return;
90
91         for (int i= 0; i < markerDeltas.length; i++) {
92             IMarkerDelta delta= markerDeltas[i];
93             switch (delta.getKind()) {
94                 case IResourceDelta.ADDED :
95                     addMarkerAnnotation(delta.getMarker());
96                     break;
97                 case IResourceDelta.REMOVED :
98                     removeMarkerAnnotation(delta.getMarker());
99                     break;
100                 case IResourceDelta.CHANGED :
101                     modifyMarkerAnnotation(delta.getMarker());
102                     break;
103             }
104         }
105
106         fireModelChanged();
107     }
108
109     /*
110      * @see AbstractMarkerAnnotationModel#listenToMarkerChanges(boolean)
111      */

112     protected void listenToMarkerChanges(boolean listen) {
113         if (listen)
114             fWorkspace.addResourceChangeListener(fResourceChangeListener);
115         else
116             fWorkspace.removeResourceChangeListener(fResourceChangeListener);
117     }
118
119     /*
120      * @see AbstractMarkerAnnotationModel#deleteMarkers(IMarker[])
121      */

122     protected void deleteMarkers(final IMarker[] markers) throws CoreException {
123         fWorkspace.run(new IWorkspaceRunnable() {
124             public void run(IProgressMonitor monitor) throws CoreException {
125                 for (int i= 0; i < markers.length; ++i) {
126                     markers[i].delete();
127                 }
128             }
129         }, null, IWorkspace.AVOID_UPDATE, null);
130     }
131
132     /*
133      * @see AbstractMarkerAnnotationModel#retrieveMarkers()
134      */

135     protected IMarker[] retrieveMarkers() throws CoreException {
136         return fResource.findMarkers(IMarker.MARKER, true, IResource.DEPTH_ZERO);
137     }
138
139     /**
140      * Returns the resource serving as the source of markers for this annotation model.
141      *
142      * @return the resource serving as the source of markers for this annotation model
143      * @since 2.0
144      */

145     protected IResource getResource() {
146         return fResource;
147     }
148 }
149
Popular Tags