-
Notifications
You must be signed in to change notification settings - Fork 241
Expand file tree
/
Copy pathSelectionChange.java
More file actions
62 lines (51 loc) · 1.69 KB
/
Copy pathSelectionChange.java
File metadata and controls
62 lines (51 loc) · 1.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package org.fxmisc.richtext;
import org.fxmisc.richtext.model.PlainTextChange;
import java.util.List;
// TODO SMA can it be that CaretPositionChange is the same as this one but for start == end ?
class SelectionChange {
private int start, end;
// TODO -> to be replaced by state of this class start(), end()
public static class Range {
private final int start, end;
public Range(int start, int end) {
this.start = start;
this.end = end;
}
public int start() {
return start;
}
public int end() {
return end;
}
}
public SelectionChange(int start, int end) {
this.start = start;
this.end = end;
}
public Range applyFor(List<PlainTextChange> changes) {
changes.forEach(this::applyFor);
return new Range(start, end);
}
private void applyFor(PlainTextChange plainTextChange) {
int netLength = plainTextChange.getNetLength();
int changeStart = plainTextChange.getPosition();
int changeEnd = changeStart + Math.abs(netLength);
if (start == changeStart) {
start += Math.max(netLength, 0);
}
else {
start = applyChange(start, changeStart, changeEnd, netLength);
}
end = applyChange(end, changeStart, changeEnd, netLength);
start = Math.min(start, end);
}
private static int applyChange(int position, int changeStart, int changeEnd, int netLength) {
if(position >= changeEnd) {
position += netLength;
}
else if(position > changeStart) {
position = changeStart;
}
return position;
}
}