Search in sources :

Example 51 with ITextSelection

use of org.eclipse.jface.text.ITextSelection in project webtools.sourceediting by eclipse.

the class JSPJavaSelectionProvider method getSelection.

static IJavaScriptElement[] getSelection(ITextEditor textEditor) {
    IJavaScriptElement[] elements = null;
    IDocument document = textEditor.getDocumentProvider().getDocument(textEditor.getEditorInput());
    ISelection selection = textEditor.getSelectionProvider().getSelection();
    if (selection instanceof ITextSelection) {
        ITextSelection textSelection = (ITextSelection) selection;
        // get the JSP translation object for this editor's document
        IStructuredModel model = null;
        try {
            model = StructuredModelManager.getModelManager().getExistingModelForRead(document);
            if (model instanceof IDOMModel) {
                IDOMModel xmlModel = (IDOMModel) model;
                IDOMDocument xmlDoc = xmlModel.getDocument();
                JsTranslationAdapter adapter = (JsTranslationAdapter) xmlDoc.getAdapterFor(IJsTranslation.class);
                if (adapter != null) {
                    IJsTranslation translation = adapter.getJsTranslation(true);
                    elements = translation.getElementsFromJsRange(translation.getJavaScriptOffset(textSelection.getOffset()), translation.getJavaScriptOffset(textSelection.getOffset() + textSelection.getLength()));
                }
            }
        } finally {
            if (model != null) {
                model.releaseFromRead();
            }
        }
    }
    if (elements == null) {
        elements = new IJavaScriptElement[0];
    }
    return elements;
}
Also used : IJsTranslation(org.eclipse.wst.jsdt.web.core.javascript.IJsTranslation) IDOMModel(org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel) IJavaScriptElement(org.eclipse.wst.jsdt.core.IJavaScriptElement) ISelection(org.eclipse.jface.viewers.ISelection) IDOMDocument(org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument) IStructuredModel(org.eclipse.wst.sse.core.internal.provisional.IStructuredModel) JsTranslationAdapter(org.eclipse.wst.jsdt.web.core.javascript.JsTranslationAdapter) IDocument(org.eclipse.jface.text.IDocument) ITextSelection(org.eclipse.jface.text.ITextSelection)

Example 52 with ITextSelection

use of org.eclipse.jface.text.ITextSelection in project webtools.sourceediting by eclipse.

the class StructuredTextEditor method handleElementContentReplaced.

protected void handleElementContentReplaced() {
    super.handleElementContentReplaced();
    // queue a full revalidation of content
    IDocument document = getDocumentProvider().getDocument(getEditorInput());
    SourceViewerConfiguration sourceViewerConfiguration = getSourceViewerConfiguration();
    if (document != null && sourceViewerConfiguration != null && sourceViewerConfiguration.getReconciler(getSourceViewer()) instanceof DirtyRegionProcessor) {
        ((DirtyRegionProcessor) sourceViewerConfiguration.getReconciler(getSourceViewer())).processDirtyRegion(new DirtyRegion(0, document.getLength(), DirtyRegion.INSERT, document.get()));
    }
    /*
		 * https://bugs.eclipse.org/bugs/show_bug.cgi?id=129906 - update
		 * selection to listeners
		 */
    ISelectionProvider selectionProvider = getSelectionProvider();
    ISelection originalSelection = selectionProvider.getSelection();
    if (selectionProvider instanceof StructuredSelectionProvider && originalSelection instanceof ITextSelection) {
        ITextSelection textSelection = (ITextSelection) originalSelection;
        // make sure the old selection is actually still valid
        if (!textSelection.isEmpty() && (document == null || textSelection.getOffset() + textSelection.getLength() <= document.getLength())) {
            SelectionChangedEvent syntheticEvent = new SelectionChangedEvent(selectionProvider, new TextSelection(textSelection.getOffset(), textSelection.getLength()));
            ((StructuredSelectionProvider) selectionProvider).handleSelectionChanged(syntheticEvent);
            ((StructuredSelectionProvider) selectionProvider).handlePostSelectionChanged(syntheticEvent);
        } else {
            SelectionChangedEvent syntheticEvent = new SelectionChangedEvent(selectionProvider, new TextSelection(0, 0));
            ((StructuredSelectionProvider) selectionProvider).handleSelectionChanged(syntheticEvent);
            ((StructuredSelectionProvider) selectionProvider).handlePostSelectionChanged(syntheticEvent);
        }
    }
}
Also used : SourceViewerConfiguration(org.eclipse.jface.text.source.SourceViewerConfiguration) ISelectionProvider(org.eclipse.jface.viewers.ISelectionProvider) ITextSelection(org.eclipse.jface.text.ITextSelection) TextSelection(org.eclipse.jface.text.TextSelection) IBlockTextSelection(org.eclipse.jface.text.IBlockTextSelection) ISelection(org.eclipse.jface.viewers.ISelection) DirtyRegion(org.eclipse.jface.text.reconciler.DirtyRegion) DirtyRegionProcessor(org.eclipse.wst.sse.ui.internal.reconcile.DirtyRegionProcessor) SelectionChangedEvent(org.eclipse.jface.viewers.SelectionChangedEvent) IDocument(org.eclipse.jface.text.IDocument) ITextSelection(org.eclipse.jface.text.ITextSelection)

