Example usage for org.eclipse.jdt.core IJavaModel getResource

List of usage examples for org.eclipse.jdt.core IJavaModel getResource

Introduction

In this page you can find the example usage for org.eclipse.jdt.core IJavaModel getResource.

Prototype

IResource getResource();

Source Link

Document

Returns the innermost resource enclosing this element.

Usage

From source file:net.sf.j2s.ui.actions.UnitJavaScriptUtil.java

License:Open Source License

public static boolean openEditor(ICompilationUnit unit) {
    String relativePath = getRelativeJSPath(unit);
    IJavaModel javaModel = unit.getJavaModel();
    File file = new File(javaModel.getResource().getLocation().toOSString(), relativePath);

    IFile[] files = javaModel.getWorkspace().getRoot()
            .findFilesForLocation(Path.fromPortableString(relativePath));
    IEditorInput editorInput = null;// w w  w. j a va  2s .c  o  m
    if (files != null && files.length != 0) {
        editorInput = new FileEditorInput(files[0]);
        try {
            Java2ScriptUIPlugin.getDefault().getWorkbench().getActiveWorkbenchWindow().getActivePage()
                    .openEditor(editorInput, getEditorID());
            return true;
        } catch (PartInitException e) {
            e.printStackTrace();
        }
    } else {
        IFileStore fileStore = EFS.getLocalFileSystem().getStore(new Path(file.getParent()));
        fileStore = fileStore.getChild(file.getName());
        if (!fileStore.fetchInfo().isDirectory() && fileStore.fetchInfo().exists()) {
            IWorkbenchWindow fWindow = Java2ScriptUIPlugin.getDefault().getWorkbench()
                    .getActiveWorkbenchWindow();
            IWorkbenchPage page = fWindow.getActivePage();
            try {
                // Copy from IDE.openEditorOnFileStore
                IEditorInput input = getEditorInput(fileStore);
                String editorId = null;
                IEditorDescriptor descriptor;
                try {
                    descriptor = IDE.getEditorDescriptor("java2script.txt"); // text editor
                    if (descriptor != null)
                        editorId = descriptor.getId();
                } catch (PartInitException e) {
                }

                // open the editor on the file
                page.openEditor(input, editorId);
                return true;
            } catch (PartInitException e) {
                String msg = NLS.bind(IDEWorkbenchMessages.OpenLocalFileAction_message_errorOnOpen,
                        fileStore.getName());
                IDEWorkbenchPlugin.log(msg, e.getStatus());
                MessageDialog.openError(fWindow.getShell(), IDEWorkbenchMessages.OpenLocalFileAction_title,
                        msg);
            }
        }
    }
    return false;
}