Browse Source

fixed #567: added drag & drop support for password fields

Tobias Hagemann 7 years ago
parent
commit
d1a9233557

+ 24 - 0
main/ui/src/main/java/org/cryptomator/ui/controls/SecPasswordField.java

@@ -11,6 +11,9 @@ package org.cryptomator.ui.controls;
 import java.util.Arrays;
 
 import javafx.scene.control.PasswordField;
+import javafx.scene.input.DragEvent;
+import javafx.scene.input.Dragboard;
+import javafx.scene.input.TransferMode;
 
 /**
  * Compromise in security. While the text can be swiped, any access to the {@link #getText()} method will create a copy of the String in the heap.
@@ -19,6 +22,27 @@ public class SecPasswordField extends PasswordField {
 
 	private static final char SWIPE_CHAR = ' ';
 
+	public SecPasswordField() {
+		this.onDragOverProperty().set(this::handleDragOver);
+		this.onDragDroppedProperty().set(this::handleDragDropped);
+	}
+
+	private void handleDragOver(DragEvent event) {
+		Dragboard dragboard = event.getDragboard();
+		if (dragboard.hasString() && dragboard.getString() != null) {
+			event.acceptTransferModes(TransferMode.COPY);
+		}
+		event.consume();
+	}
+
+	private void handleDragDropped(DragEvent event) {
+		Dragboard dragboard = event.getDragboard();
+		if (dragboard.hasString() && dragboard.getString() != null) {
+			insertText(getCaretPosition(), dragboard.getString());
+		}
+		event.consume();
+	}
+
 	/**
 	 * {@link #getContent()} uses a StringBuilder, which in turn is backed by a char[].
 	 * The delete operation of AbstractStringBuilder closes the gap, that forms by deleting chars, by moving up the following chars.