Cross Platform DnD

Drag and Drop Adapter for Classic Mac OS, Mac OS X, and Java 2 Platforms

Will Gilbert, e-mail: gilbert@Informagen.com

This package provides a functional subset of the Java 2 Drag and Drop API which will execute predictably in both a Macintosh MRJ 2.2.5 (OS 9) and Java 1.2 and greater runtime environments which included Mac OS X, Windows 9x/NT/2000/XP, Linux, as well as other varients of UNIX.

This package was heavily derived from a similiar package by James Bucanek, james@gloaming.com. His original distribution can be obtained at http://gloaming.com/DNDAdaptor/

This derivation builds on James' work by adding support for Mac OS X in that Mac OS X supports both the Java 2 and MRJ Drag and Drop API. This derivation uses the package name com.informagen.dnd so that both packages can be used interchangably.

Download Cross Platform DnD folder (976 KByte)
Installer which creates a folder containing the DNDAdapter, Documentation, and Examples.

Overview

The API provided by this package emulates a subset of the Java 2 Drag and Drop (DnD) API provided in the Java 1.2 foundation classes. At run-time, this package looks for the presence of the Java 1.2 DnD classes, and uses them if possible. If the Java 2 classes are not present, it tries to emulate the Java 2 interfaces using the MRJ (Macintosh Runtime for Java) DnD library. If neither are present, the public interfaces perform no function. Often returning a functional place-holder objects so that applications need not be burdened with conditional code when drag and drop services are not available.

The motivation behind the design of this package is simple. We want to support drag and drop as a user interface feature wherever possible. Currently, the Java 1.3 and Macintosh MRJ 2.2.5 (Java 1.1.8) have builtin support for drag and drop. In those environments we want drag and drop to work. In other environments, we want the application to be functional, even if drag and drop features are not supported.

It is interesting to note that Macintosh OS X contains support for BOTH the MRJ DnD classes the Java 2 DnD classes. Thus this library provides a excellent oppurtunity to compare the behavior and performance side by side. This library currently uses the MRJ DnD implementation when running under Macintosh OS X. This can change by setting the property "com.informagen.dnd.useJava2forOSX" to "true" either on the command line or via your IDE.

Ideally, we should be able to take source code and recompile simply by changing their import statements from import java.awt.dnd.*; to import com.informagen.dnd.*;. In most cases this has been the case, where it has not there are minor workarounds.

Limitations and Extensions

As noted above not all Java 2 class and methods have been implemented. In most cases, there are help methods in other classes which will work. Some Java 2 source my use Java 2 Collection classes like Array and List with DataFlavors and Transferable, there are work arounds.

I added one method to DragGestureEvent called setDragRect,this method is used with MRJ classes to create a better drag rectange than what MRJ uses. The default MRJ dragRect is set the bounds of the component, this work fine for labels but list and tree have huge drag rects. There are examples using this method in the examples. Calling setDragRect has no effect on Java 2 platforms but does no harm as well.

This package uses two system properties which can be set on the command line or plist. com.informagen.dnd.useJava2forOSX can be set to 'true' to have OS X systems use the Java 2 DnD. The default is 'false' and if this property is missing the MRJ DnD will be used for Mac OS X. A second property com.informagen.dnd.debug will print debug message. This not been extensively developed at this point but is a first line of debugging before peppering your own source code with print statements.

Autoscrolling is turn off this release as I didn't have to get it working. The code is for James' implementation.

Examples

TestDND
This example was written by James Bucanek in his orginal Cross Platform DnD package and features two JLists where the contents of each list can be dragged to the other or itself. Items are added to end of the list. The 'Transferable' is a StringSelection so strings from draggable text editor can drop as well.

JW-DnD-Part1
This is the examle provide in the JavaWorld article on DnD. It was converted to use the adapter by changing the import statement, commenting out some Java 2 collection classes used in the Transferable and adding a drag rectange so it looks better when using MRJ DnD.

DropZone
This could be a useful utility for finding out what a Tranferable is made up of. It accepts all drags, when the drop is made into the upper panel (colored yellow) the flavors are extracte and an attempt is made to write out the transfered data stream.

Known bugs

When running a Java application in Classic mode under Mac OS X sometimes the following messages start to appear in the MRJ Java console. I have no idea where this comes from but it does show up when running under OS 9 or running native OS X.

 Exception occurred during event dispatching:
 java.lang.IllegalStateException: Illegal overrideRef(0)
	at com.apple.mrj.dnd.Drag.overrideRef(Drag.java)
	at com.apple.mrj.dnd.DragPrivateAccess.setIncomingDragRef(DragPrivateAccess.java)
	at com.apple.mrj.internal.awt.VFramePeer$JMDragEvent.dispatch(Compiled Code)
	at java.awt.EventDispatchThread.run(EventDispatchThread.java)