/** * @return <code>true</code> if the selection has a length of 0 */ public boolean isSelectionEmpty() { return getSelection().length == 0; }
void updateSelection(int startOffset, int replacedLength, int newLength) { if (getSelection().offset + getSelection().length > startOffset && getSelection().offset < startOffset + replacedLength) { // selection intersects replaced text. set caret behind text change setSelection(new TextSelection(startOffset + newLength, 0)/*, true, false*/); } else { // move selection to keep same text selected setSelection(new TextSelection(getSelection().offset + newLength - replacedLength, getSelection().length)/*, true, false*/); if( getSelection().length > 0 ) { int delta = this.lastTextChangeNewCharCount - this.lastTextChangeReplaceCharCount; this.caretOffsetProperty.set(Math.max(0,Math.min(getCharCount()-1,getCaretOffset() + delta))); } } }
/** * Copy the current selection into the clipboard */ @SuppressWarnings("null") public void copy() { if (getSelection().length > 0) { final Clipboard clipboard = Clipboard.getSystemClipboard(); clipboard.setContent(Collections.singletonMap(DataFormat.PLAIN_TEXT, getContent().getTextRange(getSelection().offset, getSelection().length))); } }
/** * Copy the current selection into the clipboard */ @SuppressWarnings("null") public void copy() { if (getSelection().length > 0) { final Clipboard clipboard = Clipboard.getSystemClipboard(); clipboard.setContent(Collections.singletonMap(DataFormat.PLAIN_TEXT, getContent().getTextRange(getSelection().offset, getSelection().length))); } }
/** * Cut the current selection into the clipboard */ public void cut() { if( getSelection().length > 0 ) { final Clipboard clipboard = Clipboard.getSystemClipboard(); String content = getContent().getTextRange(getSelection().offset, getSelection().length); getContent().replaceTextRange(getSelection().offset, content.length(), ""); //$NON-NLS-1$ clipboard.setContent(Collections.singletonMap(DataFormat.PLAIN_TEXT, content)); } }
private boolean isInSelection(int offset) { int selOffset = getControl().getSelection().offset; int selLength = getControl().getSelection().length; boolean r = selLength > 0 && isInRange(offset, selOffset, selLength); return r; }
private Range<Integer> getSelection() { @NonNull TextSelection selection = this.control.getSelection(); return Range.closedOpen(Integer.valueOf(selection.offset), Integer.valueOf(selection.offset + selection.length)); }
void updateSelection(int startOffset, int replacedLength, int newLength) { if (getSelection().offset + getSelection().length > startOffset && getSelection().offset < startOffset + replacedLength) { // selection intersects replaced text. set caret behind text change setSelection(new TextSelection(startOffset + newLength, 0)/* , true, false */); } else { // move selection to keep same text selected int computedOffset = getSelection().offset + newLength - replacedLength; if (computedOffset >= 0 && computedOffset < getCharCount()) { // we only set this if the offset is valid!! setSelection(new TextSelection(computedOffset, getSelection().length)/* , true, false */); } if (getSelection().length > 0) { int delta = this.lastTextChangeNewCharCount - this.lastTextChangeReplaceCharCount; this.caretOffsetProperty.set(Math.max(0, Math.min(getCharCount() - 1, getCaretOffset() + delta))); } } }
@Override public IRegion getSelection() { TextSelection selection = viewer.getTextWidget().getSelection(); return convert(selection); }
private boolean isMultilineSelection() { return getControl().getLineAtOffset(getControl().getSelection().offset) != getControl().getLineAtOffset(getControl().getSelection().offset + getControl().getSelection().length); }
/** * inserts text at the caret location or replaces a given selection * * @param text */ @SuppressWarnings("null") public void insert(CharSequence text) { if (text == null) throw new NullPointerException(); int start, replaceLength; if (!isSelectionEmpty()) { // replace selection start = this.getSelection().offset; replaceLength = this.getSelection().length; } else { // insert at caret start = this.getCaretOffset(); replaceLength = 0; } String content = text.toString(); if( isInsertSpacesForTab() ) { content = content.replaceAll("\t", Util.createRepeatedString(' ', this.tabAdvance.get())); //$NON-NLS-1$ } this.getContent().replaceTextRange(start, replaceLength, content); this.setCaretOffset(start + content.length()); }
private void onTextPositionDragDetected(TextPositionEvent event) { if (this.pressedInSelection) { if( org.eclipse.fx.ui.controls.Util.isCopyEvent(event) ) { getControl().pseudoClassStateChanged(DRAG_TEXT_COPY_ACTIVE_PSEUDOCLASS_STATE, true); } else { getControl().pseudoClassStateChanged(DRAG_TEXT_MOVE_ACTIVE_PSEUDOCLASS_STATE, true); } this.dragMoveTextMode = true; this.dragMoveTextOffset = getControl().getSelection().offset; this.dragMoveTextLength = getControl().getSelection().length; } else { this.dragSelectionMode = true; } }
/** * default implementation for {@link DefaultTextEditActions#SELECT_LINE} */ protected void defaultSelectLine() { @NonNull LineRegion lineRegion = getLineRegion(getControl().getSelection()); lineRegion.selectWithCaretAtEnd(); }
/** * default implementation for {@link DefaultTextEditActions#DELETE_LINE} */ protected void defaultDeleteLine() { LineRegion lineRegion = getLineRegion(getControl().getSelection()); lineRegion.replace(""); //$NON-NLS-1$ moveCaretAbsolute(lineRegion.start); }
/** * Cut the current selection into the clipboard */ public void cut() { TextSelection selection = getSelection(); if (selection.length > 0) { final Clipboard clipboard = Clipboard.getSystemClipboard(); String content = getContent().getTextRange(selection.offset, selection.length); setCaretOffset(selection.offset); getContent().replaceTextRange(selection.offset, content.length(), ""); //$NON-NLS-1$ clipboard.setContent(Collections.singletonMap(DataFormat.PLAIN_TEXT, content)); } }
/** * default implementation for {@link DefaultTextEditActions#NEW_LINE} */ protected void defaultNewLine() { int offset = getControl().getCaretOffset(); int line = getControl().getContent().getLineAtOffset(offset); int lineOffset = offset - getControl().getContent().getOffsetAtLine(line); String lineContent = getControl().getContent().getLine(line); // Should we make this configurable char[] chars = lineContent.toCharArray(); String prefix = ""; //$NON-NLS-1$ for (int i = 0; i < Math.min(lineOffset, chars.length); i++) { if (chars[i] == ' ' || chars[i] == '\t') { prefix += chars[i]; } else { break; } } if (getControl().getSelection().length > 0) { int caretPos = getControl().getSelection().offset + getControl().getLineSeparator().getValue().length() + prefix.length(); getControl().getContent().replaceTextRange(getControl().getSelection().offset, getControl().getSelection().length, getControl().getLineSeparator().getValue() + prefix); getControl().setCaretOffset( caretPos ); } else { getControl().getContent().replaceTextRange(getControl().getCaretOffset(), 0, getControl().getLineSeparator().getValue() + prefix); getControl().setCaretOffset(offset + getControl().getLineSeparator().getValue().length() + prefix.length()); } }
/** * default implementation for {@link DefaultTextEditActions#MOVE_LINES_UP} */ protected void defaultMoveLinesUp() { LineRegion moveTarget = getLineRegion(getControl().getSelection()); if (moveTarget.firstLine > 0) { LineRegion above = new LineRegion(moveTarget.firstLine - 1); LineRegion all = new LineRegion(above.firstLine, moveTarget.lastLine); String aboveText = above.read(); String moveTargetText = moveTarget.read(); // we reach the last line if (moveTarget.lastLine + 1 == getControl().getContent().getLineCount()) { moveTargetText += getControl().getLineSeparator().getValue(); aboveText = aboveText.replaceFirst("\r?\n$", ""); //$NON-NLS-1$ //$NON-NLS-2$ } all.replace(moveTargetText + aboveText); new LineRegion(moveTarget.firstLine - 1, moveTarget.lastLine - 1).selectWithCaretAtStart(); } }
/** * default implementation for {@link DefaultTextEditActions#MOVE_LINES_DOWN} */ protected void defaultMoveLinesDown() { LineRegion moveTarget = getLineRegion(getControl().getSelection()); if (moveTarget.lastLine + 1 < getControl().getContent().getLineCount()) { LineRegion below = new LineRegion(moveTarget.lastLine + 1); LineRegion all = new LineRegion(moveTarget.firstLine, below.lastLine); String belowText = below.read(); String moveTargetText = moveTarget.read(); // we reach the last line if (below.lastLine + 1 == getControl().getContent().getLineCount()) { belowText += getControl().getLineSeparator().getValue(); moveTargetText = moveTargetText.replaceFirst("\r?\n$", ""); //$NON-NLS-1$ //$NON-NLS-2$ } all.replace(belowText + moveTargetText); new LineRegion(moveTarget.firstLine + 1, moveTarget.lastLine + 1).selectWithCaretAtStart(); } }
/** * default implementation for {@link DefaultTextEditActions#DELETE_PREVIOUS} */ protected void defaultDeletePrevious() { int offset = getControl().getCaretOffset(); TextSelection selection = getControl().getSelection(); if (selection.length > 0) { getControl().getContent().replaceTextRange(selection.offset, selection.length, ""); //$NON-NLS-1$ getControl().setCaretOffset(selection.offset); } else { int start = getControl().getCaretOffset() - 1; int del = 1; if (start - 1 >= 0) { if ("\r\n".equals(getControl().getContent().getTextRange(getControl().getCaretOffset()-2, 2))) { //$NON-NLS-1$ start = start - 1; del = 2; } } if( start >= 0 ) { getControl().getContent().replaceTextRange(start, del, ""); //$NON-NLS-1$ getControl().setCaretOffset(offset - del); } } }
/** * default implementation for {@link DefaultTextEditActions#DELETE} */ protected void defaultDelete() { int offset = getControl().getCaretOffset(); TextSelection selection = getControl().getSelection(); if (selection.length > 0) { getControl().getContent().replaceTextRange(selection.offset, selection.length, ""); //$NON-NLS-1$ getControl().setCaretOffset(selection.offset); } else { int del = 1; if (getControl().getCaretOffset() + 2 <= getControl().getContent().getCharCount()) { if ("\r\n".equals(getControl().getContent().getTextRange(getControl().getCaretOffset(), 2))) { //$NON-NLS-1$ del = 2; } } if (getControl().getCaretOffset() + del <= getControl().getContent().getCharCount()) { getControl().getContent().replaceTextRange(getControl().getCaretOffset(), del, ""); //$NON-NLS-1$ getControl().setCaretOffset(offset); } } }