ss: Move plugins to Trace Compass namespace
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ui / src / org / eclipse / linuxtools / internal / tmf / ui / project / handlers / SynchronizeTracesHandler.java
CommitLineData
e73a4ba5 1/*******************************************************************************
60ae41e1 2 * Copyright (c) 2013, 2014 É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
11 *******************************************************************************/
12
13package org.eclipse.linuxtools.internal.tmf.ui.project.handlers;
14
15import java.util.ArrayList;
16import java.util.Iterator;
f3cbd37d 17import java.util.List;
e73a4ba5
GB
18
19import org.eclipse.core.commands.AbstractHandler;
20import org.eclipse.core.commands.ExecutionEvent;
21import org.eclipse.core.commands.ExecutionException;
2564d461 22import org.eclipse.core.resources.IContainer;
f3cbd37d
PT
23import org.eclipse.core.resources.IFolder;
24import org.eclipse.core.resources.IResource;
e73a4ba5
GB
25import org.eclipse.core.runtime.CoreException;
26import org.eclipse.jface.viewers.ISelection;
27import org.eclipse.jface.viewers.ISelectionProvider;
28import org.eclipse.jface.viewers.TreeSelection;
29import org.eclipse.linuxtools.internal.tmf.ui.Activator;
30import org.eclipse.linuxtools.tmf.core.event.ITmfEvent;
31import org.eclipse.linuxtools.tmf.core.exceptions.TmfTraceException;
32import org.eclipse.linuxtools.tmf.core.synchronization.SynchronizationAlgorithm;
33import org.eclipse.linuxtools.tmf.core.trace.ITmfTrace;
34import org.eclipse.linuxtools.tmf.core.trace.TmfExperiment;
f3cbd37d 35import org.eclipse.linuxtools.tmf.core.trace.TmfTraceManager;
e73a4ba5
GB
36import org.eclipse.linuxtools.tmf.ui.project.model.TmfExperimentElement;
37import org.eclipse.linuxtools.tmf.ui.project.model.TmfTraceElement;
38import org.eclipse.linuxtools.tmf.ui.project.model.TraceUtils;
39import org.eclipse.swt.widgets.Display;
40import org.eclipse.ui.IWorkbenchPage;
41import org.eclipse.ui.IWorkbenchPart;
42import org.eclipse.ui.IWorkbenchWindow;
43import org.eclipse.ui.PlatformUI;
44
45/**
46 * Handles the synchronization of an experiment, when the user selects this
47 * option in the menu
48 */
49public class SynchronizeTracesHandler extends AbstractHandler {
50
51 // ------------------------------------------------------------------------
52 // Attributes
53 // ------------------------------------------------------------------------
54
55 private TreeSelection fSelection = null;
56 private static final String CR = System.getProperty("line.separator"); //$NON-NLS-1$
57
58 // ------------------------------------------------------------------------
59 // Validation
60 // ------------------------------------------------------------------------
61
62 @Override
63 public boolean isEnabled() {
64 return true;
65 }
66
67 // ------------------------------------------------------------------------
68 // Execution
69 // ------------------------------------------------------------------------
70
71 @Override
72 public Object execute(ExecutionEvent event) throws ExecutionException {
73
74 // Check if we are closing down
75 IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
76 if (window == null) {
77 return null;
78 }
79
80 // Get the selection
81 IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
82 IWorkbenchPart part = page.getActivePart();
83 if (part == null) {
0126a8ca 84 return Boolean.FALSE;
e73a4ba5
GB
85 }
86 ISelectionProvider selectionProvider = part.getSite().getSelectionProvider();
87 if (selectionProvider == null) {
0126a8ca 88 return Boolean.FALSE;
e73a4ba5
GB
89 }
90 ISelection selection = selectionProvider.getSelection();
91
92 // Make sure selection contains only traces
93 fSelection = null;
507b1336
AM
94 final ArrayList<TmfTraceElement> tl = new ArrayList<>();
95 final ArrayList<TmfExperimentElement> uiexperiment = new ArrayList<>();
e73a4ba5
GB
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()) {
107 tl.add(trace);
108 }
109 }
110 }
111 }
112
f3cbd37d
PT
113 if ((uiexperiment.size() != 1) || (tl.size() < 2)) {
114 TraceUtils.displayErrorMsg(Messages.SynchronizeTracesHandler_Title, Messages.SynchronizeTracesHandler_WrongTraceNumber);
115 return null;
116 }
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 }
146 traces[i] = trace;
147 }
e73a4ba5 148
f3cbd37d
PT
149 /*
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?
153 */
154 exp.refreshSupplementaryFolder();
155 final TmfExperiment experiment = new TmfExperiment(ITmfEvent.class, exp.getName(), traces, exp.getResource());
156
04ba3554
GB
157 final SynchronizationAlgorithm syncAlgo = experiment.synchronizeTraces(true);
158 TmfTraceManager.refreshSupplementaryFiles(experiment);
159
160 Display.getDefault().asyncExec(new Runnable() {
161 @Override
162 public void run() {
163 List<TmfTraceElement> tracesToAdd = new ArrayList<>();
164 List<TmfTraceElement> tracesToRemove = new ArrayList<>();
165 /*
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.
170 */
171 for (TmfTraceElement traceel : tl) {
f3cbd37d 172 /*
04ba3554
GB
173 * Find the trace corresponding to this
174 * element in the experiment
f3cbd37d 175 */
04ba3554
GB
176 ITmfTrace expTrace = null;
177 for (ITmfTrace t : experiment.getTraces()) {
178 if (t.getResource().equals(traceel.getResource())) {
179 expTrace = t;
180 break;
f3cbd37d 181 }
04ba3554
GB
182 }
183 if ((expTrace != null) && syncAlgo.isTraceSynced(expTrace.getHostId())) {
f3cbd37d 184
04ba3554
GB
185 /* Find the original trace */
186 TmfTraceElement origtrace = traceel.getElementUnderTraceFolder();
f3cbd37d 187
04ba3554
GB
188 /*
189 * Make sure a trace with the
190 * new name does not exist
191 */
192 String newname = traceel.getName();
193 IContainer parentFolder = origtrace.getResource().getParent();
194 boolean traceexists;
195 do {
196 traceexists = false;
197 newname += "_"; //$NON-NLS-1$
198 if (parentFolder.findMember(newname) != null) {
199 traceexists = true;
e73a4ba5 200 }
04ba3554
GB
201 } while (traceexists);
202
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()));
208 continue;
f3cbd37d 209 }
f3cbd37d 210
04ba3554
GB
211 /*
212 * Instantiate the new trace
213 * and set its sync formula
214 */
215 ITmfTrace trace = newtrace.instantiateTrace();
216 ITmfEvent traceEvent = newtrace.instantiateEvent();
e73a4ba5 217
f3cbd37d 218 try {
f06ca6d0 219 trace.initTrace(newtrace.getResource(), newtrace.getResource().getLocation().toOSString(), traceEvent.getClass());
04ba3554
GB
220 } catch (TmfTraceException e) {
221 Activator.getDefault().logError(String.format(Messages.SynchronizeTracesHandler_ErrorSynchingForTrace, exp.getName(), traceel.getName()), e);
f3cbd37d
PT
222 TraceUtils.displayErrorMsg(Messages.SynchronizeTracesHandler_Title, Messages.SynchronizeTracesHandler_Error + CR + CR + e.getMessage());
223 }
04ba3554
GB
224 trace.setTimestampTransform(syncAlgo.getTimestampTransform(trace));
225 TmfTraceManager.refreshSupplementaryFiles(trace);
226 trace.dispose();
e73a4ba5 227
04ba3554
GB
228 tracesToAdd.add(newtrace);
229 tracesToRemove.add(traceel);
230 }
231 }
232 experiment.dispose();
233
234 // Move synchronization file temporarily so that
235 // it doesn't get deleted by the experiment change
7f38b742 236 IFolder tmpFolder = exp.getTraceSupplementaryFolder(exp.getName() + '.' + experiment.getSynchronizationFolder(false));
04ba3554
GB
237 IResource syncFile = null;
238 for (IResource resource : exp.getSupplementaryResources()) {
7f38b742 239 if (resource.getName().equals(experiment.getSynchronizationFolder(false))) {
f3cbd37d 240 try {
04ba3554
GB
241 resource.move(tmpFolder.getFullPath(), false, null);
242 syncFile = resource;
243 break;
f3cbd37d
PT
244 } catch (CoreException e) {
245 Activator.getDefault().logError(String.format(Messages.SynchronizeTracesHandler_ErrorSynchingExperiment, exp.getName()), e);
246 }
247 }
248 }
f3cbd37d 249
04ba3554
GB
250 for (TmfTraceElement trace : tracesToRemove) {
251 try {
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());
256 }
257 }
258 for (TmfTraceElement trace : tracesToAdd) {
259 exp.addTrace(trace);
260 }
261
262 // Move synchronization file back
263 if (tmpFolder.exists() && syncFile != null) {
264 try {
265 tmpFolder.move(syncFile.getFullPath(), false, null);
266 } catch (CoreException e) {
267 Activator.getDefault().logError(String.format(Messages.SynchronizeTracesHandler_ErrorSynchingExperiment, exp.getName()), e);
268 }
269 }
270 }
271 });
f3cbd37d
PT
272 }
273 };
274 thread.start();
e73a4ba5
GB
275
276 return null;
277 }
278
279}
This page took 0.05322 seconds and 5 git commands to generate.