Search in sources :

Example 61 with ITextSelection

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

the class StructuredSelectPreviousXMLHandler method getNewSelectionRegion2.

/**
 * This method was separated out from getNewSelectionRegion2 because the
 * code in here is allowed to be called recursively.
 *
 * @param indexedRegion
 * @param textSelection
 * @return new region to select or null if none
 */
protected Region getNewSelectionRegion2(IndexedRegion indexedRegion, ITextSelection textSelection) {
    Region newRegion = null;
    if (indexedRegion instanceof Node) {
        Node node = (Node) indexedRegion;
        Node newNode = node.getPreviousSibling();
        if (newNode == null) {
            newNode = node.getParentNode();
            if (newNode instanceof IndexedRegion) {
                IndexedRegion newIndexedRegion = (IndexedRegion) newNode;
                newRegion = new Region(newIndexedRegion.getStartOffset(), newIndexedRegion.getEndOffset() - newIndexedRegion.getStartOffset());
            }
        } else {
            if (newNode instanceof IndexedRegion) {
                IndexedRegion newIndexedRegion = (IndexedRegion) newNode;
                newRegion = new Region(newIndexedRegion.getStartOffset(), textSelection.getOffset() + textSelection.getLength() - newIndexedRegion.getStartOffset());
                if (newNode.getNodeType() == Node.TEXT_NODE) {
                    newRegion = getNewSelectionRegion2(newIndexedRegion, new TextSelection(newRegion.getOffset(), newRegion.getLength()));
                }
            }
        }
    }
    return newRegion;
}
Also used : TextSelection(org.eclipse.jface.text.TextSelection) ITextSelection(org.eclipse.jface.text.ITextSelection) Node(org.w3c.dom.Node) Region(org.eclipse.jface.text.Region) IndexedRegion(org.eclipse.wst.sse.core.internal.provisional.IndexedRegion) IndexedRegion(org.eclipse.wst.sse.core.internal.provisional.IndexedRegion)

Example 62 with ITextSelection

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

the class StructuredSelectNextXMLActionDelegate method getNewSelectionRegion2.

/**
 * This method was separated out from getNewSelectionRegion2 because the
 * code in here is allowed to be called recursively.
 *
 * @param indexedRegion
 * @param textSelection
 * @return new region to select or null if none
 */
private Region getNewSelectionRegion2(IndexedRegion indexedRegion, ITextSelection textSelection) {
    Region newRegion = null;
    if (indexedRegion instanceof Node) {
        Node node = (Node) indexedRegion;
        Node newNode = node.getNextSibling();
        if (newNode == null) {
            newNode = node.getParentNode();
            if (newNode instanceof IndexedRegion) {
                IndexedRegion newIndexedRegion = (IndexedRegion) newNode;
                newRegion = new Region(newIndexedRegion.getStartOffset(), newIndexedRegion.getEndOffset() - newIndexedRegion.getStartOffset());
            }
        } else {
            if (newNode instanceof IndexedRegion) {
                IndexedRegion newIndexedRegion = (IndexedRegion) newNode;
                newRegion = new Region(textSelection.getOffset(), newIndexedRegion.getEndOffset() - textSelection.getOffset());
                if (newNode.getNodeType() == Node.TEXT_NODE) {
                    newRegion = getNewSelectionRegion2(newIndexedRegion, new TextSelection(newRegion.getOffset(), newRegion.getLength()));
                }
            }
        }
    }
    return newRegion;
}
Also used : TextSelection(org.eclipse.jface.text.TextSelection) ITextSelection(org.eclipse.jface.text.ITextSelection) Node(org.w3c.dom.Node) Region(org.eclipse.jface.text.Region) IndexedRegion(org.eclipse.wst.sse.core.internal.provisional.IndexedRegion) IndexedRegion(org.eclipse.wst.sse.core.internal.provisional.IndexedRegion)

Example 63 with ITextSelection

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

the class XMLMultiPageEditorPart method connectDesignPage.

/**
 * Connects the design viewer with the viewer selection manager. Should be
 * done after createSourcePage() is done because we need to get the
 * ViewerSelectionManager from the TextEditor. setModel is also done here
 * because getModel() needs to reference the TextEditor.
 */
