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:
[xml]



[/xml]

Second, create the startup class:
[java]
public class Startup implements IStartup {
public void earlyStartup() {
//do whatever is desired on plugin startup here
}
}
[/java]

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:

[java]
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”);
}
}

}
[/java]

Tags: , , ,

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

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