javafx.scene.input.SimpleCommand Maven / Gradle / Ivy
/*
* Copyright (c) 2020 JFXCore. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 or later,
* as published by the Free Software Foundation. This particular file is
* designated as subject to the "Classpath" exception as provided in the
* LICENSE file that accompanied this code.
*
* This code 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 Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*/
package javafx.scene.input;
import javafx.beans.value.ObservableValue;
import java.util.function.Consumer;
/**
* A command implementation that synchronously invokes a user-specified method.
*/
public class SimpleCommand extends CommandBase {
private final Consumer executeMethod;
private final Consumer exceptionHandler;
/**
* Initializes a new instance of the {@link SimpleCommand} class.
*
* @param executeMethod a parameterless operation
*/
public SimpleCommand(Runnable executeMethod) {
if (executeMethod == null) {
throw new IllegalArgumentException("executeMethod");
}
this.executeMethod = param -> executeMethod.run();
this.exceptionHandler = null;
}
/**
* Initializes a new instance of the {@link SimpleCommand} class.
*
* @param executeMethod a parameterized operation
*/
public SimpleCommand(Consumer executeMethod) {
if (executeMethod == null) {
throw new IllegalArgumentException("executeMethod");
}
this.executeMethod = executeMethod;
this.exceptionHandler = null;
}
/**
* Initializes a new instance of the {@link SimpleCommand} class.
*
* @param executeMethod a parameterless operation
* @param exceptionHandler handler for exceptions thrown by the operation; if none is provided, exceptions will be thrown on the calling thread
*/
public SimpleCommand(Runnable executeMethod, Consumer exceptionHandler) {
if (executeMethod == null) {
throw new IllegalArgumentException("executeMethod");
}
this.executeMethod = param -> executeMethod.run();
this.exceptionHandler = exceptionHandler;
}
/**
* Initializes a new instance of the {@link SimpleCommand} class.
*
* @param executeMethod a parameterized operation
* @param exceptionHandler handler for exceptions thrown by the operation; if none is provided, exceptions will be thrown on the calling thread
*/
public SimpleCommand(Consumer executeMethod, Consumer exceptionHandler) {
if (executeMethod == null) {
throw new IllegalArgumentException("executeMethod");
}
this.executeMethod = executeMethod;
this.exceptionHandler = exceptionHandler;
}
/**
* Initializes a new instance of the {@link SimpleCommand} class.
*
* @param executeMethod a parameterless operation
* @param condition a value that controls the executability of the command
*/
public SimpleCommand(Runnable executeMethod, ObservableValue condition) {
super(condition);
if (executeMethod == null) {
throw new IllegalArgumentException("executeMethod");
}
this.executeMethod = param -> executeMethod.run();
this.exceptionHandler = null;
}
/**
* Initializes a new instance of the {@link SimpleCommand} class.
*
* @param executeMethod a {@link Runnable} that represents a parameterized operation
* @param condition a value that controls the executability of the command
*/
public SimpleCommand(Consumer executeMethod, ObservableValue condition) {
super(condition);
if (executeMethod == null) {
throw new IllegalArgumentException("executeMethod");
}
this.executeMethod = executeMethod;
this.exceptionHandler = null;
}
/**
* Initializes a new instance of the {@link SimpleCommand} class.
*
* @param executeMethod a parameterless operation
* @param condition a value that controls the executability of the command
* @param exceptionHandler handler for exceptions thrown by the operation; if none is provided, exceptions will be thrown on the calling thread
*/
public SimpleCommand(Runnable executeMethod, ObservableValue condition, Consumer exceptionHandler) {
super(condition);
if (executeMethod == null) {
throw new IllegalArgumentException("executeMethod");
}
this.executeMethod = param -> executeMethod.run();
this.exceptionHandler = exceptionHandler;
}
/**
* Initializes a new instance of the {@link SimpleCommand} class.
*
* @param executeMethod a parameterized operation
* @param condition a value that controls the executability of the command
* @param exceptionHandler handler for exceptions thrown by the operation; if none is provided, exceptions will be thrown on the calling thread
*/
public SimpleCommand(Consumer executeMethod, ObservableValue condition, Consumer exceptionHandler) {
super(condition);
if (executeMethod == null) {
throw new IllegalArgumentException("executeMethod");
}
this.executeMethod = executeMethod;
this.exceptionHandler = exceptionHandler;
}
@Override
public void execute(T parameter) {
try {
startExecution();
executeMethod.accept(parameter);
endExecution();
} catch (Throwable ex) {
endExecution();
if (exceptionHandler != null) {
exceptionHandler.accept(ex);
} else {
throw ex;
}
}
}
}