private void connectDesignPage() {
    if (fDesignViewer != null) {
        fDesignViewer.setDocument(getDocument());
    }
    /*
		 * Connect selection from the Design page to the selection provider
		 * for the XMLMultiPageEditorPart so that selection changes in the
		 * Design page will propagate across the workbench
		 */
    if (fDesignViewer.getSelectionProvider() instanceof IPostSelectionProvider) {
        ((IPostSelectionProvider) fDesignViewer.getSelectionProvider()).addPostSelectionChangedListener(new ISelectionChangedListener() {

            public void selectionChanged(SelectionChangedEvent event) {
                if (getActivePage() != fSourcePageIndex) {
                    ((MultiPageSelectionProvider) getSite().getSelectionProvider()).firePostSelectionChanged(event);
                }
            }
        });
    }
    fDesignViewer.getSelectionProvider().addSelectionChangedListener(new ISelectionChangedListener() {

        public void selectionChanged(SelectionChangedEvent event) {
            if (getActivePage() != fSourcePageIndex) {
                ((MultiPageSelectionProvider) getSite().getSelectionProvider()).fireSelectionChanged(event);
            }
        }
    });
    fDesignViewer.getSelectionProvider().addSelectionChangedListener(new ISelectionChangedListener() {

        public void selectionChanged(SelectionChangedEvent event) {
            if (getActivePage() != fSourcePageIndex) {
                try {
                    updateStatusLine(event.getSelection());
                } catch (Exception exception) {
                    Logger.logException(exception);
                }
            }
        }
    });
    /*
		 * Handle double-click in the Design page by selecting the
		 * corresponding amount of text in the Source page.
		 * 
		 * Warning: This implies more knowledge of the design viewer's underlying
		 * Control than expressed in the IDesignViewer interface
		 */
    fDesignViewer.getControl().addListener(SWT.MouseDoubleClick, new Listener() {

        public void handleEvent(Event event) {
            ISelection selection = fDesignViewer.getSelectionProvider().getSelection();
            int start = -1;
            int length = -1;
            if (selection instanceof IStructuredSelection) {
                /*
					 * selection goes from the start of the first object to
					 * the end of the last
					 */
                IStructuredSelection structuredSelection = (IStructuredSelection) selection;
                Object o = structuredSelection.getFirstElement();
                Object o2 = null;
                if (structuredSelection.size() > 1) {
                    o2 = structuredSelection.toArray()[structuredSelection.size() - 1];
                } else {
                    o2 = o;
                }
                if (o instanceof IndexedRegion) {
                    start = ((IndexedRegion) o).getStartOffset();
                    length = ((IndexedRegion) o2).getEndOffset() - start;
                } else if (o2 instanceof ITextRegion) {
                    start = ((ITextRegion) o).getStart();
                    length = ((ITextRegion) o2).getEnd() - start;
                }
            } else if (selection instanceof ITextSelection) {
                start = ((ITextSelection) selection).getOffset();
                length = ((ITextSelection) selection).getLength();
            }
            if ((start > -1) && (length > -1)) {
                getTextEditor().selectAndReveal(start, length);
            }
        }
    });
    /*
		 * Connect selection from the Source page to the selection provider of
		 * the Design page so that selection in the Source page will drive
		 * selection in the Design page. Prefer post selection.
		 */
    ISelectionProvider provider = getTextEditor().getSelectionProvider();
    if (fTextEditorSelectionListener == null) {
        fTextEditorSelectionListener = new TextEditorPostSelectionAdapter();
    }
    if (provider instanceof IPostSelectionProvider) {
        fTextEditorSelectionListener.forcePostSelection = false;
        ((IPostSelectionProvider) provider).addPostSelectionChangedListener(fTextEditorSelectionListener);
    } else {
        fTextEditorSelectionListener.forcePostSelection = true;
        provider.addSelectionChangedListener(fTextEditorSelectionListener);
    }
}
Also used : IPropertyListener(org.eclipse.ui.IPropertyListener) IWindowListener(org.eclipse.ui.IWindowListener) ISelectionChangedListener(org.eclipse.jface.viewers.ISelectionChangedListener) Listener(org.eclipse.swt.widgets.Listener) ITextInputListener(org.eclipse.jface.text.ITextInputListener) IPartListener(org.eclipse.ui.IPartListener) ISelectionChangedListener(org.eclipse.jface.viewers.ISelectionChangedListener) SelectionChangedEvent(org.eclipse.jface.viewers.SelectionChangedEvent) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) IndexedRegion(org.eclipse.wst.sse.core.internal.provisional.IndexedRegion) CoreException(org.eclipse.core.runtime.CoreException) PartInitException(org.eclipse.ui.PartInitException) ITextSelection(org.eclipse.jface.text.ITextSelection) IPostSelectionProvider(org.eclipse.jface.viewers.IPostSelectionProvider) ISelectionProvider(org.eclipse.jface.viewers.ISelectionProvider) ITextRegion(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion) ISelection(org.eclipse.jface.viewers.ISelection) SelectionChangedEvent(org.eclipse.jface.viewers.SelectionChangedEvent) Event(org.eclipse.swt.widgets.Event)

