Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 9ac1c4c017de5638ac84399a04548961102c5e52 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
/*******************************************************************************
 * Copyright (c) 2013 École Polytechnique de Montréal
 *
 * All rights reserved. This program and the accompanying materials are
 * made available under the terms of the Eclipse Public License v1.0 which
 * accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *   Geneviève Bastien - Initial implementation and API
 *******************************************************************************/

package org.eclipse.linuxtools.internal.tmf.ui.project.handlers;

import java.util.ArrayList;
import java.util.Iterator;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.ISelectionProvider;
import org.eclipse.jface.viewers.TreeSelection;
import org.eclipse.linuxtools.internal.tmf.ui.Activator;
import org.eclipse.linuxtools.tmf.core.event.ITmfEvent;
import org.eclipse.linuxtools.tmf.core.exceptions.TmfTraceException;
import org.eclipse.linuxtools.tmf.core.synchronization.SynchronizationAlgorithm;
import org.eclipse.linuxtools.tmf.core.trace.ITmfTrace;
import org.eclipse.linuxtools.tmf.core.trace.TmfExperiment;
import org.eclipse.linuxtools.tmf.ui.project.model.ITmfProjectModelElement;
import org.eclipse.linuxtools.tmf.ui.project.model.TmfExperimentElement;
import org.eclipse.linuxtools.tmf.ui.project.model.TmfTraceElement;
import org.eclipse.linuxtools.tmf.ui.project.model.TraceUtils;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PlatformUI;

/**
 * Handles the synchronization of an experiment, when the user selects this
 * option in the menu
 */
public class SynchronizeTracesHandler extends AbstractHandler {

    // ------------------------------------------------------------------------
    // Attributes
    // ------------------------------------------------------------------------

    private TreeSelection fSelection = null;
    private static final String CR = System.getProperty("line.separator"); //$NON-NLS-1$

    // ------------------------------------------------------------------------
    // Validation
    // ------------------------------------------------------------------------

    @Override
    public boolean isEnabled() {
        return true;
    }

    // ------------------------------------------------------------------------
    // Execution
    // ------------------------------------------------------------------------

