1 /*******************************************************************************
2 * Copyright (c) 2013, 2014 École Polytechnique de Montréal
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
10 * Geneviève Bastien - Initial implementation and API
11 *******************************************************************************/
13 package org
.eclipse
.linuxtools
.internal
.tmf
.ui
.project
.handlers
;
15 import java
.util
.ArrayList
;
16 import java
.util
.Iterator
;
18 import org
.eclipse
.core
.commands
.AbstractHandler
;
19 import org
.eclipse
.core
.commands
.ExecutionEvent
;
20 import org
.eclipse
.core
.commands
.ExecutionException
;
21 import org
.eclipse
.core
.runtime
.CoreException
;
22 import org
.eclipse
.jface
.viewers
.ISelection
;
23 import org
.eclipse
.jface
.viewers
.ISelectionProvider
;
24 import org
.eclipse
.jface
.viewers
.TreeSelection
;
25 import org
.eclipse
.linuxtools
.internal
.tmf
.ui
.Activator
;
26 import org
.eclipse
.linuxtools
.tmf
.core
.event
.ITmfEvent
;
27 import org
.eclipse
.linuxtools
.tmf
.core
.exceptions
.TmfTraceException
;
28 import org
.eclipse
.linuxtools
.tmf
.core
.synchronization
.SynchronizationAlgorithm
;
29 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfTrace
;
30 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.TmfExperiment
;
31 import org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
.ITmfProjectModelElement
;
32 import org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
.TmfExperimentElement
;
33 import org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
.TmfTraceElement
;
34 import org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
.TraceUtils
;
35 import org
.eclipse
.swt
.widgets
.Display
;
36 import org
.eclipse
.ui
.IWorkbenchPage
;
37 import org
.eclipse
.ui
.IWorkbenchPart
;
38 import org
.eclipse
.ui
.IWorkbenchWindow
;
39 import org
.eclipse
.ui
.PlatformUI
;
42 * Handles the synchronization of an experiment, when the user selects this
45 public class SynchronizeTracesHandler
extends AbstractHandler
{
47 // ------------------------------------------------------------------------
49 // ------------------------------------------------------------------------
51 private TreeSelection fSelection
= null;
52 private static final String CR
= System
.getProperty("line.separator"); //$NON-NLS-1$
54 // ------------------------------------------------------------------------
56 // ------------------------------------------------------------------------
59 public boolean isEnabled() {
63 // ------------------------------------------------------------------------
65 // ------------------------------------------------------------------------
68 public Object
execute(ExecutionEvent event
) throws ExecutionException
{
70 // Check if we are closing down
71 IWorkbenchWindow window
= PlatformUI
.getWorkbench().getActiveWorkbenchWindow();
77 IWorkbenchPage page
= PlatformUI
.getWorkbench().getActiveWorkbenchWindow().getActivePage();
78 IWorkbenchPart part
= page
.getActivePart();
82 ISelectionProvider selectionProvider
= part
.getSite().getSelectionProvider();
83 if (selectionProvider
== null) {
86 ISelection selection
= selectionProvider
.getSelection();
88 // Make sure selection contains only traces
90 final ArrayList
<TmfTraceElement
> tl
= new ArrayList
<>();
91 final ArrayList
<TmfExperimentElement
> uiexperiment
= new ArrayList
<>();
92 if (selection
instanceof TreeSelection
) {
93 fSelection
= (TreeSelection
) selection
;
94 Iterator
<Object
> iterator
= fSelection
.iterator();
95 while (iterator
.hasNext()) {
96 Object element
= iterator
.next();
97 if (element
instanceof TmfTraceElement
) {
98 tl
.add((TmfTraceElement
) element
);
99 } else if (element
instanceof TmfExperimentElement
) {
100 TmfExperimentElement exp
= (TmfExperimentElement
) element
;
101 uiexperiment
.add(exp
);
102 for (TmfTraceElement trace
: exp
.getTraces()) {
109 if ((uiexperiment
.size() == 1) && (tl
.size() > 1)) {
111 Thread thread
= new Thread() {
115 final ITmfTrace
[] traces
= new ITmfTrace
[tl
.size()];
116 final TmfExperimentElement exp
= uiexperiment
.get(0);
118 for (int i
= 0; i
< tl
.size(); i
++) {
119 ITmfTrace trace
= tl
.get(i
).instantiateTrace();
120 ITmfEvent traceEvent
= tl
.get(i
).instantiateEvent();
122 TraceUtils
.displayErrorMsg(Messages
.SynchronizeTracesHandler_Title
, Messages
.SynchronizeTracesHandler_WrongType
+ tl
.get(i
).getName());
123 for (int j
= 0; j
< i
; j
++) {
129 trace
.initTrace(tl
.get(i
).getResource(), tl
.get(i
).getLocation().getPath(), traceEvent
.getClass());
130 } catch (TmfTraceException e
) {
131 TraceUtils
.displayErrorMsg(Messages
.SynchronizeTracesHandler_Title
, Messages
.SynchronizeTracesHandler_InitError
+ CR
+ CR
+ e
);
133 for (int j
= 0; j
< i
; j
++) {
142 * FIXME Unlike traces, there is no instanceExperiment, so
143 * we call this function here alone. Maybe it would be
144 * better to do this on experiment's element constructor?
146 exp
.refreshSupplementaryFolder();
147 final TmfExperiment experiment
= new TmfExperiment(ITmfEvent
.class, exp
.getName(), traces
, exp
.getResource());
150 final SynchronizationAlgorithm syncAlgo
= experiment
.synchronizeTraces(true);
152 Display
.getDefault().asyncExec(new Runnable() {
156 * For each trace in the experiment, if there is
157 * a transform equation, copy the original
158 * trace, so that a new state system will be
159 * generated with sync time.
161 for (int i
= 0; i
< tl
.size(); i
++) {
162 TmfTraceElement traceel
= tl
.get(i
);
164 if (syncAlgo
.isTraceSynced(traceel
.getName())) {
166 /* Find the original trace */
167 TmfTraceElement origtrace
= null;
168 for (ITmfProjectModelElement el
: traceel
.getProject().getTracesFolder().getTraces()) {
169 if (el
.getName().equals(traceel
.getName())) {
170 origtrace
= (TmfTraceElement
) el
;
174 if (origtrace
!= null) {
176 * Make sure a trace with the
177 * new name does not exist
179 String newname
= traceel
.getName();
183 newname
+= "_"; //$NON-NLS-1$
184 for (ITmfProjectModelElement el
: traceel
.getProject().getTracesFolder().getTraces()) {
185 if (el
.getName().equals(newname
)) {
189 } while (traceexists
);
191 /* Copy the original trace */
192 TmfTraceElement newtrace
= origtrace
.copy(newname
);
194 if (newtrace
!= null) {
196 syncAlgo
.renameTrace(origtrace
.getName(), newtrace
.getName());
199 * Instantiate the new trace
200 * and set its sync formula
202 ITmfTrace trace
= newtrace
.instantiateTrace();
203 ITmfEvent traceEvent
= newtrace
.instantiateEvent();
205 trace
.initTrace(newtrace
.getResource(), newtrace
.getLocation().getPath(), traceEvent
.getClass());
206 trace
.setTimestampTransform(syncAlgo
.getTimestampTransform(trace
));
209 * Add the new trace to the
212 exp
.addTrace(newtrace
);
215 * Delete the original trace
218 exp
.removeTrace(traceel
);
220 TraceUtils
.displayErrorMsg(Messages
.SynchronizeTracesHandler_Title
, Messages
.SynchronizeTracesHandler_Error
+ CR
+ CR
+ String
.format(Messages
.SynchronizeTracesHandler_CopyProblem
, origtrace
.getName()));
224 } catch (CoreException e
) {
225 Activator
.getDefault().logError(String
.format(Messages
.SynchronizeTracesHandler_ErrorSynchingForTrace
, exp
.getName(), traceel
.getName()), e
);
226 TraceUtils
.displayErrorMsg(Messages
.SynchronizeTracesHandler_Title
, Messages
.SynchronizeTracesHandler_Error
+ CR
+ CR
+ e
.getMessage());
227 } catch (TmfTraceException e
) {
228 Activator
.getDefault().logError(String
.format(Messages
.SynchronizeTracesHandler_ErrorSynchingForTrace
, exp
.getName(), traceel
.getName()), e
);
229 TraceUtils
.displayErrorMsg(Messages
.SynchronizeTracesHandler_Title
, Messages
.SynchronizeTracesHandler_Error
+ CR
+ CR
+ e
.getMessage());
235 } catch (TmfTraceException e
) {
236 Activator
.getDefault().logError(String
.format(Messages
.SynchronizeTracesHandler_ErrorSynchingExperiment
, exp
.getName()), e
);
237 TraceUtils
.displayErrorMsg(Messages
.SynchronizeTracesHandler_Title
, Messages
.SynchronizeTracesHandler_Error
+ CR
+ CR
+ e
.getMessage());
244 TraceUtils
.displayErrorMsg(Messages
.SynchronizeTracesHandler_Title
, Messages
.SynchronizeTracesHandler_WrongTraceNumber
);