Search in sources :

Example 71 with ITextEditor

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

the class ShowTranslationHandler method execute.

/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.eclipse.core.commands.IHandler#execute(org.eclipse.core.commands
	 * .ExecutionEvent)
	 */
public Object execute(final ExecutionEvent event) throws ExecutionException {
    ISelection selection = HandlerUtil.getCurrentSelection(event);
    if (selection instanceof IStructuredSelection) {
        List list = ((IStructuredSelection) selection).toList();
        if (!list.isEmpty()) {
            if (list.get(0) instanceof IDOMNode) {
                final IDOMModel model = ((IDOMNode) list.get(0)).getModel();
                INodeAdapter adapter = model.getDocument().getAdapterFor(IJSPTranslation.class);
                if (adapter != null) {
                    Job opener = new UIJob("Opening JSP Java Translation") {

                        public IStatus runInUIThread(IProgressMonitor monitor) {
                            JSPTranslationAdapter translationAdapter = (JSPTranslationAdapter) model.getDocument().getAdapterFor(IJSPTranslation.class);
                            final JSPTranslationExtension translation = translationAdapter.getJSPTranslation();
                            // create an IEditorInput for the Java editor
                            final IStorageEditorInput input = new JSPTranslationEditorInput(model);
                            try {
                                IEditorPart editor = IDE.openEditor(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage(), input, JavaUI.ID_CU_EDITOR, true);
                                // Now add the problems we found
                                if (editor instanceof ITextEditor) {
                                    IAnnotationModel annotationModel = ((ITextEditor) editor).getDocumentProvider().getAnnotationModel(input);
                                    translation.reconcileCompilationUnit();
                                    List problemsList = translation.getProblems();
                                    IProblem[] problems = (IProblem[]) problemsList.toArray(new IProblem[problemsList.size()]);
                                    AnnotationTypeLookup lookup = new AnnotationTypeLookup();
                                    for (int i = 0; i < problems.length; i++) {
                                        if (problems[i] instanceof IJSPProblem)
                                            continue;
                                        int length = problems[i].getSourceEnd() - problems[i].getSourceStart() + 1;
                                        Position position = new Position(problems[i].getSourceStart(), length);
                                        Annotation annotation = null;
                                        String type = lookup.getAnnotationType(IMarker.PROBLEM, IMarker.SEVERITY_INFO);
                                        if (problems[i].isError()) {
                                            type = lookup.getAnnotationType(IMarker.PROBLEM, IMarker.SEVERITY_ERROR);
                                        } else if (problems[i].isWarning()) {
                                            type = lookup.getAnnotationType(IMarker.PROBLEM, IMarker.SEVERITY_WARNING);
                                        }
                                        annotation = new Annotation(type, false, problems[i].getMessage());
                                        if (annotation != null) {
                                            annotationModel.addAnnotation(annotation, position);
                                        }
                                    }
                                }
                            } catch (PartInitException e) {
                                e.printStackTrace();
                                Display.getCurrent().beep();
                            }
                            return Status.OK_STATUS;
                        }
                    };
                    opener.setSystem(false);
                    opener.setUser(true);
                    opener.schedule();
                }
            }
        }
    }
    return null;
}
Also used : INodeAdapter(org.eclipse.wst.sse.core.internal.provisional.INodeAdapter) ITextEditor(org.eclipse.ui.texteditor.ITextEditor) IJSPProblem(org.eclipse.jst.jsp.core.internal.java.IJSPProblem) IDOMModel(org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) IAnnotationModel(org.eclipse.jface.text.source.IAnnotationModel) IJSPTranslation(org.eclipse.jst.jsp.core.internal.java.IJSPTranslation) IDOMNode(org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode) JSPTranslationExtension(org.eclipse.jst.jsp.core.internal.java.JSPTranslationExtension) ISelection(org.eclipse.jface.viewers.ISelection) UIJob(org.eclipse.ui.progress.UIJob) List(java.util.List) PartInitException(org.eclipse.ui.PartInitException) Job(org.eclipse.core.runtime.jobs.Job) UIJob(org.eclipse.ui.progress.UIJob) IStorageEditorInput(org.eclipse.ui.IStorageEditorInput) Position(org.eclipse.jface.text.Position) IEditorPart(org.eclipse.ui.IEditorPart) JSPTranslationAdapter(org.eclipse.jst.jsp.core.internal.java.JSPTranslationAdapter) IProblem(org.eclipse.jdt.core.compiler.IProblem) Annotation(org.eclipse.jface.text.source.Annotation) AnnotationTypeLookup(org.eclipse.ui.texteditor.AnnotationTypeLookup) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor)

Example 72 with ITextEditor

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

the class ToggleBreakpointsTarget method canToggleLineBreakpoints.

/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.debug.ui.actions.IToggleBreakpointsTarget#canToggleLineBreakpoints(org.eclipse.ui.IWorkbenchPart,
	 *      org.eclipse.jface.viewers.ISelection)
	 */
