I was never happy with how tightly coupled my image viewer is to the resources API. It seems to me that an image viewer is a pretty handy, general-purpose thing. The initial implementation can display images contained in files. But images come from other places. Items in an auction have images; so do the results of queries against Flickr.
I reworked the plug-in to make it exploit the Eclipse adapter mechanism. The adapter mechanism is one of those handy things that makes it possible to build plug-ins that work really well together but know nothing about each other. Strange? You bet. But very powerful.
The basic idea is that I want my image viewer to display an image for the selected thing. Whatever it is. So it registers a selection listener with the workbench selection service; the selection listener is notified whenever a selection occurs in a table, tree, editor, whatever. The image viewer looks at the selection and tries to determine how to display an image for it. To do this, it attempts to adapt the selected thing into an instance of the
ImageProvider type (
ImageProvider is a class that I’ve provided). It then asks the
ImageProvider for the image. If the selected thing cannot be adapted to the appropriate type, the whole process is aborted (so the image viewer displays the last thing that was selected that could provide an image).
To insulate my image viewer from having any knowledge of the resources API, I created a separate plug-in which provides an extension to the
org.eclipse.core.runtime.adapters extension point. This extension contributes a factory that knows how to adapt objects that implement
org.eclipse.core.resources.IFile into an
ImageProvider. This new plug-in knows about both the image viewer plug-in and the resources plug-in, but these other plug-ins each knows nothing about the other.
Adding support to display images for other types of objects is pretty easy: I just have to add another extension to
org.eclipse.core.runtime.adapters and we’re off to the races. No changes are required to the image viewer to display this new type of thing.
If you’re curious about how all this works import this project set. If you just want to use the image viewer, you can get the plug-ins here. Just drop the contents of this ZIP file into your “plugins” directory (I’ll get around to building an update site someday).