Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 9371aa1630402a326d3efa45838bffc4762817ec (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/*******************************************************************************
 * Copyright (c) 2013, 2018 Red Hat, Inc.
 *
 * This program and the accompanying materials are made
 * available under the terms of the Eclipse Public License 2.0
 * which is available at https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *      Red Hat - initial API and implementation
 *******************************************************************************/

package org.eclipse.linuxtools.internal.systemtap.ui.ide.handlers;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.util.Arrays;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.linuxtools.internal.systemtap.ui.ide.IDEPerspective;
import org.eclipse.linuxtools.systemtap.graphing.core.datasets.IFilteredDataSet;
import org.eclipse.linuxtools.systemtap.graphing.core.datasets.row.FilteredRowDataSet;
import org.eclipse.linuxtools.systemtap.graphing.core.datasets.row.RowDataSet;
import org.eclipse.linuxtools.systemtap.graphing.core.datasets.table.FilteredTableDataSet;
import org.eclipse.linuxtools.systemtap.graphing.core.datasets.table.TableDataSet;
import org.eclipse.linuxtools.systemtap.graphing.ui.views.GraphSelectorEditor;
import org.eclipse.linuxtools.systemtap.graphing.ui.views.GraphSelectorEditorInput;
import org.eclipse.linuxtools.systemtap.graphing.ui.widgets.ExceptionErrorDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.WorkbenchException;

/**
 * This handler imports data from an external file to populate
 * a {@link GraphSelectorEditor}.
 * into an external file, which can be imported back in later.
 */
public class ImportDataSetHandler extends AbstractHandler {

    @Override
    public Object execute(ExecutionEvent event) {
        FileDialog dialog = new FileDialog(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), SWT.OPEN);
        dialog.setFilterExtensions(new String[]{Messages.DataSetFileExtension});
        dialog.setText(Messages.ImportDataSetAction_DialogTitle);
        String path = dialog.open();
        if (path != null) {
            execute(path);
        }
        return null;
    }

    /**
     * Import a data set from the specified path.
     * @param path The path of the data set to import.
     */
    public void execute(String path) {
        IFilteredDataSet dataset = null;
        File file = new File(path);
        try (InputStreamReader fr = new InputStreamReader(new FileInputStream(file), Charset.defaultCharset());
                BufferedReader br = new BufferedReader(fr)) {
            String id = br.readLine();
            String[] titles = br.readLine().split(", "); //$NON-NLS-1$

            if (id == null && titles == null) {
                throw new IOException();
            } else if (id.equals(RowDataSet.ID)) {
                dataset = new FilteredRowDataSet(titles);
            } else if (id.equals(TableDataSet.ID)) {
                dataset = new FilteredTableDataSet(titles);
            } else {
                throw new IOException();
            }
            dataset.readFromFile(file);

            String title = path.substring(path.lastIndexOf('/')+1);
            IWorkbenchPage p = PlatformUI.getWorkbench().showPerspective(IDEPerspective.ID, PlatformUI.getWorkbench().getActiveWorkbenchWindow());
            GraphSelectorEditor ivp = (GraphSelectorEditor)p.openEditor(new GraphSelectorEditorInput(title), GraphSelectorEditor.ID);
            ivp.createScriptSets(path, Arrays.asList(title), Arrays.asList(dataset));
        } catch (FileNotFoundException fnfe) {
            ExceptionErrorDialog.openError(Messages.ImportDataSetAction_FileNotFound, fnfe);
        } catch (IOException ioe) {
            ExceptionErrorDialog.openError(Messages.ImportDataSetAction_FileInvalid, ioe);
        } catch (WorkbenchException we) {
            ExceptionErrorDialog.openError(Messages.RunScriptChartHandler_couldNotSwitchToGraphicPerspective, we);
        }
    }

}

Back to the top