/***************************************************************************
* Copyright (c) 2008 Conselleria de Infraestructuras y Transporte,
* Generalitat de la Comunitat Valenciana . 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: Francisco Javier Cano Muñoz (Prodevelop) - initial api implementation
*
******************************************************************************/
package org.eclipse.papyrus.uml.diagram.common.providers;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IMarker;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.gef.EditPart;
import org.eclipse.gmf.runtime.diagram.ui.editparts.IPrimaryEditPart;
import org.eclipse.gmf.runtime.diagram.ui.parts.DiagramEditor;
import org.eclipse.gmf.runtime.diagram.ui.parts.IDiagramWorkbenchPart;
import org.eclipse.gmf.runtime.emf.ui.providers.marker.AbstractModelMarkerNavigationProvider;
import org.eclipse.papyrus.uml.diagram.common.Activator;
/**
* Provider that sets the markers in the diagram that show the validation errors.
*
* @author <a href="mailto:fjcano@prodevelop.es">Francisco Javier Cano Muñoz</a>
*
*/
public class DiagramValidationMarkerNavigationProvider extends AbstractModelMarkerNavigationProvider {
public static final String MARKER_TYPE = "es.cv.gvcase.mdt.common.validation.diagnostic";
protected void doGotoMarker(IMarker marker) {
String elementId = marker.getAttribute(org.eclipse.gmf.runtime.common.core.resources.IMarker.ELEMENT_ID, null);
if(elementId == null || !(getEditor() instanceof DiagramEditor)) {
return;
}
DiagramEditor editor = (DiagramEditor)getEditor();
Map<?, ?> editPartRegistry = editor.getDiagramGraphicalViewer().getEditPartRegistry();
EObject targetView = editor.getDiagram().eResource().getEObject(elementId);
if(targetView == null) {
return;
}
EditPart targetEditPart = (EditPart)editPartRegistry.get(targetView);
if(targetEditPart != null) {
selectElementsInDiagram(editor, Arrays.asList(new EditPart[]{ targetEditPart }));
}
}
public static void selectElementsInDiagram(IDiagramWorkbenchPart diagramPart, List<?> editParts) {
diagramPart.getDiagramGraphicalViewer().deselectAll();
EditPart firstPrimary = null;
for(Iterator<?> it = editParts.iterator(); it.hasNext();) {
EditPart nextPart = (EditPart)it.next();
diagramPart.getDiagramGraphicalViewer().appendSelection(nextPart);
if(firstPrimary == null && nextPart instanceof IPrimaryEditPart) {
firstPrimary = nextPart;
}
}
if(!editParts.isEmpty()) {
diagramPart.getDiagramGraphicalViewer().reveal(firstPrimary != null ? firstPrimary : (EditPart)editParts.get(0));
}
}
/**
* Removes all the Markers from the given resource
*
* @param resource
*/
public static void deleteMarkers(IResource resource) {
try {
resource.deleteMarkers(MARKER_TYPE, true, IResource.DEPTH_ZERO);
} catch (CoreException e) {
Activator.getDefault().logError("Failed to delete validation markers", e); //$NON-NLS-1$
}
}
/**
* Add a single marker for the element placed in the given file
*
* @param file
* @param elementId
* @param location
* @param message
* @param statusSeverity
* @return
*/
public static IMarker addMarker(IFile file, String elementId, String location, String message, int statusSeverity) {
IMarker marker = null;
try {
marker = file.createMarker(MARKER_TYPE);
marker.setAttribute(IMarker.MESSAGE, message);
marker.setAttribute(IMarker.LOCATION, location);
marker.setAttribute(org.eclipse.gmf.runtime.common.ui.resources.IMarker.ELEMENT_ID, elementId);
int markerSeverity = IMarker.SEVERITY_INFO;
if(statusSeverity == IStatus.WARNING) {
markerSeverity = IMarker.SEVERITY_WARNING;
} else if(statusSeverity == IStatus.ERROR || statusSeverity == IStatus.CANCEL) {
markerSeverity = IMarker.SEVERITY_ERROR;
}
marker.setAttribute(IMarker.SEVERITY, markerSeverity);
} catch (CoreException e) {
Activator.getDefault().logError("Failed to create validation marker", e); //$NON-NLS-1$
}
return marker;
}
}