Example 53 with ITextSelection

use of org.eclipse.jface.text.ITextSelection in project webtools.sourceediting by eclipse.

the class FindOccurrencesActionDelegate method run.

public void run(IAction action) {
    boolean okay = false;
    if (fEditor instanceof ITextEditor) {
        ITextEditor textEditor = (ITextEditor) fEditor;
        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 (fEditor instanceof StructuredTextEditor) {
            PlatformStatusLineUtil.displayTemporaryErrorMessage(((StructuredTextEditor) fEditor).getTextViewer(), errorMessage);
        } else {
            PlatformStatusLineUtil.displayErrorMessage(errorMessage);
            PlatformStatusLineUtil.addOneTimeClearListener();
        }
    }
}
Also used : ITextEditor(org.eclipse.ui.texteditor.ITextEditor) IFile(org.eclipse.core.resources.IFile) IFileEditorInput(org.eclipse.ui.IFileEditorInput) StructuredTextEditor(org.eclipse.wst.sse.ui.StructuredTextEditor) IDocument(org.eclipse.jface.text.IDocument) ITextSelection(org.eclipse.jface.text.ITextSelection)

Example 54 with ITextSelection

use of org.eclipse.jface.text.ITextSelection 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 55 with ITextSelection

use of org.eclipse.jface.text.ITextSelection 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)

Aggregations

ITextSelection (org.eclipse.jface.text.ITextSelection)205 ISelection (org.eclipse.jface.viewers.ISelection)65 IDocument (org.eclipse.jface.text.IDocument)52 BadLocationException (org.eclipse.jface.text.BadLocationException)46 IRegion (org.eclipse.jface.text.IRegion)34 ITextEditor (org.eclipse.ui.texteditor.ITextEditor)34 Region (org.eclipse.jface.text.Region)31 IEditorPart (org.eclipse.ui.IEditorPart)29 ISelectionProvider (org.eclipse.jface.viewers.ISelectionProvider)25 IStructuredSelection (org.eclipse.jface.viewers.IStructuredSelection)21 ArrayList (java.util.ArrayList)18 TextSelection (org.eclipse.jface.text.TextSelection)17 ICompletionProposal (org.eclipse.jface.text.contentassist.ICompletionProposal)14 Point (org.eclipse.swt.graphics.Point)13 IResource (org.eclipse.core.resources.IResource)12 XtextEditor (org.eclipse.xtext.ui.editor.XtextEditor)12 IFile (org.eclipse.core.resources.IFile)11 StyledText (org.eclipse.swt.custom.StyledText)11 IndexedRegion (org.eclipse.wst.sse.core.internal.provisional.IndexedRegion)11 Template (org.eclipse.jface.text.templates.Template)10