Commit | Line | Data |
---|---|---|
e73a4ba5 | 1 | /******************************************************************************* |
dc62dbee | 2 | * Copyright (c) 2013, 2015 École Polytechnique de Montréal |
e73a4ba5 GB |
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 | * Contributors: | |
10 | * Geneviève Bastien - Initial implementation and API | |
dc62dbee | 11 | * Cédric Biancheri - Added a wizard to select the root node |
e73a4ba5 GB |
12 | *******************************************************************************/ |
13 | ||
2bdf0193 | 14 | package org.eclipse.tracecompass.internal.tmf.ui.project.handlers; |
e73a4ba5 GB |
15 | |
16 | import java.util.ArrayList; | |
17 | import java.util.Iterator; | |
f3cbd37d | 18 | import java.util.List; |
e73a4ba5 GB |
19 | |
20 | import org.eclipse.core.commands.AbstractHandler; | |
21 | import org.eclipse.core.commands.ExecutionEvent; | |
22 | import org.eclipse.core.commands.ExecutionException; | |
2564d461 | 23 | import org.eclipse.core.resources.IContainer; |
f3cbd37d PT |
24 | import org.eclipse.core.resources.IFolder; |
25 | import org.eclipse.core.resources.IResource; | |
e73a4ba5 GB |
26 | import org.eclipse.core.runtime.CoreException; |
27 | import org.eclipse.jface.viewers.ISelection; | |
e73a4ba5 | 28 | import org.eclipse.jface.viewers.TreeSelection; |
dc62dbee CB |
29 | import org.eclipse.jface.window.Window; |
30 | import org.eclipse.jface.wizard.WizardDialog; | |
e73a4ba5 | 31 | import org.eclipse.swt.widgets.Display; |
dc62dbee | 32 | import org.eclipse.swt.widgets.Shell; |
2bdf0193 AM |
33 | import org.eclipse.tracecompass.internal.tmf.ui.Activator; |
34 | import org.eclipse.tracecompass.tmf.core.event.ITmfEvent; | |
35 | import org.eclipse.tracecompass.tmf.core.exceptions.TmfTraceException; | |
36 | import org.eclipse.tracecompass.tmf.core.synchronization.SynchronizationAlgorithm; | |
37 | import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace; | |
2bdf0193 | 38 | import org.eclipse.tracecompass.tmf.core.trace.TmfTraceManager; |
5c5fa260 | 39 | import org.eclipse.tracecompass.tmf.core.trace.experiment.TmfExperiment; |
2bdf0193 AM |
40 | import org.eclipse.tracecompass.tmf.ui.project.model.TmfExperimentElement; |
41 | import org.eclipse.tracecompass.tmf.ui.project.model.TmfTraceElement; | |
42 | import org.eclipse.tracecompass.tmf.ui.project.model.TraceUtils; | |
dc62dbee | 43 | import org.eclipse.tracecompass.tmf.ui.project.wizards.SelectRootNodeWizard; |
e73a4ba5 GB |
44 | import org.eclipse.ui.IWorkbenchWindow; |
45 | import org.eclipse.ui.PlatformUI; | |
dc62dbee | 46 | import org.eclipse.ui.handlers.HandlerUtil; |
e73a4ba5 GB |
47 | |
48 | /** | |
49 | * Handles the synchronization of an experiment, when the user selects this | |
50 | * option in the menu | |
51 | */ | |
52 | public class SynchronizeTracesHandler extends AbstractHandler { | |
53 | ||
54 | // ------------------------------------------------------------------------ | |
55 | // Attributes | |
56 | // ------------------------------------------------------------------------ | |
57 | ||
58 | private TreeSelection fSelection = null; | |
59 | private static final String CR = System.getProperty("line.separator"); //$NON-NLS-1$ | |
dc62dbee CB |
60 | private TmfExperimentElement fExperiment = null; |
61 | private TmfTraceElement fRootNode = null; | |
62 | private String fRootNodeId = null; | |
e73a4ba5 GB |
63 | |
64 | // ------------------------------------------------------------------------ | |
65 | // Execution | |
66 | // ------------------------------------------------------------------------ | |
67 | ||
68 | @Override | |
69 | public Object execute(ExecutionEvent event) throws ExecutionException { | |
70 | ||
71 | // Check if we are closing down | |
72 | IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow(); | |
73 | if (window == null) { | |
74 | return null; | |
75 | } | |
76 | ||
77 | // Get the selection | |
dc62dbee | 78 | ISelection selection = HandlerUtil.getCurrentSelectionChecked(event); |
e73a4ba5 GB |
79 | |
80 | // Make sure selection contains only traces | |
81 | fSelection = null; | |
507b1336 AM |
82 | final ArrayList<TmfTraceElement> tl = new ArrayList<>(); |
83 | final ArrayList<TmfExperimentElement> uiexperiment = new ArrayList<>(); | |
e73a4ba5 GB |
84 | if (selection instanceof TreeSelection) { |
85 | fSelection = (TreeSelection) selection; | |
86 | Iterator<Object> iterator = fSelection.iterator(); | |
87 | while (iterator.hasNext()) { | |
88 | Object element = iterator.next(); | |
dc62dbee | 89 | if (element instanceof TmfExperimentElement) { |
e73a4ba5 GB |
90 | TmfExperimentElement exp = (TmfExperimentElement) element; |
91 | uiexperiment.add(exp); | |
92 | for (TmfTraceElement trace : exp.getTraces()) { | |
93 | tl.add(trace); | |
94 | } | |
95 | } | |
96 | } | |
97 | } | |
98 | ||
f3cbd37d PT |
99 | if ((uiexperiment.size() != 1) || (tl.size() < 2)) { |
100 | TraceUtils.displayErrorMsg(Messages.SynchronizeTracesHandler_Title, Messages.SynchronizeTracesHandler_WrongTraceNumber); | |
101 | return null; | |
102 | } | |
dc62dbee CB |
103 | fExperiment = uiexperiment.get(0); |
104 | fRootNode = null; | |
105 | fRootNodeId = null; | |
106 | ||
107 | // Fire the Select Root Node Wizard | |
108 | IWorkbenchWindow workbenchWindow = HandlerUtil.getActiveWorkbenchWindowChecked(event); | |
109 | Shell shell = workbenchWindow.getShell(); | |
110 | SelectRootNodeWizard wizard = new SelectRootNodeWizard(fExperiment); | |
111 | WizardDialog dialog = new WizardDialog(shell, wizard); | |
112 | int returnValue = dialog.open(); | |
113 | if (returnValue == Window.CANCEL) { | |
114 | return null; | |
115 | } | |
116 | fRootNode = wizard.getRootNode(); | |
e73a4ba5 | 117 | |
f3cbd37d PT |
118 | Thread thread = new Thread() { |
119 | @Override | |
120 | public void run() { | |
e73a4ba5 | 121 | |
f3cbd37d PT |
122 | final ITmfTrace[] traces = new ITmfTrace[tl.size()]; |
123 | final TmfExperimentElement exp = uiexperiment.get(0); | |
e73a4ba5 | 124 | |
f3cbd37d PT |
125 | for (int i = 0; i < tl.size(); i++) { |
126 | ITmfTrace trace = tl.get(i).instantiateTrace(); | |
127 | ITmfEvent traceEvent = tl.get(i).instantiateEvent(); | |
128 | if (trace == null) { | |
129 | TraceUtils.displayErrorMsg(Messages.SynchronizeTracesHandler_Title, Messages.SynchronizeTracesHandler_WrongType + tl.get(i).getName()); | |
130 | for (int j = 0; j < i; j++) { | |
131 | traces[j].dispose(); | |
e73a4ba5 | 132 | } |
f3cbd37d | 133 | return; |
e73a4ba5 | 134 | } |
e73a4ba5 | 135 | try { |
f06ca6d0 | 136 | trace.initTrace(tl.get(i).getResource(), tl.get(i).getResource().getLocation().toOSString(), traceEvent.getClass()); |
f3cbd37d PT |
137 | TmfTraceManager.refreshSupplementaryFiles(trace); |
138 | } catch (TmfTraceException e) { | |
139 | TraceUtils.displayErrorMsg(Messages.SynchronizeTracesHandler_Title, Messages.SynchronizeTracesHandler_InitError + CR + CR + e); | |
140 | trace.dispose(); | |
141 | for (int j = 0; j < i; j++) { | |
142 | traces[j].dispose(); | |
143 | } | |
144 | return; | |
145 | } | |
dc62dbee CB |
146 | if (tl.get(i).getElementPath().equals(fRootNode.getElementPath())) { |
147 | fRootNodeId = trace.getHostId(); | |
148 | } | |
f3cbd37d PT |
149 | traces[i] = trace; |
150 | } | |
e73a4ba5 | 151 | |
f3cbd37d | 152 | /* |
dc62dbee CB |
153 | * FIXME Unlike traces, there is no instanceExperiment, so we |
154 | * call this function here alone. Maybe it would be better to do | |
155 | * this on experiment's element constructor? | |
f3cbd37d PT |
156 | */ |
157 | exp.refreshSupplementaryFolder(); | |
4178260e AM |
158 | final TmfExperiment experiment = new TmfExperiment(ITmfEvent.class, |
159 | exp.getName(), traces, TmfExperiment.DEFAULT_INDEX_PAGE_SIZE, exp.getResource()); | |
f3cbd37d | 160 | |
04ba3554 | 161 | final SynchronizationAlgorithm syncAlgo = experiment.synchronizeTraces(true); |
dc62dbee CB |
162 | syncAlgo.setRootNode(fRootNodeId); |
163 | ||
04ba3554 GB |
164 | TmfTraceManager.refreshSupplementaryFiles(experiment); |
165 | ||
166 | Display.getDefault().asyncExec(new Runnable() { | |
167 | @Override | |
168 | public void run() { | |
169 | List<TmfTraceElement> tracesToAdd = new ArrayList<>(); | |
170 | List<TmfTraceElement> tracesToRemove = new ArrayList<>(); | |
171 | /* | |
dc62dbee CB |
172 | * For each trace in the experiment, if there is a |
173 | * transform equation, copy the original trace, so that | |
174 | * a new state system will be generated with sync time. | |
04ba3554 GB |
175 | */ |
176 | for (TmfTraceElement traceel : tl) { | |
f3cbd37d | 177 | /* |
dc62dbee CB |
178 | * Find the trace corresponding to this element in |
179 | * the experiment | |
f3cbd37d | 180 | */ |
04ba3554 GB |
181 | ITmfTrace expTrace = null; |
182 | for (ITmfTrace t : experiment.getTraces()) { | |
183 | if (t.getResource().equals(traceel.getResource())) { | |
184 | expTrace = t; | |
185 | break; | |
f3cbd37d | 186 | } |
04ba3554 GB |
187 | } |
188 | if ((expTrace != null) && syncAlgo.isTraceSynced(expTrace.getHostId())) { | |
04ba3554 GB |
189 | /* Find the original trace */ |
190 | TmfTraceElement origtrace = traceel.getElementUnderTraceFolder(); | |
f3cbd37d | 191 | |
04ba3554 | 192 | /* |
dc62dbee CB |
193 | * Make sure a trace with the new name does not |
194 | * exist | |
04ba3554 GB |
195 | */ |
196 | String newname = traceel.getName(); | |
197 | IContainer parentFolder = origtrace.getResource().getParent(); | |
198 | boolean traceexists; | |
199 | do { | |
200 | traceexists = false; | |
201 | newname += "_"; //$NON-NLS-1$ | |
202 | if (parentFolder.findMember(newname) != null) { | |
203 | traceexists = true; | |
e73a4ba5 | 204 | } |
04ba3554 GB |
205 | } while (traceexists); |
206 | ||
207 | /* Copy the original trace */ | |
208 | TmfTraceElement newtrace = origtrace.copy(newname); | |
209 | if (newtrace == null) { | |
210 | TraceUtils.displayErrorMsg(Messages.SynchronizeTracesHandler_Title, | |
211 | Messages.SynchronizeTracesHandler_Error + CR + CR + String.format(Messages.SynchronizeTracesHandler_CopyProblem, origtrace.getName())); | |
212 | continue; | |
f3cbd37d | 213 | } |
f3cbd37d | 214 | |
04ba3554 | 215 | /* |
dc62dbee CB |
216 | * Instantiate the new trace and set its sync |
217 | * formula | |
04ba3554 GB |
218 | */ |
219 | ITmfTrace trace = newtrace.instantiateTrace(); | |
220 | ITmfEvent traceEvent = newtrace.instantiateEvent(); | |
e73a4ba5 | 221 | |
f3cbd37d | 222 | try { |
f06ca6d0 | 223 | trace.initTrace(newtrace.getResource(), newtrace.getResource().getLocation().toOSString(), traceEvent.getClass()); |
04ba3554 GB |
224 | } catch (TmfTraceException e) { |
225 | Activator.getDefault().logError(String.format(Messages.SynchronizeTracesHandler_ErrorSynchingForTrace, exp.getName(), traceel.getName()), e); | |
f3cbd37d PT |
226 | TraceUtils.displayErrorMsg(Messages.SynchronizeTracesHandler_Title, Messages.SynchronizeTracesHandler_Error + CR + CR + e.getMessage()); |
227 | } | |
04ba3554 GB |
228 | trace.setTimestampTransform(syncAlgo.getTimestampTransform(trace)); |
229 | TmfTraceManager.refreshSupplementaryFiles(trace); | |
230 | trace.dispose(); | |
e73a4ba5 | 231 | |
04ba3554 GB |
232 | tracesToAdd.add(newtrace); |
233 | tracesToRemove.add(traceel); | |
234 | } | |
235 | } | |
236 | experiment.dispose(); | |
237 | ||
238 | // Move synchronization file temporarily so that | |
239 | // it doesn't get deleted by the experiment change | |
7f38b742 | 240 | IFolder tmpFolder = exp.getTraceSupplementaryFolder(exp.getName() + '.' + experiment.getSynchronizationFolder(false)); |
04ba3554 GB |
241 | IResource syncFile = null; |
242 | for (IResource resource : exp.getSupplementaryResources()) { | |
7f38b742 | 243 | if (resource.getName().equals(experiment.getSynchronizationFolder(false))) { |
f3cbd37d | 244 | try { |
04ba3554 GB |
245 | resource.move(tmpFolder.getFullPath(), false, null); |
246 | syncFile = resource; | |
247 | break; | |
f3cbd37d PT |
248 | } catch (CoreException e) { |
249 | Activator.getDefault().logError(String.format(Messages.SynchronizeTracesHandler_ErrorSynchingExperiment, exp.getName()), e); | |
250 | } | |
251 | } | |
252 | } | |
f3cbd37d | 253 | |
04ba3554 GB |
254 | for (TmfTraceElement trace : tracesToRemove) { |
255 | try { | |
256 | exp.removeTrace(trace); | |
257 | } catch (CoreException e) { | |
258 | Activator.getDefault().logError(String.format(Messages.SynchronizeTracesHandler_ErrorSynchingForTrace, exp.getName(), trace.getName()), e); | |
259 | TraceUtils.displayErrorMsg(Messages.SynchronizeTracesHandler_Title, Messages.SynchronizeTracesHandler_Error + CR + CR + e.getMessage()); | |
260 | } | |
261 | } | |
262 | for (TmfTraceElement trace : tracesToAdd) { | |
263 | exp.addTrace(trace); | |
264 | } | |
265 | ||
266 | // Move synchronization file back | |
267 | if (tmpFolder.exists() && syncFile != null) { | |
268 | try { | |
269 | tmpFolder.move(syncFile.getFullPath(), false, null); | |
270 | } catch (CoreException e) { | |
271 | Activator.getDefault().logError(String.format(Messages.SynchronizeTracesHandler_ErrorSynchingExperiment, exp.getName()), e); | |
272 | } | |
273 | } | |
274 | } | |
275 | }); | |
f3cbd37d PT |
276 | } |
277 | }; | |
278 | thread.start(); | |
e73a4ba5 GB |
279 | |
280 | return null; | |
281 | } | |
282 | ||
283 | } |