Java Code Examples for org.eclipse.jface.text.source.ISourceViewer#getTextWidget()

The following examples show how to use org.eclipse.jface.text.source.ISourceViewer#getTextWidget() . 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: ClassFileEditor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Posts this runnable into the event queue if not already there.
 *
 * @param input the input to be set when executed
 */
public void post(IClassFileEditorInput input) {

	synchronized(this) {
		if (fPosted) {
			if (isEqualInput(input, fClassFileEditorInput))
				fClassFileEditorInput= input;
			return;
		}
	}

	if (isEqualInput(input, getEditorInput())) {
		ISourceViewer viewer= getSourceViewer();
		if (viewer != null) {
			StyledText textWidget= viewer.getTextWidget();
			if (textWidget != null && !textWidget.isDisposed()) {
				synchronized (this) {
					fPosted= true;
					fClassFileEditorInput= input;
				}
				textWidget.getDisplay().asyncExec(this);
			}
		}
	}
}
 
Example 2
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 3
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 4
Source File: ThemeableEditorExtension.java    From APICloud-Studio with GNU General Public License v3.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
private void overrideRulerColors()
{
	IThemeableEditor editor = this.fEditor.get();

	// default to bg color of surrounding composite
	Color bg = null;
	// Use editor background color if we can
	if (editor != null)
	{
		ISourceViewer sv = editor.getISourceViewer();
		if (sv != null)
		{
			StyledText text = sv.getTextWidget();
			if (text != null)
			{
				bg = text.getBackground();
				// copy the color because for some reason it gets disposed
				bg = ThemePlugin.getDefault().getColorManager().getColor(bg.getRGB());
			}
		}

		// force the colors for all the ruler columns (specifically so we force the folding bg to match).
		CompositeRuler ruler = (CompositeRuler) editor.getIVerticalRuler();
		Iterator<IVerticalRulerColumn> iter = ruler.getDecoratorIterator();
		while (iter.hasNext())
		{
			IVerticalRulerColumn column = iter.next();
			column.getControl().setBackground(bg);
		}
	}

	if (fLineColumn != null)
	{
		fLineColumn.setBackground(bg);
	}
}
 
Example 5
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 6
Source File: PyMoveLineAction.java    From Pydev with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public void run() {
    // get involved objects
    if (pyEdit == null) {
        return;
    }

    if (!validateEditorInputState()) {
        return;
    }

    ISourceViewer viewer = pyEdit.getEditorSourceViewer();
    if (viewer == null) {
        return;
    }

    IDocument document = viewer.getDocument();
    if (document == null) {
        return;
    }

    StyledText widget = viewer.getTextWidget();
    if (widget == null) {
        return;
    }

    // get selection
    ITextSelection sel = (ITextSelection) viewer.getSelectionProvider().getSelection();
    move(pyEdit, viewer, document, new CoreTextSelection(document, sel.getOffset(), sel.getLength()));
}
 
Example 7
Source File: AbstractThemeableEditor.java    From APICloud-Studio with GNU General Public License v3.0 5 votes vote down vote up
@Override
protected void initializeViewerColors(ISourceViewer viewer)
{
	if (viewer == null || viewer.getTextWidget() == null)
		return;
	super.initializeViewerColors(viewer);
}
 
Example 8
Source File: TypeScriptEditor.java    From typescript.java with MIT License 5 votes vote down vote up
public void install() {
	ISourceViewer sourceViewer = getSourceViewer();
	if (sourceViewer == null)
		return;

	StyledText text = sourceViewer.getTextWidget();
	if (text == null || text.isDisposed())
		return;

	sourceViewer.addTextInputListener(this);

	IDocument document = sourceViewer.getDocument();
	if (document != null)
		document.addDocumentListener(this);
}
 
Example 9
Source File: PairedBracketsPainter.java    From xds-ide with Eclipse Public License 1.0 5 votes vote down vote up
private PairedBracketsPainter( ISourceViewer sourceViewer 
                             , PairedBracketsMatcher matcher ) 
{
    fSourceViewer = sourceViewer;
    fMatcher      = matcher;
    fTextWidget   = sourceViewer.getTextWidget();
    fColor        = new Color(Display.getDefault(), DEF_RGB_MATCHED); // to don't NPE on problems with store
}
 
