Search in sources :

Example 36 with IStructuredModel

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

the class ContentAssistUtils method getNodeAt.

/**
 * Returns the closest IndexedRegion for the offset and viewer allowing
 * for differences between viewer offsets and model positions. note: this
 * method returns an IndexedRegion for read only
 *
 * @param viewer
 *            the viewer whose document is used to compute the proposals
 * @param documentOffset
 *            an offset within the document for which completions should
 *            be computed
 * @return an IndexedRegion
 */
public static IndexedRegion getNodeAt(ITextViewer viewer, int documentOffset) {
    if (viewer == null)
        return null;
    IndexedRegion node = null;
    IModelManager mm = StructuredModelManager.getModelManager();
    IStructuredModel model = null;
    if (mm != null)
        model = mm.getExistingModelForRead(viewer.getDocument());
    try {
        if (model != null) {
            int lastOffset = documentOffset;
            node = model.getIndexedRegion(documentOffset);
            while (node == null && lastOffset >= 0) {
                lastOffset--;
                node = model.getIndexedRegion(lastOffset);
            }
        }
    } finally {
        if (model != null)
            model.releaseFromRead();
    }
    return node;
}
Also used : IModelManager(org.eclipse.wst.sse.core.internal.provisional.IModelManager) IStructuredModel(org.eclipse.wst.sse.core.internal.provisional.IStructuredModel) IndexedRegion(org.eclipse.wst.sse.core.internal.provisional.IndexedRegion)

Example 37 with IStructuredModel

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

the class AbstractOpenOn method getFile.

/**
 * Returns an IFile from the given uri if possible, null if cannot find
 * file from uri.
 *
 * @param fileString
 *            file system path
 * @return returns IFile if fileString exists in the workspace
 */
protected IFile getFile(String fileString) {
    IStructuredModel model = null;
    IFile file = null;
    try {
        model = StructuredModelManager.getModelManager().getExistingModelForRead(getDocument());
        if (model != null) {
            // use the base location to obtain the in-workspace IFile
            IFile modelFile = ResourcesPlugin.getWorkspace().getRoot().getFile(new Path(model.getBaseLocation()));
            if (modelFile != null) {
                // find the referenced file's location on disk
                URIResolver resolver = model.getResolver();
                if (resolver != null) {
                    String filesystemLocation = resolver.getLocationByURI(fileString);
                    if (filesystemLocation != null) {
                        IFile[] workspaceFiles = ResourcesPlugin.getWorkspace().getRoot().findFilesForLocation(new Path(filesystemLocation));
                        // favor a workspace file in the same project
                        for (int i = 0; i < workspaceFiles.length && file == null; i++) {
                            if (workspaceFiles[i].getProject().equals(modelFile.getProject())) {
                                file = workspaceFiles[i];
                            }
                        }
                        // if none were in the same project, just pick one
                        if (file == null && workspaceFiles.length > 0) {
                            file = workspaceFiles[0];
                        }
                    }
                }
            }
        }
    } catch (Exception e) {
        Logger.log(Logger.WARNING, e.getMessage());
    } finally {
        if (model != null) {
            model.releaseFromRead();
        }
    }
    if (file == null && fileString.startsWith("/")) {
        // $NON-NLS-1$
        file = ResourcesPlugin.getWorkspace().getRoot().getFile(new Path(fileString));
    }
    return file;
}
Also used : Path(org.eclipse.core.runtime.Path) IFile(org.eclipse.core.resources.IFile) URIResolver(org.eclipse.wst.sse.core.internal.util.URIResolver) IStructuredModel(org.eclipse.wst.sse.core.internal.provisional.IStructuredModel) PartInitException(org.eclipse.ui.PartInitException)

Example 38 with IStructuredModel

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

the class BreakpointRulerAction method getResource.

public static final IResource getResource(IEditorInput input) {
    IResource resource = null;
    if (input instanceof IFileEditorInput)
        resource = ((IFileEditorInput) input).getFile();
    if (resource == null)
        resource = input.getAdapter(IFile.class);
    if (resource == null)
        resource = input.getAdapter(IResource.class);
    IEditorPart editorPart = null;
    if (resource == null) {
        IWorkbench workbench = SSEUIPlugin.getDefault().getWorkbench();
        if (workbench != null) {
            IWorkbenchWindow window = workbench.getActiveWorkbenchWindow();
            if (window != null) {
                IPartService service = window.getPartService();
                if (service != null) {
                    Object part = service.getActivePart();
                    if (part != null && part instanceof IEditorPart) {
                        editorPart = (IEditorPart) part;
                        if (editorPart != null) {
                            IStructuredModel model = null;
                            ITextEditor textEditor = null;
                            try {
                                if (editorPart instanceof ITextEditor) {
                                    textEditor = (ITextEditor) editorPart;
                                }
                                if (textEditor == null) {
                                    textEditor = editorPart.getAdapter(ITextEditor.class);
                                }
                                if (textEditor != null) {
                                    IDocument textDocument = textEditor.getDocumentProvider().getDocument(input);
                                    model = StructuredModelManager.getModelManager().getExistingModelForRead(textDocument);
                                    if (model != null) {
                                        resource = BreakpointProviderBuilder.getInstance().getResource(input, model.getContentTypeIdentifier(), getFileExtension(input));
                                    }
                                }
                                if (resource == null) {
                                    IBreakpointProvider[] providers = BreakpointProviderBuilder.getInstance().getBreakpointProviders(editorPart, null, getFileExtension(input));
                                    for (int i = 0; i < providers.length && resource == null; i++) {
                                        resource = providers[i].getResource(input);
                                    }
                                }
                            } catch (Exception e) {
                                Logger.logException(e);
                            } finally {
                                if (model != null) {
                                    model.releaseFromRead();
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    return resource;
}
Also used : IWorkbenchWindow(org.eclipse.ui.IWorkbenchWindow) ITextEditor(org.eclipse.ui.texteditor.ITextEditor) IEditorPart(org.eclipse.ui.IEditorPart) IPartService(org.eclipse.ui.IPartService) IBreakpoint(org.eclipse.debug.core.model.IBreakpoint) CoreException(org.eclipse.core.runtime.CoreException) BadLocationException(org.eclipse.jface.text.BadLocationException) IWorkbench(org.eclipse.ui.IWorkbench) IFileEditorInput(org.eclipse.ui.IFileEditorInput) IStructuredModel(org.eclipse.wst.sse.core.internal.provisional.IStructuredModel) IBreakpointProvider(org.eclipse.wst.sse.ui.internal.provisional.extensions.breakpoint.IBreakpointProvider) IResource(org.eclipse.core.resources.IResource) IDocument(org.eclipse.jface.text.IDocument)

Example 39 with IStructuredModel

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

the class ToggleBreakpointAction method getContentType.

protected String getContentType(IDocument document) {
    IModelManager mgr = StructuredModelManager.getModelManager();
    String contentType = null;
    IDocumentProvider provider = fTextEditor.getDocumentProvider();
    if (provider instanceof IDocumentProviderExtension4) {
        try {
            IContentType type = ((IDocumentProviderExtension4) provider).getContentType(fTextEditor.getEditorInput());
            if (type != null)
                contentType = type.getId();
        } catch (CoreException e) {
        /*
				 * A failure accessing the underlying store really isn't
				 * interesting, although it can be a problem for
				 * IStorageEditorInputs.
				 */
        }
    }
    if (contentType == null) {
        IStructuredModel model = null;
        try {
            model = mgr.getExistingModelForRead(document);
            if (model != null) {
                contentType = model.getContentTypeIdentifier();
            }
        } finally {
            if (model != null) {
                model.releaseFromRead();
            }
        }
    }
    return contentType;
}
Also used : IDocumentProvider(org.eclipse.ui.texteditor.IDocumentProvider) CoreException(org.eclipse.core.runtime.CoreException) IModelManager(org.eclipse.wst.sse.core.internal.provisional.IModelManager) IDocumentProviderExtension4(org.eclipse.ui.texteditor.IDocumentProviderExtension4) IContentType(org.eclipse.core.runtime.content.IContentType) IStructuredModel(org.eclipse.wst.sse.core.internal.provisional.IStructuredModel)

Example 40 with IStructuredModel

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

the class ContentSettingsPropertyPage method validateEdit.

/**
 * Method validateEdit.
 *
 * @param file
 *            org.eclipse.core.resources.IFile
 * @param context
 *            org.eclipse.swt.widgets.Shell
 * @return IStatus
 */
private static IStatus validateEdit(IFile file, Shell context) {
    if (file == null || !file.exists())
        return STATUS_ERROR;
    if (!(file.isReadOnly()))
        return STATUS_OK;
    IPath location = file.getLocation();
    final long beforeModifiedFromJavaIO = (location != null) ? location.toFile().lastModified() : IResource.NULL_STAMP;
    final long beforeModifiedFromIFile = file.getModificationStamp();
    IStatus status = ResourcesPlugin.getWorkspace().validateEdit(new IFile[] { file }, context);
    if (!status.isOK())
        return status;
    final long afterModifiedFromJavaIO = (location != null) ? location.toFile().lastModified() : IResource.NULL_STAMP;
    final long afterModifiedFromIFile = file.getModificationStamp();
    if (beforeModifiedFromJavaIO != afterModifiedFromJavaIO || beforeModifiedFromIFile != afterModifiedFromIFile) {
        IModelManager manager = StructuredModelManager.getModelManager();
        IStructuredModel model = manager.getExistingModelForRead(file);
        if (model != null) {
            if (!model.isDirty()) {
                try {
                    file.refreshLocal(IResource.DEPTH_ONE, null);
                } catch (CoreException e) {
                    return STATUS_ERROR;
                } finally {
                    model.releaseFromRead();
                }
            } else {
                model.releaseFromRead();
            }
        }
    }
    if ((beforeModifiedFromJavaIO != afterModifiedFromJavaIO) || (beforeModifiedFromIFile != afterModifiedFromIFile)) {
        // applied.
        return STATUS_ERROR;
    }
    return STATUS_OK;
}
Also used : IStatus(org.eclipse.core.runtime.IStatus) IPath(org.eclipse.core.runtime.IPath) CoreException(org.eclipse.core.runtime.CoreException) IModelManager(org.eclipse.wst.sse.core.internal.provisional.IModelManager) 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