Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 77086f6c736dd6fca1be5c46f488a49d721c9eab (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
/*******************************************************************************
 * Copyright (c) 2014 Red Hat, Inc.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and Action implementation
 *     Red Hat - conversion to Handler implementation
 *******************************************************************************/

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

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.linuxtools.dataviewers.charts.actions.SaveChartAction;
import org.eclipse.linuxtools.systemtap.graphing.ui.GraphDisplaySet;
import org.eclipse.linuxtools.systemtap.graphing.ui.charts.AbstractChartBuilder;
import org.eclipse.linuxtools.systemtap.graphing.ui.views.GraphSelectorEditor;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.PlatformUI;
import org.swtchart.Chart;

/**
 * This action is designed to allow for saving of the graph in the active window.
 * It will let the user select the location to save the image, and then save it as
 * a jpg image.
 * @author Ryan Morse
 * @since 3.0 Migrated from .ui.graphing package.
 */
public class SaveGraphImageHandler extends AbstractHandler {

    private SaveChartAction saveChartAction = new SaveChartAction();

    /**
     * This is the main method of the action.  It handles getting the active graph,
     * prompting the user for a location to save the image to, and then actually doing
     * the save.
     */
    @Override
    public Object execute(ExecutionEvent event) {
        saveChartAction.setChart(getActiveChart());
        saveChartAction.run();
        return null;
    }

    private GraphSelectorEditor getActiveGraphEditor() {
        IEditorPart editor = PlatformUI.getWorkbench().getActiveWorkbenchWindow().
                getActivePage().getActiveEditor();
        return editor instanceof GraphSelectorEditor ? (GraphSelectorEditor) editor : null;
    }

    private AbstractChartBuilder getActiveGraph() {
        GraphSelectorEditor graphEditor = getActiveGraphEditor();
        if (graphEditor == null) {
            return null;
        }
        GraphDisplaySet gds = graphEditor.getActiveDisplaySet();
        return gds == null ? null : gds.getActiveGraph();
    }

    private Chart getActiveChart() {
        AbstractChartBuilder abs = getActiveGraph();
        return abs == null ? null : abs.getChart();
    }

}

Back to the top