Example 10
Source File: RenameRefactoringPopup.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
protected void start() {
	editor.getSite().getWorkbenchWindow().getPartService().addPartListener(this);
	final ISourceViewer viewer = editor.getInternalSourceViewer();
	final StyledText textWidget = viewer.getTextWidget();
	textWidget.addControlListener(this);
	textWidget.addMouseListener(this);
	textWidget.addKeyListener(this);
	editor.getSite().getShell().addControlListener(this);
	viewer.addViewportListener(this);
	popup.addDisposeListener(new DisposeListener() {
		@Override
		public void widgetDisposed(DisposeEvent e) {
			editor.getSite().getWorkbenchWindow().getPartService()
					.removePartListener(PopupVisibilityManager.this);
			if (!textWidget.isDisposed()) {
				textWidget.removeControlListener(PopupVisibilityManager.this);
				textWidget.removeMouseListener(PopupVisibilityManager.this);
				textWidget.removeKeyListener(PopupVisibilityManager.this);
			}
			editor.getSite().getShell().removeControlListener(PopupVisibilityManager.this);
			viewer.removeViewportListener(PopupVisibilityManager.this);
			if (menuImage != null) {
				menuImage.dispose();
				menuImage = null;
			}
			if (menuManager != null) {
				menuManager.dispose();
				menuManager = null;
			}
		}
	});
}
 
Example 11
Source File: EditorOverlay.java    From n4js with Eclipse Public License 1.0 5 votes vote down vote up
private void draw() {
	XtextEditor editor = EditorUtils.getActiveXtextEditor();
	if (editor != null && (hoveredElement != null || !selectedElements.isEmpty())) {
		ISourceViewer isv = editor.getInternalSourceViewer();
		styledText = isv.getTextWidget();
		drawSelection();
	} else {
		clear();
	}
}
 
Example 12
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 13
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 14
Source File: RenameLinkedMode.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public static RenameLinkedMode getActiveLinkedMode() {
	if (fgActiveLinkedMode != null) {
		ISourceViewer viewer= fgActiveLinkedMode.fEditor.getViewer();
		if (viewer != null) {
			StyledText textWidget= viewer.getTextWidget();
			if (textWidget != null && ! textWidget.isDisposed()) {
				return fgActiveLinkedMode;
			}
		}
		// make sure we don't hold onto the active linked mode if anything went wrong with canceling:
		fgActiveLinkedMode= null;
	}
	return null;
}
 
Example 15
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 16
Source File: EditorUtilities.java    From ContentAssist with MIT License 5 votes vote down vote up
/**
 * Obtains the styled text of an editor.
 * @param editor the editor
 * @return the styled text of the editor
 */
public static StyledText getStyledText(IEditorPart editor) {
    ISourceViewer viewer = getSourceViewer(editor);
    if (viewer != null) {
        return viewer.getTextWidget();
    }
    return null;
}
 
Example 17
Source File: AbstractSyntaxColoringPage.java    From birt with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * Initializes the colors of the source preview window based on the values in the Editors' UI preference store
 * 
 * @param viewer the {@link ISourceViewer} used as the source preview
 */