Example 64 with ITextSelection

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

the class XPathTemplateCompletionProcessor method computeCompletionProposals.

/*
	 * Copied from super class except instead of calling createContext(viewer,
	 * region) call createContext(viewer, region, offset) instead
	 */
public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer, int offset) {
    ITextSelection selection = (ITextSelection) viewer.getSelectionProvider().getSelection();
    // adjust offset to end of normalized selection
    if (selection.getOffset() == offset) {
        offset = selection.getOffset() + selection.getLength();
    }
    String prefix = extractPrefix(viewer, offset);
    Region region = new Region(offset - prefix.length(), prefix.length());
    TemplateContext context = createContext(viewer, region, offset);
    if (context == null) {
        return new ICompletionProposal[0];
    }
    // name of the selection variables {line, word}_selection
    // $NON-NLS-1$
    context.setVariable("selection", selection.getText());
    Template[] templates = getTemplates(context.getContextType().getId());
    List matches = new ArrayList();
    for (int i = 0; i < templates.length; i++) {
        Template template = templates[i];
        try {
            context.getContextType().validate(template.getPattern());
        } catch (TemplateException e) {
            continue;
        }
        if (template.matches(prefix, context.getContextType().getId())) {
            matches.add(createProposal(template, context, (IRegion) region, getRelevance(template, prefix)));
        }
    }
    Collections.sort(matches, fgProposalComparator);
    return (ICompletionProposal[]) matches.toArray(new ICompletionProposal[matches.size()]);
}
Also used : TemplateException(org.eclipse.jface.text.templates.TemplateException) ICompletionProposal(org.eclipse.jface.text.contentassist.ICompletionProposal) ArrayList(java.util.ArrayList) Region(org.eclipse.jface.text.Region) IRegion(org.eclipse.jface.text.IRegion) ArrayList(java.util.ArrayList) List(java.util.List) TemplateContext(org.eclipse.jface.text.templates.TemplateContext) ReplaceNameTemplateContext(org.eclipse.wst.xml.ui.internal.contentassist.ReplaceNameTemplateContext) ITextSelection(org.eclipse.jface.text.ITextSelection) IRegion(org.eclipse.jface.text.IRegion) Template(org.eclipse.jface.text.templates.Template)

Example 65 with ITextSelection

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

the class GoToMatchingTagAction method runWithEvent.

/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.jface.action.Action#runWithEvent(org.eclipse.swt.widgets.Event)
	 */
public void runWithEvent(Event event) {
    super.runWithEvent(event);
    if (getTextEditor() == null)
        return;
    ISelection selection = getTextEditor().getSelectionProvider().getSelection();
    if (!selection.isEmpty() && selection instanceof IStructuredSelection && selection instanceof ITextSelection) {
        Object o = ((IStructuredSelection) selection).getFirstElement();
        if (o instanceof IDOMNode) {
            int offset = ((ITextSelection) selection).getOffset();
            IStructuredDocumentRegion matchRegion = null;
            if (((Node) o).getNodeType() == Node.ATTRIBUTE_NODE) {
                o = ((Attr) o).getOwnerElement();
            }
            int targetOffset = -1;
            if (o instanceof IDOMNode) {
                IDOMNode node = (IDOMNode) o;
                IStructuredDocumentRegion startStructuredDocumentRegion = node.getStartStructuredDocumentRegion();
                if (startStructuredDocumentRegion != null && startStructuredDocumentRegion.containsOffset(offset)) {
                    matchRegion = ((IDOMNode) o).getEndStructuredDocumentRegion();
                    if (matchRegion != null)
                        targetOffset = matchRegion.getStartOffset() + 2;
                } else {
                    IStructuredDocumentRegion endStructuredDocumentRegion = node.getEndStructuredDocumentRegion();
                    if (endStructuredDocumentRegion != null && endStructuredDocumentRegion.containsOffset(offset)) {
                        matchRegion = ((IDOMNode) o).getStartStructuredDocumentRegion();
                        if (matchRegion != null)
                            targetOffset = matchRegion.getStartOffset() + 1;
                    }
                }
            }
            if (targetOffset >= 0) {
                getTextEditor().getSelectionProvider().setSelection(new TextSelection(targetOffset, 0));
            }
        }
    }
}
Also used : IStructuredDocumentRegion(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion) IDOMNode(org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode) ITextSelection(org.eclipse.jface.text.ITextSelection) TextSelection(org.eclipse.jface.text.TextSelection) ISelection(org.eclipse.jface.viewers.ISelection) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) 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