analysis.lami: Replace OnDemandAnalysisException with CoreException
[deliverable/tracecompass.git] / analysis / org.eclipse.tracecompass.analysis.lami.ui / src / org / eclipse / tracecompass / internal / provisional / analysis / lami / ui / handler / RunAnalysisHandler.java
1 /*******************************************************************************
2 * Copyright (c) 2015, 2016 EfficiOS Inc., Alexandre Montplaisir
3 *
4 * All rights reserved. This program and the accompanying materials are
5 * made available under the terms of the Eclipse Public License v1.0 which
6 * accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
8 *******************************************************************************/
9
10 package org.eclipse.tracecompass.internal.provisional.analysis.lami.ui.handler;
11
12 import static org.eclipse.tracecompass.common.core.NonNullUtils.nullToEmptyString;
13
14 import java.util.List;
15
16 import org.eclipse.core.commands.AbstractHandler;
17 import org.eclipse.core.commands.ExecutionEvent;
18 import org.eclipse.core.commands.ExecutionException;
19 import org.eclipse.core.runtime.CoreException;
20 import org.eclipse.core.runtime.IProgressMonitor;
21 import org.eclipse.core.runtime.IStatus;
22 import org.eclipse.core.runtime.NullProgressMonitor;
23 import org.eclipse.core.runtime.Status;
24 import org.eclipse.core.runtime.jobs.Job;
25 import org.eclipse.jdt.annotation.Nullable;
26 import org.eclipse.jface.dialogs.ErrorDialog;
27 import org.eclipse.jface.dialogs.IInputValidator;
28 import org.eclipse.jface.viewers.ISelection;
29 import org.eclipse.jface.viewers.IStructuredSelection;
30 import org.eclipse.jface.window.Window;
31 import org.eclipse.swt.widgets.Display;
32 import org.eclipse.swt.widgets.Shell;
33 import org.eclipse.tracecompass.internal.provisional.analysis.lami.core.module.LamiAnalysis;
34 import org.eclipse.tracecompass.internal.provisional.analysis.lami.core.module.LamiAnalysisReport;
35 import org.eclipse.tracecompass.internal.provisional.analysis.lami.core.module.LamiResultTable;
36 import org.eclipse.tracecompass.internal.provisional.analysis.lami.ui.views.LamiReportViewFactory;
37 import org.eclipse.tracecompass.tmf.core.analysis.ondemand.IOnDemandAnalysis;
38 import org.eclipse.tracecompass.tmf.core.analysis.ondemand.IOnDemandAnalysisReport;
39 import org.eclipse.tracecompass.tmf.core.timestamp.TmfTimeRange;
40 import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace;
41 import org.eclipse.tracecompass.tmf.core.trace.TmfTraceManager;
42 import org.eclipse.tracecompass.tmf.ui.project.model.TmfCommonProjectElement;
43 import org.eclipse.tracecompass.tmf.ui.project.model.TmfOnDemandAnalysisElement;
44 import org.eclipse.tracecompass.tmf.ui.project.model.TmfReportsElement;
45 import org.eclipse.ui.PartInitException;
46 import org.eclipse.ui.PlatformUI;
47 import org.eclipse.ui.handlers.HandlerUtil;
48
49 /**
50 * The command handler for the "Run External Analysis" menu option.
51 *
52 * @author Alexandre Montplaisir
53 */
54 public class RunAnalysisHandler extends AbstractHandler {
55
56 @Override
57 public boolean isEnabled() {
58 final Object element = HandlerUtils.getSelectedModelElement();
59 if (element == null) {
60 return false;
61 }
62
63 /*
64 * plugin.xml should have done type verifications already
65 */
66 TmfOnDemandAnalysisElement elem = (TmfOnDemandAnalysisElement) element;
67 if (elem.getAnalysis() instanceof LamiAnalysis && elem.canRun()) {
68 return true;
69 }
70
71 return false;
72 }
73
74 @Override
75 public @Nullable Object execute(@Nullable ExecutionEvent event) throws ExecutionException {
76
77 /* Types should have been checked by the plugin.xml already */
78 ISelection selection = HandlerUtil.getCurrentSelectionChecked(event);
79 Object element = ((IStructuredSelection) selection).getFirstElement();
80 final TmfOnDemandAnalysisElement analysisElem = (TmfOnDemandAnalysisElement) element;
81
82 TmfCommonProjectElement traceElem = analysisElem.getParent().getParent();
83 ITmfTrace trace = traceElem.getTrace();
84 if (trace == null) {
85 /* That trace is not currently opened */
86 return null;
87 }
88
89 /* Retrieve and initialize the analysis module, aka read the script's metadata */
90 IOnDemandAnalysis ondemandAnalysis = analysisElem.getAnalysis();
91 if (!(ondemandAnalysis instanceof LamiAnalysis)) {
92 return null;
93 }
94 LamiAnalysis analysis = (LamiAnalysis) ondemandAnalysis;
95
96 /* Retrieve the current time range, will be used as parameters to the analysis */
97 TmfTraceManager tm = TmfTraceManager.getInstance();
98 TmfTimeRange timeRange = tm.getCurrentTraceContext().getSelectionRange();
99 if (timeRange.getStartTime().equals(timeRange.getEndTime())) {
100 timeRange = null;
101 }
102 /* Job below needs a final reference... */
103 final TmfTimeRange tr = timeRange;
104
105 /* Pop the dialog to ask for extra parameters */
106 String baseCommand = analysis.getFullCommandAsString(trace, tr);
107
108 Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
109 ParameterDialog dialog = new ParameterDialog(shell, Messages.ParameterDialog_ExternalParameters,
110 Messages.ParameterDialog_ExternalParametersDescription,
111 baseCommand,
112 PARAM_STRING_VALIDATOR);
113 if (dialog.open() != Window.OK) {
114 /* User clicked Cancel, don't run */
115 return null;
116 }
117 String extraParams = nullToEmptyString(dialog.getValue());
118
119 /* Execute the analysis and produce the reports */
120 Job job = new Job(Messages.LamiAnalysis_MainTaskName) {
121 @Override
122 protected @Nullable IStatus run(@Nullable IProgressMonitor monitor) {
123 IProgressMonitor mon = (monitor == null ? new NullProgressMonitor() : monitor);
124 try {
125 List<LamiResultTable> results = analysis.execute(trace, tr, extraParams, mon);
126
127 String reportName = analysis.getName() +' ' + Messages.ParameterDialog_ReportNameSuffix;
128 LamiAnalysisReport report = new LamiAnalysisReport(reportName, results);
129 registerNewReport(analysisElem, report);
130
131 /* Automatically open the report for convenience */
132 Display.getDefault().syncExec(() -> {
133 try {
134 LamiReportViewFactory.createNewView(report);
135 } catch (PartInitException e) {
136 }
137 });
138 return Status.OK_STATUS;
139
140 } catch (CoreException e) {
141 /*
142 * The analysis execution did not complete normally, we will
143 * report it to the user.
144 */
145 IStatus status = e.getStatus();
146
147 /* Don't display a dialog if it was simply cancelled by the user */
148 if (status.matches(IStatus.CANCEL)) {
149 return status;
150 }
151
152 String dialogTitle;
153 String dialogMessage;
154 if (status.matches(IStatus.ERROR)) {
155 dialogTitle = Messages.ErrorDialog_Error;
156 dialogMessage = Messages.ErrorDialog_ErrorMessage;
157 } else {
158 dialogTitle = Messages.ErrorDialog_Info;
159 dialogMessage = Messages.ErrorDialog_InfoMessage;
160 }
161
162 Display.getDefault().asyncExec(() -> {
163 ErrorDialog.openError(shell, dialogTitle, dialogMessage, status);
164 });
165
166 /*
167 * We showed our own error message, no need for the Job to
168 * show another one.
169 */
170 return Status.OK_STATUS;
171 }
172 }
173 };
174 job.schedule();
175
176 return null;
177 }
178
179 private static final IInputValidator PARAM_STRING_VALIDATOR = text -> {
180 if (text.isEmpty() || text.matches("[a-zA-Z0-9\\,\\-\\s]+")) { //$NON-NLS-1$
181 return null;
182 }
183 return Messages.ParameterDialog_StringValidatorMessage;
184 };
185
186 /**
187 * Register a new report
188 *
189 * @param analysisElem
190 * The analysis's project element
191 * @param report
192 * The report to add
193 */
194 public void registerNewReport(TmfOnDemandAnalysisElement analysisElem, IOnDemandAnalysisReport report) {
195 /* For now the TmfProjectReportsElement manages the reports. */
196 TmfReportsElement reportsElement = analysisElem
197 .getParent()
198 .getParent()
199 .getChildElementReports();
200
201 reportsElement.addReport(report);
202 }
203
204 }
This page took 0.035547 seconds and 5 git commands to generate.