/**
* <copyright>
* </copyright>
*
*
*/
package robot.resource.robot.ui;
public class RobotMarkerResolutionGenerator implements org.eclipse.ui.IMarkerResolutionGenerator {
public org.eclipse.ui.IMarkerResolution[] getResolutions(org.eclipse.core.resources.IMarker marker) {
try {
if (!hasQuickFixes(marker)) {
return new org.eclipse.ui.IMarkerResolution[] {};
}
org.eclipse.core.resources.IResource resource = marker.getResource();
if (resource instanceof org.eclipse.core.resources.IFile) {
// load model
final org.eclipse.core.resources.IFile file = (org.eclipse.core.resources.IFile) resource;
org.eclipse.emf.common.util.URI uri = org.eclipse.emf.common.util.URI.createPlatformResourceURI(file.getFullPath().toString(), true);
org.eclipse.emf.ecore.resource.ResourceSet rs = new org.eclipse.emf.ecore.resource.impl.ResourceSetImpl();
rs.getLoadOptions().put(robot.resource.robot.IRobotOptions.DISABLE_CREATING_MARKERS_FOR_PROBLEMS, "true");
org.eclipse.emf.ecore.resource.Resource emfResource = rs.getResource(uri, true);
if (emfResource instanceof robot.resource.robot.mopp.RobotResource) {
robot.resource.robot.mopp.RobotResource customResource = (robot.resource.robot.mopp.RobotResource) emfResource;
org.eclipse.emf.ecore.util.EcoreUtil.resolveAll(customResource);
java.util.Collection<robot.resource.robot.IRobotQuickFix> quickFixes = getQuickFixes(customResource, marker);
java.util.List<org.eclipse.ui.IMarkerResolution2> resolutions = new java.util.ArrayList<org.eclipse.ui.IMarkerResolution2>();
for (final robot.resource.robot.IRobotQuickFix quickFix : quickFixes) {
resolutions.add(new org.eclipse.ui.IMarkerResolution2() {
public void run(org.eclipse.core.resources.IMarker marker) {
String newText = quickFix.apply(null);
// set new text as content for resource
try {
file.setContents(new java.io.ByteArrayInputStream(newText.getBytes()), true, true, null);
} catch (org.eclipse.core.runtime.CoreException e) {
robot.resource.robot.ui.RobotUIPlugin.logError("Exception while applying quick fix", e);
}
}
public String getLabel() {
return quickFix.getDisplayString();
}
public org.eclipse.swt.graphics.Image getImage() {
return new robot.resource.robot.ui.RobotUIMetaInformation().getImageProvider().getImage(quickFix.getImageKey());
}
public String getDescription() {
return null;
}
});
}
return resolutions.toArray(new org.eclipse.ui.IMarkerResolution[resolutions.size()]);
}
}
} catch (Exception e) {
robot.resource.robot.ui.RobotUIPlugin.logError("Exception while computing quick fix resolutions", e);
}
return new org.eclipse.ui.IMarkerResolution[] {};
}
public java.util.Collection<robot.resource.robot.IRobotQuickFix> getQuickFixes(robot.resource.robot.IRobotTextResource resource, org.eclipse.core.resources.IMarker marker) {
java.util.Collection<robot.resource.robot.IRobotQuickFix> foundQuickFixes = new java.util.ArrayList<robot.resource.robot.IRobotQuickFix>();
try {
String quickFixContexts = getQuickFixContextString(marker);
if (quickFixContexts != null) {
String[] quickFixContextParts = quickFixContexts.split("\\|");
for (String quickFixContext : quickFixContextParts) {
robot.resource.robot.IRobotQuickFix quickFix = resource.getQuickFix(quickFixContext);
if (quickFix != null) {
foundQuickFixes.add(quickFix);
}
}
}
} catch (org.eclipse.core.runtime.CoreException ce) {
if (ce.getMessage().matches("Marker.*not found.")) {
// ignore
System.out.println("getQuickFixes() marker not found: " + ce.getMessage());
} else {
ce.printStackTrace();
}
}
return foundQuickFixes;
}
private String getQuickFixContextString(org.eclipse.core.resources.IMarker marker) throws org.eclipse.core.runtime.CoreException {
Object quickFixValue = marker.getAttribute(org.eclipse.core.resources.IMarker.SOURCE_ID);
if (quickFixValue != null && quickFixValue instanceof String) {
return (String) quickFixValue;
}
return null;
}
private boolean hasQuickFixes(org.eclipse.core.resources.IMarker marker) throws org.eclipse.core.runtime.CoreException {
return getQuickFixContextString(marker) != null;
}
}