Java Code Examples for org.eclipse.ui.texteditor.ITextEditor#getAdapter()

The following examples show how to use org.eclipse.ui.texteditor.ITextEditor#getAdapter() . 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: ModulaSearchResultPage.java    From xds-ide with Eclipse Public License 1.0 6 votes vote down vote up
protected void showMatch( Match match, int currentOffset
                        , int currentLength, boolean activate 
                        ) throws PartInitException 
{
    if (match instanceof ModulaSymbolMatch) {
        try {
            ModulaSymbolMatch em = (ModulaSymbolMatch)match;
            IFile f = em.getFile();
            IWorkbenchPage page = WorkbenchUtils.getActivePage();
            IEditorDescriptor desc = PlatformUI.getWorkbench().getEditorRegistry().getDefaultEditor(f.getName());
            IEditorPart ep = page.openEditor(new FileEditorInput(f), desc.getId());
            ITextEditor te = (ITextEditor)ep;
            Control ctr = (Control)te.getAdapter(Control.class);
            ctr.setFocus();
            te.selectAndReveal(em.getOffset(), em.getLength());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
 
Example 2
Source File: ReviewMarkerContributionFactory.java    From git-appraise-eclipse with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public void createContributionItems(IServiceLocator serviceLocator, IContributionRoot additions) {
  ITextEditor editor = (ITextEditor)
      PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActivePart();
  IVerticalRulerInfo rulerInfo = editor.getAdapter(IVerticalRulerInfo.class);

  try {
    List<IMarker> markers = getMarkers(editor, rulerInfo);
    additions.addContributionItem(new ReviewMarkerMenuContribution(editor, markers), null);
    if (!markers.isEmpty()) {
      additions.addContributionItem(new Separator(), null);
    }
  } catch (CoreException e) {
    AppraiseUiPlugin.logError("Error creating marker context menus", e);
  }
}
 
Example 3
Source File: RectangleExecHandler.java    From e4macs with Eclipse Public License 1.0 6 votes vote down vote up
@Override
protected boolean doExecuteResult(ITextEditor editor, Object minibufferResult) {
	// use widget to avoid unpleasant scrolling side effects of IRewriteTarget
	Control widget = getTextWidget(editor);
	// wrap in compound change and no redraw
	IRewriteTarget rt = (IRewriteTarget) editor.getAdapter(IRewriteTarget.class);
	try {
		if (rt != null) {
			rt.beginCompoundChange();
		}
		widget.setRedraw(false);
		int offset = rs.updateRectangle(editor, document, selection, (String)minibufferResult, isReplace(),false);
		if (offset > 0) {
			selectAndReveal(editor, offset, offset);
		}
	} finally  {
		if (rt != null) {
			rt.endCompoundChange();
		}
		widget.setRedraw(true);
	}
	return true;
}
 
Example 4
Source File: MarkUtils.java    From e4macs with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Get the selection point from the text widget
 * 
 * @param editor
 * 
 * @return the selection point iff it is a (Styled)Text widget
 * x is the offset of the first	selected character, 
 * y is the offset after the last selected character.
 */
public static Point getWidgetSelection(ITextEditor editor) {
	Point result = null;
	Control text = (Control) editor.getAdapter(Control.class);
	if (text instanceof StyledText) {
		result = ((StyledText) text).getSelection();
	} else if (text instanceof Text) {
		result = ((Text) text).getSelection();
	}
	return result;
}
 
Example 5
Source File: ToggleCommentAction.java    From goclipse with Eclipse Public License 1.0 5 votes vote down vote up
protected boolean isTargetOperationEnabled() {
	ITextEditor editor = getTextEditor();
	if(editor != null) {
		fOperationTarget = (ITextOperationTarget) editor.getAdapter(ITextOperationTarget.class);
	}
	
	return fOperationTarget != null && 
		fOperationTarget.canDoOperation(ITextOperationTarget.PREFIX) && 
		fOperationTarget.canDoOperation(ITextOperationTarget.STRIP_PREFIX);
}
 
Example 6
Source File: PyMoveLineAction.java    From Pydev with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Displays information in the status line why a line move is not possible
 */
private void showStatus() {
    ITextEditor textEditor = getTextEditor();
    IEditorStatusLine status = textEditor.getAdapter(IEditorStatusLine.class);
    if (status == null) {
        return;
    }
    status.setMessage(false,
            "Move not possible - Uncheck \"Show Source of Selected Element Only\" to see the entire document",
            null);
}
 
Example 7
Source File: MacroModeStateHandler.java    From Pydev with Eclipse Public License 1.0 5 votes vote down vote up
private void restore(ITextEditor textEditor, String actionId) {
    Boolean b = fMemento.get(actionId);
    if (b != null && b) {
        Control control = textEditor.getAdapter(Control.class);
        if (control != null && !control.isDisposed()) {
            // Do nothing if already disposed.
            IAction action = textEditor.getAction(actionId);
            if (action instanceof TextEditorAction) {
                TextEditorAction textEditorAction = (TextEditorAction) action;
                textEditorAction.setEditor(textEditor);
                textEditorAction.update();
            }
        }
    }
}
 
Example 8
Source File: RegisterRectangleHandler.java    From e4macs with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * @see com.mulgasoft.emacsplus.minibuffer.IMinibufferExecutable#executeResult(org.eclipse.ui.texteditor.ITextEditor, java.lang.Object)
 */
public boolean doExecuteResult(ITextEditor editor, Object minibufferResult) {
	
	if (minibufferResult != null && ((String)minibufferResult).length() > 0) {
		String key = (String)minibufferResult;
		ITextSelection selection = getImpliedSelection(editor, getCurrentSelection(editor));
		IDocument document = getThisDocument(editor);
		// if called with ^U, then delete text as well
		boolean delete = isEditable() && getCallCount() > 1;
		String[] rect;
		// use widget to avoid unpleasant scrolling side effects of IRewriteTarget			
		Control widget = getTextWidget(editor);
		IRewriteTarget rt = (IRewriteTarget) editor.getAdapter(IRewriteTarget.class);
		try {
			if (delete) {
				// wrap in compound change and no redraw
				widget.setRedraw(false);
				if (rt != null) {
					rt.beginCompoundChange();
				}
			}
			rect = new RectangleSupport(document,editor).copyRectangle(editor, document, selection, delete);
			if (rect != null && rect.length > 0) {
				TecoRegister.getInstance().put(key,rect);
				showResultMessage(editor, String.format(COPIED, key), false);
			}
		} catch (BadLocationException e) {
			showResultMessage(editor, BAD_INSERT_LOCATION, true);
		} finally  {
			if (rt != null) {
				rt.endCompoundChange();
			}
			widget.setRedraw(true);
		}
	} else {
		showResultMessage(editor, NO_REGISTER, true);
	}
	return true;
}
 
Example 9
Source File: KbdMacroExecuteHandler.java    From e4macs with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Get the undo Runnable wrappers
 * 
 * @param editor
 * @return begin and end undoProtect wrappers
 */
protected Runnable[] undoProtect(ITextEditor editor, final MacroCount keepCount) {
	Runnable[] result = new Runnable[2];
	// use widget to avoid unpleasant scrolling side effects of IRewriteTarget
	final Control widget = getTextWidget(editor);;
	final IRewriteTarget rt = (IRewriteTarget) editor.getAdapter(IRewriteTarget.class);;
	result[0] = new Runnable() {
		public void run() {
			if (rt != null) {
				rt.beginCompoundChange();
			}
			setRedraw(widget,false);
		}
	};
	result[1] = new Runnable() {
		public void run() {
			setRedraw(widget, true);
			if (rt != null) {
				rt.endCompoundChange();
			}
			if (!isInterrupted() && keepCount != null) {
				// we've finished one more loop of the main macro
				keepCount.addCounter();
			}
		}
	};
	return result;
}
 
Example 10
Source File: MarkUtils.java    From e4macs with Eclipse Public License 1.0 5 votes vote down vote up
public static String getWidgetLineDelimiter(ITextEditor editor) {

		String result = "\n"; //$NON-NLS-1$
		Control w = (Control) editor.getAdapter(Control.class);
		if (w instanceof StyledText) {
			result = ((StyledText) w).getLineDelimiter();
		} else if (w instanceof Text) {
			result = ((Text) w).getLineDelimiter();
		}
		return result;
	}
 
Example 11
Source File: TestSyntaxHighlighting.java    From wildwebdeveloper with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testJSXHighlighting() throws CoreException {
	IFile file = project.getFile("test.jsx");
	file.create(new ByteArrayInputStream("var n = 4;\n".getBytes()), true, null);
	ITextEditor editor = (ITextEditor)IDE.openEditor(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage(), file);
	StyledText widget = (StyledText) editor.getAdapter(Control.class);
	Color defaultTextColor = widget.getForeground();
	assertTrue("Missing syntax highlighting", new DisplayHelper() {
		@Override protected boolean condition() {
			return Arrays.stream(widget.getStyleRanges()).anyMatch(range -> range.foreground != null && !defaultTextColor.equals(range.foreground));
		}
	}.waitForCondition(widget.getDisplay(), 2000));
}
 
Example 12
Source File: MarkUtils.java    From e4macs with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Get the selection point from the text widget
 * 
 * @param editor
 * 
 * @return the selection point iff it is a (Styled)Text widget
 * x is the offset of the first	selected character, 
 * y is the offset after the last selected character.
 */
public static int getCharCount(ITextEditor editor) {
	int result = -1;
	Control text = (Control) editor.getAdapter(Control.class);
	if (text instanceof StyledText) {
		result = ((StyledText) text).getCharCount();
	} else if (text instanceof Text) {
		result = ((Text) text).getCharCount();
	}
	return result;
}
 
Example 13
Source File: MarkUtils.java    From e4macs with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Set Mark at current cursor position and push the previous Mark on the
 * Mark Ring
 * 
 * @param editor
 * @return the mark position in document coords
 */
public static int setMark(ITextEditor editor) {
	IMarkRegionTarget markTarget = (IMarkRegionTarget) editor.getAdapter(IMarkRegionTarget.class);
	int localMark = getMark(editor);
	markTarget.setMarkAtCursor(true);
	int newMark = getMark(editor);
	MarkRing.addMark(editor, editor.getDocumentProvider().getDocument(editor.getEditorInput()), localMark, newMark);
	EmacsPlusUtils.showMessage(editor, MARK_SET, false);
	return newMark;
}
 
Example 14
Source File: SpecificContentAssistAction.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private boolean computeEnablement(ITextEditor editor) {
	if (editor == null)
		return false;
	
	ITextOperationTarget target= (ITextOperationTarget) editor.getAdapter(ITextOperationTarget.class);
	if (target == null || ! target.canDoOperation(ISourceViewer.CONTENTASSIST_PROPOSALS))
		return false;
	
	IJavaProject javaProject = EditorUtility.getJavaProject(editor.getEditorInput());
	if (! fCategory.matches(javaProject))
		return false;
	
	ISelection selection= editor.getSelectionProvider().getSelection();
	return isValidSelection(selection);
}
 
Example 15
Source File: ClipboardOperationAction.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private void doPasteWithImportsOperation() {
	ITextEditor editor= getTextEditor();
	IJavaElement inputElement= JavaUI.getEditorInputTypeRoot(editor.getEditorInput());

	Clipboard clipboard= new Clipboard(getDisplay());
	try {
		ClipboardData importsData= (ClipboardData)clipboard.getContents(fgTransferInstance);
		if (importsData != null && inputElement instanceof ICompilationUnit && !importsData.isFromSame(inputElement)) {
			// combine operation and adding of imports
			IRewriteTarget target= (IRewriteTarget)editor.getAdapter(IRewriteTarget.class);
			if (target != null) {
				target.beginCompoundChange();
			}
			try {
				fOperationTarget.doOperation(fOperationCode);
				addImports((ICompilationUnit)inputElement, importsData);
			} catch (CoreException e) {
				JavaPlugin.log(e);
			} finally {
				if (target != null) {
					target.endCompoundChange();
				}
			}
		} else {
			fOperationTarget.doOperation(fOperationCode);
		}
	} finally {
		clipboard.dispose();
	}
}
 
Example 16
Source File: PropertyKeyHyperlinkDetector.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private void showErrorInStatusLine(final String message, ITextEditor textEditor) {
	Display display= textEditor.getEditorSite().getShell().getDisplay();
	display.beep();
	final IEditorStatusLine statusLine= (IEditorStatusLine)textEditor.getAdapter(IEditorStatusLine.class);
	if (statusLine != null) {
		display.asyncExec(new Runnable() {
			/*
			 * @see java.lang.Runnable#run()
			 */
			public void run() {
				statusLine.setMessage(true, message, null);
			}
		});
	}
}
 
Example 17
Source File: ParagraphHandler.java    From e4macs with Eclipse Public License 1.0 4 votes vote down vote up
protected IFindReplaceTarget getTarget(ITextEditor editor) {
	return (IFindReplaceTarget)editor.getAdapter(IFindReplaceTarget.class);
}
 
Example 18
Source File: EditorUtils.java    From goclipse with Eclipse Public License 1.0 4 votes vote down vote up
public static void setStatusLineErrorMessage(ITextEditor editor, String message, Image image) {
	IEditorStatusLine statusLine= (IEditorStatusLine)editor.getAdapter(IEditorStatusLine.class);
	if(statusLine != null) {
		statusLine.setMessage(true, message, image);
	}
}
 
Example 19
Source File: TestAutoClosing.java    From tm4e with Eclipse Public License 1.0 4 votes vote down vote up
@Test
public void testAutoClose() throws Exception {
	IProject p = ResourcesPlugin.getWorkspace().getRoot().getProject(getClass().getName() + System.currentTimeMillis());
	p.create(null);
	p.open(null);
	IFile file = p.getFile("test.lc-test");
	file.create(new ByteArrayInputStream(new byte[0]), true, null);
	ITextEditor editor = (ITextEditor) IDE.openEditor(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage(), file);
	StyledText text = (StyledText)editor.getAdapter(Control.class);
	// insert closing
	text.setText("");
	text.replaceTextRange(0, 0, "(");
	Assert.assertEquals("()", text.getText());
	Assert.assertEquals(1, text.getCaretOffset());
	// nested insert closing
	text.setText("foo(String::from)");
	text.replaceTextRange(16, 0, "(");
	Assert.assertEquals("foo(String::from())", text.getText());
	Assert.assertEquals(17, text.getCaretOffset());
	// ignore already opened
	text.setText("()");
	text.replaceTextRange(0, 0, "(");
	Assert.assertEquals("()", text.getText());
	Assert.assertEquals(1, text.getCaretOffset());
	// ignore already closed
	text.setText("()");
	text.replaceTextRange(1, 0, ")");
	Assert.assertEquals("()", text.getText());
	Assert.assertEquals(2, text.getCaretOffset());
	//
	text.setText("()");
	text.replaceTextRange(2, 0, ")");
	Assert.assertEquals("())", text.getText());
	//
	text.setText("");
	text.replaceTextRange(0, 0, "\"");
	Assert.assertEquals("\"\"", text.getText());
	Assert.assertEquals(1, text.getCaretOffset());
	// continued
	text.replaceTextRange(1, 0, "\"");
	Assert.assertEquals("\"\"", text.getText());
	Assert.assertEquals(2, text.getCaretOffset());
	// continued
	text.replaceTextRange(2, 0, "\"");
	Assert.assertEquals("\"\"\"\"", text.getText());
	Assert.assertEquals(3, text.getCaretOffset());
}
 
Example 20
Source File: MarkUtils.java    From e4macs with Eclipse Public License 1.0 3 votes vote down vote up
/**
 * set the selection in the text widget
 * 
 * @param editor
 * @param start
 *            - the start of selection in widget coords
 * @param end
 *            - the end of selection in widget coords
 */
public static void setWidgetSelection(ITextEditor editor, int start, int end) {
	Control text = (Control) editor.getAdapter(Control.class);
	if (text instanceof StyledText) {
		((StyledText) text).setSelection(start, end);
	} else if (text instanceof Text) {
		((Text) text).setSelection(start, end);
	}
}