tmf.ui: Introduce TmfFileDialogFactory
[deliverable/tracecompass.git] / tmf / org.eclipse.tracecompass.tmf.ui / src / org / eclipse / tracecompass / internal / tmf / ui / commands / ExportToTextCommandHandler.java
1 /*******************************************************************************
2 * Copyright (c) 2013, 2014 Kalray, 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 * Xavier Raynaud - Initial API and implementation
11 * Bernd Hufmann - Adapted to new events table column API
12 *******************************************************************************/
13
14 package org.eclipse.tracecompass.internal.tmf.ui.commands;
15
16 import java.util.List;
17
18 import org.eclipse.core.commands.AbstractHandler;
19 import org.eclipse.core.commands.ExecutionEvent;
20 import org.eclipse.core.commands.ExecutionException;
21 import org.eclipse.core.expressions.IEvaluationContext;
22 import org.eclipse.core.runtime.jobs.Job;
23 import org.eclipse.swt.SWT;
24 import org.eclipse.swt.widgets.FileDialog;
25 import org.eclipse.tracecompass.tmf.core.filter.ITmfFilter;
26 import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace;
27 import org.eclipse.tracecompass.tmf.core.trace.TmfTraceManager;
28 import org.eclipse.tracecompass.tmf.ui.dialog.TmfFileDialogFactory;
29 import org.eclipse.tracecompass.tmf.ui.viewers.events.columns.TmfEventTableColumn;
30 import org.eclipse.ui.PlatformUI;
31
32 /**
33 * This handler exports traces to text files.
34 * @author Xavier Raynaud <xavier.raynaud@kalray.eu>
35 */
36 public class ExportToTextCommandHandler extends AbstractHandler {
37
38 /** Id of the export-to-text command */
39 public static final String COMMAND_ID = "org.eclipse.linuxtools.tmf.ui.exportToText"; //$NON-NLS-1$
40 /**
41 * Id used to retrieve the header (as a String) of the trace to export.
42 * This header is from the application context of this handler.
43 */
44 public static final String TMF_EVENT_TABLE_COLUMNS_ID = "org.eclipse.linuxtools.tmf.ui.exportToText.columns"; //$NON-NLS-1$
45
46 /**
47 * Constructor
48 */
49 public ExportToTextCommandHandler() {
50 }
51
52 @Override
53 public Object execute(ExecutionEvent event) throws ExecutionException {
54 List<TmfEventTableColumn> columns = getColumns(event.getApplicationContext());
55 ITmfTrace trace = TmfTraceManager.getInstance().getActiveTrace();
56 ITmfFilter filter = TmfTraceManager.getInstance().getCurrentTraceContext().getFilter();
57 if (trace != null) {
58 FileDialog fd = TmfFileDialogFactory.create(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), SWT.SAVE);
59 fd.setFilterExtensions(new String[] { "*.csv", "*.*", "*" }); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
60 fd.setOverwrite(true);
61 final String s = fd.open();
62 if (s != null) {
63 Job j = new ExportToTextJob(trace, filter, columns, s);
64 j.setUser(true);
65 j.schedule();
66 }
67 }
68 return null;
69 }
70
71 @SuppressWarnings("unchecked")
72 private static List<TmfEventTableColumn> getColumns(Object evaluationContext) {
73 if (evaluationContext instanceof IEvaluationContext) {
74 Object s = ((IEvaluationContext) evaluationContext).getVariable(TMF_EVENT_TABLE_COLUMNS_ID);
75 if (s instanceof List<?>) {
76 return (List<TmfEventTableColumn>) s;
77 }
78 }
79 return null;
80 }
81
82 }
This page took 0.032586 seconds and 5 git commands to generate.