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 | * Patrick Tasse - Initial API and implementation | |
11 | *******************************************************************************/ | |
12 | ||
13 | package org.eclipse.linuxtools.internal.tmf.ui.project.handlers; | |
14 | ||
15 | import java.lang.reflect.InvocationTargetException; | |
16 | import java.util.HashSet; | |
17 | import java.util.Iterator; | |
18 | import java.util.Set; | |
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.runtime.CoreException; | |
24 | import org.eclipse.core.runtime.IProgressMonitor; | |
25 | import org.eclipse.core.runtime.OperationCanceledException; | |
26 | import org.eclipse.jface.dialogs.MessageDialog; | |
27 | import org.eclipse.jface.viewers.ISelection; | |
28 | import org.eclipse.jface.viewers.StructuredSelection; | |
29 | import org.eclipse.linuxtools.internal.tmf.ui.project.operations.TmfWorkspaceModifyOperation; | |
30 | import org.eclipse.linuxtools.tmf.core.synchronization.TimestampTransformFactory; | |
31 | import org.eclipse.linuxtools.tmf.ui.project.model.TmfExperimentElement; | |
32 | import org.eclipse.linuxtools.tmf.ui.project.model.TmfTraceElement; | |
33 | import org.eclipse.linuxtools.tmf.ui.project.model.TmfTraceFolder; | |
34 | import org.eclipse.swt.SWT; | |
35 | import org.eclipse.swt.widgets.Display; | |
36 | import org.eclipse.swt.widgets.MessageBox; | |
37 | import org.eclipse.swt.widgets.Shell; | |
38 | import org.eclipse.ui.PlatformUI; | |
39 | import org.eclipse.ui.handlers.HandlerUtil; | |
40 | ||
41 | /** | |
42 | * Clear Trace Offset Handler | |
43 | * | |
44 | * @author Patrick Tasse | |
45 | */ | |
46 | public class ClearTraceOffsetHandler extends AbstractHandler { | |
47 | ||
48 | // ------------------------------------------------------------------------ | |
49 | // Execution | |
50 | // ------------------------------------------------------------------------ | |
51 | ||
52 | @Override | |
53 | public Object execute(final ExecutionEvent event) throws ExecutionException { | |
54 | ||
55 | ISelection selection = HandlerUtil.getCurrentSelection(event); | |
56 | ||
57 | // Get the set of selected trace elements | |
58 | final Set<TmfTraceElement> traceElements = new HashSet<>(); | |
59 | if (selection instanceof StructuredSelection) { | |
60 | Iterator<Object> iterator = ((StructuredSelection) selection).iterator(); | |
61 | while (iterator.hasNext()) { | |
62 | Object element = iterator.next(); | |
63 | if (element instanceof TmfTraceElement) { | |
64 | TmfTraceElement trace = (TmfTraceElement) element; | |
65 | traceElements.add(trace.getElementUnderTraceFolder()); | |
66 | } else if (element instanceof TmfExperimentElement) { | |
67 | TmfExperimentElement exp = (TmfExperimentElement) element; | |
68 | for (TmfTraceElement trace : exp.getTraces()) { | |
69 | traceElements.add(trace.getElementUnderTraceFolder()); | |
70 | } | |
71 | } else if (element instanceof TmfTraceFolder) { | |
72 | TmfTraceFolder folder = (TmfTraceFolder) element; | |
73 | traceElements.addAll(folder.getTraces()); | |
74 | } | |
75 | } | |
76 | } | |
77 | ||
78 | Shell shell = HandlerUtil.getActiveShellChecked(event); | |
79 | MessageBox mb = new MessageBox(shell, SWT.ICON_QUESTION | SWT.CANCEL | SWT.OK); | |
80 | mb.setText(Messages.ClearTraceOffsetHandler_Title); | |
81 | mb.setMessage(Messages.ClearTraceOffsetHandler_ConfirmMessage); | |
82 | if (mb.open() != SWT.OK) { | |
83 | return null; | |
84 | } | |
85 | ||
86 | TmfWorkspaceModifyOperation operation = new TmfWorkspaceModifyOperation() { | |
87 | @Override | |
88 | public void execute(IProgressMonitor monitor) throws CoreException { | |
89 | for (final TmfTraceElement trace : traceElements) { | |
90 | if (monitor.isCanceled()) { | |
91 | throw new OperationCanceledException(); | |
92 | } | |
93 | if (!TimestampTransformFactory.getTimestampTransform(trace.getResource()).equals(TimestampTransformFactory.getDefaultTransform())) { | |
94 | Display.getDefault().syncExec(new Runnable() { | |
95 | @Override | |
96 | public void run() { | |
97 | trace.closeEditors(); | |
98 | } | |
99 | }); | |
100 | trace.deleteSupplementaryResources(); | |
101 | TimestampTransformFactory.setTimestampTransform(trace.getResource(), null); | |
102 | trace.refreshSupplementaryFolder(); | |
103 | } | |
104 | } | |
105 | } | |
106 | }; | |
107 | try { | |
108 | PlatformUI.getWorkbench().getProgressService().run(true, true, operation); | |
109 | } catch (InterruptedException e) { | |
110 | return null; | |
111 | } catch (InvocationTargetException e) { | |
112 | MessageDialog.openError(shell, e.toString(), e.getTargetException().toString()); | |
113 | return null; | |
114 | } | |
115 | ||
116 | return null; | |
117 | } | |
118 | } |