[RCP] Loading image from plugin directory [message #268382] |
Tue, 24 August 2004 08:00  |
Eclipse User |
|
|
|
Hi.
I know that this question has been asked quite frequently here, but I
couldn't find the appropriate solution, yet.
How can I load an image from a file, e.g. located in the plugin's
'icons' subdrectory? One solution I found goes like this:
final Image myImage =
ImageDescriptor
.createFromFile(MyPlugin.class,"icons/myImage.gif")
.createImage();
In my case I haven't had a plugin class. I didn't understand why
the plugin class is optional, but I chose not to create one.
This seems to be one reason why it is better to have one?
So I created a plugin class an tried to execute the code above
but it simply doesn't work. Here's my plugin structure:
com.mycompany.myplugin_0.0.0/
icons/
myImage.gif
myplugin.jar
...
myplugin.jar contains the following classes:
com.mycompany.myplugin.
MyPlugin.class -> extends AbstractUIPlugin
MyPluginApp.class -> implements IPlatformRunnable
MyDialog.class -> extends TitleAreaDialog
MyDialog contains the code to load the image from a file
in the 'icons' directory. At runtime only a small red
square will be displayed telling me that my image could not
be found / loaded.
BTW: where is the right place to dispose the image?
In the finalizer?
I'm quite surprised that it seems to be so hard to load an image
from a file resource. Is there an FAQ entry on this issue yet?
I couldn't find one.
Cheers,
Marcus
|
|
|
Re: [RCP] Loading image from plugin directory [message #268390 is a reply to message #268382] |
Tue, 24 August 2004 08:54   |
Eclipse User |
|
|
|
Originally posted by: Andrew_Eidsness.ca.ibm.com
Just a guess, but something like this might work:
Bundle bundle = Platform.getBundle("your.bundle.id"); // aka your
plugin's id
IPath imagePath = new Path("icons/image.gif");
URL imageUrl = Platform.find(bundle, imagePath);
ImageDescriptor desc = ImageDescriptor.createFromFile(imageUrl);
Image image = desc.createImage();
-Andrew
On Tue, 24 Aug 2004 14:00:49 +0200, Marcus Olk <molk@comosoft.de> wrote:
> Hi.
>
> I know that this question has been asked quite frequently here, but I
> couldn't find the appropriate solution, yet.
>
> How can I load an image from a file, e.g. located in the plugin's
> 'icons' subdrectory? One solution I found goes like this:
>
> final Image myImage =
> ImageDescriptor
> .createFromFile(MyPlugin.class,"icons/myImage.gif")
> .createImage();
>
> In my case I haven't had a plugin class. I didn't understand why
> the plugin class is optional, but I chose not to create one.
> This seems to be one reason why it is better to have one?
> So I created a plugin class an tried to execute the code above
> but it simply doesn't work. Here's my plugin structure:
>
> com.mycompany.myplugin_0.0.0/
> icons/
> myImage.gif
> myplugin.jar
> ...
>
> myplugin.jar contains the following classes:
>
> com.mycompany.myplugin.
> MyPlugin.class -> extends AbstractUIPlugin
> MyPluginApp.class -> implements IPlatformRunnable
> MyDialog.class -> extends TitleAreaDialog
>
> MyDialog contains the code to load the image from a file
> in the 'icons' directory. At runtime only a small red
> square will be displayed telling me that my image could not
> be found / loaded.
>
> BTW: where is the right place to dispose the image?
> In the finalizer?
>
> I'm quite surprised that it seems to be so hard to load an image
> from a file resource. Is there an FAQ entry on this issue yet?
> I couldn't find one.
>
> Cheers,
> Marcus
|
|
|
|
|
|
|
|
|
|
|
|
Re: [RCP] Loading image from plugin directory [message #268683 is a reply to message #268637] |
Wed, 25 August 2004 10:19  |
Eclipse User |
|
|
|
Martin Klinke wrote:
> Use the dialog's shell ;) Add the following code in your dialog class:
I assumed the parent shell passed to the dialog is the same as the
Window shell. That's nonsense, of course. O.k. this works for me now.
A little helper class like this could be reused for connecting
the disposal of images created in a dialog with the disposal
of the dialog's shell:
private static class ImageDisposer
implements DisposeListener {
final Image image;
ImageDisposer(Shell shell, Image image) {
this.image = image;
shell.addDisposeListener(this);
}
public void widgetDisposed(DisposeEvent e) {
if (this.image != null && !this.image.isDisposed())
this.image.dispose();
}
}
....would be used like this:
final Image logoImage = MyPlugin.createImage("logo.gif");
new Disposer(getShell(), logoImage);
setTitleImage(logoImage);
A common utility class would be appropriate here: A disposer that
gets an IDisposeObservable (e.g. the shell) and the IDisposable object.
But there's no IDisposeObservable (demanding addDisposeListener)
nor an IDisposable interface (demanding dispose and isDisposed)
that the Shell class and the disposable SWT objects do implement.
That's a little sad because one of the 'core features' of SWT is
the manual house keeping task a developer has to cope with.
Thus it is surprising to see that IBM didn't use corresponding interfaces
like the ones I suggested above.
Marcus
|
|
|
Powered by
FUDForum. Page generated in 0.05121 seconds