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 :

» Progress Bars in Eclipse UI

In the "" article, I said the top most mistake is running long running operations in the UI thread. Assuming that you are running it in a non-UI thread, how to show the progress of the execution? Obviously thru progress monitors. But how many...

» Reload your plugins without restarting Eclipse

When you are developing Eclipse plugins, sometimes its annoying that the changes in the plugin.xml won't reflect immediately. You need to restart the target Eclipse to see the changes. This will be painful if you are playing with trial-n-error stuff...

» 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...

» Opening an Editor programmatically

When you click on a file in the Package Explorer or Navigator, the file will open the associated editor. If you are looking for a way to do the same action through code, this tip is for you.

» 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...

Subscribe To

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