org.eclipse.xtext.ui.editor.XtextReadonlyEditorInput Java Examples

The following examples show how to use org.eclipse.xtext.ui.editor.XtextReadonlyEditorInput. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: N4JSEditor.java    From n4js with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Provides input so that the Project Explorer can locate the editor's input in its tree.
 */
@Override
public ShowInContext getShowInContext() {
	IEditorInput editorInput = getEditorInput();
	if (editorInput instanceof FileEditorInput) {
		FileEditorInput fei = (FileEditorInput) getEditorInput();
		return new ShowInContext(fei.getFile(), null);
	} else if (editorInput instanceof XtextReadonlyEditorInput) {
		XtextReadonlyEditorInput readOnlyEditorInput = (XtextReadonlyEditorInput) editorInput;
		IStorage storage;
		try {
			storage = readOnlyEditorInput.getStorage();
			return new ShowInContext(storage.getFullPath(), null);
		} catch (CoreException e) {
			// Do nothing
		}
	}
	return new ShowInContext(null, null);
}
 
Example #2
Source File: N4JSResourceLinkHelper.java    From n4js with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public IStructuredSelection findSelection(IEditorInput input) {

	final IStructuredSelection selection = super.findSelection(input);
	if (null == selection || selection.isEmpty() && input instanceof XtextReadonlyEditorInput) {
		try {
			final IStorage storage = ((XtextReadonlyEditorInput) input).getStorage();
			if (storage instanceof IURIBasedStorage) {
				final URI uri = ((IURIBasedStorage) storage).getURI();
				if (uri.isFile()) {
					final File file = URIUtils.toFile(uri);
					if (file.isFile()) {
						final Node node = getResourceNode(file);
						if (null != node) {
							return new StructuredSelection(node);
						}
					}
				}
			}
		} catch (final CoreException e) {
			LOGGER.error("Error while extracting storage from read-only Xtext editor input.", e);
			return EMPTY;
		}
	}
	return selection;
}
 
Example #3
Source File: JavaClassPathResourceForIEditorInputFactoryTest.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
@Test public void testBug463258_01() throws Exception {
	IJavaProject project = createJavaProject("foo");
	IFile file = project.getProject().getFile("foo.jar");
	file.create(jarInputStream(new TextFile("foo/A.testlanguage", "//empty")), true, monitor());
	addJarToClasspath(project, file);
	
	IPackageFragmentRoot root = project.getPackageFragmentRoot(file);
	IPackageFragment foo = root.getPackageFragment("foo");
	JarEntryFile fileInJar = new JarEntryFile("A.testlanguage");
	fileInJar.setParent(foo);
	
	XtextReadonlyEditorInput editorInput = new XtextReadonlyEditorInput(fileInJar);
	Resource resource = factory.createResource(editorInput);
	assertNotNull(resource);
	resource.load(null);
}
 
Example #4
Source File: JavaClassPathResourceForIEditorInputFactoryTest.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
@Test public void testBug463258_02() throws Exception {
	IJavaProject project = createJavaProject("foo");
	IFile file = project.getProject().getFile("foo.jar");
	file.create(jarInputStream(new TextFile("do/not", "care")), true, monitor());
	addJarToClasspath(project, file);
	
	IPackageFragmentRoot root = project.getPackageFragmentRoot(file);
	IPackageFragment foo = root.getPackageFragment("unknown");
	JarEntryFile fileInJar = new JarEntryFile("doesNotExist.testlanguage");
	fileInJar.setParent(foo);
	
	XtextReadonlyEditorInput editorInput = new XtextReadonlyEditorInput(fileInJar);
	Resource resource = factory.createResource(editorInput);
	assertNotNull(resource);
	try {
		resource.load(null);
	} catch(IOException e) {
		// expected
	}
}
 
Example #5
Source File: JavaClassPathResourceForIEditorInputFactoryTest.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
@Test public void testBug463258_03a() throws Exception {
	IJavaProject project = createJavaProject("foo");
	IFile file = project.getProject().getFile("foo.jar");
	file.create(jarInputStream(new TextFile("foo/bar.testlanguage", "//empty")), true, monitor());
	addJarToClasspath(project, file);
	
	IPackageFragmentRoot root = project.getPackageFragmentRoot(file);
	IPackageFragment foo = root.getPackageFragment("foo");
	JarEntryFile fileInJar = new JarEntryFile("bar.testlanguage");
	fileInJar.setParent(foo);
	
	File jarFile = file.getRawLocation().toFile();
	assertTrue("exists", jarFile.exists());
	assertTrue("delete", jarFile.delete());
	
	XtextReadonlyEditorInput editorInput = new XtextReadonlyEditorInput(fileInJar);
	Resource resource = factory.createResource(editorInput);
	assertNotNull(resource);
	try {
		resource.load(null);
	} catch(IOException e) {
		// expected
	}
}
 
Example #6
Source File: JavaClassPathResourceForIEditorInputFactoryTest.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
@Test(expected=CoreException.class) public void testBug463258_03b() throws Throwable {
	IJavaProject project = createJavaProject("foo");
	IFile file = project.getProject().getFile("foo.jar");
	file.create(jarInputStream(new TextFile("foo/bar.testlanguage", "//empty")), true, monitor());
	
	IPackageFragmentRoot root = JarPackageFragmentRootTestUtil.getJarPackageFragmentRoot(file, (JavaProject) project);
	IPackageFragment foo = root.getPackageFragment("foo");
	JarEntryFile fileInJar = new JarEntryFile("bar.testlanguage");
	fileInJar.setParent(foo);
	
	File jarFile = file.getLocation().toFile();
	assertTrue("exists", jarFile.exists());
	assertTrue("delete", jarFile.delete());
	
	XtextReadonlyEditorInput editorInput = new XtextReadonlyEditorInput(fileInJar);
	try {
		factory.createResource(editorInput);
	} catch(WrappedException e) {
		throw e.getCause();
	}
}
 
Example #7
Source File: JavaClassPathResourceForIEditorInputFactoryTest.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
@Test(expected=CoreException.class) public void testBug463258_03c() throws Throwable {
	IJavaProject project = createJavaProject("foo");
	IFile file = project.getProject().getFile("foo.jar");
	file.create(jarInputStream(new TextFile("foo/bar.testlanguage", "//empty")), true, monitor());
	addJarToClasspath(project, file);
	
	IPackageFragmentRoot root = JarPackageFragmentRootTestUtil.getJarPackageFragmentRoot(file, (JavaProject) project);
	IPackageFragment foo = root.getPackageFragment("foo");
	JarEntryFile fileInJar = new JarEntryFile("bar.testlanguage");
	fileInJar.setParent(foo);
	
	File jarFile = file.getLocation().toFile();
	assertTrue("exists", jarFile.exists());
	assertTrue("delete", jarFile.delete());
	// simulate an automated refresh
	file.refreshLocal(IResource.DEPTH_ONE, null);
	XtextReadonlyEditorInput editorInput = new XtextReadonlyEditorInput(fileInJar);
	try {
		factory.createResource(editorInput);
	} catch(WrappedException e) {
		throw e.getCause();
	}
}
 
Example #8
Source File: JavaClassPathResourceForIEditorInputFactoryTest.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
@Test(expected=CoreException.class) public void testBug463258_04() throws Throwable {
	IFolder externalFolder = createExternalFolder("externalFolder");
	IJavaProject project = createJavaProject("foo");
	
	addExternalFolderToClasspath(project, externalFolder);
	
	IPackageFragmentRoot root = project.getPackageFragmentRoot(externalFolder);
	IPackageFragment foo = root.getPackageFragment("foo");
	NonJavaResource fileInFolder = new NonJavaResource(foo, externalFolder.getFile("foo/doesNotExist.testlanguage"));
	
	externalFolder.delete(true, null);
	XtextReadonlyEditorInput editorInput = new XtextReadonlyEditorInput(fileInFolder);
	try {
		factory.createResource(editorInput);
	} catch(WrappedException e) {
		throw e.getCause();
	}
}
 
Example #9
Source File: JavaClassPathResourceForIEditorInputFactoryTest.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
@Test(expected=CoreException.class) public void testBug463258_05() throws Throwable {
	IJavaProject project = createJavaProject("foo");
	IPackageFragmentRoot root = project.getPackageFragmentRoot("does/not/exist.jar");
	IPackageFragment foo = root.getPackageFragment("foo");
	final JarEntryFile fileInJar = new JarEntryFile("bar.testlanguage");
	fileInJar.setParent(foo);
	XtextReadonlyEditorInput editorInput = new XtextReadonlyEditorInput(fileInJar);
	try {
		factory.createResource(editorInput);
	} catch(WrappedException e) {
		throw e.getCause();
	}
}
 
Example #10
Source File: PlatformPluginAwareEditorOpener.java    From dsl-devkit with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * If a platform plugin URI is given, a read-only Xtext editor is opened and returned. {@inheritDoc}
 *
 * @see {@link org.eclipse.emf.common.util.URI#isPlatformPlugin()}
 */
@Override
public IEditorPart open(final URI uri, final EReference crossReference, final int indexInList, final boolean select) {
  IEditorPart result = super.open(uri, crossReference, indexInList, select);
  if (result == null && (uri.isPlatformPlugin() || OSGI_RESOURCE_URL_PROTOCOL.equals(uri.scheme()))) {
    final IModelLocation modelLocation = getModelLocation(uri.trimFragment());
    if (modelLocation != null) {
      PlatformPluginStorage storage = new PlatformPluginStorage(modelLocation);
      IEditorInput editorInput = new XtextReadonlyEditorInput(storage);
      IWorkbenchPage activePage = workbench.getActiveWorkbenchWindow().getActivePage();
      try {
        IEditorPart editor = IDE.openEditor(activePage, editorInput, editorID);
        selectAndReveal(editor, uri, crossReference, indexInList, select);
        return EditorUtils.getXtextEditor(editor);
      } catch (WrappedException e) {
        LOG.error("Error while opening editor part for EMF URI '" + uri + "'", e.getCause()); //$NON-NLS-1$ //$NON-NLS-2$
      } catch (PartInitException partInitException) {
        LOG.error("Error while opening editor part for EMF URI '" + uri + "'", partInitException); //$NON-NLS-1$ //$NON-NLS-2$
      }
    }
  }
  return result;
}
 
Example #11
Source File: CheckHyperlinkHelper.java    From dsl-devkit with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public void createHyperlinksByOffset(final XtextResource resource, final int offset, final IHyperlinkAcceptor acceptor) {
  IEditorPart activeEditor = workbench.getActiveWorkbenchWindow().getActivePage().getActiveEditor();
  if (activeEditor.getEditorInput() instanceof XtextReadonlyEditorInput) {
    INode crossRefNode = eObjectAtOffsetHelper.getCrossReferenceNode(resource, new TextRegion(offset, 0));
    if (crossRefNode == null) {
      return;
    }
    EObject crossLinkedEObject = eObjectAtOffsetHelper.getCrossReferencedElement(crossRefNode);
    if (crossLinkedEObject != null && crossLinkedEObject.eClass().getEPackage() != CheckPackage.eINSTANCE) {
      return;
    }
    // if EPackage of referenced object is CheckPackage, try to provide hyperlinks: works for included catalogs
  }
  super.createHyperlinksByOffset(resource, offset, acceptor);
}
 
Example #12
Source File: N4JSEditor.java    From n4js with Eclipse Public License 1.0 5 votes vote down vote up
/** This will show the icon variant according to the file extension: n4js, n4jsx, n4jsd, js, jsx */
protected Image getImageN4JSVariantOrGiven(Image titleImage) {
	IEditorInput input = getEditorInput();
	URI uri = null;
	if (input instanceof XtextReadonlyEditorInput) {
		XtextReadonlyEditorInput xrei = (XtextReadonlyEditorInput) input;
		try {
			IStorage storage = xrei.getStorage();
			if (storage instanceof URIBasedStorage) {
				URIBasedStorage ubs = (URIBasedStorage) storage;
				uri = ubs.getURI();
			}
		} catch (CoreException e) {
			e.printStackTrace();
		}
	}
	if (input instanceof FileEditorInput) {
		FileEditorInput fei = (FileEditorInput) input;
		uri = URIUtils.convert(fei.getFile());

	}
	if (uri != null) {
		Image image = labelProvider.getImageForURI(uri);
		if (image != null) {
			titleImage = image;
		}
	}
	return titleImage;
}
 
Example #13
Source File: EditorUtils.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * @since 2.8
 */
public static IEditorInput createEditorInput(IStorage storage) {
	if (storage instanceof IFile)
		return new FileEditorInput((IFile) storage);
	try {
		if (storage instanceof IJarEntryResource)
			return new JarEntryEditorInput(storage);
	} catch (NoClassDefFoundError e) {
		// ignore. can happen if JDT is not available.
	}
	if (storage instanceof FileStoreStorage) {
		return new FileStoreEditorInput(((FileStoreStorage)storage).getFileStore());
	}
	return new XtextReadonlyEditorInput(storage);
}
 
Example #14
Source File: CheckValidateActionHandler.java    From dsl-devkit with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public Object execute(final ExecutionEvent event) throws ExecutionException {
  XtextEditor xtextEditor = EditorUtils.getActiveXtextEditor(event);
  if (xtextEditor != null && xtextEditor.getEditorInput() instanceof XtextReadonlyEditorInput) {
    return null;
  }
  return super.execute(event);
}