diff options
| author | Stefan Suhren <suhren.stefan@fh-swf.de> | 2015-04-28 08:54:11 +0200 |
|---|---|---|
| committer | Stefan Suhren <suhren.stefan@fh-swf.de> | 2015-04-28 08:54:11 +0200 |
| commit | af7b9bd6a962b9a290fc5d78c08c83653961811f (patch) | |
| tree | 3566ebdd990641b9bdcf38dcef0d648f881433d1 /src/de/fhswf/in/inf/java2/aufgabe04/TextFieldWithContextMenu.java | |
| parent | 7fc9cecb0ca9019fd8f271e7d61927fcbc88cf44 (diff) | |
| download | Java2-af7b9bd6a962b9a290fc5d78c08c83653961811f.tar.gz Java2-af7b9bd6a962b9a290fc5d78c08c83653961811f.zip | |
Use the already implemented methods
Don't write methods yourself if they are provided.
Diffstat (limited to 'src/de/fhswf/in/inf/java2/aufgabe04/TextFieldWithContextMenu.java')
| -rw-r--r-- | src/de/fhswf/in/inf/java2/aufgabe04/TextFieldWithContextMenu.java | 64 |
1 files changed, 14 insertions, 50 deletions
diff --git a/src/de/fhswf/in/inf/java2/aufgabe04/TextFieldWithContextMenu.java b/src/de/fhswf/in/inf/java2/aufgabe04/TextFieldWithContextMenu.java index 41b229d..db1f2a0 100644 --- a/src/de/fhswf/in/inf/java2/aufgabe04/TextFieldWithContextMenu.java +++ b/src/de/fhswf/in/inf/java2/aufgabe04/TextFieldWithContextMenu.java @@ -4,26 +4,19 @@ 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 + * @author Stefan Suhren + * @version 1.0 */ 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"); @@ -38,39 +31,21 @@ public class TextFieldWithContextMenu extends 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 - - //Debugger is unusable while ContextMenu is open. - //contextMenu.hide(); - - undoStack.pop(); - setText(undoStack.pop()); - undoStack.pop(); + undo(); }); cut.setOnAction(e -> { - Map<DataFormat, Object> content = new HashMap<>(); - content.put(DataFormat.PLAIN_TEXT, getSelectedText()); - Clipboard.getSystemClipboard().setContent(content); - deleteText(getSelection()); + cut(); }); copy.setOnAction(e -> { - Map<DataFormat, Object> content = new HashMap<>(); - content.put(DataFormat.PLAIN_TEXT, getSelectedText()); - Clipboard.getSystemClipboard().setContent(content); + copy(); }); paste.setOnAction(e -> { - replaceSelection(Clipboard.getSystemClipboard().getString()); + paste(); }); delete.setOnAction(e -> { @@ -78,7 +53,7 @@ public class TextFieldWithContextMenu extends TextField }); selectAll.setOnAction(e -> { - selectRange(0, getText().length()); + selectAll(); }); contextMenu = new ContextMenu(undo, cut, copy, paste, delete, @@ -90,28 +65,17 @@ public class TextFieldWithContextMenu extends TextField getContextMenu().getItems().forEach(f -> { f.setDisable(true); }); - if (undoStack.size() > 1) - { - undo.setDisable(false); - } + + undo.setDisable(!isUndoable()); + paste.setDisable(Clipboard.getSystemClipboard().getString() == null); + selectAll.setDisable(getLength() <= 0 || getSelection().getLength() >= getLength()); + 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()); }); } |
