Search in sources :

Example 11 with ITextEditor

use of org.eclipse.ui.texteditor.ITextEditor in project xtext-eclipse by eclipse.

the class ToggleSLCommentAction method update.

/**
 * Implementation of the <code>IUpdate</code> prototype method discovers
 * the operation through the current editor's
 * <code>ITextOperationTarget</code> adapter, and sets the enabled state
 * accordingly.
 */
@Override
public void update() {
    super.update();
    if (!canModifyEditor()) {
        setEnabled(false);
        return;
    }
    ITextEditor editor = getTextEditor();
    if (fOperationTarget == null && editor != null)
        fOperationTarget = (ITextOperationTarget) editor.getAdapter(ITextOperationTarget.class);
    boolean isEnabled = (fOperationTarget != null && fOperationTarget.canDoOperation(ITextOperationTarget.PREFIX) && fOperationTarget.canDoOperation(ITextOperationTarget.STRIP_PREFIX));
    setEnabled(isEnabled);
}
Also used : ITextEditor(org.eclipse.ui.texteditor.ITextEditor) ITextOperationTarget(org.eclipse.jface.text.ITextOperationTarget)

Example 12 with ITextEditor

use of org.eclipse.ui.texteditor.ITextEditor in project eclipse.platform.text by eclipse.

the class TextEditorActionContributor method doSetActiveEditor.

/**
 * Internally sets the active editor to the actions provided by this contributor.
 * Cannot be overridden by subclasses.
 *
 * @param part the editor
 */
private void doSetActiveEditor(final IEditorPart part) {
    ITextEditor textEditor = null;
    if (part instanceof ITextEditor)
        textEditor = (ITextEditor) part;
    /**
     * The global actions to be connected with editor actions
     */
    IActionBars actionBars = getActionBars();
    actionBars.setGlobalActionHandler(IDEActionFactory.ADD_TASK.getId(), getAction(textEditor, IDEActionFactory.ADD_TASK.getId()));
    actionBars.setGlobalActionHandler(IDEActionFactory.BOOKMARK.getId(), getAction(textEditor, IDEActionFactory.BOOKMARK.getId()));
    IAction action = getAction(textEditor, ITextEditorActionConstants.NEXT);
    actionBars.setGlobalActionHandler(ITextEditorActionDefinitionIds.GOTO_NEXT_ANNOTATION, action);
    actionBars.setGlobalActionHandler(ITextEditorActionConstants.NEXT, action);
    action = getAction(textEditor, ITextEditorActionConstants.PREVIOUS);
    actionBars.setGlobalActionHandler(ITextEditorActionDefinitionIds.GOTO_PREVIOUS_ANNOTATION, action);
    actionBars.setGlobalActionHandler(ITextEditorActionConstants.PREVIOUS, action);
    action = getAction(textEditor, ITextEditorActionConstants.REFRESH);
    actionBars.setGlobalActionHandler(ITextEditorActionConstants.REFRESH, action);
    fChangeEncodingAction.setAction(getAction(textEditor, ITextEditorActionConstants.CHANGE_ENCODING));
    IAction quickAssistAction = getAction(textEditor, ITextEditorActionConstants.QUICK_ASSIST);
    fQuickAssistAction.setAction(quickAssistAction);
    if (textEditor == null)
        return;
    // Update Quick Assist menu entry - for now don't show disabled entry
    IMenuManager menuMgr = textEditor.getEditorSite().getActionBars().getMenuManager();
    IMenuManager editMenu = menuMgr.findMenuUsingPath(IWorkbenchActionConstants.M_EDIT);
    if (editMenu != null) {
        boolean isEnabled = quickAssistAction != null && quickAssistAction.isEnabled();
        fQuickAssistMenuEntry.setVisible(isEnabled);
        editMenu.update(true);
    }
    fRetargetShowInformationAction.setAction(getAction(textEditor, ITextEditorActionConstants.SHOW_INFORMATION));
}
Also used : ITextEditor(org.eclipse.ui.texteditor.ITextEditor) IAction(org.eclipse.jface.action.IAction) IMenuManager(org.eclipse.jface.action.IMenuManager) IActionBars(org.eclipse.ui.IActionBars)

Example 13 with ITextEditor

use of org.eclipse.ui.texteditor.ITextEditor in project eclipse.platform.text by eclipse.

the class LastSaveReferenceProvider method readDocument.

/**
 * Reads in the saved document into <code>fReference</code>.
 *
 * @param monitor a progress monitor, or <code>null</code>
 * @param force <code>true</code> if the reference document should also
 *        be read if the current document is <code>null</code>,<code>false</code>
 *        if it should only be updated if it already existed.
 */
