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

The following examples show how to use org.eclipse.swt.custom.StyledText#getCaretOffset() . 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: MarkExchangeHandler.java    From e4macs with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Support exchange for simple mark on TextConsole
 * 
 * @see com.mulgasoft.emacsplus.commands.IConsoleDispatch#consoleDispatch(org.eclipse.ui.console.TextConsoleViewer, org.eclipse.ui.console.IConsoleView, org.eclipse.core.commands.ExecutionEvent)
 */
public Object consoleDispatch(TextConsoleViewer viewer, IConsoleView activePart, ExecutionEvent event) {
	int mark = viewer.getMark();
	StyledText st = viewer.getTextWidget(); 
	if (mark != -1) {
		try {
			st.setRedraw(false);
			int offset = st.getCaretOffset();
			viewer.setMark(offset);
			st.setCaretOffset(mark);
			int len = offset - mark;
			viewer.setSelectedRange(offset, -len);
		} finally {
			st.setRedraw(true);
		}
	}
	return null;
}
 
Example 2
Source File: PyUnitView.java    From Pydev with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public void mouseUp(MouseEvent e) {
    Widget w = e.widget;
    if (w instanceof StyledText) {
        StyledText styledText = (StyledText) w;
        int offset = styledText.getCaretOffset();
        if (offset >= 0 && offset < styledText.getCharCount()) {
            StyleRange styleRangeAtOffset = styledText.getStyleRangeAtOffset(offset);
            if (styleRangeAtOffset instanceof StyleRangeWithCustomData) {
                StyleRangeWithCustomData styleRangeWithCustomData = (StyleRangeWithCustomData) styleRangeAtOffset;
                Object l = styleRangeWithCustomData.customData;
                if (l instanceof IHyperlink) {
                    ((IHyperlink) l).linkActivated();
                }
            }
        }
    }
}
 
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: AbstractThemeableEditor.java    From APICloud-Studio with GNU General Public License v3.0 6 votes vote down vote up
public int getCaretOffset()
{
	ISourceViewer sourceViewer = getSourceViewer();
	if (sourceViewer == null)
	{
		return -1;
	}
	StyledText styledText = sourceViewer.getTextWidget();
	if (styledText == null)
	{
		return -1;
	}

	if (sourceViewer instanceof ITextViewerExtension5)
	{
		ITextViewerExtension5 extension = (ITextViewerExtension5) sourceViewer;
		return extension.widgetOffset2ModelOffset(styledText.getCaretOffset());
	}
	int offset = sourceViewer.getVisibleRegion().getOffset();
	return offset + styledText.getCaretOffset();
}
 
Example 6
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 7
Source File: CellEditor.java    From tmxeditor8 with GNU General Public License v2.0 5 votes vote down vote up
public void insertCanonicalValue(Object canonicalValue) {
	StyledText text = textViewer.getTextWidget();
	if (text == null || text.isDisposed()) {
		return;
	}

	int offset = text.getCaretOffset();
	text.insert(canonicalValue.toString());
	text.setCaretOffset(offset + canonicalValue.toString().length());
}
 
Example 8
Source File: CommandExecutionUtils.java    From APICloud-Studio with GNU General Public License v3.0 5 votes vote down vote up
private static int getCaretOffset(ITextViewer textViewer)
{
	StyledText textWidget = textViewer.getTextWidget();
	int caretOffset = textWidget.getCaretOffset();
	if (textViewer instanceof ITextViewerExtension5)
	{
		ITextViewerExtension5 extension = (ITextViewerExtension5) textViewer;
		return extension.widgetOffset2ModelOffset(caretOffset);
	}
	return caretOffset;
}
 
