/*******************************************************************************
 * Copyright (c) 2010 Oak Ridge National Laboratory.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 ******************************************************************************/
package org.csstudio.email.ui;

import org.csstudio.email.Preferences;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.swt.widgets.Shell;

/** Base class for Action that sends an email
 *  @author Kay Kasemir
 */
abstract public class AbstractSendEMailAction extends Action
{
    final protected Shell shell;
    final private String from, subject;
    private String body;
    
    /** Initialize with body, awaiting image
     *  @param shell
     *  @param from
     *  @param subject
     *  @param body
     */
    public AbstractSendEMailAction(final Shell shell, final String from,
            final String subject,
            final String body)
    {
        super(Messages.SendEmail,
              Activator.getImageDescriptor("icons/mail-send-16.png")); //$NON-NLS-1$
        this.shell = shell;
        this.from = from;
        this.subject = subject;
        this.body = body;
    }

    /** Initialize with body, awaiting body and optional image
     *  @param shell
     *  @param from
     *  @param subject
     */
    public AbstractSendEMailAction(final Shell shell, final String from,
            final String subject)
    {
        super(Messages.SendEmail,
              Activator.getImageDescriptor("icons/mail-send-16.png")); //$NON-NLS-1$
        this.shell = shell;
        this.from = from;
        this.subject = subject;
        this.body = null;
    }

    /** {@inheritDoc} */
    @Override
    public void run()
    {
    	if (body == null)
    		body = getBody();
    	if (body == null)
    		body = ""; //$NON-NLS-1$
        final String image_filename = getImage();
        
        final Dialog dlg;
        if (image_filename == null)
        	dlg = new EMailSenderDialog(shell, Preferences.getSMTP_Host(), from,
                    Messages.DefaultDestination, subject, body);
        else
        	dlg = new EMailSenderDialog(shell, Preferences.getSMTP_Host(), from,
                Messages.DefaultDestination, subject, body, image_filename);
        dlg.open();
    }

    /** To override by implementations that use the constructor
     *  without body parameter
     *  @return Text for body of email
     */
    public String getBody()
    {
	    return null;
    }

	/** @return Image attachment. May return <code>null</code> for 'no image' */
    abstract protected String getImage();
}

@@ -606,16 +606,56 @@
 Dialog;%0A
+import org.eclipse.swt.widgets.Display;%0A
 import o
@@ -2090,32 +2090,794 @@
 ; //$NON-NLS-1$%0A
+%0A    %09// This action might be invoked from a context menu. In principle, RCP%0A    %09// closes the context menu before invoking this action.%0A        // Tools that need to implement getImage() by taking a screenshot thus capture the original display,%0A    %09// without the context menu.%0A    %09// On Linux (X11, GTK), however, the context menu is still visible.%0A    %09// Presumably, the X11 display update queue is not 'flushed'?%0A    %09// By delaying the getImage() call into another Runnable, the context menu%0A    %09// was successfully closed in tests on Linux.%0A    %09final Display display = shell == null ? Display.getCurrent() : shell.getDisplay();%0A    %09display.asyncExec(new Runnable()%0A        %7B%0A            @Override%0A            public void run()%0A            %7B%0A        
         final St
@@ -2914,25 +2914,41 @@
 ();%0A        
+   
-%0A
+     %0A        
         fina
@@ -2969,16 +2969,24 @@
         
+ 
+       
 if (imag
@@ -3005,33 +3005,44 @@
 = null)%0A        
+       
-%09
+     
 dlg = new EMailS
@@ -3106,32 +3106,40 @@
                 
+        
 Messages.Default
@@ -3179,21 +3179,34 @@
     
-else%0A
+        else%0A     
         
 %09dlg
@@ -3201,17 +3201,23 @@
         
-%09
+       
 dlg = ne
@@ -3285,32 +3285,44 @@
                 
+      
+      
 Messages.Default
@@ -3374,16 +3374,24 @@
         
+       
+ 
 dlg.open
@@ -3390,24 +3390,50 @@
 dlg.open();%0A
+            %7D%0A        %7D);%0A
     %7D%0A%0A    /

Move image snapshot and dialog in another UI Runnable