Java Code Examples for org.eclipse.swt.custom.StyledText#setSelectionRange()

The following examples show how to use org.eclipse.swt.custom.StyledText#setSelectionRange() . 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: XtextEditor.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
@Override
protected void setCaretPosition(final int position) {
	final ISourceViewer viewer = getSourceViewer();

	final StyledText text = viewer.getTextWidget();
	if (text != null && !text.isDisposed()) {

		final Point selection = text.getSelection();
		final int caret = text.getCaretOffset();
		final int offset = modelOffset2WidgetOffset(viewer, position);

		if (caret == selection.x)
			text.setSelectionRange(selection.y, offset - selection.y);
		else
			text.setSelectionRange(selection.x, offset - selection.x);
	}
}
 
Example 2
Source File: XtextEditor.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
@Override
protected void setCaretPosition(final int position) {
	final ISourceViewer viewer = getSourceViewer();

	final StyledText text = viewer.getTextWidget();
	if (text != null && !text.isDisposed()) {

		final Point selection = text.getSelection();
		final int caret = text.getCaretOffset();
		final int offset = modelOffset2WidgetOffset(viewer, position);

		if (caret == selection.x)
			text.setSelectionRange(selection.y, offset - selection.y);
		else
			text.setSelectionRange(selection.x, offset - selection.x);
	}
}
 
Example 3
Source File: JavaEditor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
@Override
protected void setCaretPosition(final int position) {
	final ISourceViewer viewer= getSourceViewer();

	final StyledText text= viewer.getTextWidget();
	if (text != null && !text.isDisposed()) {

		final Point selection= text.getSelection();
		final int caret= text.getCaretOffset();
		final int offset= modelOffset2WidgetOffset(viewer, position);

		if (caret == selection.x)
			text.setSelectionRange(selection.y, offset - selection.y);
		else
			text.setSelectionRange(selection.x, offset - selection.x);
	}
}
 
Example 4
Source File: JavaEditor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
@Override
protected void setCaretPosition(final int position) {
	final ISourceViewer viewer= getSourceViewer();

	final StyledText text= viewer.getTextWidget();
	if (text != null && !text.isDisposed()) {

		final Point selection= text.getSelection();
		final int caret= text.getCaretOffset();
		final int offset= modelOffset2WidgetOffset(viewer, position);

		if (caret == selection.x)
			text.setSelectionRange(selection.y, offset - selection.y);
		else
			text.setSelectionRange(selection.x, offset - selection.x);
	}
}
 
Example 5
Source File: SearchMinibuffer.java    From e4macs with Eclipse Public License 1.0 6 votes vote down vote up
public void restoreState(StyledText text) {
	// WIDGET OFFSET 
	text.setSelectionRange(selection, length);
	text.showSelection();

	// relies on the contents of the StringBuilder
	if (mb != null) {
		getMB().init(mb);
	}
	if (rx != null) {
		getRX().init(rx);
	}			
	setMBLength(findLength);
	setRXLength(regLength);
	setSearchOffset(index);
	found= sfound;
	forward= sforward;

	// Recalculate the indices
	if (findLength <= currentCasePos)
		currentCasePos= -1;
	
	if (getSearchStates().size() < wrapPosition)
		setWrapPosition();
}
 
Example 6
Source File: SubWordActions.java    From Pydev with Eclipse Public License 1.0 6 votes vote down vote up
@Override
protected void setCaretPosition(final int position) {
    final ISourceViewer viewer = getSourceViewer();

    final StyledText text = viewer.getTextWidget();
    if (text != null && !text.isDisposed()) {

        final Point selection = text.getSelection();
        final int caret = text.getCaretOffset();
        final int offset = modelOffset2WidgetOffset(viewer, position);

        if (caret == selection.x) {
            text.setSelectionRange(selection.y, offset - selection.y);
        } else {
            text.setSelectionRange(selection.x, offset - selection.x);
        }
    }
}
 
Example 7
Source File: SubWordActions.java    From Pydev with Eclipse Public License 1.0 6 votes vote down vote up
@Override
protected void setCaretPosition(final int position) {
    final ISourceViewer viewer = getSourceViewer();

    final StyledText text = viewer.getTextWidget();
    if (text != null && !text.isDisposed()) {

        final Point selection = text.getSelection();
        final int caret = text.getCaretOffset();
        final int offset = modelOffset2WidgetOffset(viewer, position);

        if (caret == selection.x) {
            text.setSelectionRange(selection.y, offset - selection.y);
        } else {
            text.setSelectionRange(selection.x, offset - selection.x);
        }
    }
}
 
Example 8
Source File: XtextEditor.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
@Override
protected void setCaretPosition(final int position) {
	if (!validateEditorInputState())
		return;

	final ISourceViewer viewer = getSourceViewer();
	StyledText text = viewer.getTextWidget();
	Point widgetSelection = text.getSelection();
	if (isBlockSelectionModeEnabled() && widgetSelection.y != widgetSelection.x) {
		final int caret = text.getCaretOffset();
		final int offset = modelOffset2WidgetOffset(viewer, position);

		if (caret == widgetSelection.x)
			text.setSelectionRange(widgetSelection.y, offset - widgetSelection.y);
		else
			text.setSelectionRange(widgetSelection.x, offset - widgetSelection.x);
		text.invokeAction(ST.DELETE_NEXT);
	} else {
		Point selection = viewer.getSelectedRange();
		final int caret, length;
		if (selection.y != 0) {
			caret = selection.x;
			length = selection.y;
		} else {
			caret = widgetOffset2ModelOffset(viewer, text.getCaretOffset());
			length = position - caret;
		}

		try {
			viewer.getDocument().replace(caret, length, ""); //$NON-NLS-1$
		} catch (BadLocationException exception) {
			// Should not happen
		}
	}
}
 
Example 9
Source File: XtextEditor.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
@Override
protected void setCaretPosition(int position) {
	if (!validateEditorInputState())
		return;

	final int length;
	final ISourceViewer viewer = getSourceViewer();
	StyledText text = viewer.getTextWidget();
	Point widgetSelection = text.getSelection();
	if (isBlockSelectionModeEnabled() && widgetSelection.y != widgetSelection.x) {
		final int caret = text.getCaretOffset();
		final int offset = modelOffset2WidgetOffset(viewer, position);

		if (caret == widgetSelection.x)
			text.setSelectionRange(widgetSelection.y, offset - widgetSelection.y);
		else
			text.setSelectionRange(widgetSelection.x, offset - widgetSelection.x);
		text.invokeAction(ST.DELETE_PREVIOUS);
	} else {
		Point selection = viewer.getSelectedRange();
		if (selection.y != 0) {
			position = selection.x;
			length = selection.y;
		} else {
			length = widgetOffset2ModelOffset(viewer, text.getCaretOffset()) - position;
		}

		try {
			viewer.getDocument().replace(position, length, ""); //$NON-NLS-1$
		} catch (BadLocationException exception) {
			// Should not happen
		}
	}
}
 
Example 10
Source File: JavaEditor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
@Override
protected void setCaretPosition(final int position) {
	if (!validateEditorInputState())
		return;

	final ISourceViewer viewer= getSourceViewer();
	StyledText text= viewer.getTextWidget();
	Point widgetSelection= text.getSelection();
	if (isBlockSelectionModeEnabled() && widgetSelection.y != widgetSelection.x) {
		final int caret= text.getCaretOffset();
		final int offset= modelOffset2WidgetOffset(viewer, position);

		if (caret == widgetSelection.x)
			text.setSelectionRange(widgetSelection.y, offset - widgetSelection.y);
		else
			text.setSelectionRange(widgetSelection.x, offset - widgetSelection.x);
		text.invokeAction(ST.DELETE_NEXT);
	} else {
		Point selection= viewer.getSelectedRange();
		final int caret, length;
		if (selection.y != 0) {
			caret= selection.x;
			length= selection.y;
		} else {
			caret= widgetOffset2ModelOffset(viewer, text.getCaretOffset());
			length= position - caret;
		}

		try {
			viewer.getDocument().replace(caret, length, ""); //$NON-NLS-1$
		} catch (BadLocationException exception) {
			// Should not happen
		}
	}
}
 
Example 11
Source File: JavaEditor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
@Override
protected void setCaretPosition(int position) {
	if (!validateEditorInputState())
		return;

	final int length;
	final ISourceViewer viewer= getSourceViewer();
	StyledText text= viewer.getTextWidget();
	Point widgetSelection= text.getSelection();
	if (isBlockSelectionModeEnabled() && widgetSelection.y != widgetSelection.x) {
		final int caret= text.getCaretOffset();
		final int offset= modelOffset2WidgetOffset(viewer, position);

		if (caret == widgetSelection.x)
			text.setSelectionRange(widgetSelection.y, offset - widgetSelection.y);
		else
			text.setSelectionRange(widgetSelection.x, offset - widgetSelection.x);
		text.invokeAction(ST.DELETE_PREVIOUS);
	} else {
		Point selection= viewer.getSelectedRange();
		if (selection.y != 0) {
			position= selection.x;
			length= selection.y;
		} else {
			length= widgetOffset2ModelOffset(viewer, text.getCaretOffset()) - position;
		}

		try {
			viewer.getDocument().replace(position, length, ""); //$NON-NLS-1$
		} catch (BadLocationException exception) {
			// Should not happen
		}
	}
}
 
