/****************************************************************************
 * Copyright (C) 2013 ecsec GmbH.
 * All rights reserved.
 * Contact: ecsec GmbH (info@ecsec.de)
 *
 * This file is part of the Open eCard App.
 *
 * GNU General Public License Usage
 * This file may be used under the terms of the GNU General Public
 * License version 3.0 as published by the Free Software Foundation
 * and appearing in the file LICENSE.GPL included in the packaging of
 * this file. Please review the following information to ensure the
 * GNU General Public License version 3.0 requirements will be met:
 * http://www.gnu.org/copyleft/gpl.html.
 *
 * Other Usage
 * Alternatively, this file may be used in accordance with the terms
 * and conditions contained in a signed written agreement between
 * you and ecsec GmbH.
 *
 ***************************************************************************/

package org.openecard.richclient.gui.manage;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.annotation.Nonnull;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingWorker;
import org.openecard.addon.AddonManager;
import org.openecard.addon.bind.AppExtensionAction;
import org.openecard.addon.manifest.AddonSpecification;
import org.openecard.addon.manifest.AppExtensionSpecification;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


/**
 * Entry for the {@link ActionPanel} representing one action.
 * The action is represented as a button and description.
 *
 * @author Tobias Wich
 */
public class ActionEntryPanel extends JPanel {

    private static final long serialVersionUID = 1L;
    private static final String LANGUAGE_CODE = System.getProperty("user.language");
    private static final Logger logger = LoggerFactory.getLogger(ActionEntryPanel.class);

    protected final JButton actionBtn;

    /**
     * Creates an entry without the actual action added.
     *
     * @param addonSpec Id of the addon this action belongs to.
     * @param actionSpec ActionDescription for which this ActionEntryPanel is constructed.
     * @param manager
     */
    public ActionEntryPanel(@Nonnull AddonSpecification addonSpec, @Nonnull AppExtensionSpecification actionSpec,
	    @Nonnull AddonManager manager) {
	setLayout(new BoxLayout(this, BoxLayout.X_AXIS));

	String name = actionSpec.getLocalizedName(LANGUAGE_CODE);
	String description = actionSpec.getLocalizedDescription(LANGUAGE_CODE);

	actionBtn = new JButton(name);
	add(actionBtn);

	Component rigidArea = Box.createRigidArea(new Dimension(15, 0));
	add(rigidArea);

	JLabel desc = new JLabel(description);
	desc.setFont(desc.getFont().deriveFont(Font.PLAIN));
	add(desc);

	AppExtensionAction action = manager.getAppExtensionAction(addonSpec, actionSpec.getId());
	addAction(action);
    }

    /**
     * Adds an action to the entry.
     *
     * @param action Action to perform when the button is pressed.
     */
    private void addAction(final AppExtensionAction action) {
	actionBtn.addActionListener(new ActionListener() {
	    @Override
	    public void actionPerformed(ActionEvent e) {
		new SwingWorker() {
		    @Override
		    protected Object doInBackground() throws Exception {
			actionBtn.setEnabled(false);
			action.execute();
			actionBtn.setEnabled(true);
			return null;
		    }
		}.execute();
	    }
	});
    }

}

@@ -92,16 +92,21 @@
 (C) 2013
+-2018
  ecsec G
@@ -1060,46 +1060,8 @@
 nt;%0A
-import java.awt.event.ActionListener;%0A
 impo
@@ -1689,16 +1689,22 @@
 %0Apublic 
+final 
 class Ac
@@ -1910,14 +1910,11 @@
 ger 
-logger
+LOG
  = L
@@ -3176,78 +3176,8 @@
 ner(
-new ActionListener() %7B%0A%09    @Override%0A%09    public void actionPerformed
 (Act
@@ -3188,20 +3188,26 @@
 vent e) 
-%7B%0A%09%09
+-%3E %7B%0A%09    
 new Swin
@@ -3217,19 +3217,27 @@
 rker
+%3CVoid, Void%3E
 () %7B%0A%09%09
-    
 @Ove
@@ -3244,20 +3244,16 @@
 rride%0A%09%09
-    
 protecte
@@ -3258,14 +3258,12 @@
 ted 
-Object
+Void
  doI
@@ -3297,17 +3297,20 @@
 ion %7B%0A%09%09
-%09
+    
 actionBt
@@ -3330,16 +3330,28 @@
 false);%0A
+%09%09    try %7B%0A
 %09%09%09actio
@@ -3362,24 +3362,232 @@
 ecute();%0A%09%09%09
+return null;%0A%09%09    %7D catch (Throwable t) %7B%0A%09%09%09// this catch is here just in case anything uncaught is thrown during execute%0A%09%09%09LOG.error(%22Execution ended with an error.%22, t);%0A%09%09%09throw t;%0A%09%09    %7D finally %7B%0A%09%09%09
 actionBtn.se
@@ -3600,32 +3600,16 @@
 ed(true)
-;%0A%09%09%09return null
 ;%0A%09%09    
@@ -3613,16 +3613,23 @@
    %7D%0A%09%09%7D
+%0A%09    %7D
 .execute
@@ -3632,23 +3632,16 @@
 cute();%0A
-%09    %7D%0A
 %09%7D);%0A   

Handle unexcpected exceptions when executing plugin actions