Tips on developing Eclipse plugins – I.

I have been recently working on a new JetMinds product (details will be disclosed soon) which included developing set of Eclipse plugins containing (but not limited to) custom editors with syntax coloring, editor outline, content assistant and other various tools similar to ones already available in Eclipse's Java editor.

I have encountered several problems regarding Eclipse complexity, the lack or obscurity of the documentation and examples etc. That's why I began to wrote this series of articles. This is not striving to be complete guide as I just wanted to pinpoint some concepts and ideas I found interesting.

In this article I try to cover:

  • UI (SWT) thread issues
  • Getting reference to an active editor
  • Some IFile issues
  • Opening file in its editor

Eclipse FAQ

Have a look at Eclipse FAQ - one of the best source of information.

UI thread

- also see FAQ

Many methods manipulating with UI components must be called from UI-thread. First possibility (somewhat low-level) is to use SWT Display's syncExec() or asyncExec() method:

JAVA:
  1. editor.getSite().getShell().getDisplay().syncExec(
  2.     //...Runnable with your code here...
  3. );

But Eclipse also offers second (higher-level) method - UIJob:

JAVA:
  1. new UIJob("your job's title") {
  2.     @Override
  3.     public IStatus runInUIThread(IProgressMonitor monitor) {
  4.         //...your code here...
  5.         return Status.OK_STATUS;
  6.     }
  7. }.schedule();

Get active editor

- also see FAQ

To get active editor use something like this (null checks are important):

JAVA:
  1. IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
  2. if (window != null) {
  3.     IWorkbenchPage page = window.getActivePage();
  4.     if (page != null) {
  5.         editor = page.getActiveEditor();
  6.     }
  7. }

Get IFile from EditorInput

Sometimes it is useful to get handle to file opened in editor. It's very easy using adapter pattern (much cleaner approach than casting EditorInput to FileEditorInput etc.).

JAVA:
  1. IFile file = (IFile) editor.getEditorInput().getAdapter(IFile.class);
  2. if (file != null) {
  3.     //...we can use file here...
  4. }

Getting absolute path of IFile

JAVA:
  1. iFile.getLocation().toString()

Getting File from IFile

JAVA:
  1. iFile.getLocation().toFile()

Open file in editor

- also see FAQ, FAQ2, FAQ3

Opening file in associated editor is possible using code like this:

JAVA:
  1. IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
  2.        
  3. if (window != null) {
  4.     IWorkbenchPage page = window.getActivePage();
  5.     if (page != null) {
  6.         try {
  7.             return IDE.openEditor(page, file, activate);
  8.         } catch (PartInitException e) {
  9.             //...something bad went here...
  10.         }
  11.     }
  12. }

I will try to focus on more complex things in later articles.

Tags: , , , ,

One Response to “Tips on developing Eclipse plugins – I.”

  1. [...] Sometimes it is needed to force the refresh of the decorations (e.g. when they depend on some internal state which has changed). It can be achieved by firing fireLabelProviderChanged() event of the decorator (if it is enabled). This has to be done in UI thread (as described here). [...]