Example 12
Source File: NewLineIndentHandler.java    From e4macs with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * @see com.mulgasoft.emacsplus.commands.EmacsPlusCmdHandler#transform(ITextEditor, IDocument, ITextSelection, ExecutionEvent)
 */
@Override
protected int transform(ITextEditor editor, IDocument document, ITextSelection currentSelection, ExecutionEvent event)
throws BadLocationException {
	int result = NO_OFFSET; 
	Control c = MarkUtils.getTextWidget(editor);
	if (c instanceof StyledText) {
		StyledText st = (StyledText)c;
		String ld = st.getLineDelimiter();
		int insertionOffset = getCursorOffset(editor,currentSelection);
		int widgetInsertionOffset = st.getCaretOffset();
		
		// the offset position will be updated by the internals on insertion/indent
		Position caret= new Position(insertionOffset, 0);
		document.addPosition(caret);
		
		st.setSelectionRange(widgetInsertionOffset, 0);
		// operate directly on the widget
		st.replaceTextRange(widgetInsertionOffset, 0, ld);
		document.removePosition(caret);
		
		if (st.getSelection().x == widgetInsertionOffset) {
			// move cursor by the amount detected
			result = getCursorOffset(editor) + caret.offset - insertionOffset;
		} 
	}
	return result;
}
 
Example 13
Source File: SubWordActions.java    From Pydev with Eclipse Public License 1.0 5 votes vote down vote up
@Override
protected void setCaretPosition(final int position) {
    if (!validateEditorInputState()) {
        return;
    }

    final ISourceViewer viewer = getSourceViewer();
    StyledText text = viewer.getTextWidget();
    Point widgetSelection = text.getSelection();
    if (isBlockSelectionModeEnabled() && widgetSelection.y != widgetSelection.x) {
        final int caret = text.getCaretOffset();
        final int offset = modelOffset2WidgetOffset(viewer, position);

        if (caret == widgetSelection.x) {
            text.setSelectionRange(widgetSelection.y, offset - widgetSelection.y);
        } else {
            text.setSelectionRange(widgetSelection.x, offset - widgetSelection.x);
        }
        text.invokeAction(ST.DELETE_NEXT);
    } else {
        Point selection = viewer.getSelectedRange();
        final int caret, length;
        if (selection.y != 0) {
            caret = selection.x;
            length = selection.y;
        } else {
            caret = widgetOffset2ModelOffset(viewer, text.getCaretOffset());
            length = position - caret;
        }

        try {
            viewer.getDocument().replace(caret, length, ""); //$NON-NLS-1$
        } catch (BadLocationException exception) {
            // Should not happen
        }
    }
}
 
Example 14
Source File: SubWordActions.java    From Pydev with Eclipse Public License 1.0 5 votes vote down vote up
@Override
protected void setCaretPosition(int position) {
    if (!validateEditorInputState()) {
        return;
    }

    final int length;
    final ISourceViewer viewer = getSourceViewer();
    StyledText text = viewer.getTextWidget();
    Point widgetSelection = text.getSelection();
    if (isBlockSelectionModeEnabled() && widgetSelection.y != widgetSelection.x) {
        final int caret = text.getCaretOffset();
        final int offset = modelOffset2WidgetOffset(viewer, position);

        if (caret == widgetSelection.x) {
            text.setSelectionRange(widgetSelection.y, offset - widgetSelection.y);
        } else {
            text.setSelectionRange(widgetSelection.x, offset - widgetSelection.x);
        }
        text.invokeAction(ST.DELETE_PREVIOUS);
    } else {
        Point selection = viewer.getSelectedRange();
        if (selection.y != 0) {
            position = selection.x;
            length = selection.y;
        } else {
            length = widgetOffset2ModelOffset(viewer, text.getCaretOffset()) - position;
        }

        try {
            viewer.getDocument().replace(position, length, ""); //$NON-NLS-1$
        } catch (BadLocationException exception) {
            // Should not happen
        }
    }
}