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

This commit is contained in:
Tobias Hagemann
2017-08-07 11:04:44 +02:00
parent 1a73f84d83
commit d1a9233557

View File

@@ -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.