110 lines
4.0 KiB
Java
110 lines
4.0 KiB
Java
package envoy.client.ui.custom;
|
|
|
|
import java.util.function.Consumer;
|
|
|
|
import javafx.event.*;
|
|
import javafx.scene.control.*;
|
|
import javafx.scene.input.Clipboard;
|
|
|
|
/**
|
|
* Displays a context menu that offers an additional option when one of
|
|
* its menu items has been clicked.
|
|
* <p>
|
|
* Current options are:
|
|
* <ul>
|
|
* <li>undo</li>
|
|
* <li>redo</li>
|
|
* <li>cut</li>
|
|
* <li>copy</li>
|
|
* <li>paste</li>
|
|
* <li>delete</li>
|
|
* <li>clear</li>
|
|
* <li>Select all</li>
|
|
* </ul>
|
|
* <p>
|
|
* Project: <strong>client</strong><br>
|
|
* File: <strong>TextInputContextMenu.java</strong><br>
|
|
* Created: <strong>20.09.2020</strong><br>
|
|
*
|
|
* @author Leon Hofmeister
|
|
* @since Envoy Client v0.2-beta
|
|
* @apiNote please refrain from using
|
|
* {@link ContextMenu#setOnShowing(EventHandler)} as this is already
|
|
* used by this component
|
|
*/
|
|
public class TextInputContextMenu extends ContextMenu {
|
|
|
|
private final MenuItem undoMI = new MenuItem("Undo");
|
|
private final MenuItem redoMI = new MenuItem("Redo");
|
|
private final MenuItem cutMI = new MenuItem("Cut");
|
|
private final MenuItem copyMI = new MenuItem("Copy");
|
|
private final MenuItem pasteMI = new MenuItem("Paste");
|
|
private final MenuItem deleteMI = new MenuItem("Delete selection");
|
|
private final MenuItem clearMI = new MenuItem("Clear");
|
|
private final MenuItem selectAllMI = new MenuItem("Select all");
|
|
private final MenuItem separatorMI = new SeparatorMenuItem();
|
|
|
|
/**
|
|
* Creates a new {@code TextInputContextMenu} with an optional action when
|
|
* this menu was clicked. Currently shows:
|
|
* <ul>
|
|
* <li>undo</li>
|
|
* <li>redo</li>
|
|
* <li>cut</li>
|
|
* <li>copy</li>
|
|
* <li>paste</li>
|
|
* <li>delete</li>
|
|
* <li>clear</li>
|
|
* <li>Select all</li>
|
|
* </ul>
|
|
*
|
|
* @param control the text input component to display this
|
|
* {@code ContextMenu}
|
|
* @param menuItemClicked the second action to perform when a menu item of this
|
|
* context menu has been clicked
|
|
* @since Envoy Client v0.2-beta
|
|
* @apiNote please refrain from using
|
|
* {@link ContextMenu#setOnShowing(EventHandler)} as this is already
|
|
* used by this component
|
|
*/
|
|
public TextInputContextMenu(TextInputControl control, Consumer<ActionEvent> menuItemClicked) {
|
|
|
|
// Define the actions when clicked
|
|
undoMI.setOnAction(addAction(e -> control.undo(), menuItemClicked));
|
|
redoMI.setOnAction(addAction(e -> control.redo(), menuItemClicked));
|
|
cutMI.setOnAction(addAction(e -> control.cut(), menuItemClicked));
|
|
copyMI.setOnAction(addAction(e -> control.copy(), menuItemClicked));
|
|
pasteMI.setOnAction(addAction(e -> control.paste(), menuItemClicked));
|
|
deleteMI.setOnAction(addAction(e -> control.replaceSelection(""), menuItemClicked));
|
|
clearMI.setOnAction(addAction(e -> control.setText(""), menuItemClicked));
|
|
selectAllMI.setOnAction(addAction(e -> control.selectAll(), menuItemClicked));
|
|
|
|
// Define the times it will be disabled
|
|
undoMI.disableProperty().bind(control.undoableProperty().not());
|
|
redoMI.disableProperty().bind(control.redoableProperty().not());
|
|
cutMI.disableProperty().bind(control.selectedTextProperty().isEmpty());
|
|
copyMI.disableProperty().bind(control.selectedTextProperty().isEmpty());
|
|
deleteMI.disableProperty().bind(control.selectedTextProperty().isEmpty());
|
|
clearMI.disableProperty().bind(control.textProperty().isEmpty());
|
|
setOnShowing(e -> pasteMI.setDisable(!Clipboard.getSystemClipboard().hasString()));
|
|
|
|
selectAllMI.getProperties().put("refreshMenu", Boolean.TRUE);
|
|
|
|
// Add all items to the ContextMenu
|
|
getItems().add(undoMI);
|
|
getItems().add(redoMI);
|
|
getItems().add(cutMI);
|
|
getItems().add(copyMI);
|
|
getItems().add(pasteMI);
|
|
getItems().add(separatorMI);
|
|
getItems().add(deleteMI);
|
|
getItems().add(clearMI);
|
|
getItems().add(separatorMI);
|
|
getItems().add(selectAllMI);
|
|
}
|
|
|
|
private EventHandler<ActionEvent> addAction(Consumer<ActionEvent> originalAction, Consumer<ActionEvent> additionalAction) {
|
|
return e -> { originalAction.accept(e); additionalAction.accept(e); };
|
|
}
|
|
}
|