1 /* 2 * Copyright (C) 2012 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.ide.eclipse.gltrace.views; 18 19 import com.android.ide.eclipse.gltrace.widgets.ImageCanvas; 20 21 import org.eclipse.jface.action.Action; 22 import org.eclipse.swt.SWT; 23 import org.eclipse.swt.widgets.FileDialog; 24 import org.eclipse.ui.ISharedImages; 25 import org.eclipse.ui.PlatformUI; 26 27 import java.io.File; 28 29 public class SaveImageAction extends Action { 30 private static String sLastUsedPath; 31 32 private ImageCanvas mImageCanvas; 33 34 public SaveImageAction(ImageCanvas canvas) { 35 super("Save Image", 36 PlatformUI.getWorkbench().getSharedImages().getImageDescriptor( 37 ISharedImages.IMG_ETOOL_SAVEAS_EDIT)); 38 setToolTipText("Save Image"); 39 mImageCanvas = canvas; 40 } 41 42 @Override 43 public void run() { 44 FileDialog fd = new FileDialog(mImageCanvas.getShell(), SWT.SAVE); 45 fd.setFilterExtensions(new String[] { "*.png" }); 46 if (sLastUsedPath != null) { 47 fd.setFilterPath(sLastUsedPath); 48 } 49 50 String path = fd.open(); 51 if (path == null) { 52 return; 53 } 54 55 File f = new File(path); 56 sLastUsedPath = f.getParent(); 57 mImageCanvas.exportImageTo(f); 58 } 59 } 60