Search in sources :

Example 96 with IStructuredModel

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

the class XMLHyperlinkDetector method getBaseLocation.

/**
 * Get the base location from the current model (local file system)
 */
private String getBaseLocation(IDocument document) {
    String baseLoc = null;
    // get the base location from the current model
    IStructuredModel sModel = null;
    try {
        sModel = StructuredModelManager.getModelManager().getExistingModelForRead(document);
        if (sModel != null) {
            IPath location = new Path(sModel.getBaseLocation());
            if (location.toFile().exists()) {
                baseLoc = location.toString();
            } else {
                if (location.segmentCount() > 1) {
                    baseLoc = ResourcesPlugin.getWorkspace().getRoot().getFile(location).getLocation().toString();
                } else {
                    baseLoc = ResourcesPlugin.getWorkspace().getRoot().getLocation().append(location).toString();
                }
            }
        }
    } finally {
        if (sModel != null) {
            sModel.releaseFromRead();
        }
    }
    return baseLoc;
}
Also used : IPath(org.eclipse.core.runtime.IPath) Path(org.eclipse.core.runtime.Path) IPath(org.eclipse.core.runtime.IPath) IStructuredModel(org.eclipse.wst.sse.core.internal.provisional.IStructuredModel)

Example 97 with IStructuredModel

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

the class XMLHyperlinkDetector method getCurrentNode.

/**
 * Returns the node the cursor is currently on in the document. null if no
 * node is selected
 *
 * @param offset
 * @return Node either element, doctype, text, or null
 */
private Node getCurrentNode(IDocument document, int offset) {
    // get the current node at the offset (returns either: element,
    // doctype, text)
    IndexedRegion inode = null;
    IStructuredModel sModel = null;
    try {
        sModel = StructuredModelManager.getModelManager().getExistingModelForRead(document);
        inode = sModel.getIndexedRegion(offset);
        if (inode == null) {
            inode = sModel.getIndexedRegion(offset - 1);
        }
    } finally {
        if (sModel != null) {
            sModel.releaseFromRead();
        }
    }
    if (inode instanceof Node) {
        return (Node) inode;
    }
    return null;
}
Also used : IDOMNode(org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode) Node(org.w3c.dom.Node) IStructuredModel(org.eclipse.wst.sse.core.internal.provisional.IStructuredModel) IndexedRegion(org.eclipse.wst.sse.core.internal.provisional.IndexedRegion)

Example 98 with IStructuredModel

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

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

the class StandardEditorActionsAction method delete.

private void delete(IAction action) {
    JsJfaceNode[] nodes = parseSelection();
    if (nodes == null || nodes.length == 0) {
        return;
    }
    IStructuredDocument lastDoc = null;
    IModelManager modelManager = StructuredModelManager.getModelManager();
    IStructuredModel model = null;
    try {
        int start;
        int length;
        for (int i = 0; i < nodes.length; i++) {
            JsJfaceNode currentNode = nodes[i];
            start = currentNode.getStartOffset();
            length = currentNode.getLength();
            IStructuredDocument doc = currentNode.getStructuredDocument();
            if (doc != lastDoc) {
                lastDoc = doc;
                if (model != null) {
                    model.endRecording(action);
                    model.changedModel();
                    model.releaseFromEdit();
                }
                if (modelManager != null) {
                    model = modelManager.getExistingModelForEdit(doc);
                    model.aboutToChangeModel();
                    model.beginRecording(action, "Delete JavaScript Element", "Delete JavaScript Element");
                }
            }
            // $NON-NLS-1$
            doc.replaceText(action, start, length, "");
        }
        model.endRecording(action);
    } catch (Exception e) {
        // $NON-NLS-1$
        System.out.println(Messages.getString("StandardEditorActionsAction.8") + e);
    } finally {
        if (model != null) {
            model.changedModel();
            model.releaseFromEdit();
        }
    }
}
Also used : IModelManager(org.eclipse.wst.sse.core.internal.provisional.IModelManager) JsJfaceNode(org.eclipse.wst.jsdt.web.ui.views.contentoutline.JsJfaceNode) IStructuredDocument(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument) IStructuredModel(org.eclipse.wst.sse.core.internal.provisional.IStructuredModel) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 100 with IStructuredModel

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

