1 | /* |
2 | * Copyright 2006 SDQ Research Group, University of Karlsruhe (TH) |
3 | */ |
4 | package de.uka.ipd.sdq.pcm.gmf.seff.part; |
5 | |
6 | import java.lang.reflect.InvocationTargetException; |
7 | import java.util.ArrayList; |
8 | import java.util.HashSet; |
9 | import java.util.Iterator; |
10 | import java.util.List; |
11 | import java.util.Set; |
12 | |
13 | import org.eclipse.core.resources.IFile; |
14 | import org.eclipse.core.runtime.IProgressMonitor; |
15 | import org.eclipse.core.runtime.IStatus; |
16 | import org.eclipse.core.runtime.NullProgressMonitor; |
17 | import org.eclipse.emf.common.util.Diagnostic; |
18 | import org.eclipse.emf.ecore.EObject; |
19 | import org.eclipse.emf.ecore.util.Diagnostician; |
20 | import org.eclipse.emf.transaction.TransactionalEditingDomain; |
21 | import org.eclipse.emf.transaction.util.TransactionUtil; |
22 | import org.eclipse.emf.validation.model.EvaluationMode; |
23 | import org.eclipse.emf.validation.model.IConstraintStatus; |
24 | import org.eclipse.emf.validation.service.IBatchValidator; |
25 | import org.eclipse.emf.validation.service.ModelValidationService; |
26 | import org.eclipse.emf.workspace.util.WorkspaceSynchronizer; |
27 | import org.eclipse.gef.EditPartViewer; |
28 | import org.eclipse.gmf.runtime.diagram.ui.OffscreenEditPartFactory; |
29 | import org.eclipse.gmf.runtime.diagram.ui.editparts.DiagramEditPart; |
30 | import org.eclipse.gmf.runtime.diagram.ui.parts.IDiagramWorkbenchPart; |
31 | import org.eclipse.gmf.runtime.emf.core.util.EMFCoreUtil; |
32 | import org.eclipse.gmf.runtime.notation.View; |
33 | import org.eclipse.jface.action.Action; |
34 | import org.eclipse.jface.operation.IRunnableWithProgress; |
35 | import org.eclipse.ui.IEditorPart; |
36 | import org.eclipse.ui.IWorkbenchPage; |
37 | import org.eclipse.ui.IWorkbenchPart; |
38 | import org.eclipse.ui.PlatformUI; |
39 | import org.eclipse.ui.actions.WorkspaceModifyDelegatingOperation; |
40 | |
41 | import de.uka.ipd.sdq.pcm.gmf.seff.providers.PalladioComponentModelMarkerNavigationProvider; |
42 | import de.uka.ipd.sdq.pcm.gmf.seff.providers.PalladioComponentModelValidationProvider; |
43 | |
44 | /** |
45 | * @generated |
46 | */ |
47 | public class ValidateAction extends Action { |
48 | |
49 | /** |
50 | * @generated |
51 | */ |
52 | private IWorkbenchPage page; |
53 | |
54 | /** |
55 | * @generated |
56 | */ |
57 | public ValidateAction(IWorkbenchPage page) { |
58 | setText(Messages.ValidateActionMessage); |
59 | this.page = page; |
60 | } |
61 | |
62 | /** |
63 | * @generated |
64 | */ |
65 | public void run() { |
66 | IWorkbenchPart workbenchPart = page.getActivePart(); |
67 | if (workbenchPart instanceof IDiagramWorkbenchPart) { |
68 | final IDiagramWorkbenchPart part = (IDiagramWorkbenchPart) workbenchPart; |
69 | try { |
70 | new WorkspaceModifyDelegatingOperation( |
71 | new IRunnableWithProgress() { |
72 | |
73 | public void run(IProgressMonitor monitor) |
74 | throws InterruptedException, |
75 | InvocationTargetException { |
76 | runValidation(part.getDiagramEditPart(), part |
77 | .getDiagram()); |
78 | } |
79 | }).run(new NullProgressMonitor()); |
80 | } catch (Exception e) { |
81 | PalladioComponentModelSeffDiagramEditorPlugin.getInstance() |
82 | .logError("Validation action failed", e); //$NON-NLS-1$ |
83 | } |
84 | } |
85 | } |
86 | |
87 | /** |
88 | * @generated |
89 | */ |
90 | public static void runValidation(View view) { |
91 | try { |
92 | if (PalladioComponentModelDiagramEditorUtil.openDiagram(view |
93 | .eResource())) { |
94 | IEditorPart editorPart = PlatformUI.getWorkbench() |
95 | .getActiveWorkbenchWindow().getActivePage() |
96 | .getActiveEditor(); |
97 | if (editorPart instanceof IDiagramWorkbenchPart) { |
98 | runValidation(((IDiagramWorkbenchPart) editorPart) |
99 | .getDiagramEditPart(), view); |
100 | } else { |
101 | runNonUIValidation(view); |
102 | } |
103 | } |
104 | } catch (Exception e) { |
105 | PalladioComponentModelSeffDiagramEditorPlugin.getInstance() |
106 | .logError("Validation action failed", e); //$NON-NLS-1$ |
107 | } |
108 | } |
109 | |
110 | /** |
111 | * @generated |
112 | */ |
113 | public static void runNonUIValidation(View view) { |
114 | DiagramEditPart diagramEditPart = OffscreenEditPartFactory |
115 | .getInstance().createDiagramEditPart(view.getDiagram()); |
116 | runValidation(diagramEditPart, view); |
117 | } |
118 | |
119 | /** |
120 | * @generated |
121 | */ |
122 | public static void runValidation(DiagramEditPart diagramEditPart, View view) { |
123 | final DiagramEditPart fpart = diagramEditPart; |
124 | final View fview = view; |
125 | TransactionalEditingDomain txDomain = TransactionUtil |
126 | .getEditingDomain(view); |
127 | PalladioComponentModelValidationProvider.runWithConstraints(txDomain, |
128 | new Runnable() { |
129 | |
130 | public void run() { |
131 | validate(fpart, fview); |
132 | } |
133 | }); |
134 | } |
135 | |
136 | /** |
137 | * @generated |
138 | */ |
139 | private static Diagnostic runEMFValidator(View target) { |
140 | if (target.isSetElement() && target.getElement() != null) { |
141 | return new Diagnostician() { |
142 | |
143 | public String getObjectLabel(EObject eObject) { |
144 | return EMFCoreUtil.getQualifiedName(eObject, true); |
145 | } |
146 | }.validate(target.getElement()); |
147 | } |
148 | return Diagnostic.OK_INSTANCE; |
149 | } |
150 | |
151 | /** |
152 | * @generated |
153 | */ |
154 | private static void validate(DiagramEditPart diagramEditPart, View view) { |
155 | IFile target = view.eResource() != null ? WorkspaceSynchronizer |
156 | .getFile(view.eResource()) : null; |
157 | if (target != null) { |
158 | PalladioComponentModelMarkerNavigationProvider |
159 | .deleteMarkers(target); |
160 | } |
161 | Diagnostic diagnostic = runEMFValidator(view); |
162 | createMarkers(target, diagnostic, diagramEditPart); |
163 | IBatchValidator validator = (IBatchValidator) ModelValidationService |
164 | .getInstance().newValidator(EvaluationMode.BATCH); |
165 | validator.setIncludeLiveConstraints(true); |
166 | if (view.isSetElement() && view.getElement() != null) { |
167 | IStatus status = validator.validate(view.getElement()); |
168 | createMarkers(target, status, diagramEditPart); |
169 | } |
170 | } |
171 | |
172 | /** |
173 | * @generated |
174 | */ |
175 | private static void createMarkers(IFile target, IStatus validationStatus, |
176 | DiagramEditPart diagramEditPart) { |
177 | if (validationStatus.isOK()) { |
178 | return; |
179 | } |
180 | final IStatus rootStatus = validationStatus; |
181 | List allStatuses = new ArrayList(); |
182 | PalladioComponentModelDiagramEditorUtil.LazyElement2ViewMap element2ViewMap = new PalladioComponentModelDiagramEditorUtil.LazyElement2ViewMap( |
183 | diagramEditPart.getDiagramView(), collectTargetElements( |
184 | rootStatus, new HashSet(), allStatuses)); |
185 | for (Iterator it = allStatuses.iterator(); it.hasNext();) { |
186 | IConstraintStatus nextStatus = (IConstraintStatus) it.next(); |
187 | View view = PalladioComponentModelDiagramEditorUtil.findView( |
188 | diagramEditPart, nextStatus.getTarget(), element2ViewMap); |
189 | addMarker(diagramEditPart.getViewer(), target, view.eResource() |
190 | .getURIFragment(view), EMFCoreUtil.getQualifiedName( |
191 | nextStatus.getTarget(), true), nextStatus.getMessage(), |
192 | nextStatus.getSeverity()); |
193 | } |
194 | } |
195 | |
196 | /** |
197 | * @generated |
198 | */ |
199 | private static void createMarkers(IFile target, |
200 | Diagnostic emfValidationStatus, DiagramEditPart diagramEditPart) { |
201 | if (emfValidationStatus.getSeverity() == Diagnostic.OK) { |
202 | return; |
203 | } |
204 | final Diagnostic rootStatus = emfValidationStatus; |
205 | List allDiagnostics = new ArrayList(); |
206 | PalladioComponentModelDiagramEditorUtil.LazyElement2ViewMap element2ViewMap = new PalladioComponentModelDiagramEditorUtil.LazyElement2ViewMap( |
207 | diagramEditPart.getDiagramView(), collectTargetElements( |
208 | rootStatus, new HashSet(), allDiagnostics)); |
209 | for (Iterator it = emfValidationStatus.getChildren().iterator(); it |
210 | .hasNext();) { |
211 | Diagnostic nextDiagnostic = (Diagnostic) it.next(); |
212 | List data = nextDiagnostic.getData(); |
213 | if (data != null && !data.isEmpty() |
214 | && data.get(0) instanceof EObject) { |
215 | EObject element = (EObject) data.get(0); |
216 | View view = PalladioComponentModelDiagramEditorUtil.findView( |
217 | diagramEditPart, element, element2ViewMap); |
218 | addMarker( |
219 | diagramEditPart.getViewer(), |
220 | target, |
221 | view.eResource().getURIFragment(view), |
222 | EMFCoreUtil.getQualifiedName(element, true), |
223 | nextDiagnostic.getMessage(), |
224 | diagnosticToStatusSeverity(nextDiagnostic.getSeverity())); |
225 | } |
226 | } |
227 | } |
228 | |
229 | /** |
230 | * @generated |
231 | */ |
232 | private static void addMarker(EditPartViewer viewer, IFile target, |
233 | String elementId, String location, String message, |
234 | int statusSeverity) { |
235 | if (target == null) { |
236 | return; |
237 | } |
238 | PalladioComponentModelMarkerNavigationProvider.addMarker(target, |
239 | elementId, location, message, statusSeverity); |
240 | } |
241 | |
242 | /** |
243 | * @generated |
244 | */ |
245 | private static int diagnosticToStatusSeverity(int diagnosticSeverity) { |
246 | if (diagnosticSeverity == Diagnostic.OK) { |
247 | return IStatus.OK; |
248 | } else if (diagnosticSeverity == Diagnostic.INFO) { |
249 | return IStatus.INFO; |
250 | } else if (diagnosticSeverity == Diagnostic.WARNING) { |
251 | return IStatus.WARNING; |
252 | } else if (diagnosticSeverity == Diagnostic.ERROR |
253 | || diagnosticSeverity == Diagnostic.CANCEL) { |
254 | return IStatus.ERROR; |
255 | } |
256 | return IStatus.INFO; |
257 | } |
258 | |
259 | /** |
260 | * @generated |
261 | */ |
262 | private static Set collectTargetElements(IStatus status, |
263 | Set targetElementCollector, List allConstraintStatuses) { |
264 | if (status instanceof IConstraintStatus) { |
265 | targetElementCollector |
266 | .add(((IConstraintStatus) status).getTarget()); |
267 | allConstraintStatuses.add(status); |
268 | } |
269 | if (status.isMultiStatus()) { |
270 | IStatus[] children = status.getChildren(); |
271 | for (int i = 0; i < children.length; i++) { |
272 | collectTargetElements(children[i], targetElementCollector, |
273 | allConstraintStatuses); |
274 | } |
275 | } |
276 | return targetElementCollector; |
277 | } |
278 | |
279 | /** |
280 | * @generated |
281 | */ |
282 | private static Set collectTargetElements(Diagnostic diagnostic, |
283 | Set targetElementCollector, List allDiagnostics) { |
284 | List data = diagnostic.getData(); |
285 | EObject target = null; |
286 | if (data != null && !data.isEmpty() && data.get(0) instanceof EObject) { |
287 | target = (EObject) data.get(0); |
288 | targetElementCollector.add(target); |
289 | allDiagnostics.add(diagnostic); |
290 | } |
291 | if (diagnostic.getChildren() != null |
292 | && !diagnostic.getChildren().isEmpty()) { |
293 | for (Iterator it = diagnostic.getChildren().iterator(); it |
294 | .hasNext();) { |
295 | collectTargetElements((Diagnostic) it.next(), |
296 | targetElementCollector, allDiagnostics); |
297 | } |
298 | } |
299 | return targetElementCollector; |
300 | } |
301 | } |