frameworkplugins/com.nokia.s60tools.ui/src/com/nokia/s60tools/ui/actions/CopyImageToClipboardAction.java
changeset 0 61163b28edca
equal deleted inserted replaced
-1:000000000000 0:61163b28edca
       
     1 /*
       
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). 
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:
       
    15 *
       
    16 */ 
       
    17 package com.nokia.s60tools.ui.actions;
       
    18 
       
    19 import org.eclipse.jface.action.IAction;
       
    20 import org.eclipse.swt.dnd.Clipboard;
       
    21 import org.eclipse.swt.dnd.ImageTransfer;
       
    22 import org.eclipse.swt.dnd.Transfer;
       
    23 import org.eclipse.swt.graphics.Drawable;
       
    24 import org.eclipse.swt.graphics.GC;
       
    25 import org.eclipse.swt.graphics.Image;
       
    26 import org.eclipse.swt.widgets.Display;
       
    27 
       
    28 import com.nokia.s60tools.ui.IImageProvider;
       
    29 import com.nokia.s60tools.ui.internal.Messages;
       
    30 
       
    31 /**
       
    32  * Action class for copying Image to clipboard
       
    33  */
       
    34 public class CopyImageToClipboardAction extends S60ToolsBaseAction {
       
    35 	
       
    36 	
       
    37 	/**
       
    38 	 * Image to copy to clipboard
       
    39 	 */
       
    40 	private IImageProvider imageProvider;
       
    41 
       
    42 	/**
       
    43 	 * Constructor
       
    44 	 * @param provider image provider that provides {@link Image} and {@link Drawable}
       
    45 	 * @see com.nokia.s60tools.ui.IImageProvider
       
    46 	 */
       
    47 	public CopyImageToClipboardAction(IImageProvider provider) {
       
    48 		super(Messages.getString("CopyFromTableViewerAction.Copy_Action_Text"), //$NON-NLS-N1
       
    49 				Messages.getString("CopyFromTableViewerAction.Copy_Action_Tooltip"), //$NON-NLS-N1
       
    50 				IAction.AS_PUSH_BUTTON, null);
       
    51 		this.imageProvider = provider;
       
    52 	}
       
    53 	/* (non-Javadoc)
       
    54 	 * @see org.eclipse.jface.action.Action#run()
       
    55 	 */
       
    56 	public void run() {
       
    57 		performCopy();
       
    58 	}	
       
    59 	
       
    60 	/**
       
    61 	 * Performs copy action by copying Image to clipboard.
       
    62 	 */
       
    63 	private void performCopy(){
       
    64 		
       
    65 		//Runnable is used to avoid e.g. pop-up menu to appear in image in case when 
       
    66 		//image is based on screen capture.
       
    67 		Runnable runCopy = new Runnable(){
       
    68 
       
    69 			public void run() {
       
    70 				if (imageProvider != null && imageProvider.getDrawable() != null && imageProvider.getImage() != null) {
       
    71 					GC gc = new GC(imageProvider.getDrawable());
       
    72 					Image image = imageProvider.getImage();
       
    73 					gc.copyArea(image, 0, 0);
       
    74 					gc.dispose();
       
    75 					
       
    76 					Display disp = Display.getCurrent();
       
    77 					Clipboard clipBrd  = new Clipboard(disp);
       
    78 					
       
    79 					ImageTransfer imageTransfer = ImageTransfer.getInstance();
       
    80 					clipBrd.setContents(new Object[]{image.getImageData()}, 
       
    81 							new Transfer[]{imageTransfer});
       
    82 					clipBrd.dispose();
       
    83 				}				
       
    84 			}						
       
    85 		};
       
    86 		
       
    87 		Display.getDefault().asyncExec(runCopy);
       
    88 
       
    89 	}
       
    90 	
       
    91 }