protected void initializeSourcePreviewColors(ISourceViewer viewer) {
	final IPreferenceStore store = EditorsUI.getPreferenceStore();
	if (store != null && viewer != null) {

		final StyledText styledText = viewer.getTextWidget();

		// ----------- foreground color --------------------
		Color color = store.getBoolean(AbstractTextEditor.PREFERENCE_COLOR_FOREGROUND_SYSTEM_DEFAULT) ? null : createColor(store, AbstractTextEditor.PREFERENCE_COLOR_FOREGROUND, styledText.getDisplay());
		styledText.setForeground(color);

		if (fForegroundColor != null)
			fForegroundColor.dispose();

		fForegroundColor = color;

		// ---------- background color ----------------------
		color = store.getBoolean(AbstractTextEditor.PREFERENCE_COLOR_BACKGROUND_SYSTEM_DEFAULT) ? null : createColor(store, AbstractTextEditor.PREFERENCE_COLOR_BACKGROUND, styledText.getDisplay());
		styledText.setBackground(color);

		if (fBackgroundColor != null)
			fBackgroundColor.dispose();

		fBackgroundColor = color;

		// ----------- selection foreground color --------------------
		color = store.getBoolean(AbstractTextEditor.PREFERENCE_COLOR_SELECTION_FOREGROUND_SYSTEM_DEFAULT) ? null : createColor(store, AbstractTextEditor.PREFERENCE_COLOR_SELECTION_FOREGROUND, styledText.getDisplay());
		styledText.setSelectionForeground(color);

		if (fSelectionForegroundColor != null)
			fSelectionForegroundColor.dispose();

		fSelectionForegroundColor = color;

		// ---------- selection background color ----------------------
		color = store.getBoolean(AbstractTextEditor.PREFERENCE_COLOR_SELECTION_BACKGROUND_SYSTEM_DEFAULT) ? null : createColor(store, AbstractTextEditor.PREFERENCE_COLOR_SELECTION_BACKGROUND, styledText.getDisplay());
		styledText.setSelectionBackground(color);

		if (fSelectionBackgroundColor != null)
			fSelectionBackgroundColor.dispose();

		fSelectionBackgroundColor = color;
	}
}
 
Example 18
Source File: TypeScriptEditor.java    From typescript.java with MIT License 4 votes vote down vote up
/**
 * Highlights and moves to a corresponding element in editor
 * 
 * @param reference
 *            corresponding entity in editor
 * @param moveCursor
 *            if true, moves cursor to the reference
 */
private void setSelection(NavigationBarItem reference, boolean moveCursor) {
	if (reference == null) {
		return;
	}

	if (moveCursor) {
		markInNavigationHistory();
	}

	ISourceViewer sourceViewer = getSourceViewer();
	if (sourceViewer == null) {
		return;
	}
	StyledText textWidget = sourceViewer.getTextWidget();
	if (textWidget == null) {
		return;
	}
	try {
		Location start = reference.getSpans().get(0).getStart();
		Location end = reference.getSpans().get(0).getEnd();

		if (start == null || end == null)
			return;

		ITypeScriptFile tsFile = getTypeScriptFile();

		int offset = tsFile.getPosition(start);
		int length = tsFile.getPosition(end) - offset;

		if (offset < 0 || length < 0 || length > sourceViewer.getDocument().getLength()) {
			return;
		}
		textWidget.setRedraw(false);

		// Uncomment that if we wish to select only variable and not the
		// whole block.
		// but there is a bug with this code with
		// private a: string. it's the first 'a' (of private) which is
		// selected and not the second.
		// String documentPart = sourceViewer.getDocument().get(offset,
		// length);
		//
		// // Try to find name because position returns for whole block
		// String name = reference.getText();
		// if (name != null) {
		// int nameoffset = documentPart.indexOf(name);
		// if (nameoffset != -1) {
		// offset += nameoffset;
		// length = name.length();
		// }
		// }
		if (length > 0) {
			setHighlightRange(offset, length, moveCursor);
		}

		if (!moveCursor) {
			return;
		}

		if (offset > -1 && length > 0) {
			sourceViewer.revealRange(offset, length);
			// Selected region begins one index after offset
			sourceViewer.setSelectedRange(offset, length);
			markInNavigationHistory();
		}
	} catch (Exception e) {

	} finally {
		textWidget.setRedraw(true);
	}
}
 
Example 19
Source File: XtextEditor.java    From xtext-eclipse with Eclipse Public License 2.0 4 votes vote down vote up
@Override
protected void initializeViewerColors(ISourceViewer viewer) {
	if (viewer == null || viewer.getTextWidget() == null)
		return;
	super.initializeViewerColors(viewer);
}
 
Example 20
Source File: AbstractDirtyStateAwareEditorCallback.java    From xtext-eclipse with Eclipse Public License 2.0 4 votes vote down vote up
@Override
public void addVerifyListener(VerifyListener listener) {
	ISourceViewer sourceViewer = currentEditor.getInternalSourceViewer();
	StyledText widget = sourceViewer.getTextWidget();
	widget.addVerifyListener(listener);
}