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

org.bidib.wizard.script.AbstractScriptCommand Maven / Gradle / Ivy

There is a newer version: 2.0.0-M1
Show newest version
package org.bidib.wizard.script;

import java.util.Map;

import javax.swing.SwingUtilities;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public abstract class AbstractScriptCommand implements ScriptCommand {
    private static final Logger LOGGER = LoggerFactory.getLogger(AbstractScriptCommand.class);

    private final String key;

    protected String line;

    protected AbstractScriptCommand(String key) {
        this.key = key;
    }

    @Override
    public String getKey() {
        return key;
    }

    @Override
    public void execute(final T scripting, final Map context) {
        LOGGER.info("Execute the command: {}", this);
        try {
            if (SwingUtilities.isEventDispatchThread()) {
                internalExecute(scripting, context);
            }
            else {
                SwingUtilities.invokeAndWait(new Runnable() {

                    @Override
                    public void run() {
                        try {
                            internalExecute(scripting, context);
                        }
                        catch (Exception ex) {
                            LOGGER.warn("Execute command in AWT-thread failed.", ex);
                        }
                    }
                });
            }
        }
        catch (Exception ex) {
            LOGGER.warn("Execute command in AWT-thread failed.", ex);
        }
    }

    protected abstract void internalExecute(T scripting, final Map context);

    @Override
    public String toString() {
        if (StringUtils.isNotBlank(line)) {
            return line;
        }
        return ToStringBuilder.reflectionToString(this);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy