Search in sources :

Example 86 with ITextEditor

use of org.eclipse.ui.texteditor.ITextEditor in project webtools.sourceediting by eclipse.

the class ViewerTestDTD method getActiveEditor.

/**
 * Returns the current active text editor if possible
 *
 * @return ITextEditor
 */
private ITextEditor getActiveEditor() {
    ITextEditor editor = null;
    IEditorPart editorPart = getSite().getWorkbenchWindow().getActivePage().getActiveEditor();
    if (editorPart instanceof ITextEditor)
        editor = (ITextEditor) editorPart;
    if (editor == null && editorPart != null)
        editor = (ITextEditor) editorPart.getAdapter(ITextEditor.class);
    return editor;
}
Also used : ITextEditor(org.eclipse.ui.texteditor.ITextEditor) IEditorPart(org.eclipse.ui.IEditorPart)

Example 87 with ITextEditor

use of org.eclipse.ui.texteditor.ITextEditor in project webtools.sourceediting by eclipse.

the class ViewerTestXML method getActiveEditor.

/**
 * Returns the current active text editor if possible
 *
 * @return ITextEditor
 */
private ITextEditor getActiveEditor() {
    ITextEditor editor = null;
    IEditorPart editorPart = getSite().getWorkbenchWindow().getActivePage().getActiveEditor();
    if (editorPart instanceof ITextEditor)
        editor = (ITextEditor) editorPart;
    if (editor == null && editorPart != null)
        editor = editorPart.getAdapter(ITextEditor.class);
    return editor;
}
Also used : ITextEditor(org.eclipse.ui.texteditor.ITextEditor) IEditorPart(org.eclipse.ui.IEditorPart)

Example 88 with ITextEditor

use of org.eclipse.ui.texteditor.ITextEditor in project webtools.sourceediting by eclipse.

the class ViewerTestXML method followSelection.

/**
 * Hooks up the viewer to follow the selection made in the active editor
 */
private void followSelection() {
    ITextEditor editor = getActiveEditor();
    if (editor != null) {
        setupViewerForEditor(editor);
        if (fHighlightRangeListener == null)
            fHighlightRangeListener = new NodeRangeSelectionListener();
        fContentOutlinePage = (editor.getAdapter(IContentOutlinePage.class));
        if (fContentOutlinePage != null) {
            fContentOutlinePage.addSelectionChangedListener(fHighlightRangeListener);
            if (!fContentOutlinePage.getSelection().isEmpty() && fContentOutlinePage.getSelection() instanceof IStructuredSelection) {
                fSourceViewer.resetVisibleRegion();
                Object[] nodes = ((IStructuredSelection) fContentOutlinePage.getSelection()).toArray();
                IndexedRegion startNode = (IndexedRegion) nodes[0];
                IndexedRegion endNode = (IndexedRegion) nodes[nodes.length - 1];
                if (startNode instanceof Attr)
                    startNode = (IndexedRegion) ((Attr) startNode).getOwnerElement();
                if (endNode instanceof Attr)
                    endNode = (IndexedRegion) ((Attr) endNode).getOwnerElement();
                int start = startNode.getStartOffset();
                int end = endNode.getEndOffset();
                fSourceViewer.setVisibleRegion(start, end - start);
                fSourceViewer.setSelectedRange(start, 0);
            }
        }
    }
}
Also used : ITextEditor(org.eclipse.ui.texteditor.ITextEditor) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) IndexedRegion(org.eclipse.wst.sse.core.internal.provisional.IndexedRegion) Attr(org.w3c.dom.Attr)

Example 89 with ITextEditor

use of org.eclipse.ui.texteditor.ITextEditor in project webtools.sourceediting by eclipse.

the class NewXSLFileWizard method openEditor.

private void openEditor(final IFile file, final int cursorOffset) {
    // Open editor on new file.
    String editorId = null;
    try {
        IEditorDescriptor editor = PlatformUI.getWorkbench().getEditorRegistry().getDefaultEditor(file.getFullPath().toString(), file.getContentDescription().getContentType());
        if (editor != null) {
            editorId = editor.getId();
        }
    } catch (CoreException e1) {
        // editor id could not be retrieved, so we can not open editor
        return;
    }
    final String finalEditorId = editorId;
    Display.getDefault().asyncExec(new Runnable() {

        public void run() {
            IWorkbenchWindow dw = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
            try {
                if (dw != null) {
                    IWorkbenchPage page = dw.getActivePage();
                    if (page != null) {
                        IEditorPart editor = page.openEditor(new FileEditorInput(file), finalEditorId, true);
                        ITextEditor textEditor = (ITextEditor) editor.getAdapter(ITextEditor.class);
                        if (textEditor != null)
                            textEditor.selectAndReveal(cursorOffset, 0);
                        editor.setFocus();
                    }
                }
            } catch (PartInitException e) {
                // editor can not open for some reason
                XSLUIPlugin.log(e);
            }
        }
    });
}
Also used : IWorkbenchWindow(org.eclipse.ui.IWorkbenchWindow) ITextEditor(org.eclipse.ui.texteditor.ITextEditor) CoreException(org.eclipse.core.runtime.CoreException) IEditorDescriptor(org.eclipse.ui.IEditorDescriptor) FileEditorInput(org.eclipse.ui.part.FileEditorInput) IWorkbenchPage(org.eclipse.ui.IWorkbenchPage) IEditorPart(org.eclipse.ui.IEditorPart) PartInitException(org.eclipse.ui.PartInitException)

Example 90 with ITextEditor

use of org.eclipse.ui.texteditor.ITextEditor in project webtools.sourceediting by eclipse.

the class ToggleEditModeHandler method execute.

public Object execute(ExecutionEvent event) throws ExecutionException {
    IEditorPart editor = HandlerUtil.getActiveEditor(event);
    ITextEditor textEditor = null;
    if (editor instanceof ITextEditor)
        textEditor = (ITextEditor) editor;
    else {
        Object o = editor.getAdapter(ITextEditor.class);
        if (o != null)
            textEditor = (ITextEditor) o;
    }
    if (textEditor != null) {
        IDocument document = textEditor.getDocumentProvider().getDocument(textEditor.getEditorInput());
        IStructuredModel model = StructuredModelManager.getModelManager().getExistingModelForRead(document);
        if (model != null) {
            ModelQuery modelQuery;
            try {
                modelQuery = ModelQueryUtil.getModelQuery(model);
            } finally {
                model.releaseFromRead();
            }
            if (modelQuery != null) {
                int newState = getNextState(modelQuery.getEditMode());
                modelQuery.setEditMode(newState);
                // Force a Refresh on this command so that the image can
                // be
                // updated.
                ICommandService commandService = HandlerUtil.getActiveWorkbenchWindow(event).getService(ICommandService.class);
                Map<String, IWorkbenchWindow> filter = new HashMap<>();
                filter.put(IServiceScopes.WINDOW_SCOPE, HandlerUtil.getActiveWorkbenchWindow(event));
                commandService.refreshElements(event.getCommand().getId(), filter);
            }
        }
    }
    return null;
}
Also used : IWorkbenchWindow(org.eclipse.ui.IWorkbenchWindow) ITextEditor(org.eclipse.ui.texteditor.ITextEditor) HashMap(java.util.HashMap) ModelQuery(org.eclipse.wst.xml.core.internal.contentmodel.modelquery.ModelQuery) IEditorPart(org.eclipse.ui.IEditorPart) IStructuredModel(org.eclipse.wst.sse.core.internal.provisional.IStructuredModel) IDocument(org.eclipse.jface.text.IDocument) ICommandService(org.eclipse.ui.commands.ICommandService)

Aggregations

ITextEditor (org.eclipse.ui.texteditor.ITextEditor)236 IEditorPart (org.eclipse.ui.IEditorPart)92 IDocument (org.eclipse.jface.text.IDocument)76 IWorkbenchPage (org.eclipse.ui.IWorkbenchPage)42 IFile (org.eclipse.core.resources.IFile)36 ITextSelection (org.eclipse.jface.text.ITextSelection)34 Test (org.junit.Test)33 BadLocationException (org.eclipse.jface.text.BadLocationException)28 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 CoreException (org.eclipse.core.runtime.CoreException)15 IAnnotationModel (org.eclipse.jface.text.source.IAnnotationModel)15 IResource (org.eclipse.core.resources.IResource)14 IEditorInput (org.eclipse.ui.IEditorInput)14 TextSelection (org.eclipse.jface.text.TextSelection)12