All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.nuiton.jaxx.application.swing.action.ApplicationUIAction Maven / Gradle / Ivy

There is a newer version: 3.0-alpha-1
Show newest version
package org.nuiton.jaxx.application.swing.action;

/*
 * #%L
 * JAXX :: Application Swing
 * %%
 * Copyright (C) 2008 - 2014 Code Lutin, Tony Chemit
 * %%
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as 
 * published by the Free Software Foundation, either version 3 of the 
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Lesser Public License for more details.
 * 
 * You should have received a copy of the GNU General Lesser Public 
 * License along with this program.  If not, see
 * .
 * #L%
 */

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.nuiton.jaxx.application.swing.ApplicationUIContext;
import org.nuiton.util.TimeLog;

import javax.swing.AbstractAction;
import javax.swing.AbstractButton;
import javax.swing.Icon;
import javax.swing.SwingUtilities;
import java.awt.event.ActionEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.concurrent.CancellationException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * Abstract tutti ui action which launch a {@link AbstractApplicationAction}.
 *
 * @author Tony Chemit - [email protected]
 * @since 2.8.2
 */
public class ApplicationUIAction extends AbstractAction {

    private static final long serialVersionUID = 1L;

    /** Logger. */
    private static final Log log = LogFactory.getLog(ApplicationUIAction.class);

    private static final TimeLog TIME_LOG = new TimeLog(ApplicationUIAction.class);

    public static final ExecutorService waitingThread =
            Executors.newSingleThreadExecutor();

    private static final String LOGIC_ACTION = "logicAction";

    private final Object lock = new Object();

    private boolean wait;

    private long t0;

    public ApplicationUIAction(final AbstractButton button, A action) {

        putValue(LOGIC_ACTION, action);

        // fill the ui action from the button
        setActionKey(action.getClass().getName());
        if (button != null) {

            setActionIcon(button.getIcon());
            setActionName(button.getText());
            setActionDescription(button.getToolTipText());
            setActionMnemonic(button.getMnemonic());
            setEnabled(button.isEnabled());

            // see https://forge.nuiton.org/issues/3525
            button.addPropertyChangeListener(new PropertyChangeListener() {
                @Override
                public void propertyChange(PropertyChangeEvent evt) {
                    setEnabled(button.isEnabled());
                }
            });

        }
    }

    public void launchActionAndWait() {
        wait = true;
        actionPerformed(null);
        lock();
    }


    @Override
    public final void actionPerformed(final ActionEvent event) {

        t0 = TimeLog.getTime();

        if (log.isInfoEnabled()) {
            log.info("Task [" + getLogicAction().getClass().getSimpleName() + "] starting");
        }

        // prepare action
        boolean doAction;

        A action = getLogicAction();

        ApplicationUIContext actionContext = action.getContext();
        if (actionContext.isActionInProgress(this)) {
            if (log.isInfoEnabled()) {
                log.info("Task [" + getLogicAction().getClass().getSimpleName() + "] stopped: action already in progress");
            }
            return;
        }

        actionContext.setActionInProgress(this, true);

        // reset status message
        action.sendMessage("");

        try {
            doAction = action.prepareAction();
        } catch (Exception e) {
            action.releaseAction();
            actionContext.setActionInProgress(this, false);
            throw ApplicationActionException.propagateError(action, e);
        }

        if (doAction) {

            final ApplicationActionSwingWorker worker =
                    new ApplicationActionSwingWorker(action);

            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {

                    // make ui busy
                    worker.updateBusyState(true);

                }
            });

            if (log.isDebugEnabled()) {
                log.debug("Before execute of action " + action);
            }

            // perform and release action
            worker.execute();

            // wait until action is done
            waitingThread.execute(
                    new Runnable() {
                        @Override
                        public void run() {

                            A action = getLogicAction();
                            try {
                                try {
                                    worker.get();
                                } catch (ExecutionException e) {
                                    // don't care .
                                } catch (CancellationException e) {
                                    // dont care ?
                                } catch (InterruptedException e) {
                                    // don't care ?
                                }
                                if (log.isDebugEnabled()) {
                                    log.debug("After execute of action " + action + " (worker done? " + worker.isDone() + ")");
                                }

                                if (worker.isFailed()) {

                                    throw ApplicationActionException.propagateError(action, worker.getError());
                                }
                            } finally {
                                unlock();
                            }
                        }
                    }
            );

        } else {

            try {
                // release action
                action.releaseAction();

            } finally {
                unlock();
            }
        }
    }

    public void setActionIcon(Icon actionIcon) {
        putValue(SMALL_ICON, actionIcon);
        putValue(LARGE_ICON_KEY, actionIcon);
    }

    public void setActionKey(String actionKey) {
        putValue(ACTION_COMMAND_KEY, actionKey);
    }

    public void setActionName(String actionName) {
        putValue(NAME, actionName);
    }

    public void setActionDescription(String actionDescription) {
        putValue(SHORT_DESCRIPTION, actionDescription);
        getLogicAction().setActionDescription(actionDescription);
    }

    public void setActionMnemonic(int key) {
        putValue(MNEMONIC_KEY, key);
    }

    public A getLogicAction() {
        return (A) getValue(LOGIC_ACTION);
    }

    protected void lock() {
        if (wait) {
            synchronized (lock) {
                try {

                    lock.wait();

                } catch (InterruptedException e) {
                    throw ApplicationActionException.propagateError(getLogicAction(), e);
                } finally {
                    wait = false;
                }
            }
        }
    }

    protected void unlock() {
        TIME_LOG.log(t0, "Task [" + getLogicAction().getClass().getSimpleName() + "] End");
        if (wait) {
            synchronized (lock) {
                lock.notifyAll();
            }
        }
        getLogicAction().getContext().setActionInProgress(this, false);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy