KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ant > internal > ui > editor > outline > AntEditorMarkerUpdater


1 /*******************************************************************************
2  * Copyright (c) 2004, 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.ant.internal.ui.editor.outline;
13
14 import java.util.ArrayList JavaDoc;
15 import java.util.HashMap JavaDoc;
16 import java.util.Iterator JavaDoc;
17 import java.util.List JavaDoc;
18 import java.util.Map JavaDoc;
19
20 import org.eclipse.ant.core.AntCorePlugin;
21 import org.eclipse.ant.internal.ui.AntUIPlugin;
22 import org.eclipse.ant.internal.ui.model.IAntModel;
23 import org.eclipse.ant.internal.ui.model.IProblem;
24 import org.eclipse.core.resources.IFile;
25 import org.eclipse.core.resources.IMarker;
26 import org.eclipse.core.resources.IResource;
27 import org.eclipse.core.resources.ResourcesPlugin;
28 import org.eclipse.core.resources.WorkspaceJob;
29 import org.eclipse.core.runtime.CoreException;
30 import org.eclipse.core.runtime.IProgressMonitor;
31 import org.eclipse.core.runtime.IStatus;
32 import org.eclipse.core.runtime.Status;
33 import org.eclipse.core.runtime.content.IContentDescription;
34 import org.eclipse.core.runtime.content.IContentType;
35 import org.eclipse.ui.texteditor.MarkerUtilities;
36
37 public class AntEditorMarkerUpdater {
38     
39     class AntEditorMarkerUpdaterJob extends WorkspaceJob {
40         
41         private final List JavaDoc fProblems;
42
43         public AntEditorMarkerUpdaterJob (List JavaDoc problems) {
44             super("Ant editor marker updater job"); //$NON-NLS-1$
45
fProblems= problems;
46             setSystem(true);
47         }
48
49         /* (non-Javadoc)
50          * @see org.eclipse.core.internal.resources.WorkspaceJob#runInWorkspace(org.eclipse.core.runtime.IProgressMonitor)
51          */

52         public IStatus runInWorkspace(IProgressMonitor monitor) {
53             updateMarkers0(fProblems);
54             return new Status(IStatus.OK, AntUIPlugin.getUniqueIdentifier(), IStatus.OK, "", null); //$NON-NLS-1$
55
}
56     }
57     
58     private IAntModel fModel= null;
59     private List JavaDoc fCollectedProblems= new ArrayList JavaDoc();
60     public static final String JavaDoc BUILDFILE_PROBLEM_MARKER = AntUIPlugin.PI_ANTUI + ".buildFileProblem"; //$NON-NLS-1$
61
private IFile fFile= null;
62     
63     /* (non-Javadoc)
64      * @see org.eclipse.ant.internal.ui.editor.outline.IProblemRequestor#acceptProblem(org.eclipse.ant.internal.ui.editor.outline.IProblem)
65      */

66     public synchronized void acceptProblem(IProblem problem) {
67         if (fCollectedProblems.contains(problem)) {
68             return;
69         }
70         fCollectedProblems.add(problem);
71     }
72     
73     public synchronized void beginReporting() {
74         fCollectedProblems.clear();
75     }
76     
77     private void removeProblems() {
78         IFile file= getFile();
79         if (file == null || !file.exists()) {
80             return;
81         }
82         try {
83             file.deleteMarkers(BUILDFILE_PROBLEM_MARKER, false, IResource.DEPTH_INFINITE);
84         } catch (CoreException e) {
85             AntUIPlugin.log(e);
86         }
87     }
88     
89     private void createMarker(IProblem problem) {
90         IFile file = getFile();
91         Map JavaDoc attributes= getMarkerAttributes(problem);
92         try {
93             MarkerUtilities.createMarker(file, attributes, BUILDFILE_PROBLEM_MARKER);
94         } catch (CoreException e) {
95             AntUIPlugin.log(e);
96         }
97     }
98     
99     public void setModel(IAntModel model) {
100         fModel= model;
101     }
102     
103     public synchronized void updateMarkers() {
104         IFile file = getFile();
105         if (file != null) {
106             List JavaDoc problems = new ArrayList JavaDoc(fCollectedProblems.size());
107             Iterator JavaDoc e= fCollectedProblems.iterator();
108             while (e.hasNext()) {
109                 problems.add(e.next());
110             }
111             fCollectedProblems.clear();
112             AntEditorMarkerUpdaterJob job = new AntEditorMarkerUpdaterJob(problems);
113             job.setRule(ResourcesPlugin.getWorkspace().getRuleFactory().markerRule(file));
114             job.schedule();
115         }
116     }
117     
118     private void updateMarkers0(List JavaDoc problems) {
119         removeProblems();
120         if (!shouldAddMarkers()) {
121             return;
122         }
123
124         if (problems.size() > 0) {
125             Iterator JavaDoc e= problems.iterator();
126             while (e.hasNext()) {
127                 IProblem problem= (IProblem) e.next();
128                 createMarker(problem);
129             }
130         }
131     }
132     
133     private IFile getFile() {
134         if (fFile == null) {
135             fFile= fModel.getFile();
136         }
137         return fFile;
138     }
139     
140     /**
141      * Returns the attributes with which a newly created marker will be
142      * initialized.
143      *
144      * @return the initial marker attributes
145      */

146     private Map JavaDoc getMarkerAttributes(IProblem problem) {
147         
148         Map JavaDoc attributes= new HashMap JavaDoc(11);
149         int severity= IMarker.SEVERITY_ERROR;
150         if (problem.isWarning()) {
151             severity= IMarker.SEVERITY_WARNING;
152         }
153         // marker line numbers are 1-based
154
MarkerUtilities.setMessage(attributes, problem.getUnmodifiedMessage());
155         MarkerUtilities.setLineNumber(attributes, problem.getLineNumber());
156         MarkerUtilities.setCharStart(attributes, problem.getOffset());
157         MarkerUtilities.setCharEnd(attributes, problem.getOffset() + problem.getLength());
158         attributes.put(IMarker.SEVERITY, new Integer JavaDoc(severity));
159         return attributes;
160     }
161     
162     /**
163      * Returns whether or not to add markers to the file based on the file's content type.
164      * The content type is considered an Ant buildfile if the XML has a root "project" element.
165      * Content type is defined in the org.eclipse.ant.core plugin.xml.
166      * @return whether or not to add markers to the file based on the files content type
167      */

168     private boolean shouldAddMarkers() {
169         IFile file= getFile();
170         if (file == null || !file.exists()) {
171             return false;
172         }
173         IContentDescription description;
174         try {
175             description = file.getContentDescription();
176         } catch (CoreException e) {
177             return false;
178         }
179         if (description != null) {
180             IContentType type= description.getContentType();
181             return type != null && AntCorePlugin.ANT_BUILDFILE_CONTENT_TYPE.equals(type.getId());
182         }
183         return false;
184     }
185 }
186
Popular Tags