1 11 12 package org.eclipse.ant.internal.ui.editor.outline; 13 14 import java.util.ArrayList ; 15 import java.util.HashMap ; 16 import java.util.Iterator ; 17 import java.util.List ; 18 import java.util.Map ; 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 fProblems; 42 43 public AntEditorMarkerUpdaterJob (List problems) { 44 super("Ant editor marker updater job"); fProblems= problems; 46 setSystem(true); 47 } 48 49 52 public IStatus runInWorkspace(IProgressMonitor monitor) { 53 updateMarkers0(fProblems); 54 return new Status(IStatus.OK, AntUIPlugin.getUniqueIdentifier(), IStatus.OK, "", null); } 56 } 57 58 private IAntModel fModel= null; 59 private List fCollectedProblems= new ArrayList (); 60 public static final String BUILDFILE_PROBLEM_MARKER = AntUIPlugin.PI_ANTUI + ".buildFileProblem"; private IFile fFile= null; 62 63 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 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 problems = new ArrayList (fCollectedProblems.size()); 107 Iterator 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 problems) { 119 removeProblems(); 120 if (!shouldAddMarkers()) { 121 return; 122 } 123 124 if (problems.size() > 0) { 125 Iterator 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 146 private Map getMarkerAttributes(IProblem problem) { 147 148 Map attributes= new HashMap (11); 149 int severity= IMarker.SEVERITY_ERROR; 150 if (problem.isWarning()) { 151 severity= IMarker.SEVERITY_WARNING; 152 } 153 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 (severity)); 159 return attributes; 160 } 161 162 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 |