Commit | Line | Data |
---|---|---|
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 | ||
13 | package org.eclipse.linuxtools.internal.tmf.ui.project.handlers; | |
14 | ||
15 | import java.util.ArrayList; | |
16 | import java.util.Iterator; | |
17 | ||
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; | |
40 | ||
41 | /** | |
42 | * Handles the synchronization of an experiment, when the user selects this | |
43 | * option in the menu | |
44 | */ | |
45 | public class SynchronizeTracesHandler extends AbstractHandler { | |
46 | ||
47 | // ------------------------------------------------------------------------ | |
48 | // Attributes | |
49 | // ------------------------------------------------------------------------ | |
50 | ||
51 | private TreeSelection fSelection = null; | |
52 | private static final String CR = System.getProperty("line.separator"); //$NON-NLS-1$ | |
53 | ||
54 | // ------------------------------------------------------------------------ | |
55 | // Validation | |
56 | // ------------------------------------------------------------------------ | |
57 | ||
58 | @Override | |
59 | public boolean isEnabled() { | |
60 | return true; | |
61 | } | |
62 | ||
63 | // ------------------------------------------------------------------------ | |
64 | // Execution | |
65 | // ------------------------------------------------------------------------ | |
66 | ||
67 | @Override | |
68 | public Object execute(ExecutionEvent event) throws ExecutionException { | |
69 | ||
70 | // Check if we are closing down | |
71 | IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow(); | |
72 | if (window == null) { | |
73 | return null; | |
74 | } | |
75 | ||
76 | // Get the selection | |
77 | IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage(); | |
78 | IWorkbenchPart part = page.getActivePart(); | |
79 | if (part == null) { | |
0126a8ca | 80 | return Boolean.FALSE; |
e73a4ba5 GB |
81 | } |
82 | ISelectionProvider selectionProvider = part.getSite().getSelectionProvider(); | |
83 | if (selectionProvider == null) { | |
0126a8ca | 84 | return Boolean.FALSE; |
e73a4ba5 GB |
85 | } |
86 | ISelection selection = selectionProvider.getSelection(); | |
87 | ||
88 | // Make sure selection contains only traces | |
89 | fSelection = null; | |
507b1336 AM |
90 | final ArrayList<TmfTraceElement> tl = new ArrayList<>(); |
91 | final ArrayList<TmfExperimentElement> uiexperiment = new ArrayList<>(); | |
e73a4ba5 GB |
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()) { | |
103 | tl.add(trace); | |
104 | } | |
105 | } | |
106 | } | |
107 | } | |
108 | ||
109 | if ((uiexperiment.size() == 1) && (tl.size() > 1)) { | |
110 | ||
111 | Thread thread = new Thread() { | |
112 | @Override | |
113 | public void run() { | |
114 | ||
115 | final ITmfTrace[] traces = new ITmfTrace[tl.size()]; | |
116 | final TmfExperimentElement exp = uiexperiment.get(0); | |
117 | ||
118 | for (int i = 0; i < tl.size(); i++) { | |
119 | ITmfTrace trace = tl.get(i).instantiateTrace(); | |
120 | ITmfEvent traceEvent = tl.get(i).instantiateEvent(); | |
121 | if (trace == null) { | |
122 | TraceUtils.displayErrorMsg(Messages.SynchronizeTracesHandler_Title, Messages.SynchronizeTracesHandler_WrongType + tl.get(i).getName()); | |
123 | for (int j = 0; j < i; j++) { | |
124 | traces[j].dispose(); | |
125 | } | |
126 | return; | |
127 | } | |
128 | try { | |
129 | trace.initTrace(tl.get(i).getResource(), tl.get(i).getLocation().getPath(), traceEvent.getClass()); | |
130 | } catch (TmfTraceException e) { | |
e3254d3a | 131 | TraceUtils.displayErrorMsg(Messages.SynchronizeTracesHandler_Title, Messages.SynchronizeTracesHandler_InitError + CR + CR + e); |
e73a4ba5 GB |
132 | trace.dispose(); |
133 | for (int j = 0; j < i; j++) { | |
134 | traces[j].dispose(); | |
135 | } | |
136 | return; | |
137 | } | |
138 | traces[i] = trace; | |
139 | } | |
140 | ||
141 | /* | |
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? | |
145 | */ | |
146 | exp.refreshSupplementaryFolder(); | |
147 | final TmfExperiment experiment = new TmfExperiment(ITmfEvent.class, exp.getName(), traces, exp.getResource()); | |
148 | ||
149 | try { | |
150 | final SynchronizationAlgorithm syncAlgo = experiment.synchronizeTraces(true); | |
151 | ||
152 | Display.getDefault().asyncExec(new Runnable() { | |
153 | @Override | |
154 | public void run() { | |
155 | /* | |
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. | |
160 | */ | |
161 | for (int i = 0; i < tl.size(); i++) { | |
162 | TmfTraceElement traceel = tl.get(i); | |
163 | try { | |
164 | if (syncAlgo.isTraceSynced(traceel.getName())) { | |
165 | ||
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; | |
171 | } | |
172 | } | |
173 | ||
174 | if (origtrace != null) { | |
175 | /* | |
176 | * Make sure a trace with the | |
177 | * new name does not exist | |
178 | */ | |
179 | String newname = traceel.getName(); | |
180 | boolean traceexists; | |
181 | do { | |
182 | traceexists = false; | |
183 | newname += "_"; //$NON-NLS-1$ | |
184 | for (ITmfProjectModelElement el : traceel.getProject().getTracesFolder().getTraces()) { | |
185 | if (el.getName().equals(newname)) { | |
186 | traceexists = true; | |
187 | } | |
188 | } | |
189 | } while (traceexists); | |
190 | ||
191 | /* Copy the original trace */ | |
192 | TmfTraceElement newtrace = origtrace.copy(newname); | |
193 | ||
194 | if (newtrace != null) { | |
195 | ||
196 | syncAlgo.renameTrace(origtrace.getName(), newtrace.getName()); | |
197 | ||
198 | /* | |
199 | * Instantiate the new trace | |
200 | * and set its sync formula | |
201 | */ | |
202 | ITmfTrace trace = newtrace.instantiateTrace(); | |
203 | ITmfEvent traceEvent = newtrace.instantiateEvent(); | |
204 | ||
205 | trace.initTrace(newtrace.getResource(), newtrace.getLocation().getPath(), traceEvent.getClass()); | |
206 | trace.setTimestampTransform(syncAlgo.getTimestampTransform(trace)); | |
207 | ||
208 | /* | |
209 | * Add the new trace to the | |
210 | * experiment | |
211 | */ | |
212 | exp.addTrace(newtrace); | |
213 | ||
214 | /* | |
215 | * Delete the original trace | |
216 | * element | |
217 | */ | |
218 | exp.removeTrace(traceel); | |
219 | } else { | |
220 | TraceUtils.displayErrorMsg(Messages.SynchronizeTracesHandler_Title, Messages.SynchronizeTracesHandler_Error + CR + CR + String.format(Messages.SynchronizeTracesHandler_CopyProblem, origtrace.getName())); | |
221 | } | |
222 | } | |
223 | } | |
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()); | |
230 | } | |
231 | } | |
232 | } | |
233 | }); | |
234 | ||
235 | } catch (TmfTraceException e) { | |
236 | Activator.getDefault().logError(String.format(Messages.SynchronizeTracesHandler_ErrorSynchingExperiment, exp.getName()), e); | |
e3254d3a | 237 | TraceUtils.displayErrorMsg(Messages.SynchronizeTracesHandler_Title, Messages.SynchronizeTracesHandler_Error + CR + CR + e.getMessage()); |
e73a4ba5 GB |
238 | } |
239 | } | |
240 | }; | |
241 | thread.start(); | |
242 | ||
243 | } else { | |
244 | TraceUtils.displayErrorMsg(Messages.SynchronizeTracesHandler_Title, Messages.SynchronizeTracesHandler_WrongTraceNumber); | |
245 | } | |
246 | ||
247 | return null; | |
248 | } | |
249 | ||
250 | } |