Justplayit94
Justplayit94

Reputation: 423

Eclipse Plugin Set Image icon for a custom dialog

I made a custom Eclipse plugin that uses and displays multiple dialogs, and I want to know if I could set the top left icon image with the one I use in the plugin's icons folder. I want to get that icon and set it instead of the default one that Eclipse uses. I'm overriding the configureShell() method to change the dialog title, and I also want to change the icon.

@Override
protected void configureShell(Shell parent){
    super.configureShell(parent);
    parent.setText("Choose variant...");
    Image icon = new Image(parent.getDisplay(), "icons/best.gif"); - this method does not work as it cannot find the file
    parent.setImage(icon);
}

I also tried using the getClass().getResource("best.gif") and having the image in the same package, still can't find the location I'm giving(FileNotFoundException), and also, the Image constructor does not accept URL objects.

@Override
protected void configureShell(Shell parent){
    super.configureShell(parent);
    parent.setText("Choose variant...");
    Image icon = new Image(parent.getDisplay(), getClass().getResource("icons/best.gif"));
    parent.setImage(icon);
}

Is there a way to use the icon that I already have in my eclipse plugin? The main problem is getting the icon from the icons folder of the plugin and making it a Image object.
Thank you.

Upvotes: 1

Views: 1653

Answers (2)

moe
moe

Reputation: 1806

You can register the icon in your plugins activator class like this:

@Override
protected void initializeImageRegistry(final ImageRegistry reg) {
    reg.put(IMAGE_PATH, imageDescriptorFromPlugin(PLUGIN_ID, IMAGE_PATH));
}

The image path is relative to your plugin, e.g. icons/icon.png.

You can access these images via the activator class as well:

final Image image = MyActivatorClass.getDefault().getImageRegistry().get(IMAGE_PATH);
myShell.setImage(image);

(Note that I used the image path as key in the image registry, you do not have to do it like this but it makes everything a little bit less complicated by just using the same static String.)

Upvotes: 3

greg-449
greg-449

Reputation: 111142

For an Eclipse plugin you use the FileLocator class to find resources in your plugin.

For an image use something like:

String path = "icons/best.gif";

URL url = FileLocator.find(bundle, new Path(path), null);

ImageDescriptor desc = ImageDescriptor.createFromURL(url);

Image image = desc.createImage();

Note: You must arrange for the image to be disposed when no longer needed.

If your activator extends AbstractUIPlugin you can also use the ImageRegistry available from that. The registry will deal with disposing.

Be sure that the icons directory is listed in the build.properties file. Missing this will causes issues when you export your plugin.

Upvotes: 2

Related Questions