    @Override
    public Object execute(ExecutionEvent event) throws ExecutionException {

        // Check if we are closing down
        IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
        if (window == null) {
            return null;
        }

        // Get the selection
        IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
        IWorkbenchPart part = page.getActivePart();
        if (part == null) {
            return false;
        }
        ISelectionProvider selectionProvider = part.getSite().getSelectionProvider();
        if (selectionProvider == null) {
            return false;
        }
        ISelection selection = selectionProvider.getSelection();

        // Make sure selection contains only traces
        fSelection = null;
        final ArrayList<TmfTraceElement> tl = new ArrayList<TmfTraceElement>();
        final ArrayList<TmfExperimentElement> uiexperiment = new ArrayList<TmfExperimentElement>();
        if (selection instanceof TreeSelection) {
            fSelection = (TreeSelection) selection;
            Iterator<Object> iterator = fSelection.iterator();
            while (iterator.hasNext()) {
                Object element = iterator.next();
                if (element instanceof TmfTraceElement) {
                    tl.add((TmfTraceElement) element);
                } else if (element instanceof TmfExperimentElement) {
                    TmfExperimentElement exp = (TmfExperimentElement) element;
                    uiexperiment.add(exp);
                    for (TmfTraceElement trace : exp.getTraces()) {
                        tl.add(trace);
                    }
                }
            }
        }

        if ((uiexperiment.size() == 1) && (tl.size() > 1)) {

            Thread thread = new Thread() {
                @Override
                public void run() {

                    final ITmfTrace[] traces = new ITmfTrace[tl.size()];
                    final TmfExperimentElement exp = uiexperiment.get(0);

                    for (int i = 0; i < tl.size(); i++) {
                        ITmfTrace trace = tl.get(i).instantiateTrace();
                        ITmfEvent traceEvent = tl.get(i).instantiateEvent();
                        if (trace == null) {
                            TraceUtils.displayErrorMsg(Messages.SynchronizeTracesHandler_Title, Messages.SynchronizeTracesHandler_WrongType + tl.get(i).getName());
                            for (int j = 0; j < i; j++) {
                                traces[j].dispose();
                            }
                            return;
                        }
                        try {
                            trace.initTrace(tl.get(i).getResource(), tl.get(i).getLocation().getPath(), traceEvent.getClass());
                        } catch (TmfTraceException e) {
                            TraceUtils.displayErrorMsg(Messages.SynchronizeTracesHandler_Title, Messages.OpenTraceHandler_InitError + CR + CR + e);
                            trace.dispose();
                            for (int j = 0; j < i; j++) {
                                traces[j].dispose();
                            }
                            return;
                        }
                        traces[i] = trace;
                    }

                    /*
                     * FIXME Unlike traces, there is no instanceExperiment, so
                     * we call this function here alone. Maybe it would be
                     * better to do this on experiment's element constructor?
                     */
                    exp.refreshSupplementaryFolder();
                    final TmfExperiment experiment = new TmfExperiment(ITmfEvent.class, exp.getName(), traces, exp.getResource());

                    try {
                        final SynchronizationAlgorithm syncAlgo = experiment.synchronizeTraces(true);

                        Display.getDefault().asyncExec(new Runnable() {
                            @Override
                            public void run() {
                                /*
                                 * For each trace in the experiment, if there is
                                 * a transform equation, copy the original
                                 * trace, so that a new state system will be
                                 * generated with sync time.
                                 */
                                for (int i = 0; i < tl.size(); i++) {
                                    TmfTraceElement traceel = tl.get(i);
                                    try {
                                        if (syncAlgo.isTraceSynced(traceel.getName())) {

                                            /* Find the original trace */
                                            TmfTraceElement origtrace = null;
                                            for (ITmfProjectModelElement el : traceel.getProject().getTracesFolder().getTraces()) {
                                                if (el.getName().equals(traceel.getName())) {
                                                    origtrace = (TmfTraceElement) el;
                                                }
                                            }

                                            if (origtrace != null) {
                                                /*
                                                 * Make sure a trace with the
                                                 * new name does not exist
                                                 */
                                                String newname = traceel.getName();
                                                boolean traceexists;
                                                do {
                                                    traceexists = false;
                                                    newname += "_"; //$NON-NLS-1$
                                                    for (ITmfProjectModelElement el : traceel.getProject().getTracesFolder().getTraces()) {
                                                        if (el.getName().equals(newname)) {
                                                            traceexists = true;
                                                        }
                                                    }
                                                } while (traceexists);

                                                /* Copy the original trace */
                                                TmfTraceElement newtrace = origtrace.copy(newname);

                                                if (newtrace != null) {

                                                    syncAlgo.renameTrace(origtrace.getName(), newtrace.getName());

                                                    /*
                                                     * Instantiate the new trace
                                                     * and set its sync formula
                                                     */
                                                    ITmfTrace trace = newtrace.instantiateTrace();
                                                    ITmfEvent traceEvent = newtrace.instantiateEvent();

                                                    trace.initTrace(newtrace.getResource(), newtrace.getLocation().getPath(), traceEvent.getClass());
                                                    trace.setTimestampTransform(syncAlgo.getTimestampTransform(trace));

                                                    /*
                                                     * Add the new trace to the
                                                     * experiment
                                                     */
                                                    exp.addTrace(newtrace);

                                                    /*
                                                     * Delete the original trace
                                                     * element
                                                     */
                                                    exp.removeTrace(traceel);
                                                } else {
                                                    TraceUtils.displayErrorMsg(Messages.SynchronizeTracesHandler_Title, Messages.SynchronizeTracesHandler_Error + CR + CR + String.format(Messages.SynchronizeTracesHandler_CopyProblem, origtrace.getName()));
                                                }
                                            }
                                        }
                                    } catch (CoreException e) {
                                        Activator.getDefault().logError(String.format(Messages.SynchronizeTracesHandler_ErrorSynchingForTrace, exp.getName(), traceel.getName()), e);
                                        TraceUtils.displayErrorMsg(Messages.SynchronizeTracesHandler_Title, Messages.SynchronizeTracesHandler_Error + CR + CR + e.getMessage());
                                    } catch (TmfTraceException e) {
                                        Activator.getDefault().logError(String.format(Messages.SynchronizeTracesHandler_ErrorSynchingForTrace, exp.getName(), traceel.getName()), e);
                                        TraceUtils.displayErrorMsg(Messages.SynchronizeTracesHandler_Title, Messages.SynchronizeTracesHandler_Error + CR + CR + e.getMessage());
                                    }
                                }
                            }
                        });

                    } catch (TmfTraceException e) {
                        Activator.getDefault().logError(String.format(Messages.SynchronizeTracesHandler_ErrorSynchingExperiment, exp.getName()), e);
                        TraceUtils.displayErrorMsg(Messages.SynchronizeTracesHandler_Title, Messages.OpenExperimentHandler_Error + CR + CR + e.getMessage());
                    }
                }
            };
            thread.start();

        } else {
            TraceUtils.displayErrorMsg(Messages.SynchronizeTracesHandler_Title, Messages.SynchronizeTracesHandler_WrongTraceNumber);
        }

        return null;
    }

}

Back to the top