Managing Heavy Resources on the NetBeans Platform
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);
previewImageProvider.load(IMAGE);
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);
metadataProvider.load();
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.
- Login or register to post comments
- 3208 reads
- Printer-friendly version
(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)









