Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Eclipse editor plugin: "ERROR" when opening file outside project

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

like image 614
Michael Borgwardt Avatar asked Jan 18 '09 14:01

Michael Borgwardt


2 Answers

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)

like image 117
Martin Lazar Avatar answered Sep 17 '22 19:09

Martin Lazar


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.

like image 44
jamesh Avatar answered Sep 19 '22 19:09

jamesh