use of javafx.scene.control.IndexRange in project RichTextFX by FXMisc.
the class RichText method updateStyleInSelection.
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);
}
}
use of javafx.scene.control.IndexRange in project RichTextFX by FXMisc.
the class CaretSelectionBind method moveTo.
/**
* Moves the caret to the position indicated by {@code pos}.
* Based on the selection policy, the selection is either <em>cleared</em>
* (i.e. anchor is set to the same position as caret), <em>adjusted</em>
* (i.e. anchor is not moved at all), or <em>extended</em>
* (i.e. {@code pos} becomes the new caret and, if {@code pos} points
* outside the current selection, the far end of the current selection
* becomes the anchor.
*/
default void moveTo(int pos, NavigationActions.SelectionPolicy selectionPolicy) {
switch(selectionPolicy) {
case CLEAR:
selectRange(pos, pos);
break;
case ADJUST:
selectRange(getAnchorPosition(), pos);
break;
case EXTEND:
IndexRange sel = getRange();
int anchor;
if (pos <= sel.getStart())
anchor = sel.getEnd();
else if (pos >= sel.getEnd())
anchor = sel.getStart();
else
anchor = getAnchorPosition();
selectRangeExpl(anchor, pos);
break;
}
}
use of javafx.scene.control.IndexRange in project RichTextFX by FXMisc.
the class CaretSelectionBindImpl method moveTo.
@Override
public void moveTo(int pos, NavigationActions.SelectionPolicy selectionPolicy) {
switch(selectionPolicy) {
case CLEAR:
selectRangeExpl(pos, pos);
break;
case ADJUST:
selectRangeExpl(getAnchorPosition(), pos);
break;
case EXTEND:
IndexRange sel = getRange();
int anchor;
if (pos <= sel.getStart()) {
anchor = sel.getEnd();
} else if (pos >= sel.getEnd()) {
anchor = sel.getStart();
} else {
anchor = getAnchorPosition();
}
selectRangeExpl(anchor, pos);
break;
}
}
use of javafx.scene.control.IndexRange in project RichTextFX by FXMisc.
the class ClipboardHelper method copy.
/**
* 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);
}
}
use of javafx.scene.control.IndexRange in project RichTextFX by FXMisc.
the class EditActions method moveSelectedText.
/**
* 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());
}
}
Aggregations