public boolean canToggleLineBreakpoints(IWorkbenchPart part, ISelection selection) {
    ITextEditor editor = part.getAdapter(ITextEditor.class);
    if (selection instanceof ITextSelection) {
        ITextSelection textSelection = (ITextSelection) selection;
        IDocument document = editor.getDocumentProvider().getDocument(editor.getEditorInput());
        if (document != null && textSelection.getOffset() > -1) {
            int lineNumber = -1;
            try {
                lineNumber = document.getLineOfOffset(textSelection.getOffset());
            } catch (BadLocationException e) {
            }
            if (lineNumber >= 0) {
                ToggleBreakpointAction toggler = new ToggleBreakpointAction(editor, null);
                toggler.update();
                return toggler.isEnabled();
            }
        }
    }
    return false;
}
Also used : ITextEditor(org.eclipse.ui.texteditor.ITextEditor) ITextSelection(org.eclipse.jface.text.ITextSelection) IDocument(org.eclipse.jface.text.IDocument) IBreakpoint(org.eclipse.debug.core.model.IBreakpoint) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 73 with ITextEditor

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

the class FindOccurrencesHandler method execute.

public Object execute(ExecutionEvent event) throws ExecutionException {
    IEditorPart editorPart = HandlerUtil.getActiveEditor(event);
    ITextEditor textEditor = null;
    if (editorPart instanceof ITextEditor)
        textEditor = (ITextEditor) editorPart;
    else {
        Object o = editorPart.getAdapter(ITextEditor.class);
        if (o != null)
            textEditor = (ITextEditor) o;
    }
    boolean okay = false;
    if (textEditor != null) {
        IDocument document = textEditor.getDocumentProvider().getDocument(textEditor.getEditorInput());
        if (document != null) {
            ITextSelection textSelection = getTextSelection(textEditor);
            FindOccurrencesProcessor findOccurrenceProcessor = getProcessorForCurrentSelection(document, textSelection);
            if (findOccurrenceProcessor != null) {
                if (textEditor.getEditorInput() instanceof IFileEditorInput) {
                    IFile file = ((IFileEditorInput) textEditor.getEditorInput()).getFile();
                    okay = findOccurrenceProcessor.findOccurrences(document, textSelection, file);
                }
            }
        }
    }
    if (okay) {
        // clear status message
        PlatformStatusLineUtil.clearStatusLine();
    } else {
        // $NON-NLS-1$
        String errorMessage = SSEUIMessages.FindOccurrencesActionProvider_0;
        if (textEditor instanceof StructuredTextEditor) {
            PlatformStatusLineUtil.displayTemporaryErrorMessage(((StructuredTextEditor) textEditor).getTextViewer(), errorMessage);
        } else {
            PlatformStatusLineUtil.displayErrorMessage(errorMessage);
            PlatformStatusLineUtil.addOneTimeClearListener();
        }
    }
    return null;
}
Also used : ITextEditor(org.eclipse.ui.texteditor.ITextEditor) IFile(org.eclipse.core.resources.IFile) IFileEditorInput(org.eclipse.ui.IFileEditorInput) FindOccurrencesProcessor(org.eclipse.wst.sse.ui.internal.search.FindOccurrencesProcessor) IEditorPart(org.eclipse.ui.IEditorPart) StructuredTextEditor(org.eclipse.wst.sse.ui.StructuredTextEditor) IDocument(org.eclipse.jface.text.IDocument) ITextSelection(org.eclipse.jface.text.ITextSelection)

Example 74 with ITextEditor

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

the class JSPRenameChange method findOpenEditor.

/**
 * <p>Checks if a document is open in an editor and returns it if it is</p>
 *
 * @param jspDoc check to see if this {@link IDocument} is currently open in an editor
 * @return the open {@link ITextEditor} associated with the given {@link IDocument} or
 * <code>null</code> if none can be found.
 */
private static ITextEditor findOpenEditor(IDocument jspDoc) {
    IWorkbenchWindow[] windows = PlatformUI.getWorkbench().getWorkbenchWindows();
    IWorkbenchWindow w = null;
    for (int i = 0; i < windows.length; i++) {
        w = windows[i];
        IWorkbenchPage page = w.getActivePage();
        if (page != null) {
            IEditorReference[] references = page.getEditorReferences();
            IEditorPart editor = null;
            Object o = null;
            IDocument doc = null;
            for (int j = 0; j < references.length; j++) {
                editor = references[j].getEditor(false);
                if (editor != null) {
                    // editor might not be instantiated
                    // https://w3.opensource.ibm.com/bugzilla/show_bug.cgi?id=3764
                    // use adapter to get ITextEditor (for things like
                    // page designer)
                    o = editor.getAdapter(ITextEditor.class);
                    if (o != null && o instanceof ITextEditor) {
                        doc = ((ITextEditor) o).getDocumentProvider().getDocument(editor.getEditorInput());
                        if (doc != null && doc.equals(jspDoc)) {
                            return (ITextEditor) o;
                        }
                    }
                }
            }
        }
    }
    return null;
}
Also used : IWorkbenchWindow(org.eclipse.ui.IWorkbenchWindow) IEditorReference(org.eclipse.ui.IEditorReference) ITextEditor(org.eclipse.ui.texteditor.ITextEditor) IWorkbenchPage(org.eclipse.ui.IWorkbenchPage) IEditorPart(org.eclipse.ui.IEditorPart) IDocument(org.eclipse.jface.text.IDocument)

