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