Package: AbstractExportCompleteOCLEvaluationResultAction$1

AbstractExportCompleteOCLEvaluationResultAction$1

nameinstructionbranchcomplexitylinemethod
run(IProgressMonitor)
M: 24 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 7 C: 0
0%
M: 1 C: 0
0%
{...}
M: 12 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2014 Obeo and others.
3: * All rights reserved. This program and the accompanying materials
4: * are made available under the terms of the Eclipse Public License v1.0
5: * which accompanies this distribution, and is available at
6: * http://www.eclipse.org/legal/epl-v10.html
7: *
8: * Contributors:
9: * Obeo - initial API and implementation
10: *******************************************************************************/
11: package org.eclipse.acceleo.ui.interpreter.completeocl.internal.action;
12:
13: import java.io.IOException;
14: import java.lang.reflect.InvocationTargetException;
15:
16: import org.eclipse.acceleo.ui.interpreter.InterpreterPlugin;
17: import org.eclipse.acceleo.ui.interpreter.completeocl.IEvaluationExporter;
18: import org.eclipse.acceleo.ui.interpreter.completeocl.evaluationresult.OCLElement;
19: import org.eclipse.acceleo.ui.interpreter.completeocl.internal.CompleteOCLEvaluator;
20: import org.eclipse.acceleo.ui.interpreter.language.EvaluationResult;
21: import org.eclipse.core.runtime.CoreException;
22: import org.eclipse.core.runtime.IProgressMonitor;
23: import org.eclipse.core.runtime.IStatus;
24: import org.eclipse.core.runtime.NullProgressMonitor;
25: import org.eclipse.core.runtime.Status;
26: import org.eclipse.emf.ecore.resource.Resource;
27: import org.eclipse.jface.action.Action;
28: import org.eclipse.jface.action.IAction;
29: import org.eclipse.jface.dialogs.ErrorDialog;
30: import org.eclipse.jface.operation.IRunnableWithProgress;
31: import org.eclipse.ocl.pivot.Element;
32: import org.eclipse.ocl.pivot.Model;
33: import org.eclipse.ocl.pivot.utilities.OCL;
34: import org.eclipse.ocl.pivot.utilities.Pivotable;
35: import org.eclipse.swt.widgets.FileDialog;
36: import org.eclipse.ui.PlatformUI;
37: import org.eclipse.xtext.parser.IParseResult;
38: import org.eclipse.xtext.resource.XtextResource;
39:
40: /**
41: * Abstract constraint and operations evaluation results exporter.
42: *
43: * @author <a href="mailto:marwa.rostren@obeo.fr">Marwa Rostren</a>
44: */
45: public class AbstractExportCompleteOCLEvaluationResultAction extends Action {
46:         /** The export actions tooltip text. */
47:         private final static String TOOLTIP_TEXT = "Evaluate the current compilation result against the currently selected resource";
48:
49:         /** The xtext resource to evaluate. */
50:         private final XtextResource resource;
51:
52:         /** The target resource. */
53:         private final Resource target;
54:
55:         /** The exporter. */
56:         protected final IEvaluationExporter exporter;
57:
58:         /** The meta model manager. */
59:         private final OCL ocl;
60:
61:         /**
62:          * Constructor.
63:          *
64:          * @param text
65:          * the action title.
66:          * @param resource
67:          * the xtext resource to evaluate.
68:          * @param target
69:          * the target resource.
70:          * @param metamodelManager
71:          * the meta model manager.
72:          * @param exporter
73:          * the used exporter.
74:          */
75:         public AbstractExportCompleteOCLEvaluationResultAction(String text, XtextResource resource,
76:                         Resource target, OCL ocl, IEvaluationExporter exporter) {
77:                 super(text, IAction.AS_PUSH_BUTTON);
78:                 setToolTipText(TOOLTIP_TEXT);
79:                 this.resource = resource;
80:                 this.target = target;
81:                 this.ocl = ocl;
82:                 this.exporter = exporter;
83:         }
84:
85:         /*
86:          * (non-Javadoc)
87:          * @see org.eclipse.jface.action.Action#run()
88:          */
89:         @Override
90:         public void run() {
91:                 final FileDialog fileDialog = new FileDialog(PlatformUI.getWorkbench().getDisplay().getActiveShell());
92:                 final String savePath = fileDialog.open();
93:                 if (savePath != null) {
94:                         final IParseResult parseResult = resource.getParseResult();
95:
96:                         final EvaluationResult evalutionResult;
97:                         if (parseResult != null && parseResult.getRootASTElement() instanceof Pivotable) {
98:                                 final Element pivotElement = ((Pivotable)parseResult.getRootASTElement()).getPivot();
99:
100:                                 if (pivotElement instanceof Model) {
101:                                         evalutionResult = new CompleteOCLEvaluator(ocl.getEnvironmentFactory())
102:                                                         .evaluateCompleteOCLElement(pivotElement, target);
103:                                 } else {
104:                                         evalutionResult = null;
105:                                 }
106:                         } else {
107:                                 evalutionResult = null;
108:                         }
109:
110:                         if (evalutionResult != null
111:                                         && (evalutionResult.getStatus() == null || evalutionResult.getStatus().isOK())) {
112:                                 if (evalutionResult.getEvaluationResult() instanceof OCLElement) {
113:
114:                                         final IRunnableWithProgress op = new IRunnableWithProgress() {
115:                                                 public void run(IProgressMonitor monitor) {
116:                                                         try {
117:                                                                 exporter.export(savePath, (OCLElement)evalutionResult.getEvaluationResult(),
118:                                                                                 monitor);
119:                                                         } catch (final CoreException e) {
120:                                                                 handleError(e.getCause(), true);
121:                                                         } catch (final IOException e) {
122:                                                                 handleError(e.getCause(), true);
123:                                                         }
124:
125:                                                 }
126:                                         };
127:
128:                                         try {
129:                                                 op.run(new NullProgressMonitor());
130:                                         } catch (InvocationTargetException e) {
131:                                                 handleError(e, false);
132:                                         } catch (InterruptedException e) {
133:                                                 handleError(e, false);
134:                                         }
135:                                 }
136:                         }
137:                 }
138:         }
139:
140:         /**
141:          * Handles error cases.
142:          *
143:          * @param t
144:          * the throwable.
145:          * @param popup
146:          * true if we need to activate the error dialog, false otherwise.
147:          */
148:         static void handleError(Throwable t, boolean popup) {
149:                 final String message = "Internal error:" + t.getMessage(); //$NON-NLS-1$
150:                 final IStatus status;
151:                 if (t instanceof CoreException) {
152:                         status = new Status(((CoreException)t).getStatus().getSeverity(), InterpreterPlugin.PLUGIN_ID,
153:                                         message, t);
154:                 } else {
155:                         status = new Status(IStatus.ERROR, InterpreterPlugin.PLUGIN_ID, message, t);
156:                 }
157:                 InterpreterPlugin.getDefault().getLog().log(status);
158:
159:                 if (popup) {
160:                         PlatformUI.getWorkbench().getDisplay().syncExec(new Runnable() {
161:                                 public void run() {
162:                                         ErrorDialog.openError(PlatformUI.getWorkbench().getDisplay().getActiveShell(),
163:                                                         "Creation problems", message, status); //$NON-NLS-1$
164:                                 }
165:                         });
166:                 }
167:         }
168:
169:         /*
170:          * (non-Javadoc)
171:          * @see org.eclipse.jface.action.Action#isEnabled()
172:          */
173:         @Override
174:         public boolean isEnabled() {
175:                 return resource != null && !resource.getParseResult().hasSyntaxErrors() && target != null;
176:         }
177: }