BufferedCanvas.java
Go to the documentation of this file.00001 package edu.rice.cs.hpc.traceviewer.painter;
00002
00003 import org.eclipse.jface.action.Action;
00004 import org.eclipse.jface.action.MenuManager;
00005 import org.eclipse.swt.SWT;
00006 import org.eclipse.swt.events.DisposeEvent;
00007 import org.eclipse.swt.events.DisposeListener;
00008 import org.eclipse.swt.events.PaintEvent;
00009 import org.eclipse.swt.events.PaintListener;
00010 import org.eclipse.swt.graphics.Image;
00011 import org.eclipse.swt.graphics.ImageData;
00012 import org.eclipse.swt.graphics.ImageLoader;
00013 import org.eclipse.swt.graphics.Rectangle;
00014 import org.eclipse.swt.widgets.Canvas;
00015 import org.eclipse.swt.widgets.Composite;
00016 import org.eclipse.swt.widgets.FileDialog;
00017 import org.eclipse.swt.widgets.Menu;
00018
00019
00020
00021
00022
00023
00024
00025
00026 public abstract class BufferedCanvas extends Canvas
00027 implements PaintListener, DisposeListener
00028 {
00029
00030
00031 protected Image imageBuffer;
00032
00033 public BufferedCanvas(Composite parent)
00034 {
00035 super(parent, SWT.NO_BACKGROUND);
00036 addPaintListener(this);
00037 addDisposeListener(this);
00038
00039 setContextMenus();
00040 }
00041
00042 @Override
00043
00044
00045
00046
00047 public void paintControl(PaintEvent e)
00048 {
00049 if (imageBuffer != null)
00050 {
00051 final Rectangle area = getClientArea();
00052 final Rectangle rect = imageBuffer.getBounds();
00053
00054 try {
00055
00056 e.gc.drawImage(imageBuffer, 0, 0, rect.width, rect.height,
00057 0, 0, area.width, area.height);
00058 }
00059 catch (Exception ex)
00060 {
00061
00062
00063
00064
00065
00066 return;
00067 }
00068 }
00069 }
00070
00071
00072
00073
00074 protected void initBuffer()
00075 {
00076 if (imageBuffer != null && !imageBuffer.isDisposed())
00077 {
00078 imageBuffer.dispose();
00079 }
00080 imageBuffer = null;
00081 }
00082
00083 protected void setBuffer(Image buffer)
00084 {
00085 this.imageBuffer = buffer;
00086 }
00087
00088 protected Image getBuffer()
00089 {
00090 return imageBuffer;
00091 }
00092
00093
00094 @Override
00095
00096
00097
00098
00099 public void widgetDisposed(DisposeEvent e)
00100 {
00101 if (imageBuffer != null)
00102 imageBuffer.dispose();
00103 }
00104
00105
00106
00107
00108 private void setContextMenus() {
00109
00110 final Action saveImage = new Action("Save image ...") {
00111
00112 public void run() {
00113
00114 if (imageBuffer == null)
00115 return;
00116
00117 FileDialog dialog = new FileDialog(getShell(), SWT.SAVE);
00118 dialog.setText("Save image ... ");
00119 dialog.setFilterExtensions(new String[] {"*.png"});
00120 String filename = dialog.open();
00121 if (filename == null) {
00122 return;
00123 }
00124
00125
00126 ImageData data = BufferedCanvas.this.imageBuffer.getImageData();
00127 ImageLoader loader = new ImageLoader();
00128 loader.data = new ImageData[] {data};
00129
00130
00131 loader.save(filename, SWT.IMAGE_PNG);
00132 }
00133 };
00134
00135
00136 MenuManager mnuMgr = new MenuManager();
00137 Menu menu = mnuMgr.createContextMenu(this);
00138 mnuMgr.add(saveImage);
00139
00140 setMenu(menu);
00141 }
00142 }