| 1 | /******************************************************************************* |
| 2 | * Copyright (c) 2013, 2015 É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 | * Cédric Biancheri - Added a wizard to select the root node |
| 12 | *******************************************************************************/ |
| 13 | |
| 14 | package org.eclipse.tracecompass.internal.tmf.ui.project.handlers; |
| 15 | |
| 16 | import java.util.ArrayList; |
| 17 | import java.util.Iterator; |
| 18 | import java.util.List; |
| 19 | |
| 20 | import org.eclipse.core.commands.AbstractHandler; |
| 21 | import org.eclipse.core.commands.ExecutionEvent; |
| 22 | import org.eclipse.core.commands.ExecutionException; |
| 23 | import org.eclipse.core.resources.IContainer; |
| 24 | import org.eclipse.core.resources.IFolder; |
| 25 | import org.eclipse.core.resources.IResource; |
| 26 | import org.eclipse.core.runtime.CoreException; |
| 27 | import org.eclipse.jface.viewers.ISelection; |
| 28 | import org.eclipse.jface.viewers.TreeSelection; |
| 29 | import org.eclipse.jface.window.Window; |
| 30 | import org.eclipse.jface.wizard.WizardDialog; |
| 31 | import org.eclipse.swt.widgets.Display; |
| 32 | import org.eclipse.swt.widgets.Shell; |
| 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; |
| 38 | import org.eclipse.tracecompass.tmf.core.trace.TmfTraceManager; |
| 39 | import org.eclipse.tracecompass.tmf.core.trace.experiment.TmfExperiment; |
| 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; |
| 43 | import org.eclipse.tracecompass.tmf.ui.project.wizards.SelectRootNodeWizard; |
| 44 | import org.eclipse.ui.IWorkbenchWindow; |
| 45 | import org.eclipse.ui.PlatformUI; |
| 46 | import org.eclipse.ui.handlers.HandlerUtil; |
| 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$ |
| 60 | private TmfExperimentElement fExperiment = null; |
| 61 | private TmfTraceElement fRootNode = null; |
| 62 | private String fRootNodeId = null; |
| 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 |
| 78 | ISelection selection = HandlerUtil.getCurrentSelectionChecked(event); |
| 79 | |
| 80 | // Make sure selection contains only traces |
| 81 | fSelection = null; |
| 82 | final ArrayList<TmfTraceElement> tl = new ArrayList<>(); |
| 83 | final ArrayList<TmfExperimentElement> uiexperiment = new ArrayList<>(); |
| 84 | if (selection instanceof TreeSelection) { |
| 85 | fSelection = (TreeSelection) selection; |
| 86 | Iterator<Object> iterator = fSelection.iterator(); |
| 87 | while (iterator.hasNext()) { |
| 88 | Object element = iterator.next(); |
| 89 | if (element instanceof TmfExperimentElement) { |
| 90 | TmfExperimentElement exp = (TmfExperimentElement) element; |
| 91 | uiexperiment.add(exp); |
| 92 | for (TmfTraceElement trace : exp.getTraces()) { |
| 93 | tl.add(trace); |
| 94 | } |
| 95 | } |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | if ((uiexperiment.size() != 1) || (tl.size() < 2)) { |
| 100 | TraceUtils.displayErrorMsg(Messages.SynchronizeTracesHandler_Title, Messages.SynchronizeTracesHandler_WrongTraceNumber); |
| 101 | return null; |
| 102 | } |
| 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(); |
| 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 | if (tl.get(i).getElementPath().equals(fRootNode.getElementPath())) { |
| 147 | fRootNodeId = trace.getHostId(); |
| 148 | } |
| 149 | traces[i] = trace; |
| 150 | } |
| 151 | |
| 152 | /* |
| 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? |
| 156 | */ |
| 157 | exp.refreshSupplementaryFolder(); |
| 158 | final TmfExperiment experiment = new TmfExperiment(ITmfEvent.class, |
| 159 | exp.getName(), traces, TmfExperiment.DEFAULT_INDEX_PAGE_SIZE, exp.getResource()); |
| 160 | |
| 161 | final SynchronizationAlgorithm syncAlgo = experiment.synchronizeTraces(true); |
| 162 | syncAlgo.setRootNode(fRootNodeId); |
| 163 | |
| 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 | /* |
| 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. |
| 175 | */ |
| 176 | for (TmfTraceElement traceel : tl) { |
| 177 | /* |
| 178 | * Find the trace corresponding to this element in |
| 179 | * the experiment |
| 180 | */ |
| 181 | ITmfTrace expTrace = null; |
| 182 | for (ITmfTrace t : experiment.getTraces()) { |
| 183 | if (t.getResource().equals(traceel.getResource())) { |
| 184 | expTrace = t; |
| 185 | break; |
| 186 | } |
| 187 | } |
| 188 | if ((expTrace != null) && syncAlgo.isTraceSynced(expTrace.getHostId())) { |
| 189 | /* Find the original trace */ |
| 190 | TmfTraceElement origtrace = traceel.getElementUnderTraceFolder(); |
| 191 | |
| 192 | /* |
| 193 | * Make sure a trace with the new name does not |
| 194 | * exist |
| 195 | */ |
| 196 | StringBuilder newname = new StringBuilder(traceel.getName()); |
| 197 | IContainer parentFolder = origtrace.getResource().getParent(); |
| 198 | boolean traceexists; |
| 199 | do { |
| 200 | traceexists = false; |
| 201 | newname.append('_'); |
| 202 | if (parentFolder.findMember(newname.toString()) != null) { |
| 203 | traceexists = true; |
| 204 | } |
| 205 | } while (traceexists); |
| 206 | |
| 207 | /* Copy the original trace */ |
| 208 | TmfTraceElement newtrace = origtrace.copy(newname.toString()); |
| 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; |
| 213 | } |
| 214 | |
| 215 | /* |
| 216 | * Instantiate the new trace and set its sync |
| 217 | * formula |
| 218 | */ |
| 219 | ITmfTrace trace = newtrace.instantiateTrace(); |
| 220 | ITmfEvent traceEvent = newtrace.instantiateEvent(); |
| 221 | |
| 222 | try { |
| 223 | trace.initTrace(newtrace.getResource(), newtrace.getResource().getLocation().toOSString(), traceEvent.getClass()); |
| 224 | } catch (TmfTraceException 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 | } |
| 228 | trace.setTimestampTransform(syncAlgo.getTimestampTransform(trace)); |
| 229 | TmfTraceManager.refreshSupplementaryFiles(trace); |
| 230 | trace.dispose(); |
| 231 | |
| 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 |
| 240 | IFolder tmpFolder = exp.getTraceSupplementaryFolder(exp.getName() + '.' + experiment.getSynchronizationFolder(false)); |
| 241 | IResource syncFile = null; |
| 242 | for (IResource resource : exp.getSupplementaryResources()) { |
| 243 | if (resource.getName().equals(experiment.getSynchronizationFolder(false))) { |
| 244 | try { |
| 245 | resource.move(tmpFolder.getFullPath(), false, null); |
| 246 | syncFile = resource; |
| 247 | break; |
| 248 | } catch (CoreException e) { |
| 249 | Activator.getDefault().logError(String.format(Messages.SynchronizeTracesHandler_ErrorSynchingExperiment, exp.getName()), e); |
| 250 | } |
| 251 | } |
| 252 | } |
| 253 | |
| 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 | }); |
| 276 | } |
| 277 | }; |
| 278 | thread.start(); |
| 279 | |
| 280 | return null; |
| 281 | } |
| 282 | |
| 283 | } |