1 /* 2 * Copyright (C) 2012 The Android Open Source Project 3 * 4 * Licensed under the Eclipse Public License, Version 1.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.eclipse.org/org/documents/epl-v10.php 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 package com.android.ide.eclipse.adt.internal.editors.layout.gle2; 17 18 import static com.android.SdkConstants.DOT_PNG; 19 20 import com.android.ide.eclipse.adt.AdtPlugin; 21 22 import org.eclipse.jface.action.Action; 23 import org.eclipse.jface.dialogs.MessageDialog; 24 import org.eclipse.swt.SWT; 25 import org.eclipse.swt.widgets.FileDialog; 26 import org.eclipse.swt.widgets.Shell; 27 28 import java.awt.image.BufferedImage; 29 import java.io.File; 30 import java.io.IOException; 31 32 import javax.imageio.ImageIO; 33 34 /** Saves the current layout editor's rendered image to disk */ 35 class ExportScreenshotAction extends Action { 36 private final LayoutCanvas mCanvas; 37 38 ExportScreenshotAction(LayoutCanvas canvas) { 39 super("Export Screenshot..."); 40 mCanvas = canvas; 41 } 42 43 @Override 44 public void run() { 45 Shell shell = AdtPlugin.getShell(); 46 47 ImageOverlay imageOverlay = mCanvas.getImageOverlay(); 48 BufferedImage image = imageOverlay.getAwtImage(); 49 if (image != null) { 50 FileDialog dialog = new FileDialog(shell, SWT.SAVE); 51 dialog.setFilterExtensions(new String[] { "*.png" }); //$NON-NLS-1$ 52 String path = dialog.open(); 53 if (path != null) { 54 if (!path.endsWith(DOT_PNG)) { 55 path = path + DOT_PNG; 56 } 57 File file = new File(path); 58 if (file.exists()) { 59 MessageDialog d = new MessageDialog(null, "File Already Exists", null, 60 String.format( 61 "%1$s already exists.\nWould you like to replace it?", 62 path), 63 MessageDialog.QUESTION, new String[] { 64 // Yes will be moved to the end because it's the default 65 "Yes", "No" 66 }, 0); 67 int result = d.open(); 68 if (result != 0) { 69 return; 70 } 71 } 72 try { 73 ImageIO.write(image, "PNG", file); //$NON-NLS-1$ 74 } catch (IOException e) { 75 AdtPlugin.log(e, null); 76 } 77 } 78 } else { 79 MessageDialog.openError(shell, "Error", "Image not available"); 80 } 81 } 82 } 83