Cancel command from initialize method

Hi all,

I am writing a SciJava Command which implements Cancelable and Initializable. I would like to check if a JNI library can be loaded in the initialize() method and if it fails, I want to cancel the command so no window pops up to ask for the input parameters.

Is that possible?

Best,
Debo

Figured it out, my mistake was to not implement isCanceled() correctly.

Here is my solution, preventing the command from being executed if an initial test fails:

public class MyCommand implements
		Command, Cancelable, Initializable {

	private boolean canceled = false;
	private String reason;

	@Override
	public void initialize() {
		if(someTestFails()) {
			cancel("Test failed");
		}
	}

	@Override
	public boolean isCanceled() {
		return canceled;
	}

	@Override
	public void cancel(final String reason) {
		canceled = true;
		this.reason = reason;
	}

	@Override
	public String getCancelReason() {
		return reason;
	}
}
4 Likes