18
18
import org .jspecify .annotations .Nullable ;
19
19
20
20
public class SelectableTextFlow extends TextFlow {
21
- @ Nullable protected HitInfo startHit ;
22
- @ Nullable protected HitInfo endHit ;
23
- @ Nullable protected Path selectionPath ;
21
+ @ Nullable private HitInfo startHit ;
22
+ @ Nullable private HitInfo endHit ;
23
+ @ Nullable private Path selectionPath ;
24
24
25
25
private final Pane parentPane ;
26
26
private boolean isDragging = false ;
@@ -46,12 +46,12 @@ public SelectableTextFlow(Pane parent) {
46
46
}
47
47
48
48
public void copySelectedText () {
49
- if (startHit == null || endHit == null ) {
49
+ if (! isSelectionActive () ) {
50
50
return ;
51
51
}
52
52
53
- int startIndex = Math . min ( startHit . getCharIndex (), endHit . getCharIndex () );
54
- int endIndex = Math . max ( startHit . getCharIndex () + 1 , endHit . getCharIndex () + 1 );
53
+ int startIndex = getSelectionStartIndex ( );
54
+ int endIndex = getSelectionEndIndex ( );
55
55
56
56
String fullText = getTextFlowContent ();
57
57
if (startIndex < 0 || endIndex > fullText .length () || startIndex >= endIndex ) {
@@ -77,6 +77,22 @@ public void clearSelection() {
77
77
removeHighlight ();
78
78
}
79
79
80
+ public boolean isSelectionActive () {
81
+ return startHit != null && endHit != null && startHit .getCharIndex () != endHit .getCharIndex ();
82
+ }
83
+
84
+ /// Returns the start index of the selection. Assumes that the selection is active.
85
+ public int getSelectionStartIndex () {
86
+ assert isSelectionActive ();
87
+ return Math .min (startHit .getCharIndex (), endHit .getCharIndex ());
88
+ }
89
+
90
+ /// Returns the end index of the selection. Assumes that the selection is active.
91
+ public int getSelectionEndIndex () {
92
+ assert isSelectionActive ();
93
+ return Math .max (startHit .getCharIndex () + 1 , endHit .getCharIndex () + 1 );
94
+ }
95
+
80
96
private String getTextFlowContent () {
81
97
StringBuilder sb = new StringBuilder ();
82
98
for (Node node : getChildren ()) {
@@ -90,14 +106,11 @@ private String getTextFlowContent() {
90
106
private void updateSelectionHighlight () {
91
107
removeHighlight ();
92
108
93
- if (startHit == null || endHit == null || startHit . getCharIndex () == endHit . getCharIndex ()) {
109
+ if (! isSelectionActive ()) {
94
110
return ;
95
111
}
96
112
97
- int startIndex = Math .min (startHit .getCharIndex (), endHit .getCharIndex ());
98
- int endIndex = Math .max (startHit .getCharIndex () + 1 , endHit .getCharIndex () + 1 );
99
-
100
- PathElement [] elements = rangeShape (startIndex , endIndex );
113
+ PathElement [] elements = rangeShape (getSelectionStartIndex (), getSelectionEndIndex ());
101
114
102
115
Path path = new Path ();
103
116
path .getElements ().addAll (elements );
0 commit comments