diff options
| author | Stefan Suhren <suhren.stefan@fh-swf.de> | 2015-04-27 23:02:18 +0200 |
|---|---|---|
| committer | Stefan Suhren <suhren.stefan@fh-swf.de> | 2015-04-27 23:02:18 +0200 |
| commit | caf910331b0910cef782b0890c8c0a3c80f4db51 (patch) | |
| tree | 4e316617ff5cf19cdf6cb4961f07686bde5a7d8c /src/de | |
| parent | d1a8c2522dd10ca112ccda0cf1407814b772e6e6 (diff) | |
| download | Java2-caf910331b0910cef782b0890c8c0a3c80f4db51.tar.gz Java2-caf910331b0910cef782b0890c8c0a3c80f4db51.zip | |
Reimplement a textfield with a contextmenu
Diffstat (limited to 'src/de')
| -rw-r--r-- | src/de/fhswf/in/inf/java2/aufgabe04/CountdownButtonMain.java | 9 | ||||
| -rw-r--r-- | src/de/fhswf/in/inf/java2/aufgabe04/TextFieldWithContextMenu.java | 114 |
2 files changed, 122 insertions, 1 deletions
diff --git a/src/de/fhswf/in/inf/java2/aufgabe04/CountdownButtonMain.java b/src/de/fhswf/in/inf/java2/aufgabe04/CountdownButtonMain.java index 5af329e..be0f3bf 100644 --- a/src/de/fhswf/in/inf/java2/aufgabe04/CountdownButtonMain.java +++ b/src/de/fhswf/in/inf/java2/aufgabe04/CountdownButtonMain.java @@ -3,6 +3,7 @@ package de.fhswf.in.inf.java2.aufgabe04; import javafx.application.Application; import javafx.application.Platform; import javafx.scene.Scene; +import javafx.scene.layout.VBox; import javafx.stage.Stage; public class CountdownButtonMain extends Application @@ -15,10 +16,16 @@ public class CountdownButtonMain extends Application ceb.setOnAction(e -> { Platform.exit(); }); - Scene scene = new Scene(ceb); + + TextFieldWithContextMenu tfwcm = new TextFieldWithContextMenu(); + + VBox vbox = new VBox(ceb,tfwcm); + Scene scene = new Scene(vbox); primaryStage.setScene(scene); primaryStage.show(); + ceb.startCountdown(); + ceb.stopCountdown(); } diff --git a/src/de/fhswf/in/inf/java2/aufgabe04/TextFieldWithContextMenu.java b/src/de/fhswf/in/inf/java2/aufgabe04/TextFieldWithContextMenu.java new file mode 100644 index 0000000..7884659 --- /dev/null +++ b/src/de/fhswf/in/inf/java2/aufgabe04/TextFieldWithContextMenu.java @@ -0,0 +1,114 @@ +/** + * File for the TextFieldWithContextMenu. + */ + +package de.fhswf.in.inf.java2.aufgabe04; + +import java.util.HashMap; +import java.util.Map; +import java.util.Stack; + +import javafx.scene.control.ContextMenu; +import javafx.scene.control.MenuItem; +import javafx.scene.control.TextField; +import javafx.scene.input.Clipboard; +import javafx.scene.input.DataFormat; + +/** + * A TextField with a ContextMenu. + * + * @author $Author: $ + * @version $Revision: $, $Date: $ UTC + */ +public class TextFieldWithContextMenu extends TextField +{ + Stack<String> undoStack = new Stack<>(); + + private MenuItem undo = new MenuItem("Rückgängig"); + private MenuItem cut = new MenuItem("Ausschneiden"); + private MenuItem copy = new MenuItem("Kopieren"); + private MenuItem paste = new MenuItem("Einfügen"); + private MenuItem delete = new MenuItem("Löschen"); + private MenuItem selectAll = new MenuItem("Alles Markieren"); + + private ContextMenu contextMenu; + + /** + * Build the ContextMenu for the TextField. + * + */ + public TextFieldWithContextMenu() + { + + //So empty is a valid pop and we never pop an empty stack + undoStack.push(""); + + undo.setOnAction(e -> { + //The first time properties are strange. + //The multiple pops are needed as the TextChangeListener + //adds the actual text before and after the setText + undoStack.pop(); + setText(undoStack.pop()); + undoStack.pop(); + }); + + cut.setOnAction(e -> { + Map<DataFormat, Object> content = new HashMap<>(); + content.put(DataFormat.PLAIN_TEXT, getSelectedText()); + Clipboard.getSystemClipboard().setContent(content); + deleteText(getSelection()); + }); + + copy.setOnAction(e -> { + Map<DataFormat, Object> content = new HashMap<>(); + content.put(DataFormat.PLAIN_TEXT, getSelectedText()); + Clipboard.getSystemClipboard().setContent(content); + }); + + paste.setOnAction(e -> { + replaceSelection(Clipboard.getSystemClipboard().getString()); + }); + + delete.setOnAction(e -> { + replaceSelection(""); + }); + + selectAll.setOnAction(e -> { + selectRange(0, getText().length()); + }); + + contextMenu = new ContextMenu(undo, cut, copy, paste, delete, + selectAll); + + setContextMenu(contextMenu); + + contextMenu.setOnShowing(e -> { + getContextMenu().getItems().forEach(f -> { + f.setDisable(true); + }); + if (undoStack.size() > 1) + { + undo.setDisable(false); + } + if (getSelection().getLength() > 0) + { + copy.setDisable(false); + cut.setDisable(false); + delete.setDisable(false); + } + if (Clipboard.getSystemClipboard().getString() != null) + { + paste.setDisable(false); + } + if (getLength() > 0 && getSelection().getLength() < getLength()) + { + selectAll.setDisable(false); + } + }); + + textProperty().addListener(e -> { + undoStack.push(getText()); + }); + } + +} |
