Search in sources :

Example 61 with IndexedRegion

use of org.eclipse.wst.sse.core.internal.provisional.IndexedRegion 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 62 with IndexedRegion

use of org.eclipse.wst.sse.core.internal.provisional.IndexedRegion in project webtools.sourceediting by eclipse.

the class TestStructuredModel method testGetNodeResource.

public void testGetNodeResource() throws IOException, CoreException {
    IStructuredModel model = getTestModel();
    IndexedRegion region = model.getIndexedRegion(0);
    assertTrue("The region is not adaptable", region instanceof IAdaptable);
    IResource resource = ((IAdaptable) region).getAdapter(IResource.class);
    assertNotNull("A resource wasn't obtained from the model", resource);
    assertEquals("The resource doesn't correspond to the model's base location", new Path("/" + fProjectName + "/files/simple.xml"), resource.getFullPath());
}
Also used : Path(org.eclipse.core.runtime.Path) IAdaptable(org.eclipse.core.runtime.IAdaptable) IStructuredModel(org.eclipse.wst.sse.core.internal.provisional.IStructuredModel) IndexedRegion(org.eclipse.wst.sse.core.internal.provisional.IndexedRegion) IResource(org.eclipse.core.resources.IResource)

Example 63 with IndexedRegion

use of org.eclipse.wst.sse.core.internal.provisional.IndexedRegion 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 64 with IndexedRegion

use of org.eclipse.wst.sse.core.internal.provisional.IndexedRegion in project webtools.sourceediting by eclipse.

the class OpenOnSelectionHelper method revealObject.

protected boolean revealObject(final XSDConcreteComponent component) {
    if (component.getRootContainer().equals(xsdSchema)) {
        Node element = component.getElement();
        if (element instanceof IndexedRegion) {
            IndexedRegion indexNode = (IndexedRegion) element;
            textEditor.getTextViewer().setRangeIndication(indexNode.getStartOffset(), indexNode.getEndOffset() - indexNode.getStartOffset(), true);
            return true;
        }
        return false;
    } else {
        lastResult = false;
        if (component.getSchema() != null) {
            String schemaLocation = URIHelper.removePlatformResourceProtocol(component.getSchema().getSchemaLocation());
            IPath schemaPath = new Path(schemaLocation);
            final IFile schemaFile = ResourcesPlugin.getWorkspace().getRoot().getFile(schemaPath);
            Display.getDefault().syncExec(new Runnable() {

                /**
                 * @see java.lang.Runnable#run()
                 */
                public void run() {
                    final IWorkbenchWindow workbenchWindow = XSDEditorPlugin.getPlugin().getWorkbench().getActiveWorkbenchWindow();
                    if (workbenchWindow != null) {
                        try {
                            IEditorPart editorPart = workbenchWindow.getActivePage().openEditor(new FileEditorInput(schemaFile), XSDEditorPlugin.PLUGIN_ID);
                            if (editorPart instanceof InternalXSDMultiPageEditor) {
                                ((InternalXSDMultiPageEditor) editorPart).openOnGlobalReference(component);
                                lastResult = true;
                            }
                        } catch (PartInitException initEx) {
                        }
                    }
                }
            });
        }
        return lastResult;
    }
}
Also used : IPath(org.eclipse.core.runtime.IPath) Path(org.eclipse.core.runtime.Path) IWorkbenchWindow(org.eclipse.ui.IWorkbenchWindow) IFile(org.eclipse.core.resources.IFile) IPath(org.eclipse.core.runtime.IPath) Node(org.w3c.dom.Node) FileEditorInput(org.eclipse.ui.part.FileEditorInput) InternalXSDMultiPageEditor(org.eclipse.wst.xsd.ui.internal.editor.InternalXSDMultiPageEditor) IEditorPart(org.eclipse.ui.IEditorPart) PartInitException(org.eclipse.ui.PartInitException) IndexedRegion(org.eclipse.wst.sse.core.internal.provisional.IndexedRegion)

Example 65 with IndexedRegion

use of org.eclipse.wst.sse.core.internal.provisional.IndexedRegion in project webtools.sourceediting by eclipse.

the class StructuredSelectNextXMLHandler method getNewSelectionRegion.

protected Region getNewSelectionRegion(IndexedRegion indexedRegion, ITextSelection textSelection) {
    Region newRegion = null;
    if (indexedRegion instanceof Node) {
        Node cursorNode = (Node) indexedRegion;
        // use parent node for empty text node
        if ((cursorNode.getNodeType() == Node.TEXT_NODE) && (cursorNode.getNodeValue().trim().length() == 0)) {
            cursorNode = cursorNode.getParentNode();
            if (cursorNode instanceof IndexedRegion) {
                indexedRegion = (IndexedRegion) cursorNode;
            }
        }
        Region cursorNodeRegion = new Region(indexedRegion.getStartOffset(), indexedRegion.getEndOffset() - indexedRegion.getStartOffset());
        int currentOffset = textSelection.getOffset();
        int currentEndOffset = currentOffset + textSelection.getLength();
        if ((cursorNodeRegion.getOffset() >= currentOffset) && (cursorNodeRegion.getOffset() <= currentEndOffset) && (cursorNodeRegion.getOffset() + cursorNodeRegion.getLength() >= currentOffset) && (cursorNodeRegion.getOffset() + cursorNodeRegion.getLength() <= currentEndOffset)) {
            newRegion = getNewSelectionRegion2(indexedRegion, textSelection);
        } else {
            newRegion = cursorNodeRegion;
        }
    }
    return newRegion;
}
Also used : 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)

Aggregations

IndexedRegion (org.eclipse.wst.sse.core.internal.provisional.IndexedRegion)148 IStructuredDocument (org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument)33 IStructuredDocumentRegion (org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion)32 IStructuredModel (org.eclipse.wst.sse.core.internal.provisional.IStructuredModel)31 ITextRegion (org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion)27 Node (org.w3c.dom.Node)27 Region (org.eclipse.jface.text.Region)21 ICSSNode (org.eclipse.wst.css.core.internal.provisional.document.ICSSNode)21 IDOMNode (org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode)21 CSSCleanupStrategy (org.eclipse.wst.css.core.internal.cleanup.CSSCleanupStrategy)20 RegionIterator (org.eclipse.wst.css.core.internal.util.RegionIterator)17 ArrayList (java.util.ArrayList)11 List (java.util.List)11 ITextSelection (org.eclipse.jface.text.ITextSelection)11 IDOMModel (org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel)10 ICompletionProposal (org.eclipse.jface.text.contentassist.ICompletionProposal)8 ITextEditor (org.eclipse.ui.texteditor.ITextEditor)8 ITextRegionList (org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionList)8 Preferences (org.eclipse.core.runtime.Preferences)7 IDocument (org.eclipse.jface.text.IDocument)7