/*******************************************************************************
* Copyright (c) 2009, 2010 Innovation Gate GmbH.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Innovation Gate GmbH - initial API and implementation
******************************************************************************/
package de.innovationgate.eclipse.editors.helpers;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IMarker;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jface.text.source.Annotation;
import org.eclipse.jface.text.source.ISourceViewer;
import org.eclipse.ui.texteditor.MarkerAnnotation;
import org.eclipse.ui.texteditor.MarkerUtilities;
import de.innovationgate.eclipse.editors.Plugin;
public class MarkerFactory {
public static void createErrorMarker(String markerID, IFile file, int line, String message) {
createErrorMarker(markerID, file, line, message, null);
}
public static void createErrorMarker(String markerID, IFile file, int line, String message, Map<String,Object>attributes) {
Map<String,Object> map = new HashMap<String,Object>();
MarkerUtilities.setLineNumber(map, line);
MarkerUtilities.setMessage(map, message);
map.put(IMarker.LOCATION, file.getFullPath().toString());
map.put(IMarker.SEVERITY, new Integer(IMarker.SEVERITY_ERROR));
if (attributes != null) {
map.putAll(attributes);
}
try {
MarkerUtilities.createMarker(file, map, markerID);
} catch (CoreException e) {
Plugin.getDefault().logError("Unable to create error marker '" + markerID + "'.", e);
}
}
public static void createErrorMarker(String markerID, IFile file, int charStart, int charEnd, String message) {
createErrorMarker(markerID, file, charStart, charEnd, message, null);
}
public static void createErrorMarker(String markerID, IFile file, int charStart, int charEnd, String message, Map<String,Object>attributes) {
createMarker(markerID, file, charStart, charEnd, message, attributes, IMarker.SEVERITY_ERROR);
}
public static void createWarningMarker(String markerID, IFile file, int charStart, int charEnd, String message) {
createWarningMarker(markerID, file, charStart, charEnd, message, null);
}
public static void createWarningMarker(String markerID, IFile file, int charStart, int charEnd, String message, Map<String,Object>attributes) {
createMarker(markerID, file, charStart, charEnd, message, attributes, IMarker.SEVERITY_WARNING);
}
private static void createMarker(String markerID, IFile file, int charStart, int charEnd, String message, Map<String,Object>attributes, Integer severity) {
Map<String,Object> map = new HashMap<String,Object>();
MarkerUtilities.setMessage(map, message);
map.put(IMarker.LOCATION, file.getFullPath().toString());
if (charStart != -1 && charEnd != -1) {
map.put(IMarker.CHAR_START, charStart);
map.put(IMarker.CHAR_END, charEnd);
}
map.put(IMarker.SEVERITY, severity);
if (attributes != null) {
map.putAll(attributes);
}
try {
MarkerUtilities.createMarker(file, map, markerID);
} catch (CoreException e) {
Plugin.getDefault().logError("Unable to create error marker '" + markerID + "'.", e);
}
}
@SuppressWarnings("unchecked")
public static MarkerAnnotation findMarkerAnnotationByTypeAndOffset(String type, int offset, ISourceViewer sourceViewer) {
Iterator<Annotation> annotations = sourceViewer.getAnnotationModel().getAnnotationIterator();
while (annotations.hasNext()) {
Annotation tmpAnnotation = annotations.next();
if (tmpAnnotation instanceof MarkerAnnotation) {
MarkerAnnotation markerAnnotation = (MarkerAnnotation) tmpAnnotation;
try {
if (markerAnnotation.getMarker().getType().equals(type)) {
Integer charStart = markerAnnotation.getMarker().getAttribute(IMarker.CHAR_START, -1);
Integer charEnd = markerAnnotation.getMarker().getAttribute(IMarker.CHAR_END, -1);
if (offset >= charStart && offset <= charEnd) {
return markerAnnotation;
}
}
} catch (CoreException e) {
e.printStackTrace();
}
}
}
return null;
}
}