Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: da7c00338f017517e7351870707b5eacb1f1fab9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/*
* Copyright (c) 2002 IBM Corporation and others.
* All rights reserved.   This program and the accompanying materials
* are made available under the terms of the Common Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/cpl-v10.html
* 
* Contributors:
*   IBM - Initial API and implementation
*   Jens Lukowski/Innoopract - initial renaming/restructuring
* 
*/
package org.eclipse.wst.sse.core.builder.participants;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IMarker;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.resources.IWorkspaceRunnable;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.wst.sse.core.builder.IBuilderModelProvider;
import org.eclipse.wst.sse.core.internal.Logger;


/**
 * A Builder Participant that updates a set of Markers for a given resource
 * on each build by removing all of the existing Markers. It collects the
 * existing markers in prebuild and deletes them in postbuild. Subclasses may
 * create new markers during the build itself.
 */

public abstract class MarkerParticipant extends AbstractBuilderParticipant {
	List oldMarkers = null;

	public MarkerParticipant() {
		super();
	}

	/**
	 * Returns the attributes with which a newly created marker will be
	 * initialized. Modified from the method in MarkerRulerAction
	 * 
	 * @return the initial marker attributes
	 */
	protected Map createInitialMarkerAttributes(String text, int documentLine, int startOffset, int length, int priority) {
		Map attributes = new HashMap(6);
		// marker line numbers are 1-based
		attributes.put(IMarker.LINE_NUMBER, new Integer(documentLine + 1));
		attributes.put(IMarker.CHAR_START, new Integer(startOffset));
		attributes.put(IMarker.CHAR_END, new Integer(startOffset + length));
		attributes.put(IMarker.MESSAGE, text);
		attributes.put(getParticipantMarkerType(), Boolean.TRUE);
		attributes.put(IMarker.USER_EDITABLE, Boolean.FALSE);

		switch (priority) {
			case IMarker.PRIORITY_HIGH :
				{
					attributes.put(IMarker.PRIORITY, new Integer(IMarker.PRIORITY_HIGH));
				}
				break;
			case IMarker.PRIORITY_LOW :
				{
					attributes.put(IMarker.PRIORITY, new Integer(IMarker.PRIORITY_LOW));
				}
				break;
			default :
				{
					attributes.put(IMarker.PRIORITY, new Integer(IMarker.PRIORITY_NORMAL));
				}
		}

		return attributes;
	}

	/**
	 * @return - The IMarker type (ex.: IMarker.TASK) manipulated by this
	 *         participant
	 */
	protected abstract String getMarkerType();

	/**
	 * Returns an extra attribute name to indicate that the IMarker type was
	 * created by this participant, an "owner" ID.
	 */
	protected String getParticipantMarkerType() {
		return getClass().getName();
	}

	protected void postBuild(IFile file, IBuilderModelProvider provider, IProgressMonitor monitor) {
		IWorkspaceRunnable r = new IWorkspaceRunnable() {
			public void run(IProgressMonitor progressMonitor) throws CoreException {
				Iterator i = oldMarkers.iterator();
				while (i.hasNext()) {
					((IMarker) i.next()).delete();
				}
			}
		};
		try {
			if (file.isAccessible()) {
				file.getWorkspace().run(r, null, IWorkspace.AVOID_UPDATE, null);
			}
		}
		catch (CoreException e) {
			Logger.logException(e);
		}
		super.postBuild(file, provider, monitor);
	}

	protected void preBuild(IFile file, IBuilderModelProvider provider, IProgressMonitor monitor) {
		super.preBuild(file, provider, monitor);
		oldMarkers = new ArrayList(0);
		IMarker[] markers = null;
		try {
			// can't use deleteMarkers--not specific enough
			if (file.isAccessible()) {
				markers = file.findMarkers(getMarkerType(), true, IResource.DEPTH_INFINITE);
			}
		}
		catch (CoreException e) {
			Logger.logException(e);
		}
		if (markers != null) {
			for (int i = 0; i < markers.length; i++) {
				if (markers[i].getResource().equals(file) && markers[i].getAttribute(getParticipantMarkerType(), true)) {
					oldMarkers.add(markers[i]);
				}
			}
		}
	}
}

Back to the top