Fabrizio Giudici is a Senior Java Architect with a long Java experience in the industrial field. He runs Tidalwave, his own consultancy company, and has contributed to Java success stories in a number of fields, including Formula One. Fabrizio often appears as a speaker at international Java conferences such as JavaOne and Devoxx and is member of JUG Milano and the NetBeans Dream Team. Fabrizio is a DZone MVB and is not an employee of DZone and has posted 67 posts at DZone. You can read more from them at their website. View Full User Profile

Managing Heavy Resources on the NetBeans Platform

  • submit to reddit
If you have a bit of experience with the NetBeans Platform, you know that one of the basic classes in the API is DataObject. It models a datum represented by one (or more) files, thus it offers links to the file system plus other facilities for being rendered and selected in different kind of views.

DataObject is a sort of abstract object, so you normally subclass it for your needs. For instance, one of the very first classes that I wrote in blueMarine when I ported it to NetBeans Platform was PhotoDataObject, which extends DataObject and acts as an adapter to Image I/O.

Even if subclassing is ok for providing alternate implementations, inheritance is evil if you have direct dependencies on subclasses! Instead, DataObject provides a very simple and effective delegation mechanism, by means of Lookup, another basic class in the NetBeans Platform APIs. Lookup is a generic container of objects and every instance of DataObject has got one; just declare what your objects can do as interfaces ('capabilities') and put their implementations into the Lookup.

For instance, the following code is used by the component that manages thumbnails to extract the image that will be scaled down and turned into a thumbnail:

DataObject dataObject = ...
PreviewImageProvider previewImageProvider = dataObject.getLookup().lookup(PreviewImageProvider.class);
EditableImage image = previewImageProvider.getImage();

Similar code allows to get the metadata of an object (e.g. EXIF for a photo):

DataObject dataObject = ...
MetadataProvider metadataProvider = dataObject.getLookup().lookup(MetadataProvider.class);
Object metadata = metadataProvider.getMetadata();

The same code works for photos, PDF files and movies (and could for instance work with audio files, where the thumbnail could be the waveform, etc...).

This is a very powerful approach. After the Latest Big Refactoring held in August, blueMarine basically no more depends on PhotoDataObject (that, in fact, has been turned into a closed API, that is it's not published to the whole application). This means that blueMarine is able to work with every kind of DataObject you provide - in less than one hour, for instance, I've added to the incubator the support for PDF files (thanks to pdf-renderer by Joshua Marinacci) and I also have a prototype for supporting movies, that is just waiting for Sun to release the new portable codecs announced at JavaOne. 

Published at DZone with permission of Fabrizio Giudici, author and DZone MVB.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)