Search in sources :

Example 21 with IModelManager

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

use of org.eclipse.wst.sse.core.internal.provisional.IModelManager 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)

Example 23 with IModelManager

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

the class JsSearchDocument method getJSTranslation.

/**
 * It's not recommended for clients to hold on to this JSPTranslation
 * since it's kind of large. If possible, hold on to the
 * JSPSearchDocument, which is more of a lightweight proxy.
 *
 * @return the JSPTranslation for the jsp file, or null if it's an
 *         unsupported file.
 */
public final IJsTranslation getJSTranslation() {
    IJsTranslation translation = null;
    IFile jspFile = getFile();
    if (!JsSearchSupport.isJsp(jspFile)) {
        return translation;
    }
    IStructuredModel model = null;
    try {
        // get existing model for read, then get document from it
        IModelManager modelManager = getModelManager();
        if (modelManager != null) {
            model = modelManager.getModelForRead(jspFile);
        }
        // handle unsupported
        if (model instanceof IDOMModel) {
            IDOMModel xmlModel = (IDOMModel) model;
            JsTranslationAdapterFactory.setupAdapterFactory(xmlModel);
            IDOMDocument doc = xmlModel.getDocument();
            JsTranslationAdapter adapter = (JsTranslationAdapter) doc.getAdapterFor(IJsTranslation.class);
            translation = adapter.getJsTranslation(false);
        }
    } catch (IOException e) {
        Logger.logException(e);
    } catch (CoreException e) {
        Logger.logException(e);
    } catch (UnsupportedCharsetExceptionWithDetail e) {
    // no need to log this. Just consider it an invalid file for our
    // purposes.
    // Logger.logException(e);
    } finally {
        if (model != null) {
            model.releaseFromRead();
        }
    }
    return translation;
}
Also used : IJsTranslation(org.eclipse.wst.jsdt.web.core.javascript.IJsTranslation) IFile(org.eclipse.core.resources.IFile) CoreException(org.eclipse.core.runtime.CoreException) IDOMModel(org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel) IModelManager(org.eclipse.wst.sse.core.internal.provisional.IModelManager) IDOMDocument(org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument) UnsupportedCharsetExceptionWithDetail(org.eclipse.wst.sse.core.internal.exceptions.UnsupportedCharsetExceptionWithDetail) IStructuredModel(org.eclipse.wst.sse.core.internal.provisional.IStructuredModel) JsTranslationAdapter(org.eclipse.wst.jsdt.web.core.javascript.JsTranslationAdapter) IOException(java.io.IOException)

Example 24 with IModelManager

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

the class TestFormatProcessorCSS method getModelForEdit.

/**
 * must release model (from edit) after
 *
 * @param filename
 *            relative to this class (TestFormatProcessorCSS)
 */
private IStructuredModel getModelForEdit(final String filename) {
    IStructuredModel model = null;
    try {
        IModelManager modelManager = StructuredModelManager.getModelManager();
        InputStream inStream = getClass().getResourceAsStream(filename);
        if (inStream == null)
            throw new FileNotFoundException("Can't file resource stream " + filename);
        final String baseFile = getClass().getResource(filename).toString();
        model = modelManager.getModelForEdit(baseFile, inStream, null);
    } catch (IOException ex) {
        ex.printStackTrace();
    }
    return model;
}
Also used : InputStream(java.io.InputStream) IModelManager(org.eclipse.wst.sse.core.internal.provisional.IModelManager) FileNotFoundException(java.io.FileNotFoundException) IStructuredModel(org.eclipse.wst.sse.core.internal.provisional.IStructuredModel) IOException(java.io.IOException)

Example 25 with IModelManager

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

the class ModelTest method createHTMLModel.

public static IDOMModel createHTMLModel() {
    // return new XMLModelImpl();
    IStructuredModel model = null;
    try {
        IModelManager modelManager = StructuredModelManager.getModelManager();
        model = modelManager.getModelForEdit("test.html", new NullInputStream(), null);
        // always use the same line delimiter for these tests, regardless
        // of
        // plaform or preference settings
        model.getStructuredDocument().setLineDelimiter(TestWriter.commonEOL);
    } catch (Exception e) {
        e.printStackTrace();
    }
    // null);
    return (IDOMModel) model;
}
Also used : IDOMModel(org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel) IModelManager(org.eclipse.wst.sse.core.internal.provisional.IModelManager) IStructuredModel(org.eclipse.wst.sse.core.internal.provisional.IStructuredModel) IOException(java.io.IOException)

Aggregations

IModelManager (org.eclipse.wst.sse.core.internal.provisional.IModelManager)139 IStructuredModel (org.eclipse.wst.sse.core.internal.provisional.IStructuredModel)102 IDOMModel (org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel)45 IOException (java.io.IOException)37 InputStream (java.io.InputStream)27 IStructuredDocument (org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument)27 IFile (org.eclipse.core.resources.IFile)23 CoreException (org.eclipse.core.runtime.CoreException)21 BadLocationException (org.eclipse.jface.text.BadLocationException)13 FileNotFoundException (java.io.FileNotFoundException)12 IDOMDocument (org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument)11 Document (org.w3c.dom.Document)10 ArrayList (java.util.ArrayList)9 IProject (org.eclipse.core.resources.IProject)9 StringBufferInputStream (java.io.StringBufferInputStream)8 IDocumentPartitioner (org.eclipse.jface.text.IDocumentPartitioner)7 IDocument (org.eclipse.jface.text.IDocument)6 PageDirectiveAdapter (org.eclipse.jst.jsp.core.internal.document.PageDirectiveAdapter)6 ModelQuery (org.eclipse.wst.xml.core.internal.contentmodel.modelquery.ModelQuery)6 UnsupportedEncodingException (java.io.UnsupportedEncodingException)5