Example 75 with ITextEditor

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

the class ViewerTestJSP method addActions.

protected void addActions(IContributionManager mgr) {
    if (mgr != null) {
        mgr.add(new Action() {

            public String getText() {
                return getToolTipText();
            }

            public String getToolTipText() {
                return "New JSP";
            }

            public void run() {
                super.run();
                BusyIndicator.showWhile(getSite().getShell().getDisplay(), new Runnable() {

                    public void run() {
                        setupViewerForNew();
                        fSourceViewer.setEditable(true);
                    }
                });
            }
        });
        mgr.add(new Separator());
        mgr.add(new Action() {

            public String getText() {
                return getToolTipText();
            }

            public String getToolTipText() {
                return "Change Visibility";
            }

            public void run() {
                super.run();
                NumberInputDialog dlg = new NumberInputDialog(fSourceViewer.getControl().getShell());
                int proceed = dlg.open();
                if (proceed == Window.CANCEL)
                    return;
                fSourceViewer.resetVisibleRegion();
                fSourceViewer.setVisibleRegion(dlg.startValue, dlg.lengthValue);
            }
        });
        mgr.add(new Action() {

            public String getText() {
                return getToolTipText();
            }

            public String getToolTipText() {
                return "Show All";
            }

            public void run() {
                super.run();
                fSourceViewer.resetVisibleRegion();
            }
        });
        mgr.add(new Action() {

            public String getText() {
                return getToolTipText();
            }

            public String getToolTipText() {
                return "Change Visibility in Editor";
            }

            public void run() {
                super.run();
                StructuredTextViewer sourceViewer = null;
                IEditorPart part = getViewSite().getWorkbenchWindow().getActivePage().getActiveEditor();
                if (part != null && part instanceof StructuredTextEditor) {
                    sourceViewer = ((StructuredTextEditor) part).getTextViewer();
                }
                if (sourceViewer != null) {
                    NumberInputDialog dlg = new NumberInputDialog(sourceViewer.getControl().getShell());
                    int proceed = dlg.open();
                    if (proceed == Window.CANCEL)
                        return;
                    sourceViewer.resetVisibleRegion();
                    sourceViewer.setVisibleRegion(dlg.startValue, dlg.lengthValue);
                }
            }
        });
        mgr.add(new Action() {

            public String getText() {
                return getToolTipText();
            }

            public String getToolTipText() {
                return "Show All in Editor";
            }

            public void run() {
                super.run();
                StructuredTextViewer sourceViewer = null;
                IEditorPart part = getViewSite().getWorkbenchWindow().getActivePage().getActiveEditor();
                if (part != null && part instanceof StructuredTextEditor) {
                    sourceViewer = ((StructuredTextEditor) part).getTextViewer();
                }
                if (sourceViewer != null) {
                    sourceViewer.resetVisibleRegion();
                }
            }
        });
        mgr.add(new Separator());
        // no longer able to set input to NULL
        // mgr.add(new Action() {
        // public String getText() {
        // return getToolTipText();
        // }
        // 
        // public String getToolTipText() {
        // return "Set Input to NULL";
        // }
        // public void run() {
        // super.run();
        // viewer.setInput(null);
        // }
        // });
        mgr.add(new Action() {

            public String getText() {
                return getToolTipText();
            }

            public String getToolTipText() {
                return "Take Input from Active Editor";
            }

            public void run() {
                super.run();
                ITextEditor textEditor = getActiveEditor();
                if (textEditor != null) {
                    setupViewerForEditor(textEditor);
                    fSourceViewer.setEditable(true);
                }
            }
        });
        mgr.add(new Action() {

            public String getText() {
                return getToolTipText();
            }

            public String getToolTipText() {
                return "Take Input and Follow Selection";
            }

            public void run() {
                super.run();
                followSelection();
                fSourceViewer.setEditable(true);
            }
        });
        mgr.add(new Action() {

            public String getText() {
                return getToolTipText();
            }

            public String getToolTipText() {
                return "Take Input and Follow Selection As ReadOnly";
            }

            public void run() {
                super.run();
                followSelection();
                fSourceViewer.setEditable(false);
            }
        });
        mgr.add(new Action() {

            public String getText() {
                return getToolTipText();
            }

            public String getToolTipText() {
                return "Stop Following Selection";
            }

            public void run() {
                super.run();
                stopFollowSelection();
            }
        });
    }
}
Also used : Action(org.eclipse.jface.action.Action) ITextEditor(org.eclipse.ui.texteditor.ITextEditor) IEditorPart(org.eclipse.ui.IEditorPart) StructuredTextViewer(org.eclipse.wst.sse.ui.internal.StructuredTextViewer) StructuredTextEditor(org.eclipse.wst.sse.ui.StructuredTextEditor) Separator(org.eclipse.jface.action.Separator)

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