10 of 21
The application implements this functionality by requesting the list of available commands from the DataHandler object associated with a file. The DataHandler retrieves the MIME type of the data from the DataSource object and queries the CommandMap for operations that are available on that type. The application interprets the list and presents it to the user on a popup menu. The user then selects one of the operations from that list.
// get the command list for an object CommandInfo cmdInfo = dh.getPreferredCommands();
PopupMenu popup = new PopupMenu(“Item Menu”);
// populate the popup with available commands for (i = 0; i < cmdInfo.length; i++) popup.add(cmdInfo[i].getCommandName());
// add and show popup add(popup); popup.show(x pos, y pos);
Performing a Command
After the user has selected a command from the popup menu, the application uses the appropriate CommandInfo class to retrieve the Bean that corresponds to the selected command, and associates the data with that Bean using the appropriate mechanism (DataHandler, Externalization etc.). Some CommandObjects (viewers for instance) are subclassed from java.awt.Component and require that they are given a parent container. Others (like a default print Command) might not present a user interface. This allows them to be flexible enough to function as stand alone viewer/editors, or perhaps as components in a compound document system. The ‘application’ is responsible for providing the proper environment (containment, life cycle, etc.) for the CommandObject to execute in. We expect that the requirements will be lightweight (not much beyond JavaBeans™ containers and AWT containment for visible components).
// get the command object Object cmdBean = cmdInfo[cmd_id].getCommandObject(dh,
this.getClassLoader()); ... // use serialization/externalization where appropriate
my awt container.add((Component)cmdBean);
5.5 An Alternative Scenario
The first scenario was the ‘canonical’ case. There are also circumstances where the application has already created objects to represent its data. In this case creating an in- memory instance of a DataSource that converted an existing object into an InputStream is an inefficient use of system resources and can result in a loss of data fidelity.
In these cases, the application can instantiate a DataHandler, using the DataHandler(Object obj, String mimeType) constructor. DataHandler implements the Transferable interface, so the consuming Bean can request representations other than