001: package newprocess.diagram.providers;
002:
003: import java.util.ArrayList;
004: import java.util.Arrays;
005: import java.util.HashSet;
006: import java.util.Iterator;
007: import java.util.List;
008: import java.util.Map;
009: import java.util.Set;
010:
011: import newprocess.diagram.part.New_processDiagramEditorPlugin;
012: import newprocess.diagram.part.New_processDiagramEditorUtil;
013:
014: import org.eclipse.core.resources.IFile;
015: import org.eclipse.core.resources.IMarker;
016: import org.eclipse.core.resources.IResource;
017:
018: import org.eclipse.core.runtime.CoreException;
019: import org.eclipse.core.runtime.IStatus;
020:
021: import org.eclipse.emf.common.util.Diagnostic;
022:
023: import org.eclipse.emf.ecore.EObject;
024:
025: import org.eclipse.emf.validation.model.IConstraintStatus;
026:
027: import org.eclipse.gef.EditPart;
028:
029: import org.eclipse.gmf.runtime.diagram.ui.editparts.DiagramEditPart;
030:
031: import org.eclipse.gmf.runtime.diagram.ui.parts.DiagramEditor;
032:
033: import org.eclipse.gmf.runtime.emf.core.util.EMFCoreUtil;
034:
035: import org.eclipse.gmf.runtime.emf.ui.providers.marker.AbstractModelMarkerNavigationProvider;
036:
037: import org.eclipse.gmf.runtime.notation.View;
038:
039: /**
040: * @generated
041: */
042: public class New_processMarkerNavigationProvider extends
043: AbstractModelMarkerNavigationProvider {
044: /**
045: * @generated
046: */
047: public static final String MARKER_TYPE = New_processDiagramEditorPlugin.ID
048: + ".diagnostic"; //$NON-NLS-1$
049:
050: /**
051: * @generated
052: */
053: protected void doGotoMarker(IMarker marker) {
054: String elementId = marker
055: .getAttribute(
056: org.eclipse.gmf.runtime.common.core.resources.IMarker.ELEMENT_ID,
057: null);
058: if (elementId == null
059: || !(getEditor() instanceof DiagramEditor)) {
060: return;
061: }
062:
063: DiagramEditor editor = (DiagramEditor) getEditor();
064: Map editPartRegistry = editor.getDiagramGraphicalViewer()
065: .getEditPartRegistry();
066: EObject targetView = editor.getDiagram().eResource()
067: .getEObject(elementId);
068: if (targetView == null) {
069: return;
070: }
071: EditPart targetEditPart = (EditPart) editPartRegistry
072: .get(targetView);
073: if (targetEditPart != null) {
074: New_processDiagramEditorUtil.selectElementsInDiagram(
075: editor, Arrays
076: .asList(new EditPart[] { targetEditPart }));
077: }
078: }
079:
080: /**
081: * @generated
082: */
083: public static void createMarkers(IFile diagramFile,
084: IStatus validationStatus, DiagramEditPart diagramEditPart) {
085: if (validationStatus.isOK())
086: return;
087:
088: final IStatus rootStatus = validationStatus;
089: List allStatuses = new ArrayList();
090: New_processDiagramEditorUtil.LazyElement2ViewMap element2ViewMap = new New_processDiagramEditorUtil.LazyElement2ViewMap(
091: diagramEditPart.getDiagramView(),
092: collectTargetElements(rootStatus, new HashSet(),
093: allStatuses));
094:
095: for (Iterator it = allStatuses.iterator(); it.hasNext();) {
096: IConstraintStatus nextStatus = (IConstraintStatus) it
097: .next();
098: View view = New_processDiagramEditorUtil.findView(
099: diagramEditPart, nextStatus.getTarget(),
100: element2ViewMap);
101: IMarker newMarker = addMarker(diagramFile, view.eResource()
102: .getURIFragment(view), EMFCoreUtil
103: .getQualifiedName(nextStatus.getTarget(), true),
104: nextStatus.getMessage(), nextStatus.getSeverity());
105: if (newMarker != null) {
106: adjustMarker(newMarker, nextStatus);
107: }
108: }
109: }
110:
111: /**
112: * @generated
113: */
114: public static void createMarkers(IFile diagramFile,
115: Diagnostic emfValidationStatus,
116: DiagramEditPart diagramEditPart) {
117: if (emfValidationStatus.getSeverity() == Diagnostic.OK)
118: return;
119:
120: final Diagnostic rootStatus = emfValidationStatus;
121: List allDiagnostics = new ArrayList();
122: New_processDiagramEditorUtil.LazyElement2ViewMap element2ViewMap = new New_processDiagramEditorUtil.LazyElement2ViewMap(
123: diagramEditPart.getDiagramView(),
124: collectTargetElements(rootStatus, new HashSet(),
125: allDiagnostics));
126:
127: for (Iterator it = emfValidationStatus.getChildren().iterator(); it
128: .hasNext();) {
129: Diagnostic nextDiagnostic = (Diagnostic) it.next();
130: List data = nextDiagnostic.getData();
131: if (data != null && !data.isEmpty()
132: && data.get(0) instanceof EObject) {
133: EObject element = (EObject) data.get(0);
134: View view = New_processDiagramEditorUtil.findView(
135: diagramEditPart, element, element2ViewMap);
136: IMarker newMarker = addMarker(diagramFile, view
137: .eResource().getURIFragment(view), EMFCoreUtil
138: .getQualifiedName(element, true),
139: nextDiagnostic.getMessage(),
140: diagnosticToStatusSeverity(nextDiagnostic
141: .getSeverity()));
142: if (newMarker != null) {
143: adjustMarker(newMarker, emfValidationStatus);
144: }
145: }
146: }
147: }
148:
149: /**
150: * @generated
151: */
152: public static void deleteMarkers(IResource resource) {
153: try {
154: resource.deleteMarkers(MARKER_TYPE, true,
155: IResource.DEPTH_ZERO);
156: } catch (CoreException e) {
157: New_processDiagramEditorPlugin.getInstance().logError(
158: "Failed to delete validation markers", e); //$NON-NLS-1$
159: }
160: }
161:
162: /**
163: * @generated
164: */
165: public static void adjustMarker(IMarker marker,
166: Diagnostic sourceDiagnostic) {
167: assert marker != null && sourceDiagnostic != null;
168: }
169:
170: /**
171: * @generated
172: */
173: public static void adjustMarker(IMarker marker,
174: IConstraintStatus sourceStatus) {
175: assert marker != null && sourceStatus != null;
176: }
177:
178: /**
179: * @generated
180: */
181: private static IMarker addMarker(IFile file, String elementId,
182: String location, String message, int statusSeverity) {
183: IMarker marker = null;
184: try {
185: marker = file.createMarker(MARKER_TYPE);
186: marker.setAttribute(IMarker.MESSAGE, message);
187: marker.setAttribute(IMarker.LOCATION, location);
188: marker
189: .setAttribute(
190: org.eclipse.gmf.runtime.common.ui.resources.IMarker.ELEMENT_ID,
191: elementId);
192: int markerSeverity = IMarker.SEVERITY_INFO;
193: if (statusSeverity == IStatus.WARNING) {
194: markerSeverity = IMarker.SEVERITY_WARNING;
195: } else if (statusSeverity == IStatus.ERROR
196: || statusSeverity == IStatus.CANCEL) {
197: markerSeverity = IMarker.SEVERITY_ERROR;
198: }
199: marker.setAttribute(IMarker.SEVERITY, markerSeverity);
200: } catch (CoreException e) {
201: New_processDiagramEditorPlugin.getInstance().logError(
202: "Failed to create validation marker", e); //$NON-NLS-1$
203: }
204: return marker;
205: }
206:
207: /**
208: * @generated
209: */
210: private static int diagnosticToStatusSeverity(int diagnosticSeverity) {
211: if (diagnosticSeverity == Diagnostic.OK) {
212: return IStatus.OK;
213: } else if (diagnosticSeverity == Diagnostic.INFO) {
214: return IStatus.INFO;
215: } else if (diagnosticSeverity == Diagnostic.WARNING) {
216: return IStatus.WARNING;
217: } else if (diagnosticSeverity == Diagnostic.ERROR
218: || diagnosticSeverity == Diagnostic.CANCEL) {
219: return IStatus.ERROR;
220: }
221: return IStatus.INFO;
222: }
223:
224: /**
225: * @generated
226: */
227: private static Set collectTargetElements(IStatus status,
228: Set targetElementCollector, List allConstraintStatuses) {
229: if (status instanceof IConstraintStatus) {
230: targetElementCollector.add(((IConstraintStatus) status)
231: .getTarget());
232: allConstraintStatuses.add(status);
233: }
234: if (status.isMultiStatus()) {
235: IStatus[] children = status.getChildren();
236: for (int i = 0; i < children.length; i++) {
237: collectTargetElements(children[i],
238: targetElementCollector, allConstraintStatuses);
239: }
240: }
241: return targetElementCollector;
242: }
243:
244: /**
245: * @generated
246: */
247: private static Set collectTargetElements(Diagnostic diagnostic,
248: Set targetElementCollector, List allDiagnostics) {
249: List data = diagnostic.getData();
250: EObject target = null;
251: if (data != null && !data.isEmpty()
252: && data.get(0) instanceof EObject) {
253: target = (EObject) data.get(0);
254: targetElementCollector.add(target);
255: allDiagnostics.add(diagnostic);
256: }
257: if (diagnostic.getChildren() != null
258: && !diagnostic.getChildren().isEmpty()) {
259: for (Iterator it = diagnostic.getChildren().iterator(); it
260: .hasNext();) {
261: collectTargetElements((Diagnostic) it.next(),
262: targetElementCollector, allDiagnostics);
263: }
264: }
265: return targetElementCollector;
266: }
267: }
|