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
9
* IBM Corporation - Jeff Briggs, Henry Hughes, Ryan Morse
10
*******************************************************************************/
12
package org.eclipse.linuxtools.internal.systemtap.ui.graphing.actions;
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;
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
42
public class SaveGraphImageAction extends Action implements IWorkbenchWindowActionDelegate {
44
public void init(IWorkbenchWindow window) {
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
52
* @param act The action that fired this method.
55
public void run(IAction act) {
56
AbstractChartBuilder g = getGraph();
58
PlatformUI.getWorkbench().getDisplay().update();
59
} catch(SWTException swte) {
60
ExceptionErrorDialog.openError(Localization.getString("SaveGraphImageAction.UnableToSaveGraph"), swte); //$NON-NLS-1$
63
displayError(Localization.getString("SaveGraphImageAction.CanNotGetGraph")); //$NON-NLS-1$
67
ImageData image = getImage(g);
69
displayError(Localization.getString("SaveGraphImageAction.CanNotCreateImage")); //$NON-NLS-1$
73
String path = getFile();
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.
86
public AbstractChartBuilder getGraph() {
87
IViewPart ivp = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().findView(GraphSelectorEditor.ID);
88
AbstractChartBuilder g = null;
90
GraphDisplaySet gds = ((GraphSelectorEditor)ivp).getActiveDisplaySet();
92
g = gds.getActiveGraph();
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.
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);
108
ImageData data = image.getImageData();
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.
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();
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.
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);
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.
141
private void displayError(String message) {
142
MessageDialog.openWarning(fWindow.getShell(), Localization.getString("SaveGraphImageAction.Problem"), message); //$NON-NLS-1$
146
* This method is used to generate the checks to see it this button
147
* should be enabled or not.
150
public void selectionChanged(IAction a, ISelection s) {
152
action.setEnabled(false);
153
buildEnablementChecks();
157
* This method is used to generate the checks to see it this button
158
* should be enabled or not.
160
private void buildEnablementChecks() {
161
IViewPart ivp = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().findView(GraphSelectorEditor.ID);
163
final GraphSelectorEditor gsv = (GraphSelectorEditor)ivp;
164
gsv.addTabListener(new ITabListener() {
166
public void tabClosed() {
167
if(null == gsv.getActiveDisplaySet() || null == gsv.getActiveDisplaySet().getActiveGraph()) {
168
action.setEnabled(false);
173
public void tabOpened() {
174
gsv.getActiveDisplaySet().addTabListener(new ITabListener() {
176
public void tabClosed() {
177
if(null == gsv.getActiveDisplaySet().getActiveGraph()) {
178
action.setEnabled(false);
183
public void tabOpened() {
184
if(null != gsv.getActiveDisplaySet().getActiveGraph()) {
185
action.setEnabled(true);
190
public void tabChanged() {
191
if(null == gsv.getActiveDisplaySet() || null == gsv.getActiveDisplaySet().getActiveGraph()) {
192
action.setEnabled(false);
194
action.setEnabled(true);
201
public void tabChanged() {
202
if(null == gsv.getActiveDisplaySet() || null == gsv.getActiveDisplaySet().getActiveGraph()) {
203
action.setEnabled(false);
205
action.setEnabled(true);
213
* Removes all internal references in this class. Nothing should make any references
214
* to anyting in this class after calling the dispose method.
217
public void dispose() {
222
private IWorkbenchWindow fWindow;
223
private IAction action;