Java Code Examples for javafx.scene.control.IndexRange#getLength()

The following examples show how to use javafx.scene.control.IndexRange#getLength() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BytesEditerController.java    From MyBox with Apache License 2.0 6 votes vote down vote up
@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;
}
 
Example 2
Source File: ClipboardActions.java    From RichTextFX with BSD 2-Clause "Simplified" License 6 votes vote down vote up
/**
 * 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);
    }
}
 
Example 3
Source File: EditActions.java    From RichTextFX with BSD 2-Clause "Simplified" License 6 votes vote down vote up
/**
 * 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());
    }
}
 
Example 4
Source File: GenericStyledAreaBehavior.java    From RichTextFX with BSD 2-Clause "Simplified" License 6 votes vote down vote up
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);
    }
}
 
Example 5
Source File: MarkdownEditerController.java    From MyBox with Apache License 2.0 5 votes vote down vote up
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();
}
 
Example 6
Source File: IntTools.java    From MyBox with Apache License 2.0 5 votes vote down vote up
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);
}
 
Example 7
Source File: SmartEdit.java    From markdown-writer-fx with BSD 2-Clause "Simplified" License 5 votes vote down vote up
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);
			}
		}
	}
}
 
Example 8
Source File: SmartEdit.java    From markdown-writer-fx with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * 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;
	}
}
 
Example 9
Source File: RichTextDemo.java    From RichTextFX with BSD 2-Clause "Simplified" License 5 votes vote down vote up
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);
    }
}
 
Example 10
Source File: RichTextDemo.java    From RichTextFX with BSD 2-Clause "Simplified" License 5 votes vote down vote up
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);
    }
}
 
Example 11
Source File: GenericStyledAreaBehavior.java    From RichTextFX with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private void deleteBackward(KeyEvent ignore) {
    IndexRange selection = view.getSelection();
    if(selection.getLength() == 0) {
        view.deletePreviousChar();
    } else {
        view.replaceSelection("");
    }
}
 
Example 12
Source File: GenericStyledAreaBehavior.java    From RichTextFX with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private void deleteForward(KeyEvent ignore) {
    IndexRange selection = view.getSelection();
    if(selection.getLength() == 0) {
        view.deleteNextChar();
    } else {
        view.replaceSelection("");
    }
}
 
Example 13
Source File: GenericStyledAreaBehavior.java    From RichTextFX with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private void left(KeyEvent ignore) {
    IndexRange sel = view.getSelection();
    if(sel.getLength() == 0) {
        view.previousChar(SelectionPolicy.CLEAR);
    } else {
        view.moveTo(sel.getStart(), SelectionPolicy.CLEAR);
    }
}
 
Example 14
Source File: GenericStyledAreaBehavior.java    From RichTextFX with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private void right(KeyEvent ignore) {
    IndexRange sel = view.getSelection();
    if(sel.getLength() == 0) {
        view.nextChar(SelectionPolicy.CLEAR);
    } else {
        view.moveTo(sel.getEnd(), SelectionPolicy.CLEAR);
    }
}