下面列出了javafx.scene.control.IndexRange#getLength ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
protected void setSecondAreaSelection() {
if (isSettingValues || displayArea == null || !contentSplitPane.getItems().contains(displayArea)) {
return;
}
displayArea.deselect();
IndexRange hexRange = mainArea.getSelection();
if (hexRange.getLength() == 0) {
return;
}
isSettingValues = true;
final String text = displayArea.getText();
if (!text.isEmpty()) {
IndexRange textRange = ByteTools.textIndex(mainArea.getText(), sourceInformation.getCharset(), hexRange);
displayArea.selectRange(textRange.getStart(), textRange.getEnd());
displayArea.setScrollTop(mainArea.getScrollTop());
}
isSettingValues = false;
}
/**
* Transfers the currently selected text to the clipboard,
* leaving the current selection.
*/
default void copy() {
IndexRange selection = getSelection();
if(selection.getLength() > 0) {
ClipboardContent content = new ClipboardContent();
content.putString(getSelectedText());
getStyleCodecs().ifPresent(codecs -> {
Codec<StyledDocument<PS, SEG, S>> codec = ReadOnlyStyledDocument.codec(codecs._1, codecs._2, getSegOps());
DataFormat format = dataFormat(codec.getName());
StyledDocument<PS, SEG, S> doc = subDocument(selection.getStart(), selection.getEnd());
ByteArrayOutputStream os = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(os);
try {
codec.encode(dos, doc);
content.put(format, os.toByteArray());
} catch (IOException e) {
System.err.println("Codec error: Exception in encoding '" + codec.getName() + "':");
e.printStackTrace();
}
});
Clipboard.getSystemClipboard().setContent(content);
}
}
/**
* If something is currently selected and the given position is outside of the selection, moves the selected
* rich-text document to the given position by deleting it from the area and re-inserting it at the given position.
* If nothing is selected, moves the caret ot that position.
*/
default void moveSelectedText(int position) {
IndexRange sel = getSelection();
if((position >= sel.getStart() && position <= sel.getEnd()) || sel.equals(GenericStyledArea.EMPTY_RANGE)) {
// no move, just position the caret
selectRange(position, position);
} else {
StyledDocument<PS, SEG, S> text = this.subDocument(sel.getStart(), sel.getEnd());
if(position > sel.getEnd())
position -= sel.getLength();
createMultiChange(2)
.deleteText(sel)
.insertAbsolutely(position, text)
.commit();
// select moved text
selectRange(position, position + text.length());
}
}
private void handleFirstPrimaryPress(MouseEvent e) {
// ensure focus
view.requestFocus();
CharacterHit hit = view.hit(e.getX(), e.getY());
view.clearTargetCaretOffset();
IndexRange selection = view.getSelection();
if(view.isEditable() &&
selection.getLength() != 0 &&
hit.getCharacterIndex().isPresent() &&
hit.getCharacterIndex().getAsInt() >= selection.getStart() &&
hit.getCharacterIndex().getAsInt() < selection.getEnd()) {
// press inside selection
dragSelection = DragState.POTENTIAL_DRAG;
dragNewSelection = DragState.NO_DRAG;
} else {
dragSelection = DragState.NO_DRAG;
dragNewSelection = DragState.NO_DRAG;
view.getOnOutsideSelectionMousePressed().handle(e);
}
}
protected void addTextAround(String prefix, String suffix) {
IndexRange range = mainArea.getSelection();
if (range.getLength() == 0) {
String s = prefix + message("Text") + suffix;
mainArea.insertText(range.getStart(), s);
mainArea.selectRange(range.getStart() + prefix.length(),
range.getStart() + prefix.length() + message("Text").length());
} else {
mainArea.insertText(range.getStart(), prefix);
mainArea.insertText(range.getEnd() + prefix.length(), suffix);
}
mainArea.requestFocus();
}
public static int mapInt(int value, IndexRange originalRange, IndexRange newlRange) {
if (originalRange == null || newlRange == null || originalRange.getStart() > value || originalRange.getEnd() < value) {
return value;
}
int len = value - originalRange.getStart() + 1;
double ratio = newlRange.getLength() * 1.0 / originalRange.getLength();
return newlRange.getStart() + (int) Math.round(len * ratio);
}
private void backspacePressed(KeyEvent e) {
IndexRange selection = textArea.getSelection();
int start = selection.getStart();
int end = selection.getEnd();
if (selection.getLength() > 0) {
// selection is not empty --> delete selected text
deleteText(textArea, start, end);
} else {
// selection is empty
int startLine = offsetToLine(start);
int startLineOffset = lineToStartOffset(startLine);
if (start > startLineOffset && textArea.getText(startLineOffset, start).trim().isEmpty()) {
// selection is empty and caret is in leading whitespace of a line,
// but not at the beginning of a line --> unindent line
indentSelectedLines(false);
} else {
String line = textArea.getText(startLine);
int startLineEndOffset = startLineOffset + line.length();
Matcher matcher = (start == startLineEndOffset) ? AUTO_INDENT_PATTERN.matcher(line) : null;
if (matcher != null && matcher.matches() && matcher.group(2).isEmpty()) {
// caret is at end of line and line contains only whitespace characters
// and auto-indentable markers --> empty line
deleteText(textArea, startLineOffset, startLineEndOffset);
} else if (start > 0) {
// delete character before caret
deleteText(textArea, start - 1, start);
}
}
}
}
/**
* Returns whether an indent operation should used for the selection.
*
* Returns true if:
* - selection spans multiple lines
* - selection is empty and caret is in leading whitespace of a line
* - a single line is completely selected (excluding line separator)
*/
private boolean isIndentSelection() {
IndexRange selection = textArea.getSelection();
int start = selection.getStart();
int startLine = offsetToLine(start);
if (selection.getLength() == 0)
return textArea.getText(lineToStartOffset(startLine), start).trim().isEmpty();
else {
int end = selection.getEnd();
int endLine = offsetToLine(end);
return endLine > startLine ||
lineToStartOffset(startLine) == start && lineToEndOffset(startLine) == end;
}
}
private void updateStyleInSelection(Function<StyleSpans<TextStyle>, TextStyle> mixinGetter) {
IndexRange selection = area.getSelection();
if(selection.getLength() != 0) {
StyleSpans<TextStyle> styles = area.getStyleSpans(selection);
TextStyle mixin = mixinGetter.apply(styles);
StyleSpans<TextStyle> newStyles = styles.mapStyles(style -> style.updateWith(mixin));
area.setStyleSpans(selection.getStart(), newStyles);
}
}
private void updateStyleInSelection(TextStyle mixin) {
IndexRange selection = area.getSelection();
if (selection.getLength() != 0) {
StyleSpans<TextStyle> styles = area.getStyleSpans(selection);
StyleSpans<TextStyle> newStyles = styles.mapStyles(style -> style.updateWith(mixin));
area.setStyleSpans(selection.getStart(), newStyles);
}
}
private void deleteBackward(KeyEvent ignore) {
IndexRange selection = view.getSelection();
if(selection.getLength() == 0) {
view.deletePreviousChar();
} else {
view.replaceSelection("");
}
}
private void deleteForward(KeyEvent ignore) {
IndexRange selection = view.getSelection();
if(selection.getLength() == 0) {
view.deleteNextChar();
} else {
view.replaceSelection("");
}
}
private void left(KeyEvent ignore) {
IndexRange sel = view.getSelection();
if(sel.getLength() == 0) {
view.previousChar(SelectionPolicy.CLEAR);
} else {
view.moveTo(sel.getStart(), SelectionPolicy.CLEAR);
}
}
private void right(KeyEvent ignore) {
IndexRange sel = view.getSelection();
if(sel.getLength() == 0) {
view.nextChar(SelectionPolicy.CLEAR);
} else {
view.moveTo(sel.getEnd(), SelectionPolicy.CLEAR);
}
}