Eclipse editor plugin: "ERROR" when opening file outside project
Asked Answered
C

3

8

I'm developing an editor plugin for eclipse. It works fine on files within eclipse projects, but when an external file is opened via the "File -> Open File" menu (which works file with, e.g. Java files), I get a page displaying nothing but a horizontal blue line and the word "ERROR". The Error Log of eclipse is empty, as is the log file in the .metadata directory.

What could cause this? How can I diagnose the error when I have no error message that tells me where to look? There doesn't seem to be a way to get more detailed logging from eclipse.

Edit:

I've found that the source of the problem is close to what jamesh mentioned, but not a ClassCastException - there simply is no IDocument instance for the text viewer to display because StorageDocumentProvider.createDocument() returns null. The reason for this is that it only knows how to create documents for instances of org.eclipse.ui.IStorageEditorInput, but in this case it gets an instance of org.eclipse.ui.ide.FileStoreEditorInput, which does not implement that interface, but instead implements org.eclipse.ui.IURIEditorInput

Cuba answered 18/1, 2009 at 14:25 Comment(0)
N
8

I had the same probleam and finally found solution working for me. You have to provide 2 different document providers - first extending FileDocumentProvider for files inside your workbench, and second extending TextFileDocumentProvider for other resources outside your workspace. Then you register the right provider acording to the input in your editors doSetInput method like this:

private IDocumentProvider createDocumentProvider(IEditorInput input) {
    if(input instanceof IFileEditorInput){
        return new XMLTextDocumentProvider();
    } else if(input instanceof IStorageEditorInput){
        return new XMLFileDocumentProvider();
    } else {
        return new XMLTextDocumentProvider();
    }
}

@Override
protected final void doSetInput(IEditorInput input) throws CoreException {
    setDocumentProvider(createDocumentProvider(input));
    super.doSetInput(input);
}

then in your new document provider (extending TextFileDocumentProvider) insert somethnig like this:

protected FileInfo createFileInfo(Object element) throws CoreException {
        FileInfo info = super.createFileInfo(element);
        if(info==null){
            info = createEmptyFileInfo();
        }
        IDocument document = info.fTextFileBuffer.getDocument();
        if (document != null) {

            /* register your partitioner and other things here 
                       same way as in your fisrt document provider */
        }
        return info;
    }

This works for me :) Finally I have to mention, that I'm not so clever and that I copied this solution from project Amateras (Opensource HTML editor plugin for eclipse)

Necessary answered 25/3, 2009 at 13:12 Comment(1)
Thanks for this! I was really frustrated that the Editor Plug-in example (Eclipse 3.7) didn't work out of the box with non-workspace files. FYI, in 3.7, the type checking in this code doesn't work as-is. It works if you instead do: instanceof IFileEditorInput => XMLFileDocumentProvider, else => XMLTextDocumentProvider. Furthermore, it also works if you simply use the XMLTextDocumentProvider (i.e. a TextFileDocumentProvider) in both cases. More info in this forum post. I'm not sure of a reason to extend FileDocumentProvider.Spill
I
3

I'm a little away from the source code at the moment, though I suspect the problem is a ClassCastException:

  • For a workspace file, the IEditorInput is org.eclipse.ui.IFileEditorInput.
  • For a local non-workspace file, the IEditorInput is org.eclipse.ui.IStorageEditorInput

The difference is in how you get the contents from the IEditorInput. The JDT does an explicit instanceof check to make the switch.

I don't think that the getAdapter(Class clazz) will return a java.io.InputStream if you offer it.

I don't quite understand why they do it like this, but it feels ugly.

Edit: A more general point about debugging eclipse apps - it's really very useful to try and assemble all your logs into one place (i.e. the console).

To do this, make sure you use the command line options -console and -consoleLog. The latter has helped save countless hours of time. If you haven't already, learn the most basic things about how to use the console (ss and start are my most often used). This will save some more time diagnosing a certain class of problem.

Inter answered 20/1, 2009 at 8:56 Comment(1)
That definitely sounds like it could be the cause - will check that when I can (I'm away from that code right now as well :)Cuba
S
0

Did you try creating a JAVA file using the editor, outside the workspace?

When calling the editor with the file path, concat "file://" at the beginning of the file path.e.g: if the path is C://temp//Sample.java, then modify it as file://C://temp//Sample.java.

Sooty answered 18/1, 2009 at 17:57 Comment(1)
I'm not calling the editor at all, I'm opening a file via eclipse's menu. It's a specific file type whose extension causes it to be opened in my editor plugin. I merely mentioned Java as an example of an editor where this works.Cuba

© 2022 - 2024 — McMap. All rights reserved.