Example 9
Source File: CommandElementsProvider.java    From APICloud-Studio with GNU General Public License v3.0 5 votes vote down vote up
public Point getCommandElementsPopupLocation()
{
	Object control = textEditor.getAdapter(Control.class);
	if (control instanceof StyledText)
	{
		StyledText textWidget = (StyledText) control;
		int caretOffset = textWidget.getCaretOffset();
		Point locationAtOffset = textWidget.getLocationAtOffset(caretOffset);
		locationAtOffset = textWidget.toDisplay(locationAtOffset.x,
				locationAtOffset.y + textWidget.getLineHeight(caretOffset) + 2);
		return locationAtOffset;
	}
	return null;
}
 
Example 10
Source File: YankPopHandler.java    From e4macs with Eclipse Public License 1.0 5 votes vote down vote up
/**
 *  Simulate yank pop
 *  
 * @see com.mulgasoft.emacsplus.commands.BaseYankHandler#paste(org.eclipse.core.commands.ExecutionEvent, org.eclipse.swt.custom.StyledText)
 */
protected void paste(ExecutionEvent event, StyledText widget, boolean isProcess) {
	KillRing kb = KillRing.getInstance();
	if (kb.isYanked()) {
		String cacheText = KillRing.getInstance().getClipboardText();
		String prevText = convertDelimiters(kb.lastYank(),isProcess);
		String yankText = convertDelimiters(kb.yankPop(),isProcess);
		try {
			int offset = widget.getCaretOffset();
			if (prevText == null || !prevText.equals(widget.getText(offset-prevText.length(), offset-1))) {
				kb.setYanked(false);
				EmacsPlusUtils.showMessage(HandlerUtil.getActivePart(event), EmacsPlusActivator.getString(YP_DISABLED),false);
				return;
			}
			widget.setRedraw(false);
			widget.setSelection(offset - prevText.length(), offset);
			kb.setClipboardText(yankText);
			super.paste(event, widget);
		} finally {
			if (cacheText != null) {
				kb.setClipboardText(cacheText);
			}
			widget.setRedraw(true);
		}
	} else {
		EmacsPlusUtils.showMessage(HandlerUtil.getActivePart(event), EmacsPlusActivator.getString(YP_DISABLED),false);
	}
}
 
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(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 12
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 13
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 14
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 15
Source File: SearchMinibuffer.java    From e4macs with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Allow single character operations on an existing search string
 * 
 * @param event
 * @param search - true if we should search after operation
 * @return true if event handled
 */
protected boolean dispatchAltCtrl(VerifyEvent event, boolean search) {
	// allows editing of history/yanked search string
	boolean result = false;
	switch (event.keyCode) {
	case CM_DEL:
		// remove the last character and search
		super.backSpaceChar(event);
		getRX().bsChar();
		if (search) {
			findNext(getSearchString(),true);
		}
		updateStatusLine(getMBString());
		result = true;
		break;
	case CM_ADD:
		StyledText w = getTextWidget(); 
		int catPos = w.getCaretOffset() + (isForward() ? 0 : getMBLength());
		String text = w.getText(catPos, catPos);
		if (w.getLineDelimiter().contains(text)) {
			text = w.getLineDelimiter();
		}
		addIt(text);
		if (search) {
			findNext(getSearchString(),true);
		}
		updateStatusLine(getMBString());
		result = true;
		break;
	}
	if (!result && hasBinding(event)) {
		// exit search and execute the command
		ITextEditor ed = getEditor();					
		leave();
		executeBinding(ed,event);
		result = true;
	}
	return result;
}
 
Example 16
Source File: AbstractJavaCompletionProposal.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Creates a style range for the text viewer.
 * 
 * @param viewer the text viewer
 * @return the new style range for the text viewer or <code>null</code>
 * @since 3.6
 */
private StyleRange createStyleRange(ITextViewer viewer) {
	StyledText text= viewer.getTextWidget();
	if (text == null || text.isDisposed())
		return null;

	int widgetCaret= text.getCaretOffset();

	int modelCaret= 0;
	if (viewer instanceof ITextViewerExtension5) {
		ITextViewerExtension5 extension= (ITextViewerExtension5) viewer;
		modelCaret= extension.widgetOffset2ModelOffset(widgetCaret);
	} else {
		IRegion visibleRegion= viewer.getVisibleRegion();
		modelCaret= widgetCaret + visibleRegion.getOffset();
	}

	if (modelCaret >= getReplacementOffset() + getReplacementLength())
		return null;

	int length= getReplacementOffset() + getReplacementLength() - modelCaret;

	Color foreground= getForegroundColor();
	Color background= getBackgroundColor();

	return new StyleRange(modelCaret, length, foreground, background);
}
 
Example 17
Source File: StyledTextCellEditor.java    From translationstudio8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 将指定文本添加到光标所在位置。 robert 2011-12-21
 * @param canonicalValue
 *            ;
 */
public void insertCanonicalValue(Object canonicalValue) {
	StyledText text = viewer.getTextWidget();
	if (text == null || text.isDisposed()) {
		return;
	}

	int offset = text.getCaretOffset();
	text.insert(canonicalValue.toString());
	text.setCaretOffset(offset + canonicalValue.toString().length());
}
 
Example 18
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
        }
    }
}
 