the class StandardEditorActionsAction method paste.

private void paste(IAction action, boolean atEnd) {
    JsJfaceNode[] nodes = parseSelection();
    if (nodes == null || nodes.length == 0) {
        return;
    }
    int startOfPaste = -1;
    IStructuredDocument doc = null;
    /* Figure out where to paste the content */
    if (atEnd) {
        for (int i = 0; i < nodes.length; i++) {
            if ((nodes[i].getStartOffset() + nodes[i].getLength()) > startOfPaste) {
                startOfPaste = (nodes[i].getStartOffset() + nodes[i].getLength());
                doc = nodes[i].getStructuredDocument();
            }
        }
    } else {
        for (int i = 0; i < nodes.length; i++) {
            if ((nodes[i].getStartOffset() < startOfPaste || startOfPaste < 0)) {
                startOfPaste = nodes[i].getStartOffset();
                doc = nodes[i].getStructuredDocument();
            }
        }
    }
    Clipboard clipboard = null;
    IModelManager modelManager = StructuredModelManager.getModelManager();
    IStructuredModel model = null;
    try {
        clipboard = new Clipboard(Display.getCurrent());
        String pasteString = (String) clipboard.getContents(TextTransfer.getInstance());
        model = modelManager.getExistingModelForEdit(doc);
        model.aboutToChangeModel();
        // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$ //$NON-NLS-7$ //$NON-NLS-8$
        model.beginRecording(action, Messages.getString("StandardEditorActionsAction.9") + (atEnd ? Messages.getString("StandardEditorActionsAction.10") : Messages.getString("StandardEditorActionsAction.11")) + Messages.getString("StandardEditorActionsAction.12"), Messages.getString("StandardEditorActionsAction.13") + (atEnd ? Messages.getString("StandardEditorActionsAction.14") : Messages.getString("StandardEditorActionsAction.15")) + Messages.getString("StandardEditorActionsAction.16"));
        doc.replaceText(action, startOfPaste, 0, pasteString);
    } finally {
        if (clipboard != null) {
            clipboard.dispose();
        }
        if (model != null) {
            model.endRecording(action);
            model.changedModel();
            model.releaseFromEdit();
        }
    }
}
Also used : IModelManager(org.eclipse.wst.sse.core.internal.provisional.IModelManager) JsJfaceNode(org.eclipse.wst.jsdt.web.ui.views.contentoutline.JsJfaceNode) IStructuredDocument(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument) Clipboard(org.eclipse.swt.dnd.Clipboard) IStructuredModel(org.eclipse.wst.sse.core.internal.provisional.IStructuredModel)

Aggregations

IStructuredModel (org.eclipse.wst.sse.core.internal.provisional.IStructuredModel)482 IDOMModel (org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel)110 IModelManager (org.eclipse.wst.sse.core.internal.provisional.IModelManager)102 IStructuredDocument (org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument)81 IFile (org.eclipse.core.resources.IFile)75 IOException (java.io.IOException)69 CoreException (org.eclipse.core.runtime.CoreException)49 IDocument (org.eclipse.jface.text.IDocument)46 IDOMDocument (org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument)42 InputStream (java.io.InputStream)40 IDOMNode (org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode)36 Path (org.eclipse.core.runtime.Path)35 BadLocationException (org.eclipse.jface.text.BadLocationException)34 IJsTranslation (org.eclipse.wst.jsdt.web.core.javascript.IJsTranslation)32 JsTranslationAdapter (org.eclipse.wst.jsdt.web.core.javascript.JsTranslationAdapter)32 IProject (org.eclipse.core.resources.IProject)31 IndexedRegion (org.eclipse.wst.sse.core.internal.provisional.IndexedRegion)31 IPath (org.eclipse.core.runtime.IPath)27 ByteArrayInputStream (java.io.ByteArrayInputStream)22 Document (org.w3c.dom.Document)19