/** * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * * See the NOTICE file distributed with this work for additional * information regarding copyright ownership. */ package org.sintef.thingml.resource.thingml.ui; public class ThingmlMarkerResolutionGenerator 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(org.sintef.thingml.resource.thingml.IThingmlOptions.DISABLE_CREATING_MARKERS_FOR_PROBLEMS, "true"); org.eclipse.emf.ecore.resource.Resource emfResource = rs.getResource(uri, true); if (emfResource instanceof org.sintef.thingml.resource.thingml.mopp.ThingmlResource) { org.sintef.thingml.resource.thingml.mopp.ThingmlResource customResource = (org.sintef.thingml.resource.thingml.mopp.ThingmlResource) emfResource; org.eclipse.emf.ecore.util.EcoreUtil.resolveAll(customResource); java.util.Collection<org.sintef.thingml.resource.thingml.IThingmlQuickFix> quickFixes = getQuickFixes(customResource, marker); java.util.List<org.eclipse.ui.IMarkerResolution2> resolutions = new java.util.ArrayList<org.eclipse.ui.IMarkerResolution2>(); for (final org.sintef.thingml.resource.thingml.IThingmlQuickFix 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) { org.sintef.thingml.resource.thingml.ui.ThingmlUIPlugin.logError("Exception while applying quick fix", e); } } public String getLabel() { return quickFix.getDisplayString(); } public org.eclipse.swt.graphics.Image getImage() { return new org.sintef.thingml.resource.thingml.ui.ThingmlUIMetaInformation().getImageProvider().getImage(quickFix.getImageKey()); } public String getDescription() { return null; } }); } return resolutions.toArray(new org.eclipse.ui.IMarkerResolution[resolutions.size()]); } } } catch (Exception e) { org.sintef.thingml.resource.thingml.ui.ThingmlUIPlugin.logError("Exception while computing quick fix resolutions", e); } return new org.eclipse.ui.IMarkerResolution[] {}; } public java.util.Collection<org.sintef.thingml.resource.thingml.IThingmlQuickFix> getQuickFixes(org.sintef.thingml.resource.thingml.IThingmlTextResource resource, org.eclipse.core.resources.IMarker marker) { java.util.Collection<org.sintef.thingml.resource.thingml.IThingmlQuickFix> foundQuickFixes = new java.util.ArrayList<org.sintef.thingml.resource.thingml.IThingmlQuickFix>(); try { String quickFixContexts = getQuickFixContextString(marker); if (quickFixContexts != null) { String[] quickFixContextParts = quickFixContexts.split("\\|"); for (String quickFixContext : quickFixContextParts) { org.sintef.thingml.resource.thingml.IThingmlQuickFix 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; } }