org.peace_tools.generic.dndTabs
Class DnDTabHandler

java.lang.Object
  extended by org.peace_tools.generic.dndTabs.DnDTabHandler
All Implemented Interfaces:
java.awt.dnd.DragGestureListener, java.awt.dnd.DragSourceListener, java.awt.dnd.DragSourceMotionListener, java.awt.dnd.DropTargetListener, java.util.EventListener

public class DnDTabHandler
extends java.lang.Object
implements java.awt.dnd.DragSourceListener, java.awt.dnd.DragGestureListener, java.awt.dnd.DropTargetListener, java.awt.dnd.DragSourceMotionListener

This class provides the necessary infrastructure for handling Drag-and-Drop features of a Tab from one TabbedPane to another. In other words, this class enables a user to move tabs from one tab to another within the same application. This implementation has been adapted from the code examples provided in the Java Swing text book.


Nested Class Summary
 class DnDTabHandler.TransferableTab
          This inner class provides a wrapper that impelements Transferable interface to simply provide a place holder for DnD operations.
 
Field Summary
private static java.awt.Cursor[] ArrowCursors
          The 4 block arrow cursors that are internally used by this DnD handler.
private static java.awt.Component componentMoved
          The tab (JComponent) to be moved via this DnD handler.
private static java.awt.dnd.DragSourceContext dragContext
          This instance variable is filled in by the source pane when a drag operation is started and reset to null once the drag operation is completed.
private  java.awt.dnd.DragSource dragSource
          The drag source object associated with this DnD handler.
static java.awt.datatransfer.DataFlavor TAB_FLAVOR
          The only DataFlavor that is currently supported by all of the DnD related classes and methods in this class.
private static javax.swing.Icon tabIcon
          The icon for the tab currently being moved.
private static java.lang.String tabName
          The name of the tab currently being moved.
private  DnDTabbedPane tabPane
          This member object hold a reference to the JTabbedPane with which this DnD handler is associated.
 
Constructor Summary
DnDTabHandler(DnDTabbedPane tab)
          The constructor.
 
Method Summary
private static void createCursors(DnDTabbedPane tabPane)
          This is a helper method that is used to create some of the custom cursors used by this class.
 void dragDropEnd(java.awt.dnd.DragSourceDropEvent dsde)
          This method is a part of the DragSourceListener interface.
 void dragEnter(java.awt.dnd.DragSourceDragEvent dsde)
          This method is a part of the DragSourceListener interface.
 void dragEnter(java.awt.dnd.DropTargetDragEvent dtde)
          This method is part of the DropTargetListener.
 void dragExit(java.awt.dnd.DragSourceEvent dse)
          This method is a part of the DragSourceListener interface.
 void dragExit(java.awt.dnd.DropTargetEvent dte)
          This method is part of the DropTargetListener.
 void dragGestureRecognized(java.awt.dnd.DragGestureEvent dge)
          This method is a part of the DragGestureListener interface.
 void dragMouseMoved(java.awt.dnd.DragSourceDragEvent dsde)
          This method is part of the DragSourceMotionListener method.
 void dragOver(java.awt.dnd.DragSourceDragEvent dsde)
          This method is a part of the DragSourceListener interface.
 void dragOver(java.awt.dnd.DropTargetDragEvent dtde)
          This method is part of the DropTargetListener.
 void drop(java.awt.dnd.DropTargetDropEvent dtde)
          This method is part of the DropTargetListener interface.
 void dropActionChanged(java.awt.dnd.DragSourceDragEvent dsde)
          This method is a part of the DragSourceListener interface.
 void dropActionChanged(java.awt.dnd.DropTargetDragEvent dtde)
          This method is part of the DropTargetListener.
protected  DnDTabbedPane.Location getLocation(int x, int y)
          Helper method used to determine where a given x and y coordinates (typically the mouse pointer positions) is with respect to the DnDTabbedPane that owns this handler.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

TAB_FLAVOR

public static final java.awt.datatransfer.DataFlavor TAB_FLAVOR
The only DataFlavor that is currently supported by all of the DnD related classes and methods in this class. The flavor class essentially defines the type of data being dragged-n-dropped. In our case we are just dragging a generic AWT component (which implies it could be any thing from a simple panel all the way to a complex display).


tabPane

private final DnDTabbedPane tabPane
This member object hold a reference to the JTabbedPane with which this DnD handler is associated. This value is set in the constructor and is never changed during the life time of this class.


dragSource

private java.awt.dnd.DragSource dragSource
The drag source object associated with this DnD handler. The drag source is needed to install a gesture handler and to initiate a drag operation.


componentMoved

private static transient java.awt.Component componentMoved
The tab (JComponent) to be moved via this DnD handler. This value has been made static so that multiple instances of the DnD handler can access this value.


tabName

private static transient java.lang.String tabName
The name of the tab currently being moved. This value is set when the drag operation commences.


tabIcon

private static transient javax.swing.Icon tabIcon
The icon for the tab currently being moved. This value is set when the drag operation commences.


dragContext

private static transient java.awt.dnd.DragSourceContext dragContext
This instance variable is filled in by the source pane when a drag operation is started and reset to null once the drag operation is completed. The context is used to change the cursor depending on its location in the target drop panel.


ArrowCursors

private static transient java.awt.Cursor[] ArrowCursors
The 4 block arrow cursors that are internally used by this DnD handler. These cursors are generated by a static helper method when the first DnDHandler class is created.

Constructor Detail

DnDTabHandler

