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
;
17 import java
.util
.List
;
19 import org
.eclipse
.core
.commands
.AbstractHandler
;
20 import org
.eclipse
.core
.commands
.ExecutionEvent
;
21 import org
.eclipse
.core
.commands
.ExecutionException
;
22 import org
.eclipse
.core
.resources
.IContainer
;
23 import org
.eclipse
.core
.resources
.IFolder
;
24 import org
.eclipse
.core
.resources
.IResource
;
25 import org
.eclipse
.core
.runtime
.CoreException
;
26 import org
.eclipse
.jface
.viewers
.ISelection
;
27 import org
.eclipse
.jface
.viewers
.ISelectionProvider
;
28 import org
.eclipse
.jface
.viewers
.TreeSelection
;
29 import org
.eclipse
.linuxtools
.internal
.tmf
.ui
.Activator
;
30 import org
.eclipse
.linuxtools
.tmf
.core
.event
.ITmfEvent
;
31 import org
.eclipse
.linuxtools
.tmf
.core
.exceptions
.TmfTraceException
;
32 import org
.eclipse
.linuxtools
.tmf
.core
.synchronization
.SynchronizationAlgorithm
;
33 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfTrace
;
34 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.TmfExperiment
;
35 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.TmfTraceManager
;
36 import org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
.TmfExperimentElement
;
37 import org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
.TmfTraceElement
;
38 import org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
.TraceUtils
;
39 import org
.eclipse
.swt
.widgets
.Display
;
40 import org
.eclipse
.ui
.IWorkbenchPage
;
41 import org
.eclipse
.ui
.IWorkbenchPart
;
42 import org
.eclipse
.ui
.IWorkbenchWindow
;
43 import org
.eclipse
.ui
.PlatformUI
;
46 * Handles the synchronization of an experiment, when the user selects this
49 public class SynchronizeTracesHandler
extends AbstractHandler
{
51 // ------------------------------------------------------------------------
53 // ------------------------------------------------------------------------
55 private TreeSelection fSelection
= null;
56 private static final String CR
= System
.getProperty("line.separator"); //$NON-NLS-1$
58 // ------------------------------------------------------------------------
60 // ------------------------------------------------------------------------
63 public boolean isEnabled() {
67 // ------------------------------------------------------------------------
69 // ------------------------------------------------------------------------
72 public Object
execute(ExecutionEvent event
) throws ExecutionException
{
74 // Check if we are closing down
75 IWorkbenchWindow window
= PlatformUI
.getWorkbench().getActiveWorkbenchWindow();
81 IWorkbenchPage page
= PlatformUI
.getWorkbench().getActiveWorkbenchWindow().getActivePage();
82 IWorkbenchPart part
= page
.getActivePart();
86 ISelectionProvider selectionProvider
= part
.getSite().getSelectionProvider();
87 if (selectionProvider
== null) {
90 ISelection selection
= selectionProvider
.getSelection();
92 // Make sure selection contains only traces
94 final ArrayList
<TmfTraceElement
> tl
= new ArrayList
<>();
95 final ArrayList
<TmfExperimentElement
> uiexperiment
= new ArrayList
<>();
96 if (selection
instanceof TreeSelection
) {
97 fSelection
= (TreeSelection
) selection
;
98 Iterator
<Object
> iterator
= fSelection
.iterator();
99 while (iterator
.hasNext()) {
100 Object element
= iterator
.next();
101 if (element
instanceof TmfTraceElement
) {
102 tl
.add((TmfTraceElement
) element
);
103 } else if (element
instanceof TmfExperimentElement
) {
104 TmfExperimentElement exp
= (TmfExperimentElement
) element
;
105 uiexperiment
.add(exp
);
106 for (TmfTraceElement trace
: exp
.getTraces()) {
113 if ((uiexperiment
.size() != 1) || (tl
.size() < 2)) {
114 TraceUtils
.displayErrorMsg(Messages
.SynchronizeTracesHandler_Title
, Messages
.SynchronizeTracesHandler_WrongTraceNumber
);
118 Thread thread
= new Thread() {
122 final ITmfTrace
[] traces
= new ITmfTrace
[tl
.size()];
123 final TmfExperimentElement exp
= uiexperiment
.get(0);
125 for (int i
= 0; i
< tl
.size(); i
++) {
126 ITmfTrace trace
= tl
.get(i
).instantiateTrace();
127 ITmfEvent traceEvent
= tl
.get(i
).instantiateEvent();
129 TraceUtils
.displayErrorMsg(Messages
.SynchronizeTracesHandler_Title
, Messages
.SynchronizeTracesHandler_WrongType
+ tl
.get(i
).getName());
130 for (int j
= 0; j
< i
; j
++) {
136 trace
.initTrace(tl
.get(i
).getResource(), tl
.get(i
).getResource().getLocation().toOSString(), traceEvent
.getClass());
137 TmfTraceManager
.refreshSupplementaryFiles(trace
);
138 } catch (TmfTraceException e
) {
139 TraceUtils
.displayErrorMsg(Messages
.SynchronizeTracesHandler_Title
, Messages
.SynchronizeTracesHandler_InitError
+ CR
+ CR
+ e
);
141 for (int j
= 0; j
< i
; j
++) {
150 * FIXME Unlike traces, there is no instanceExperiment, so
151 * we call this function here alone. Maybe it would be
152 * better to do this on experiment's element constructor?
154 exp
.refreshSupplementaryFolder();
155 final TmfExperiment experiment
= new TmfExperiment(ITmfEvent
.class, exp
.getName(), traces
, exp
.getResource());
157 final SynchronizationAlgorithm syncAlgo
= experiment
.synchronizeTraces(true);
158 TmfTraceManager
.refreshSupplementaryFiles(experiment
);
160 Display
.getDefault().asyncExec(new Runnable() {
163 List
<TmfTraceElement
> tracesToAdd
= new ArrayList
<>();
164 List
<TmfTraceElement
> tracesToRemove
= new ArrayList
<>();
166 * For each trace in the experiment, if there is
167 * a transform equation, copy the original
168 * trace, so that a new state system will be
169 * generated with sync time.
171 for (TmfTraceElement traceel
: tl
) {
173 * Find the trace corresponding to this
174 * element in the experiment
176 ITmfTrace expTrace
= null;
177 for (ITmfTrace t
: experiment
.getTraces()) {
178 if (t
.getResource().equals(traceel
.getResource())) {
183 if ((expTrace
!= null) && syncAlgo
.isTraceSynced(expTrace
.getHostId())) {
185 /* Find the original trace */
186 TmfTraceElement origtrace
= traceel
.getElementUnderTraceFolder();
189 * Make sure a trace with the
190 * new name does not exist
192 String newname
= traceel
.getName();
193 IContainer parentFolder
= origtrace
.getResource().getParent();
197 newname
+= "_"; //$NON-NLS-1$
198 if (parentFolder
.findMember(newname
) != null) {
201 } while (traceexists
);
203 /* Copy the original trace */
204 TmfTraceElement newtrace
= origtrace
.copy(newname
);
205 if (newtrace
== null) {
206 TraceUtils
.displayErrorMsg(Messages
.SynchronizeTracesHandler_Title
,
207 Messages
.SynchronizeTracesHandler_Error
+ CR
+ CR
+ String
.format(Messages
.SynchronizeTracesHandler_CopyProblem
, origtrace
.getName()));
212 * Instantiate the new trace
213 * and set its sync formula
215 ITmfTrace trace
= newtrace
.instantiateTrace();
216 ITmfEvent traceEvent
= newtrace
.instantiateEvent();
219 trace
.initTrace(newtrace
.getResource(), newtrace
.getResource().getLocation().toOSString(), traceEvent
.getClass());
220 } catch (TmfTraceException e
) {
221 Activator
.getDefault().logError(String
.format(Messages
.SynchronizeTracesHandler_ErrorSynchingForTrace
, exp
.getName(), traceel
.getName()), e
);
222 TraceUtils
.displayErrorMsg(Messages
.SynchronizeTracesHandler_Title
, Messages
.SynchronizeTracesHandler_Error
+ CR
+ CR
+ e
.getMessage());
224 trace
.setTimestampTransform(syncAlgo
.getTimestampTransform(trace
));
225 TmfTraceManager
.refreshSupplementaryFiles(trace
);
228 tracesToAdd
.add(newtrace
);
229 tracesToRemove
.add(traceel
);
232 experiment
.dispose();
234 // Move synchronization file temporarily so that
235 // it doesn't get deleted by the experiment change
236 IFolder tmpFolder
= exp
.getTraceSupplementaryFolder(exp
.getName() + '.' + experiment
.getSynchronizationFolder(false));
237 IResource syncFile
= null;
238 for (IResource resource
: exp
.getSupplementaryResources()) {
239 if (resource
.getName().equals(experiment
.getSynchronizationFolder(false))) {
241 resource
.move(tmpFolder
.getFullPath(), false, null);
244 } catch (CoreException e
) {
245 Activator
.getDefault().logError(String
.format(Messages
.SynchronizeTracesHandler_ErrorSynchingExperiment
, exp
.getName()), e
);
250 for (TmfTraceElement trace
: tracesToRemove
) {
252 exp
.removeTrace(trace
);
253 } catch (CoreException e
) {
254 Activator
.getDefault().logError(String
.format(Messages
.SynchronizeTracesHandler_ErrorSynchingForTrace
, exp
.getName(), trace
.getName()), e
);
255 TraceUtils
.displayErrorMsg(Messages
.SynchronizeTracesHandler_Title
, Messages
.SynchronizeTracesHandler_Error
+ CR
+ CR
+ e
.getMessage());
258 for (TmfTraceElement trace
: tracesToAdd
) {
262 // Move synchronization file back
263 if (tmpFolder
.exists() && syncFile
!= null) {
265 tmpFolder
.move(syncFile
.getFullPath(), false, null);
266 } catch (CoreException e
) {
267 Activator
.getDefault().logError(String
.format(Messages
.SynchronizeTracesHandler_ErrorSynchingExperiment
, exp
.getName()), e
);