KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ant > internal > ui > console > JavacMarkerCreator


1 /*******************************************************************************
2  * Copyright (c) 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.ant.internal.ui.console;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.HashMap JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.List JavaDoc;
17 import java.util.Map JavaDoc;
18
19 import org.eclipse.ant.internal.ui.AntUIPlugin;
20 import org.eclipse.core.resources.IFile;
21 import org.eclipse.core.resources.IMarker;
22 import org.eclipse.core.resources.IResource;
23 import org.eclipse.core.resources.IResourceRuleFactory;
24 import org.eclipse.core.resources.IWorkspaceRunnable;
25 import org.eclipse.core.resources.ResourcesPlugin;
26 import org.eclipse.core.runtime.CoreException;
27 import org.eclipse.core.runtime.IProgressMonitor;
28 import org.eclipse.core.runtime.jobs.ISchedulingRule;
29 import org.eclipse.debug.core.model.IProcess;
30 import org.eclipse.debug.internal.ui.views.console.ProcessConsole;
31 import org.eclipse.jdt.core.IJavaModelMarker;
32 import org.eclipse.jface.text.BadLocationException;
33 import org.eclipse.jface.text.FindReplaceDocumentAdapter;
34 import org.eclipse.jface.text.IDocument;
35 import org.eclipse.jface.text.IRegion;
36 import org.eclipse.ui.console.TextConsole;
37
38 public class JavacMarkerCreator {
39
40     private final TextConsole fConsole;
41     private IProcess fProcess;
42     private static List JavaDoc fgFilesToBeCleaned= new ArrayList JavaDoc();
43     private Map JavaDoc fFileToMarkerInfo= new HashMap JavaDoc();
44     private final boolean fUseCustomMessage;
45
46     private class MarkerInfo {
47         
48         public int fLineNumber;
49         public String JavaDoc fMessage;
50         public int fOffset;
51         public int fLength;
52         public Integer JavaDoc fType;
53
54         public void setLineNumber(int lineNumber) {
55             fLineNumber= lineNumber;
56         }
57         public void setMessage(String JavaDoc message) {
58             fMessage= message;
59         }
60         public void setOffset(int offset) {
61             fOffset = offset;
62         }
63         public void setLength(int length) {
64             fLength = length;
65         }
66         public void setType(Integer JavaDoc type) {
67             fType= type;
68             
69         }
70     }
71     public JavacMarkerCreator(TextConsole console, boolean useCustomMessage) {
72         fConsole = console;
73         fUseCustomMessage = useCustomMessage;
74         if (fConsole instanceof ProcessConsole) {
75             fProcess= ((ProcessConsole) fConsole).getProcess();
76         }
77     }
78
79     protected ISchedulingRule getMarkerRule(IResource resource) {
80         ISchedulingRule rule = null;
81         if (resource != null) {
82             IResourceRuleFactory ruleFactory = ResourcesPlugin.getWorkspace().getRuleFactory();
83             rule = ruleFactory.markerRule(resource);
84         }
85         return rule;
86     }
87
88     protected void run(ISchedulingRule rule, IWorkspaceRunnable wr) {
89         try {
90             ResourcesPlugin.getWorkspace().run(wr, rule, 0, null);
91         } catch (CoreException e) {
92             AntUIPlugin.log(e.getStatus());
93         }
94     }
95
96     protected void addFileToBeCleaned(IFile file) {
97         fgFilesToBeCleaned.add(file);
98     }
99     
100     protected void addMarker(IFile file, int lineNumber, int offset, int length, Integer JavaDoc type) {
101         MarkerInfo info= new MarkerInfo();
102         info.setLineNumber(lineNumber);
103         info.setOffset(offset);
104         info.setLength(length);
105         info.setType(type);
106         List JavaDoc infos= (List JavaDoc) fFileToMarkerInfo.get(file);
107         if (infos == null) {
108             infos= new ArrayList JavaDoc();
109             fFileToMarkerInfo.put(file, infos);
110         }
111         infos.add(info);
112     }
113     
114     private void createMarkers(final IFile file, final List JavaDoc infos) {
115         IWorkspaceRunnable wr= new IWorkspaceRunnable() {
116             public void run(IProgressMonitor monitor) throws CoreException {
117                 
118                 try {
119                     for (Iterator JavaDoc iter = infos.iterator(); iter.hasNext();) {
120                         MarkerInfo info = (MarkerInfo) iter.next();
121                         IMarker marker= file.createMarker(IJavaModelMarker.JAVA_MODEL_PROBLEM_MARKER);
122                         Map JavaDoc attributes= new HashMap JavaDoc(3);
123                         attributes.put(IMarker.LINE_NUMBER, new Integer JavaDoc(info.fLineNumber));
124                         String JavaDoc message= getMessage(info);
125                         attributes.put(IMarker.MESSAGE, message);
126                         attributes.put(IMarker.SEVERITY, info.fType);
127                         marker.setAttributes(attributes);
128                     }
129                 } catch (CoreException e) {
130                     AntUIPlugin.log(e.getStatus());
131                 }
132             }
133         };
134         run(getMarkerRule(file), wr);
135     }
136
137     protected String JavaDoc getMessage(MarkerInfo info) {
138         IDocument doc= fConsole.getDocument();
139         String JavaDoc message= ConsoleMessages.JavacMarkerCreator_0;
140         if (fUseCustomMessage) {
141             FindReplaceDocumentAdapter adapter= new FindReplaceDocumentAdapter(doc);
142             try {
143                 IRegion match=adapter.find(info.fOffset, "[javac] ----------", true, false, false, false); //$NON-NLS-1$
144
if (match != null) {
145                     match= adapter.find(match.getOffset(), "[javac]", false, false, false, false); //$NON-NLS-1$
146
if (match != null) {
147                         int start= match.getOffset() + match.getLength() + 1;
148                         IRegion lineInfo= doc.getLineInformationOfOffset(start);
149                         message= doc.get(start, lineInfo.getOffset() - start + lineInfo.getLength());
150                     }
151                 }
152             } catch (BadLocationException e) {
153                 AntUIPlugin.log(e);
154             }
155         }
156         
157         return message;
158     }
159
160     protected void finished(IProcess process) {
161         if (process.equals(fProcess)) {
162             if (!fgFilesToBeCleaned.isEmpty()) {
163                 Iterator JavaDoc itr= fgFilesToBeCleaned.iterator();
164                 while (itr.hasNext()) {
165                     IFile file = (IFile) itr.next();
166                     try {
167                         file.deleteMarkers(IJavaModelMarker.JAVA_MODEL_PROBLEM_MARKER, true, IResource.DEPTH_INFINITE);
168                     } catch (CoreException e) {
169                         AntUIPlugin.log(e.getStatus());
170                     }
171                 }
172                 //generate markers for all the warnings and errors
173
Iterator JavaDoc files= fFileToMarkerInfo.keySet().iterator();
174                 while (files.hasNext()) {
175                     IFile file = (IFile) files.next();
176                     List JavaDoc infos= (List JavaDoc) fFileToMarkerInfo.get(file);
177                     createMarkers(file, infos);
178                 }
179             }
180             
181             fFileToMarkerInfo.clear();
182             fgFilesToBeCleaned.clear();
183         }
184     }
185 }
Popular Tags