public DnDTabHandler(DnDTabbedPane tab)
The constructor. The DnDTabbedPane whith which this DnD Handler is associated must be passed in as the parameter. The constructor creates a new DragSource and DropTarget for this tree to handle dnd operations.

Parameters:
tab - The DnDTabbedPane this DnD handler is associated.
Method Detail

dragDropEnd

public void dragDropEnd(java.awt.dnd.DragSourceDropEvent dsde)
This method is a part of the DragSourceListener interface. This method is called whenever a drag operation is terminated immaterial of wether it was successful or not. If the drag operation was indeed successful, then this method removes the dragged panel from the tabPane (and the panel is added to another tab by the drop listener).

Specified by:
dragDropEnd in interface java.awt.dnd.DragSourceListener
Parameters:
dsde - The source drop event to be processed.

dragMouseMoved

public void dragMouseMoved(java.awt.dnd.DragSourceDragEvent dsde)
This method is part of the DragSourceMotionListener method. This method is invoked whenever the mouse is moved during a drag operation. This method currently just saves the context information for further use.

Specified by:
dragMouseMoved in interface java.awt.dnd.DragSourceMotionListener

dragEnter

public void dragEnter(java.awt.dnd.DragSourceDragEvent dsde)
This method is a part of the DragSourceListener interface. This method is invoked whenever the user drags a tab onto another location that will accept the object. In this case, this method changes the cursor to let the user know that they are a valid drop point.

Specified by:
dragEnter in interface java.awt.dnd.DragSourceListener
Parameters:
dsde - The source drag event to be processed.

dragOver

public void dragOver(java.awt.dnd.DragSourceDragEvent dsde)
This method is a part of the DragSourceListener interface. This method is invoked whenever the user drags a tab associated with this DnD handler onto another location that will accept the object. In this case, this method simply changes the cursor to let the user know that they are at a valid drop point.

Specified by:
dragOver in interface java.awt.dnd.DragSourceListener
Parameters:
dsde - The source drag event to be processed.

dropActionChanged

public void dropActionChanged(java.awt.dnd.DragSourceDragEvent dsde)
This method is a part of the DragSourceListener interface. Currently this method is not used.

Specified by:
dropActionChanged in interface java.awt.dnd.DragSourceListener
Parameters:
dsde - This parameter is not used.

dragExit

public void dragExit(java.awt.dnd.DragSourceEvent dse)
This method is a part of the DragSourceListener interface. This method is invoked whenever the user drags a tab associated with this DnD handler onto a location that will not accept the component. In this case, this method simply changes the cursor to let the user know that they are at an invalid drop point.

Specified by:
dragExit in interface java.awt.dnd.DragSourceListener
Parameters:
dse - The source drag event to be processed.

dragGestureRecognized

public void dragGestureRecognized(java.awt.dnd.DragGestureEvent dge)
This method is a part of the DragGestureListener interface. This method is invoked whenever the user starts to drag an object in the tab associated with this dnd Handler. This method creates a suitable transferable object and initiates the DnD operation via the drag source.

Specified by:
dragGestureRecognized in interface java.awt.dnd.DragGestureListener
Parameters:
dge - The drag gesture event to be processed.

dragEnter

public void dragEnter(java.awt.dnd.DropTargetDragEvent dtde)
This method is part of the DropTargetListener. This method is invoked whenever the user drags a tab onto the DnDTabbedPane object associated with this class. This method checks to see the location of the cursor with respect to the bounds of the tabbed node and decides on the accept action to be performed. If it cannot accept the tab then it indicates it will not accept it.

Specified by:
dragEnter in interface java.awt.dnd.DropTargetListener
Parameters:
dtde - The drop target event to be processed.

getLocation

protected DnDTabbedPane.Location getLocation(int x,
                                             int y)
Helper method used to determine where a given x and y coordinates (typically the mouse pointer positions) is with respect to the DnDTabbedPane that owns this handler.

Note: The return value from this method is meaningful only when the mouse pointer is within the bounds of the owning tabbed pane.

Parameters:
x - The x-coordinate to test.
y - The y-coordinate to test
Returns:
This method returns one of the five predetermined location constants (left, right, top, bottom, or center) depending on the position of the mouse.

dragOver

public void dragOver(java.awt.dnd.DropTargetDragEvent dtde)
This method is part of the DropTargetListener. It simply calls the dragEnter() method to perform the same action that dragEnter() method does.

Specified by:
dragOver in interface java.awt.dnd.DropTargetListener

dropActionChanged

public void dropActionChanged(java.awt.dnd.DropTargetDragEvent dtde)
This method is part of the DropTargetListener. It simply calls the dargEnter() method to perfrom the same action as it does.

Specified by:
dropActionChanged in interface java.awt.dnd.DropTargetListener
Parameters:
dtde - This parameter is not used.

dragExit

public void dragExit(java.awt.dnd.DropTargetEvent dte)
This method is part of the DropTargetListener. It currently resets the drag cue position in the tab so that docking cue box is no longer drawn by the tab.

Specified by:
dragExit in interface java.awt.dnd.DropTargetListener
Parameters:
dte - This parameter is not used.

drop

public void drop(java.awt.dnd.DropTargetDropEvent dtde)
This method is part of the DropTargetListener interface. It is invoked whenever the user drops a Component onto the DnDTabbePane associated with this handler. This method adds the Component transferred to the DnDTabbedPane and highlights it.

Specified by:
drop in interface java.awt.dnd.DropTargetListener
Parameters:
dtde - The drop event to be processed.

createCursors

private static void createCursors(DnDTabbedPane tabPane)
This is a helper method that is used to create some of the custom cursors used by this class.

Parameters:
tabPane - The tabbed pane to operate on