Přemysl Šťastný Přemysl Šťastný - 4 months ago 5
Java Question

How can make IEditorInput, that will read file from memory?

I am writing plug-in to eclipse and I need to open editor for file loaded in RAM. When I am opening new editor, it takes input from IEditorInput. There are many classes, which are implements this interface, but they are usually reading file from HDD, or they are useless for this purpose.

So the question is:
How can make IEditorInput, that will read file from memory?

Answer

You can use an object implementing IStorageEditorInput as the editor input. This uses an IStorage object to hold the in memory data to be edited.

A simple IStorageEditorInput implementation would look something like:

class StorageEditorInput extends PlatformObject implements IStorageEditorInput {
    private IStorage fStorage;

    public StorageEditorInput(IStorage storage) {
        fStorage = storage;
    }

    @Override
    public IStorage getStorage() {
        return fStorage;
    }

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

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

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

    @Override
    public String getToolTipText() {
        return getStorage().getFullPath().toOSString();
    }

    @Override
    public boolean equals(Object object) {
        return object instanceof StorageEditorInput &&
         getStorage().equals(((StorageEditorInput)object).getStorage());
    }

    @Override
    public int hashCode() {
        return getStorage().hashCode();
    }

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

(above adapted from org.eclipse.debug.internal.ui.views.console.ProcessConsole)

You will also have to provide your own implementation of IStorage, in fact you should probably implement IEncodedStorage which extends IStorage to include the character encoding.

Note that some editors may not understand IStorageEditorInput.