private void readDocument(IProgressMonitor monitor, boolean force) {
    // protect against concurrent disposal
    IDocumentProvider prov = fDocumentProvider;
    IEditorInput inp = fEditorInput;
    IDocument doc = fReference;
    ITextEditor editor = fEditor;
    if (prov instanceof IStorageDocumentProvider && inp instanceof IStorageEditorInput) {
        IStorageEditorInput input = (IStorageEditorInput) inp;
        IStorageDocumentProvider provider = (IStorageDocumentProvider) prov;
        if (doc == null)
            if (force || fDocumentRead)
                doc = new Document();
            else
                return;
        IJobManager jobMgr = Job.getJobManager();
        try {
            IStorage storage = input.getStorage();
            // check for null for backward compatibility (we used to check before...)
            if (storage == null)
                return;
            fProgressMonitor = monitor;
            ISchedulingRule rule = getSchedulingRule(storage);
            // delay for any other job requiring the lock on file
            try {
                lockDocument(monitor, jobMgr, rule);
                String encoding;
                if (storage instanceof IEncodedStorage)
                    encoding = ((IEncodedStorage) storage).getCharset();
                else
                    encoding = null;
                boolean skipUTF8BOM = isUTF8BOM(encoding, storage);
                setDocumentContent(doc, storage, encoding, monitor, skipUTF8BOM);
            } finally {
                unlockDocument(jobMgr, rule);
                fProgressMonitor = null;
            }
        } catch (CoreException e) {
            return;
        }
        if (monitor != null && monitor.isCanceled())
            return;
        // update state
        synchronized (fLock) {
            if (fDocumentProvider == provider && fEditorInput == input) {
                // only update state if our provider / input pair has not
                // been updated in between (dispose or setActiveEditor)
                fReference = doc;
                fDocumentRead = true;
                addElementStateListener(editor, prov);
            }
        }
    }
}
Also used : IStorageDocumentProvider(org.eclipse.ui.editors.text.IStorageDocumentProvider) IStorageEditorInput(org.eclipse.ui.IStorageEditorInput) ITextEditor(org.eclipse.ui.texteditor.ITextEditor) IEncodedStorage(org.eclipse.core.resources.IEncodedStorage) IJobManager(org.eclipse.core.runtime.jobs.IJobManager) Document(org.eclipse.jface.text.Document) IDocument(org.eclipse.jface.text.IDocument) IStorage(org.eclipse.core.resources.IStorage) ISchedulingRule(org.eclipse.core.runtime.jobs.ISchedulingRule) IDocumentProvider(org.eclipse.ui.texteditor.IDocumentProvider) CoreException(org.eclipse.core.runtime.CoreException) IEditorInput(org.eclipse.ui.IEditorInput) IDocument(org.eclipse.jface.text.IDocument)

Example 14 with ITextEditor

use of org.eclipse.ui.texteditor.ITextEditor in project eclipse.platform.text by eclipse.

the class QuickDiffRestoreAction method run.

@Override
public void run() {
    ITextEditor editor = getTextEditor();
    if (editor == null || !validateEditorInputState())
        return;
    IRewriteTarget target = editor.getAdapter(IRewriteTarget.class);
    if (target != null)
        target.beginCompoundChange();
    runCompoundChange();
    if (target != null)
        target.endCompoundChange();
}
Also used : ITextEditor(org.eclipse.ui.texteditor.ITextEditor) IRewriteTarget(org.eclipse.jface.text.IRewriteTarget)

Example 15 with ITextEditor

use of org.eclipse.ui.texteditor.ITextEditor in project eclipse.platform.text by eclipse.

the class GotoLineTest method goToLine.

private void goToLine(int line, int expectedResult) {
    IWorkbench workbench = PlatformUI.getWorkbench();
    IWorkbenchPage page = workbench.getActiveWorkbenchWindow().getActivePage();
    try {
        IEditorPart part = IDE.openEditor(page, fFile);
        if (part instanceof ITextEditor) {
            ITextEditor editor = (ITextEditor) part;
            IAction action = editor.getAction(ITextEditorActionConstants.GOTO_LINE);
            Accessor accessor = new Accessor(action, GotoLineAction.class);
            accessor.invoke("gotoLine", new Class[] { int.class }, new Integer[] { Integer.valueOf(line) });
            Control control = part.getAdapter(Control.class);
            if (control instanceof StyledText) {
                int caretLine = -1;
                StyledText styledText = (StyledText) control;
                int caret = styledText.getCaretOffset();
                try {
                    IDocument document = editor.getDocumentProvider().getDocument(editor.getEditorInput());
                    caretLine = document.getLineOfOffset(caret);
                } catch (BadLocationException e1) {
                    fail();
                }
                assertEquals(expectedResult, caretLine);
            } else
                fail();
        } else
            fail();
    } catch (PartInitException e) {
        fail();
    }
}
Also used : IWorkbench(org.eclipse.ui.IWorkbench) Control(org.eclipse.swt.widgets.Control) ITextEditor(org.eclipse.ui.texteditor.ITextEditor) StyledText(org.eclipse.swt.custom.StyledText) IAction(org.eclipse.jface.action.IAction) IWorkbenchPage(org.eclipse.ui.IWorkbenchPage) IEditorPart(org.eclipse.ui.IEditorPart) PartInitException(org.eclipse.ui.PartInitException) Accessor(org.eclipse.text.tests.Accessor) IDocument(org.eclipse.jface.text.IDocument) BadLocationException(org.eclipse.jface.text.BadLocationException)

Aggregations

ITextEditor (org.eclipse.ui.texteditor.ITextEditor)233 IEditorPart (org.eclipse.ui.IEditorPart)91 IDocument (org.eclipse.jface.text.IDocument)73 IWorkbenchPage (org.eclipse.ui.IWorkbenchPage)39 ITextSelection (org.eclipse.jface.text.ITextSelection)34 IFile (org.eclipse.core.resources.IFile)33 Test (org.junit.Test)31 BadLocationException (org.eclipse.jface.text.BadLocationException)27 IDocumentProvider (org.eclipse.ui.texteditor.IDocumentProvider)25 PartInitException (org.eclipse.ui.PartInitException)23 ISelection (org.eclipse.jface.viewers.ISelection)19 IWorkbenchWindow (org.eclipse.ui.IWorkbenchWindow)17 IRegion (org.eclipse.jface.text.IRegion)16 Annotation (org.eclipse.jface.text.source.Annotation)16 ArrayList (java.util.ArrayList)15 IAnnotationModel (org.eclipse.jface.text.source.IAnnotationModel)15 IResource (org.eclipse.core.resources.IResource)14 IEditorInput (org.eclipse.ui.IEditorInput)14 CoreException (org.eclipse.core.runtime.CoreException)13 IStructuredModel (org.eclipse.wst.sse.core.internal.provisional.IStructuredModel)12