tmf: Consolidate constructors in TmfExperiment
[deliverable/tracecompass.git] / org.eclipse.tracecompass.tmf.ui / src / org / eclipse / tracecompass / internal / tmf / ui / project / handlers / SynchronizeTracesHandler.java
1 /*******************************************************************************
2 * Copyright (c) 2013, 2014 École Polytechnique de Montréal
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
13 package org.eclipse.tracecompass.internal.tmf.ui.project.handlers;
14
15 import java.util.ArrayList;
16 import java.util.Iterator;
17 import java.util.List;
18
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.swt.widgets.Display;
30 import org.eclipse.tracecompass.internal.tmf.ui.Activator;
31 import org.eclipse.tracecompass.tmf.core.event.ITmfEvent;
32 import org.eclipse.tracecompass.tmf.core.exceptions.TmfTraceException;
33 import org.eclipse.tracecompass.tmf.core.synchronization.SynchronizationAlgorithm;
34 import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace;
35 import org.eclipse.tracecompass.tmf.core.trace.TmfExperiment;
36 import org.eclipse.tracecompass.tmf.core.trace.TmfTraceManager;
37 import org.eclipse.tracecompass.tmf.ui.project.model.TmfExperimentElement;
38 import org.eclipse.tracecompass.tmf.ui.project.model.TmfTraceElement;
39 import org.eclipse.tracecompass.tmf.ui.project.model.TraceUtils;
40 import org.eclipse.ui.IWorkbenchPage;
41 import org.eclipse.ui.IWorkbenchPart;
42 import org.eclipse.ui.IWorkbenchWindow;
43 import org.eclipse.ui.PlatformUI;
44
45 /**
46 * Handles the synchronization of an experiment, when the user selects this
47 * option in the menu
48 */
49 public 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) {
84 return Boolean.FALSE;
85 }
86 ISelectionProvider selectionProvider = part.getSite().getSelectionProvider();
87 if (selectionProvider == null) {
88 return Boolean.FALSE;
89 }
90 ISelection selection = selectionProvider.getSelection();
91
92 // Make sure selection contains only traces
93 fSelection = null;
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()) {
107 tl.add(trace);
108 }
109 }
110 }
111 }
112
113 if ((uiexperiment.size() != 1) || (tl.size() < 2)) {
114 TraceUtils.displayErrorMsg(Messages.SynchronizeTracesHandler_Title, Messages.SynchronizeTracesHandler_WrongTraceNumber);
115 return null;
116 }
117
118 Thread thread = new Thread() {
119 @Override
120 public void run() {
121
122 final ITmfTrace[] traces = new ITmfTrace[tl.size()];
123 final TmfExperimentElement exp = uiexperiment.get(0);
124
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();
132 }
133 return;
134 }
135 try {
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);
140 trace.dispose();
141 for (int j = 0; j < i; j++) {
142 traces[j].dispose();
143 }
144 return;
145 }
146 traces[i] = trace;
147 }
148
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,
156 exp.getName(), traces, TmfExperiment.DEFAULT_INDEX_PAGE_SIZE, exp.getResource());
157
158 final SynchronizationAlgorithm syncAlgo = experiment.synchronizeTraces(true);
159 TmfTraceManager.refreshSupplementaryFiles(experiment);
160
161 Display.getDefault().asyncExec(new Runnable() {
162 @Override
163 public void run() {
164 List<TmfTraceElement> tracesToAdd = new ArrayList<>();
165 List<TmfTraceElement> tracesToRemove = new ArrayList<>();
166 /*
167 * For each trace in the experiment, if there is
168 * a transform equation, copy the original
169 * trace, so that a new state system will be
170 * generated with sync time.
171 */
172 for (TmfTraceElement traceel : tl) {
173 /*
174 * Find the trace corresponding to this
175 * element in the experiment
176 */
177 ITmfTrace expTrace = null;
178 for (ITmfTrace t : experiment.getTraces()) {
179 if (t.getResource().equals(traceel.getResource())) {
180 expTrace = t;
181 break;
182 }
183 }
184 if ((expTrace != null) && syncAlgo.isTraceSynced(expTrace.getHostId())) {
185
186 /* Find the original trace */
187 TmfTraceElement origtrace = traceel.getElementUnderTraceFolder();
188
189 /*
190 * Make sure a trace with the
191 * new name does not exist
192 */
193 String newname = traceel.getName();
194 IContainer parentFolder = origtrace.getResource().getParent();
195 boolean traceexists;
196 do {
197 traceexists = false;
198 newname += "_"; //$NON-NLS-1$
199 if (parentFolder.findMember(newname) != null) {
200 traceexists = true;
201 }
202 } while (traceexists);
203
204 /* Copy the original trace */
205 TmfTraceElement newtrace = origtrace.copy(newname);
206 if (newtrace == null) {
207 TraceUtils.displayErrorMsg(Messages.SynchronizeTracesHandler_Title,
208 Messages.SynchronizeTracesHandler_Error + CR + CR + String.format(Messages.SynchronizeTracesHandler_CopyProblem, origtrace.getName()));
209 continue;
210 }
211
212 /*
213 * Instantiate the new trace
214 * and set its sync formula
215 */
216 ITmfTrace trace = newtrace.instantiateTrace();
217 ITmfEvent traceEvent = newtrace.instantiateEvent();
218
219 try {
220 trace.initTrace(newtrace.getResource(), newtrace.getResource().getLocation().toOSString(), traceEvent.getClass());
221 } catch (TmfTraceException e) {
222 Activator.getDefault().logError(String.format(Messages.SynchronizeTracesHandler_ErrorSynchingForTrace, exp.getName(), traceel.getName()), e);
223 TraceUtils.displayErrorMsg(Messages.SynchronizeTracesHandler_Title, Messages.SynchronizeTracesHandler_Error + CR + CR + e.getMessage());
224 }
225 trace.setTimestampTransform(syncAlgo.getTimestampTransform(trace));
226 TmfTraceManager.refreshSupplementaryFiles(trace);
227 trace.dispose();
228
229 tracesToAdd.add(newtrace);
230 tracesToRemove.add(traceel);
231 }
232 }
233 experiment.dispose();
234
235 // Move synchronization file temporarily so that
236 // it doesn't get deleted by the experiment change
237 IFolder tmpFolder = exp.getTraceSupplementaryFolder(exp.getName() + '.' + experiment.getSynchronizationFolder(false));
238 IResource syncFile = null;
239 for (IResource resource : exp.getSupplementaryResources()) {
240 if (resource.getName().equals(experiment.getSynchronizationFolder(false))) {
241 try {
242 resource.move(tmpFolder.getFullPath(), false, null);
243 syncFile = resource;
244 break;
245 } catch (CoreException e) {
246 Activator.getDefault().logError(String.format(Messages.SynchronizeTracesHandler_ErrorSynchingExperiment, exp.getName()), e);
247 }
248 }
249 }
250
251 for (TmfTraceElement trace : tracesToRemove) {
252 try {
253 exp.removeTrace(trace);
254 } catch (CoreException e) {
255 Activator.getDefault().logError(String.format(Messages.SynchronizeTracesHandler_ErrorSynchingForTrace, exp.getName(), trace.getName()), e);
256 TraceUtils.displayErrorMsg(Messages.SynchronizeTracesHandler_Title, Messages.SynchronizeTracesHandler_Error + CR + CR + e.getMessage());
257 }
258 }
259 for (TmfTraceElement trace : tracesToAdd) {
260 exp.addTrace(trace);
261 }
262
263 // Move synchronization file back
264 if (tmpFolder.exists() && syncFile != null) {
265 try {
266 tmpFolder.move(syncFile.getFullPath(), false, null);
267 } catch (CoreException e) {
268 Activator.getDefault().logError(String.format(Messages.SynchronizeTracesHandler_ErrorSynchingExperiment, exp.getName()), e);
269 }
270 }
271 }
272 });
273 }
274 };
275 thread.start();
276
277 return null;
278 }
279
280 }
This page took 0.036017 seconds and 5 git commands to generate.