Example 19
Source File: SearchMinibuffer.java    From e4macs with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * Find the next instance of the search string in the buffer
 * 
 * @param searchStr
 * @param addit - true when just added text to search string
 * @return true if we found a match
 */
protected boolean findNext(String searchStr,boolean addit) {
	if (isSearchEnabled()) {
		setRegexLD(false);
		StyledText text = getTextWidget();
		IFindReplaceTarget target = getTarget();
		if (text != null && target != null) {
			int searchIndex = getSearchOffset();
			if (searchIndex != WRAP_INDEX) {
				Point p = getSelection();
				// if not building the string (or wrapping) search from caret position
				if (!addit) {
					if (isFound() && p != null) {
						searchIndex = p.x;
						if (isForward()) {
							// increment index by (actual or implicit) selection width
							searchIndex += (p.y == 0 ? getEol().length() : p.y); 
						}
					} else {
						// Cannot use target.getSelection since that does not return which side of the
						// selection the caret is on.
						searchIndex = text.getCaretOffset();
					}
				}
				if (!forward && p != null) {
					// if at beginning of line, back up by full eol, else just 1
					searchIndex -= ((p.x == text.getOffsetAtLine(text.getLineAtOffset(p.x))) ? getEol().length() : 1);
				}
			}
			int index = findTarget(target,searchStr,searchIndex, forward);
			boolean justFound = (index != WRAP_INDEX);
			if (isFound() && !justFound && (addit && !isRegexp())) {
				beep();
			} else if (!addit) {
				setSearchOffset(index);
			}
			setFound(justFound);
		}
	}
	return isFound();
}
 
Example 20
Source File: AbstractCompletionProposalExtension.java    From Pydev with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * Called when Ctrl is selected during the completions
 * @see org.eclipse.jface.text.contentassist.ICompletionProposalExtension2#selected(org.eclipse.jface.text.ITextViewer, boolean)
 */
@Override
public void selected(ITextViewer viewer, boolean smartToggle) {
    if (smartToggle) {
        StyledText text = viewer.getTextWidget();
        if (text == null || text.isDisposed()) {
            return;
        }

        int widgetCaret = text.getCaretOffset();
        IDocument document = viewer.getDocument();
        int finalOffset = widgetCaret;

        try {
            if (finalOffset >= document.getLength()) {
                unselected(viewer);
                return;
            }
            char c;
            do {
                c = document.getChar(finalOffset);
                finalOffset++;
            } while (isValidChar(c) && finalOffset < document.getLength());

            if (c == '(') {
                fLastIsPar = true;
            } else {
                fLastIsPar = false;
            }

            if (!isValidChar(c)) {
                finalOffset--;
            }

            this.fLen = finalOffset - widgetCaret;
            this.getPresentationUpdater().selected(viewer, widgetCaret, this.fLen);
        } catch (BadLocationException e) {
            Log.log(e);
        }

    } else {
        unselected(viewer);
    }
}