Commit | Line | Data |
---|---|---|
6b44794a MK |
1 | /******************************************************************************* |
2 | * Copyright (c) 2014 Ericsson | |
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 | * Matthew Khouzam - Initial API and implementation | |
11 | * Patrick Tasse - Initial API and implementation | |
12 | *******************************************************************************/ | |
13 | ||
14 | package org.eclipse.linuxtools.internal.tmf.ui.project.handlers; | |
15 | ||
16 | import java.lang.reflect.InvocationTargetException; | |
17 | import java.util.HashSet; | |
18 | import java.util.Iterator; | |
19 | import java.util.LinkedHashMap; | |
20 | import java.util.Map; | |
21 | import java.util.Set; | |
22 | ||
23 | import org.eclipse.core.commands.AbstractHandler; | |
24 | import org.eclipse.core.commands.ExecutionEvent; | |
25 | import org.eclipse.core.commands.ExecutionException; | |
26 | import org.eclipse.core.runtime.CoreException; | |
27 | import org.eclipse.core.runtime.IProgressMonitor; | |
28 | import org.eclipse.core.runtime.OperationCanceledException; | |
29 | import org.eclipse.jface.dialogs.MessageDialog; | |
30 | import org.eclipse.jface.viewers.ISelection; | |
31 | import org.eclipse.jface.viewers.StructuredSelection; | |
32 | import org.eclipse.jface.window.Window; | |
33 | import org.eclipse.linuxtools.internal.tmf.ui.project.dialogs.offset.OffsetDialog; | |
34 | import org.eclipse.linuxtools.internal.tmf.ui.project.operations.TmfWorkspaceModifyOperation; | |
35 | import org.eclipse.linuxtools.tmf.core.synchronization.ITmfTimestampTransform; | |
36 | import org.eclipse.linuxtools.tmf.core.synchronization.TimestampTransformFactory; | |
37 | import org.eclipse.linuxtools.tmf.ui.project.model.TmfExperimentElement; | |
38 | import org.eclipse.linuxtools.tmf.ui.project.model.TmfTraceElement; | |
39 | import org.eclipse.linuxtools.tmf.ui.project.model.TmfTraceFolder; | |
40 | import org.eclipse.swt.widgets.Display; | |
41 | import org.eclipse.swt.widgets.Shell; | |
42 | import org.eclipse.ui.PlatformUI; | |
43 | import org.eclipse.ui.handlers.HandlerUtil; | |
44 | ||
45 | /** | |
46 | * Offset Handler | |
47 | * | |
48 | * @author Matthew Khouzam | |
49 | */ | |
50 | public class OffsetTraceHandler extends AbstractHandler { | |
51 | ||
52 | // ------------------------------------------------------------------------ | |
53 | // Execution | |
54 | // ------------------------------------------------------------------------ | |
55 | ||
56 | @Override | |
57 | public Object execute(final ExecutionEvent event) throws ExecutionException { | |
58 | ||
59 | ISelection selection = HandlerUtil.getCurrentSelection(event); | |
60 | ||
61 | // Get the set of selected trace elements | |
62 | final Set<TmfTraceElement> traceElements = new HashSet<>(); | |
63 | if (selection instanceof StructuredSelection) { | |
64 | Iterator<Object> iterator = ((StructuredSelection) selection).iterator(); | |
65 | while (iterator.hasNext()) { | |
66 | Object element = iterator.next(); | |
67 | if (element instanceof TmfTraceElement) { | |
68 | TmfTraceElement trace = (TmfTraceElement) element; | |
69 | traceElements.add(trace.getElementUnderTraceFolder()); | |
70 | } else if (element instanceof TmfExperimentElement) { | |
71 | TmfExperimentElement exp = (TmfExperimentElement) element; | |
72 | for (TmfTraceElement trace : exp.getTraces()) { | |
73 | traceElements.add(trace.getElementUnderTraceFolder()); | |
74 | } | |
75 | } else if (element instanceof TmfTraceFolder) { | |
76 | TmfTraceFolder folder = (TmfTraceFolder) element; | |
77 | traceElements.addAll(folder.getTraces()); | |
78 | } | |
79 | } | |
80 | } | |
81 | ||
82 | final Map<TmfTraceElement, Long> offsets = new LinkedHashMap<>(traceElements.size()); | |
83 | for (TmfTraceElement trace : traceElements) { | |
84 | offsets.put(trace, 0L); | |
85 | } | |
86 | ||
87 | Shell shell = HandlerUtil.getActiveShellChecked(event); | |
88 | OffsetDialog dialog = new OffsetDialog(shell, offsets); | |
89 | dialog.open(); | |
90 | ||
91 | if (dialog.getReturnCode() != Window.OK) { | |
92 | return null; | |
93 | } | |
94 | ||
95 | TmfWorkspaceModifyOperation operation = new TmfWorkspaceModifyOperation() { | |
96 | @Override | |
97 | public void execute(IProgressMonitor monitor) throws CoreException { | |
98 | for (final TmfTraceElement trace : offsets.keySet()) { | |
99 | if (monitor.isCanceled()) { | |
100 | throw new OperationCanceledException(); | |
101 | } | |
102 | Long offset = offsets.get(trace); | |
103 | if (offset != 0 && trace.getResource().exists()) { | |
104 | Display.getDefault().syncExec(new Runnable() { | |
105 | @Override | |
106 | public void run() { | |
107 | trace.closeEditors(); | |
108 | } | |
109 | }); | |
110 | long previousOffset = TimestampTransformFactory.getTimestampTransform(trace.getResource()).transform(0); | |
111 | ITmfTimestampTransform transform = TimestampTransformFactory.createWithOffset(previousOffset + offset); | |
112 | trace.deleteSupplementaryResources(); | |
113 | // make sure the supplementary folder exists | |
114 | trace.refreshSupplementaryFolder(); | |
115 | TimestampTransformFactory.setTimestampTransform(trace.getResource(), transform); | |
116 | trace.refreshSupplementaryFolder(); | |
117 | } | |
118 | } | |
119 | } | |
120 | }; | |
121 | try { | |
122 | PlatformUI.getWorkbench().getProgressService().run(true, true, operation); | |
123 | } catch (InterruptedException e) { | |
124 | return null; | |
125 | } catch (InvocationTargetException e) { | |
126 | MessageDialog.openError(shell, e.toString(), e.getTargetException().toString()); | |
127 | return null; | |
128 | } | |
129 | ||
130 | return null; | |
131 | } | |
132 | } |