Eclipse Search

Loading

Jun 4, 2008

Opening an editor without IFile

When we use Eclipse, we are used to the notion of editors operating on a workspace file. But workspace and resources are optional for an RCP application. You might have to invoke an editor on a non-file object like a database record or an in-memory object. So one of the frequently asked questions is how to open an editor with a file? This tip explains how to do that.



For this I'll be using a simple sting as the model to be edited and use the default text editor to edit it. You can switch the model or editor with whatever you prefer (say a result of a database query edited in a form based editor).



Editors in Eclipse know nothing about the files/actual objects they edit. They always operate on IEditorInput. There are standard implementations like FileEditorInput (for workspace files), FileStoreEditorInput (for files on disk). In our case we have to create our own implementation. I have extended the IStorageEditorInput to make things easier:



public class StringEditorInput implements IStorageEditorInput {



    private final String inputString;



    public StringEditorInput(String inputString) {

        this.inputString = inputString;

    }



    public boolean exists() {

        return false;

    }



    public ImageDescriptor getImageDescriptor() {

        return null;

    }



    public IPersistableElement getPersistable() {

        return null;

    }



    public Object getAdapter(Class adapter) {

        return null;

    }



    public String getName() {

        return "input name";

    }



    public String getToolTipText() {

        return "tool tip";

    }



    public IStorage getStorage() throws CoreException {

        return new IStorage() {



            public InputStream getContents() throws CoreException {

                return new StringBufferInputStream(inputString);

            }



            public IPath getFullPath() {

                return null;

            }



            public String getName() {

                return StringEditorInput.this.getName();

            }



            public boolean isReadOnly() {

                return false;

            }



            public Object getAdapter(Class adapter) {

                return null;

            }



        };

    }



}






In a command, I've opened the text editor with this call:



workbenchPage.openEditor(new StringEditorInput("text to be edited"), "org.eclipse.ui.DefaultTextEditor");



The output:





Hope the above picture shows where the various strings I've used in the class goes into. I'm returning null for the getPersistable() method. So you cannot Save the contents of this editor. If you want to do that, you have to return an instance of IPersistableElement. Write back to the database or send a Http Post message - the implementation is left to your application.

5 comments:

  1. Excellent article. This is exactly what I needed!

    ReplyDelete
  2. Really interesting blog!

    How would I go about when I want the same editor to support both IFile resources and your above sketched functionality?

    The reason I ask, is that we want to set up an Editor where we can instantiate one with content, as you described, but which does allow SaveAs as to work from the IFile afterwards. How should I do that?

    ReplyDelete
  3. > How would I go about when I want the same editor to support both
    >IFile resources and your above sketched functionality?

    The editors operate on IEditorInput, which is the base class for our StringEditorInput and IFileEditorInput. So there is nothing special needs to be done in the editor to accomplish the behaviour you want.

    > Really interesting blog!
    Thanks :-) You can subscribe thru RSS/EMail to get my future tips.

    ReplyDelete
  4. Very good tip - however I am confused about your final comments regarding the use if IPersistableElement if you need to save the contents of the editor. I thought that IPersistableElement was only used to maintain editor state between invocations of Eclipse. I didn't think it was involved in the saving of the editor contents when the Save/Save As options are selected. In fact I cannot find any articles on how to save the editor contents back to internal storage - examples of this would be very very useful.

    ReplyDelete
  5. Implement this entry WebPageEditor, the editor takes me entry well that happened as a string but does not the context or the palette, someone could help me with this thanks

    ReplyDelete