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() {

return new MyStorage();
}

private final class MyStorage implements 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.

 

 

More articles :

» Remember the State

As a rule of thumb, your application should try to remember the state across sessions. So when a user hits the close button and then opens it after few mins/days, it should present exactly in the same way where he left. In this tip, I'm going to...

» A glimpse at the Faceted Project Framework

If you haven't noticed, the Faceted Project Framework from WTP is now proposed as a separate project. I like the framework very much, probably because my very first Eclipse Plug-in is simply a WTP Facet :-) When I wrote that plug-in, there was no...

» Selection Dialogs in Eclipse

If you are an Eclipse Plug-in developer, you must have used the MessageDialog. There are many other Dialogs provided by Eclipse Platform are reusable and part of the API. I'll try to explain the various selection dialogs that I know of. In case I've...

» Adding a new editorAction for Orion

In case you have not heard it yet,  is the new Web IDE from Eclipse. In a blog entry , I said that this one is going to stand out from the crowd and going to rule the world. Why can't the other Web IDEs do that? Because, Orion also would be walking...

» How to add TrayItem in Eclipse RCP application?

In an application like Yahoo Messenger or Skype, you don't want to quit the application when the main window is closed. Most of these applications will move themselves to System Tray when the main window is closed and clicking on the Tray icon would...

Subscribe To

Unless stated, all the text contents of this site is available under Eclipse Public License