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

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

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.AbstractApplicationUIHandler;

import javax.swing.AbstractButton;

/**
 * To create and consume {@link AbstractApplicationAction}.
 * Created on 11/24/13.
 *
 * @author Tony Chemit - [email protected]
 * @since 2.8
 */
public class ApplicationActionEngine {

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

    private final ApplicationActionFactory actionFactory;

    public ApplicationActionEngine(ApplicationActionFactory actionFactory) {
        this.actionFactory = actionFactory;
    }

    public  void runInternalAction(A action) {
        Throwable error = null;

        try {
            action.doAction();
            action.postSuccessAction();
        } catch (Throwable e) {
            error = e;
            if (log.isErrorEnabled()) {
                log.error("Error in action:", e);
            }
            throw ApplicationActionException.propagateError(action, e);
        } finally {
            try {
                if (error != null) {
                    action.postFailedAction(error);
                }
            } finally {

                action.releaseAction();
            }
        }
    }

    public  void runFullInternalAction(A action) {
        Throwable error = null;

        try {
            boolean doAction = action.prepareAction();
            if (doAction) {
                action.doAction();
                action.postSuccessAction();
            }
        } catch (Throwable e) {
            error = e;
            if (log.isErrorEnabled()) {
                log.error("Error in action:", e);
            }
            throw ApplicationActionException.propagateError(action, e);
        } finally {
            try {
                if (error != null) {
                    action.postFailedAction(error);
                }
            } finally {

                action.releaseAction();
            }
        }
    }

    public  void runInternalAction(AbstractApplicationUIHandler handler,
                                                                        Class actionName) {
        A action = actionFactory.createLogicAction(handler, actionName);
        runInternalAction(action);
    }

    public  void runAction(A action) {
        ApplicationUIAction uiAction = actionFactory.createUIAction(null, action);
        uiAction.actionPerformed(null);
    }

    public void runAction(AbstractButton button) {
        button.getAction().actionPerformed(null);
    }

    public  void runActionAndWait(A action) {
        ApplicationUIAction uiAction = actionFactory.createUIAction(null, action);
        uiAction.launchActionAndWait();
    }

    public  void runActionAndWait(AbstractApplicationUIHandler handler,
                                                                       Class actionName) {
        A logicAction = actionFactory.createLogicAction(handler, actionName);
        runActionAndWait(logicAction);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy