Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 320374

Summary: [editor] JavaScript editor does not support alternative DocumentProviders
Product: [WebTools] JSDT Reporter: Christian Pontesegger <christian.pontesegger>
Component: GeneralAssignee: Project Inbox <jsdt.javascript-inbox>
Status: NEW --- QA Contact: Chris Jaun <cmjaun>
Severity: enhancement    
Priority: P3 CC: thatnitind
Version: unspecified   
Target Milestone: Future   
Hardware: PC   
OS: All   
Whiteboard:

Description Christian Pontesegger CLA 2010-07-20 06:56:25 EDT
Build Identifier: I20100608-0911

I am using the javascript editor (CompilationUnitEditor.class) within my RCP to edit script files - which works fine for all kinds of IFile. Now I wanted to add another IDocumentProvider to edit some JavaScript code that is generated by the RCP (and not written to any IFile). Unfortunately the editor fires up with a single line stating "error".

This is how I am accessing the editor:
final IEditorDescriptor editorDescriptor = PlatformUI.getWorkbench().getEditorRegistry()
				.getDefaultEditor("foo.js");
final IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
final MacroEditorInput editorInput = new MacroEditorInput("//test\nvar a = 5;");
page.openEditor(editorInput, editorDescriptor.getId(), true);


I tracked it down to 
CompilationUnitEditor.setDocumentProvider()
here my registered provider is assigned to fImplicitDocumentProvider. But as there is an explicit provider too (which is of type CompilationunitDocumentProvider) my implicit provider is never used.

How is it possible to edit such files?


Reproducible: Always
Comment 1 Christian Pontesegger CLA 2011-03-02 07:46:43 EST
I found a way to make this work:

The editorInput used for the editor needs to implement IStorageEditorInput.
here is a reference implementation:

public class MacroEditorInput implements IStorageEditorInput {

	@Override
	public Object getAdapter(final Class adapter) {
		return null;
	}

	@Override
	public IPersistableElement getPersistable() {
		return null;
	}

	@Override
	public boolean exists() {
		return true;
	}

	@Override
	public IStorage getStorage() {
		return new IStorage() {
			@Override
			public Object getAdapter(final Class adapter) {
				return null;
			}

			@Override
			public boolean isReadOnly() {
				return false;
			}

			@Override
			public String getName() {
				return MacroEditorInput.this.getName();
			}

			@Override
			public IPath getFullPath() {
				return null;
			}

			@Override
			public InputStream getContents() throws CoreException {
				return new StringBufferInputStream("this is the content for the editor");
			}
		};
	}

	@Override
	public final ImageDescriptor getImageDescriptor() {
		return null;
	}

	@Override
	public final String getName() {
		return "Editor with generated content";
	}

	@Override
	public final String getToolTipText() {
		return "My editor";
	}
}