tmf: Move plugins to the Trace Compass namespace
[deliverable/tracecompass.git] / org.eclipse.tracecompass.tmf.ui / src / org / eclipse / linuxtools / internal / tmf / ui / project / handlers / ClearTraceOffsetHandler.java
CommitLineData
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
13package org.eclipse.linuxtools.internal.tmf.ui.project.handlers;
14
15import java.lang.reflect.InvocationTargetException;
16import java.util.HashSet;
17import java.util.Iterator;
18import java.util.Set;
19
20import org.eclipse.core.commands.AbstractHandler;
21import org.eclipse.core.commands.ExecutionEvent;
22import org.eclipse.core.commands.ExecutionException;
23import org.eclipse.core.runtime.CoreException;
24import org.eclipse.core.runtime.IProgressMonitor;
25import org.eclipse.core.runtime.OperationCanceledException;
26import org.eclipse.jface.dialogs.MessageDialog;
27import org.eclipse.jface.viewers.ISelection;
28import org.eclipse.jface.viewers.StructuredSelection;
29import org.eclipse.linuxtools.internal.tmf.ui.project.operations.TmfWorkspaceModifyOperation;
30import org.eclipse.linuxtools.tmf.core.synchronization.TimestampTransformFactory;
31import org.eclipse.linuxtools.tmf.ui.project.model.TmfExperimentElement;
32import org.eclipse.linuxtools.tmf.ui.project.model.TmfTraceElement;
33import org.eclipse.linuxtools.tmf.ui.project.model.TmfTraceFolder;
34import org.eclipse.swt.SWT;
35import org.eclipse.swt.widgets.Display;
36import org.eclipse.swt.widgets.MessageBox;
37import org.eclipse.swt.widgets.Shell;
38import org.eclipse.ui.PlatformUI;
39import org.eclipse.ui.handlers.HandlerUtil;
40
41/**
42 * Clear Trace Offset Handler
43 *
44 * @author Patrick Tasse
45 */
46public 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}
This page took 0.030117 seconds and 5 git commands to generate.