Search in sources :

Example 6 with IndexRange

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);
    }
}
Also used : IndexRange(javafx.scene.control.IndexRange)

Example 7 with IndexRange

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;
    }
}
Also used : IndexRange(javafx.scene.control.IndexRange)

Example 8 with IndexRange

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;
    }
}
Also used : IndexRange(javafx.scene.control.IndexRange)

Example 9 with IndexRange

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);
    }
}
Also used : IndexRange(javafx.scene.control.IndexRange) ClipboardContent(javafx.scene.input.ClipboardContent) DataOutputStream(java.io.DataOutputStream) ReadOnlyStyledDocument(org.fxmisc.richtext.model.ReadOnlyStyledDocument) StyledDocument(org.fxmisc.richtext.model.StyledDocument) DataFormat(javafx.scene.input.DataFormat) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException)

Example 10 with IndexRange

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());
    }
}
Also used : IndexRange(javafx.scene.control.IndexRange)

Aggregations

IndexRange (javafx.scene.control.IndexRange)20 TextSearchAndReplaceView (com.kyj.fx.voeditor.visual.component.popup.TextSearchAndReplaceView)2 DataOutputStream (java.io.DataOutputStream)2 IOException (java.io.IOException)2 List (java.util.List)2 Optional (java.util.Optional)2 BiConsumer (java.util.function.BiConsumer)2 Function (java.util.function.Function)2 FXCollections (javafx.collections.FXCollections)2 Node (javafx.scene.Node)2 Paint (javafx.scene.paint.Paint)2 ReadOnlyStyledDocument (org.fxmisc.richtext.model.ReadOnlyStyledDocument)2 StyledDocument (org.fxmisc.richtext.model.StyledDocument)2 TableOpenResourceView (com.kyj.fx.voeditor.visual.component.popup.TableOpenResourceView)1 TextSearchComposite (com.kyj.fx.voeditor.visual.component.popup.TextSearchComposite)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 DataInputStream (java.io.DataInputStream)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 FileOutputStream (java.io.FileOutputStream)1