| 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.viewers.events.columns.TmfEventTableColumn; |
| 29 | import org.eclipse.ui.PlatformUI; |
| 30 | |
| 31 | /** |
| 32 | * This handler exports traces to text files. |
| 33 | * @author Xavier Raynaud <xavier.raynaud@kalray.eu> |
| 34 | */ |
| 35 | public class ExportToTextCommandHandler extends AbstractHandler { |
| 36 | |
| 37 | /** Id of the export-to-text command */ |
| 38 | public static final String COMMAND_ID = "org.eclipse.linuxtools.tmf.ui.exportToText"; //$NON-NLS-1$ |
| 39 | /** |
| 40 | * Id used to retrieve the header (as a String) of the trace to export. |
| 41 | * This header is from the application context of this handler. |
| 42 | */ |
| 43 | public static final String TMF_EVENT_TABLE_COLUMNS_ID = "org.eclipse.linuxtools.tmf.ui.exportToText.columns"; //$NON-NLS-1$ |
| 44 | |
| 45 | /** |
| 46 | * Constructor |
| 47 | */ |
| 48 | public ExportToTextCommandHandler() { |
| 49 | } |
| 50 | |
| 51 | @Override |
| 52 | public Object execute(ExecutionEvent event) throws ExecutionException { |
| 53 | List<TmfEventTableColumn> columns = getColumns(event.getApplicationContext()); |
| 54 | ITmfTrace trace = TmfTraceManager.getInstance().getActiveTrace(); |
| 55 | ITmfFilter filter = TmfTraceManager.getInstance().getCurrentTraceContext().getFilter(); |
| 56 | if (trace != null) { |
| 57 | FileDialog fd = new FileDialog(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), SWT.SAVE); |
| 58 | fd.setFilterExtensions(new String[] { "*.csv", "*.*", "*" }); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ |
| 59 | fd.setOverwrite(true); |
| 60 | final String s = fd.open(); |
| 61 | if (s != null) { |
| 62 | Job j = new ExportToTextJob(trace, filter, columns, s); |
| 63 | j.setUser(true); |
| 64 | j.schedule(); |
| 65 | } |
| 66 | } |
| 67 | return null; |
| 68 | } |
| 69 | |
| 70 | @SuppressWarnings("unchecked") |
| 71 | private static List<TmfEventTableColumn> getColumns(Object evaluationContext) { |
| 72 | if (evaluationContext instanceof IEvaluationContext) { |
| 73 | Object s = ((IEvaluationContext) evaluationContext).getVariable(TMF_EVENT_TABLE_COLUMNS_ID); |
| 74 | if (s instanceof List<?>) { |
| 75 | return (List<TmfEventTableColumn>) s; |
| 76 | } |
| 77 | } |
| 78 | return null; |
| 79 | } |
| 80 | |
| 81 | } |