| 1 | /******************************************************************************* |
| 2 | * Copyright (c) 2016 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 | |
| 10 | package org.eclipse.tracecompass.internal.analysis.timing.ui.views.segmentstore; |
| 11 | |
| 12 | import java.io.FileOutputStream; |
| 13 | import java.io.IOException; |
| 14 | import java.io.OutputStream; |
| 15 | |
| 16 | import org.eclipse.jdt.annotation.Nullable; |
| 17 | import org.eclipse.jface.action.Action; |
| 18 | import org.eclipse.swt.widgets.FileDialog; |
| 19 | import org.eclipse.swt.widgets.Shell; |
| 20 | import org.eclipse.tracecompass.internal.analysis.timing.ui.Activator; |
| 21 | |
| 22 | /** |
| 23 | * The export to TSV abstract action |
| 24 | * |
| 25 | * TODO: improve testing when there is a way to test native widgets |
| 26 | * |
| 27 | * @author Matthew Khouzam |
| 28 | */ |
| 29 | public abstract class ExportToTsvAction extends Action { |
| 30 | |
| 31 | private static final String[] EXTENSTIONS = { "*.tsv", "*.*" };//$NON-NLS-1$//$NON-NLS-2$ |
| 32 | |
| 33 | /** |
| 34 | * Gets the extension of TSV |
| 35 | * |
| 36 | * @return the extension of TSV |
| 37 | */ |
| 38 | protected String[] getExtension() { |
| 39 | return EXTENSTIONS; |
| 40 | } |
| 41 | |
| 42 | @Override |
| 43 | public String getText() { |
| 44 | return String.valueOf(Messages.AbstractSegmentStoreTableView_exportToTsv); |
| 45 | } |
| 46 | |
| 47 | @Override |
| 48 | public String getToolTipText() { |
| 49 | return String.valueOf(Messages.ExportToTsvAction_exportToTsvToolTip); |
| 50 | } |
| 51 | |
| 52 | @Override |
| 53 | public void run() { |
| 54 | Shell shell = getShell(); |
| 55 | if (shell == null) { |
| 56 | return; |
| 57 | } |
| 58 | FileDialog fd = new FileDialog(shell); |
| 59 | fd.setFilterExtensions(getExtension()); |
| 60 | String fileName = fd.open(); |
| 61 | if (fileName == null) { |
| 62 | return; |
| 63 | } |
| 64 | try (FileOutputStream fos = new FileOutputStream(fileName)) { |
| 65 | exportToTsv(fos); |
| 66 | } catch (IOException e) { |
| 67 | Activator.getDefault().logError("IO Error " + fileName, e); //$NON-NLS-1$ |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Get the shell to open the file dialog |
| 73 | * |
| 74 | * @return the shell |
| 75 | */ |
| 76 | protected abstract @Nullable Shell getShell(); |
| 77 | |
| 78 | /** |
| 79 | * Export a given items's TSV |
| 80 | * |
| 81 | * @param stream |
| 82 | * an output stream to write the TSV to |
| 83 | */ |
| 84 | protected abstract void exportToTsv(OutputStream stream); |
| 85 | } |