jaxx.runtime.swing.application.ActionWorker Maven / Gradle / Ivy
/*
* #%L
* JAXX :: Runtime
*
* $Id: ActionWorker.java 2382 2012-07-05 14:56:58Z tchemit $
* $HeadURL: http://svn.nuiton.org/svn/jaxx/tags/jaxx-2.5.19/jaxx-runtime/src/main/java/jaxx/runtime/swing/application/ActionWorker.java $
* %%
* Copyright (C) 2008 - 2010 CodeLutin
* %%
* 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%
*/
package jaxx.runtime.swing.application;
import com.google.common.base.Preconditions;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.nuiton.util.StringUtil;
import javax.swing.SwingWorker;
import java.util.concurrent.Callable;
/**
* Action worker to execute a incoming action.
*
* @author tchemit
* @since 2.1
*/
public class ActionWorker extends SwingWorker {
/** Logger */
private static final Log log = LogFactory.getLog(ActionWorker.class);
protected final String actionLabel;
protected Callable target;
protected ActionStatus status;
protected Exception error;
protected long startTime;
protected long endTime;
public ActionWorker(String actionLabel) {
this.actionLabel = actionLabel;
}
public ActionWorker(String actionLabel, Runnable target) {
this.target = new RunnableBridge(target);
this.actionLabel = actionLabel;
}
public Callable getTarget() {
return target;
}
public void setTarget(Runnable target) {
setTarget(new RunnableBridge(target));
}
/**
* Set the target to execute.
*
* @param target the target to execute
* @since 2.5.1
*/
public void setTarget(Callable target) {
this.target = target;
}
@Override
protected R doInBackground() throws Exception {
Preconditions.checkNotNull(target != null, "Targe field can not be null here.");
startTime = System.nanoTime();
if (log.isDebugEnabled()) {
log.debug("Action [" + getActionLabel() + "] is starting...");
}
R result = null;
try {
result = getTarget().call();
} catch (Exception e) {
error = e;
} finally {
if (log.isDebugEnabled()) {
log.debug("Action [" + getActionLabel() + "] is ending...");
}
}
return result;
}
public boolean isFailed() {
return (isDone() || isCancelled()) && error != null;
}
public Exception getError() {
return error;
}
public ActionStatus getStatus() {
return status;
}
public String getActionLabel() {
return actionLabel;
}
@Override
protected void done() {
super.done();
endTime = System.nanoTime();
if (error != null) {
status = ActionStatus.FAIL;
} else if (isCancelled()) {
status = ActionStatus.CANCEL;
} else {
status = ActionStatus.OK;
}
if (log.isDebugEnabled()) {
log.debug("Action [" + getActionLabel() + "] ends with status : " + status + " in " + getTime());
}
}
public String getTime() {
return StringUtil.convertTime(endTime - startTime);
}
public long getStartTime() {
return startTime;
}
public long getEndTime() {
return endTime;
}
/** State of a running action */
public enum ActionStatus {
OK,
CANCEL,
FAIL
}
/**
* Transform a {@link Runnable} into a {@link Callable}.
*
* @param type of return (used to maintain generic checks in this class).
* @since 2.5.1
*/
private static class RunnableBridge implements Callable {
private final Runnable target;
public RunnableBridge(Runnable target) {
this.target = target;
}
@Override
public R call() throws Exception {
target.run();
return null;
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy