Tips on developing Eclipse plugins - VII.

Have you ever wondered how to:

  • Run some code on plugin startup?
  • Set text file encoding?

If the answer to any of these questions is yes, be sure to read on!

How to run code when plugin starts

Sometimes it is necessary to run custom code on plugin startup. Two steps are needed to do this. First, add the following to your plugin.xml:

<extension point="org.eclipse.ui.startup">
    <startup class="com.ourcompany.Startup" />
</extension>

Second, create the startup class:

public class Startup implements IStartup {
    public void earlyStartup() {
        //do whatever is desired on plugin startup here
    }
}

How to set text file encoding

We needed to support special text files that have character encoding specified inside the file. In custom editors this can be achieved:

public class OurEditor extends TextEditor {
    @Override
    protected void installEncodingSupport() {
        super.installEncodingSupport();
       
        //This is just for setting encoding at the first time,
        //subsequent changes are handled by doSetInput().
        //But first doSetInput is called before encoding support
        //is installed so we have to do it here.
        refreshEncoding();
    }

    @Override
    protected void doSetInput(IEditorInput input) throws CoreException {
        super.doSetInput(input);
        refreshEncoding();
    }

    protected void refreshEncoding() {
        IEncodingSupport encodingSupport =
            (IEncodingSupport) getAdapter(IEncodingSupport.class);
       
        if (encodingSupport != null) {
            //we can set any encoding supported by Java
            encodingSupport.setEncoding("UTF-8");
        }
    }

}

Tags: , , ,

One Response to “Tips on developing Eclipse plugins - VII.”

  1. Tips on developing Eclipse plugins - VI. - Automatic exception reporting to Bugzilla - dolejsky.com Says:

    […] Things good to know… « Tweaking XOM parser - element position support Tips on developing Eclipse plugins - VII. […]

Leave a Reply