~ubuntu-branches/ubuntu/wily/eclipse-linuxtools/wily

« back to all changes in this revision

Viewing changes to systemtap/org.eclipse.linuxtools.systemtap.ui.graphing/src/org/eclipse/linuxtools/internal/systemtap/ui/graphing/actions/SaveGraphImageAction.java

  • Committer: Package Import Robot
  • Author(s): Jakub Adam, Jakub Adam, tony mancill
  • Date: 2014-10-11 11:44:05 UTC
  • mfrom: (1.2.4)
  • Revision ID: package-import@ubuntu.com-20141011114405-yazjvxfzzhmi5sgj
Tags: 3.1.0-1
[ Jakub Adam ]
* New upstream release (Closes: #761524).
* Refreshed d/patches.
* Don't build removed feature org.eclipse.linuxtools.tools.launch
  - merged into org.eclipse.linuxtools.profiling.
* Use javac target 1.7.
* Build new feature org.eclipse.linuxtools.dataviewers.feature
  - required by Valgrind integration.
* Build-depend on eclipse-remote-services-api and eclipse-cdt-autotools.
* Bump Standards-Version to 3.9.6.
* Override incompatible-java-bytecode-format - linuxtools needs Java 7.
* Remove unused codeless-jar override.

[ tony mancill ]
* Tweak short package description to make lintian happy.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*******************************************************************************
2
 
 * Copyright (c) 2006 IBM Corporation.
3
 
 * All rights reserved. This program and the accompanying materials
4
 
 * are made available under the terms of the Eclipse Public License v1.0
5
 
 * which accompanies this distribution, and is available at
6
 
 * http://www.eclipse.org/legal/epl-v10.html
7
 
 *
8
 
 * Contributors:
9
 
 *     IBM Corporation - Jeff Briggs, Henry Hughes, Ryan Morse
10
 
 *******************************************************************************/
11
 
 
12
 
package org.eclipse.linuxtools.internal.systemtap.ui.graphing.actions;
13
 
 
14
 
import org.eclipse.jface.action.Action;
15
 
import org.eclipse.jface.action.IAction;
16
 
import org.eclipse.jface.dialogs.MessageDialog;
17
 
import org.eclipse.jface.viewers.ISelection;
18
 
import org.eclipse.linuxtools.internal.systemtap.ui.graphing.Localization;
19
 
import org.eclipse.linuxtools.systemtap.graphingapi.ui.charts.AbstractChartBuilder;
20
 
import org.eclipse.linuxtools.systemtap.graphingapi.ui.widgets.ExceptionErrorDialog;
21
 
import org.eclipse.linuxtools.systemtap.structures.listeners.ITabListener;
22
 
import org.eclipse.linuxtools.systemtap.ui.graphing.GraphDisplaySet;
23
 
import org.eclipse.linuxtools.systemtap.ui.graphing.views.GraphSelectorEditor;
24
 
import org.eclipse.swt.SWT;
25
 
import org.eclipse.swt.SWTException;
26
 
import org.eclipse.swt.graphics.GC;
27
 
import org.eclipse.swt.graphics.Image;
28
 
import org.eclipse.swt.graphics.ImageData;
29
 
import org.eclipse.swt.graphics.ImageLoader;
30
 
import org.eclipse.swt.widgets.FileDialog;
31
 
import org.eclipse.ui.IViewPart;
32
 
import org.eclipse.ui.IWorkbenchWindow;
33
 
import org.eclipse.ui.IWorkbenchWindowActionDelegate;
34
 
import org.eclipse.ui.PlatformUI;
35
 
 
36
 
/**
37
 
 * This action is designed to allow for saving of the graph in the active window.
38
 
 * It will let the user select the location to save the image, and then save it as
39
 
 * a jpg image.
40
 
 * @author Ryan Morse
41
 
 */
42
 
public class SaveGraphImageAction extends Action implements IWorkbenchWindowActionDelegate {
43
 
        @Override
44
 
        public void init(IWorkbenchWindow window) {
45
 
                fWindow = window;
46
 
        }
47
 
 
48
 
        /**
49
 
         * This is the main method of the action.  It handles getting the active graph,
50
 
         * prompting the user for a location to save the image to, and then actually doing
51
 
         * the save.
52
 
         * @param act The action that fired this method.
53
 
         */
54
 
        @Override
55
 
        public void run(IAction act) {
56
 
                AbstractChartBuilder g = getGraph();
57
 
                try {
58
 
                        PlatformUI.getWorkbench().getDisplay().update();
59
 
                } catch(SWTException swte) {
60
 
                        ExceptionErrorDialog.openError(Localization.getString("SaveGraphImageAction.UnableToSaveGraph"), swte); //$NON-NLS-1$
61
 
                }
62
 
                if(null == g) {
63
 
                        displayError(Localization.getString("SaveGraphImageAction.CanNotGetGraph")); //$NON-NLS-1$
64
 
                        return;
65
 
                }
66
 
 
67
 
                ImageData image = getImage(g);
68
 
                if(null == image) {
69
 
                        displayError(Localization.getString("SaveGraphImageAction.CanNotCreateImage")); //$NON-NLS-1$
70
 
                        return;
71
 
                }
72
 
 
73
 
                String path = getFile();
74
 
                if(null == path) {
75
 
                        return;
76
 
                }
77
 
 
78
 
                save(image, path);
79
 
        }
80
 
 
81
 
        /**
82
 
         * This method retreives the active graph from the GraphSelectorView.  If no
83
 
         * graph is active it will return null.
84
 
         * @return The IGraph in tha active display set.
85
 
         */
86
 
        public AbstractChartBuilder getGraph() {
87
 
                IViewPart ivp = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().findView(GraphSelectorEditor.ID);
88
 
                AbstractChartBuilder g = null;
89
 
 
90
 
                GraphDisplaySet gds = ((GraphSelectorEditor)ivp).getActiveDisplaySet();
91
 
                if(null != gds) {
92
 
                        g = gds.getActiveGraph();
93
 
                }
94
 
                return g;
95
 
        }
96
 
 
97
 
        /**
98
 
         * This method converts the Graph into an actual image.
99
 
         * @param canvas The graph that needs to be converted to an image
100
 
         * @return The Image that was generated by the supplied Graph.
101
 
         */
102
 
        public ImageData getImage(AbstractChartBuilder canvas) {
103
 
                GC gc = new GC(canvas);
104
 
                Image image = new Image(canvas.getDisplay(), canvas.getSize().x, canvas.getSize().y);
105
 
                gc.copyArea(image, 0, 0);
106
 
                gc.dispose();
107
 
 
108
 
                ImageData data = image.getImageData();
109
 
                image.dispose();
110
 
                return data;
111
 
        }
112
 
 
113
 
        /**
114
 
         * This method will display a dialog box for the user to select a
115
 
         * location to save the graph image.
116
 
         * @return The String location selected to save the image to.
117
 
         */
118
 
        public String getFile() {
119
 
                FileDialog dialog= new FileDialog(fWindow.getShell(), SWT.SAVE);
120
 
                dialog.setText(Localization.getString("SaveGraphImageAction.NewFile")); //$NON-NLS-1$
121
 
                return dialog.open();
122
 
        }
123
 
 
124
 
        /**
125
 
         * This method will perform the save operation to store the generated
126
 
         * image as an image file on the computer
127
 
         * @param image The image data generated from the graph
128
 
         * @param path The location to create the new file in and save to.
129
 
         */
130
 
        public void save(ImageData image, String path) {
131
 
                ImageLoader loader = new ImageLoader();
132
 
                loader.data = new ImageData[] {image};
133
 
                loader.save(path, SWT.IMAGE_JPEG);
134
 
        }
135
 
 
136
 
        /**
137
 
         * This method will display the error message to the user in the case
138
 
         * that something went wrong.
139
 
         * @param message The message that should be shown in the error dialog.
140
 
         */
141
 
        private void displayError(String message) {
142
 
                MessageDialog.openWarning(fWindow.getShell(), Localization.getString("SaveGraphImageAction.Problem"), message); //$NON-NLS-1$
143
 
        }
144
 
 
145
 
        /**
146
 
         * This method is used to generate the checks to see it this button
147
 
         * should be enabled or not.
148
 
         */
149
 
        @Override
150
 
        public void selectionChanged(IAction a, ISelection s) {
151
 
                action = a;
152
 
                action.setEnabled(false);
153
 
                buildEnablementChecks();
154
 
        }
155
 
 
156
 
        /**
157
 
         * This method is used to generate the checks to see it this button
158
 
         * should be enabled or not.
159
 
         */
160
 
        private void buildEnablementChecks() {
161
 
                IViewPart ivp = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().findView(GraphSelectorEditor.ID);
162
 
                if(null != ivp) {
163
 
                        final GraphSelectorEditor gsv = (GraphSelectorEditor)ivp;
164
 
                        gsv.addTabListener(new ITabListener() {
165
 
                                @Override
166
 
                                public void tabClosed() {
167
 
                                        if(null == gsv.getActiveDisplaySet() || null == gsv.getActiveDisplaySet().getActiveGraph()) {
168
 
                                                action.setEnabled(false);
169
 
                                        }
170
 
                                }
171
 
 
172
 
                                @Override
173
 
                                public void tabOpened() {
174
 
                                        gsv.getActiveDisplaySet().addTabListener(new ITabListener() {
175
 
                                                @Override
176
 
                                                public void tabClosed() {
177
 
                                                        if(null == gsv.getActiveDisplaySet().getActiveGraph()) {
178
 
                                                                action.setEnabled(false);
179
 
                                                        }
180
 
                                                }
181
 
 
182
 
                                                @Override
183
 
                                                public void tabOpened() {
184
 
                                                        if(null != gsv.getActiveDisplaySet().getActiveGraph()) {
185
 
                                                                action.setEnabled(true);
186
 
                                                        }
187
 
                                                }
188
 
 
189
 
                                                @Override
190
 
                                                public void tabChanged() {
191
 
                                                        if(null == gsv.getActiveDisplaySet() || null == gsv.getActiveDisplaySet().getActiveGraph()) {
192
 
                                                                action.setEnabled(false);
193
 
                                                        } else {
194
 
                                                                action.setEnabled(true);
195
 
                                                        }
196
 
                                                }
197
 
                                        });
198
 
                                }
199
 
 
200
 
                                @Override
201
 
                                public void tabChanged() {
202
 
                                        if(null == gsv.getActiveDisplaySet() || null == gsv.getActiveDisplaySet().getActiveGraph()) {
203
 
                                                action.setEnabled(false);
204
 
                                        } else {
205
 
                                                action.setEnabled(true);
206
 
                                        }
207
 
                                }
208
 
                        });
209
 
                }
210
 
        }
211
 
 
212
 
        /**
213
 
         * Removes all internal references in this class.  Nothing should make any references
214
 
         * to anyting in this class after calling the dispose method.
215
 
         */
216
 
        @Override
217
 
        public void dispose() {
218
 
                fWindow = null;
219
 
                action = null;
220
 
        }
221
 
 
222
 
        private IWorkbenchWindow fWindow;
223
 
        private IAction action;
224
 
}