edu.northwestern.at.utils.swing
Class TreeDropTargetOnItem

java.lang.Object
  extended by edu.northwestern.at.utils.swing.XDropTarget
      extended by edu.northwestern.at.utils.swing.TreeDropTargetOnItem
All Implemented Interfaces:
java.awt.dnd.DropTargetListener, java.util.EventListener

public abstract class TreeDropTargetOnItem
extends XDropTarget

An abstract base class for JTree drop targets where the targets are the nodes.

This base class is appropriate for use in situations where drops are on nodes rather than between them.

As the user moves the mouse over the tree, the node under the mouse is hilighted by outlining it and painting over it with translucent green. Only "valid" target nodes are hilighted. The concrete subclass must implement the targetIsValid method to make the decision about which nodes are valid targets.

The concrete subclass must also implement the acceptDrop method to accept or reject a drop on a node.

This class implements "hovering". If the user pauses over a node for two seconds, the node is toggled - it is expanded if it was collapsed, and it is collapsed if it was expanded.

Thanks to the superclass XDropTarget trees also autoscroll during drags.


Nested Class Summary
 class TreeDropTargetOnItem.MyRenderer
          The renderer interceptor.
 
Field Summary
protected  int hilightRow
          The current hilighted row, or -1 if none.
protected  javax.swing.Timer hoverTimer
          The hover timer.
protected  boolean targetIsValid
          True if the current target is valid and should be hilighted.
protected  javax.swing.tree.TreePath targetPath
          The current target path, or null if none.
protected  javax.swing.JTree tree
          The tree.
 
Constructor Summary
TreeDropTargetOnItem(javax.swing.JTree tree, javax.swing.JScrollPane scrollPane)
          Constructs a new tree drop target.
 
Method Summary
abstract  void acceptDrop(javax.swing.tree.TreePath path, java.awt.dnd.DropTargetDropEvent dtde)
          Accepts a drop on a target.
 void dragExit(java.awt.dnd.DropTargetEvent dte)
          Handles drag exit events.
 void dragOver(java.awt.dnd.DropTargetDragEvent dtde)
          Handles drag over events.
 void drop(java.awt.dnd.DropTargetDropEvent dtde)
          Handles drop events.
 int getScrollIncrement(boolean up)
          Gets the scroll increment for autoscrolling.
protected  void repaintTarget()
          Repaints the target node.
 void stopDrag()
          Stops a drag.
abstract  boolean targetIsValid(javax.swing.tree.TreePath path, java.util.List flavors)
          Returns true if a target node is valid.
 
Methods inherited from class edu.northwestern.at.utils.swing.XDropTarget
dragEnter, dropActionChanged, setGhost, stopCurrentTarget
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

tree

protected javax.swing.JTree tree
The tree.


targetPath

protected javax.swing.tree.TreePath targetPath
The current target path, or null if none.


targetIsValid

protected boolean targetIsValid
True if the current target is valid and should be hilighted.


hilightRow

protected int hilightRow
The current hilighted row, or -1 if none.


hoverTimer

protected javax.swing.Timer hoverTimer
The hover timer.

This timer fires if the user hovers over a node for two seconds. It toggles the node, expanding it if it was collapsed, and collapsing it if it was expanded.

Constructor Detail

TreeDropTargetOnItem

public TreeDropTargetOnItem(javax.swing.JTree tree,
                            javax.swing.JScrollPane scrollPane)
Constructs a new tree drop target.

Parameters:
tree - The tree.
scrollPane - The scroll pane if the tree is scrollable, else null.
Method Detail

repaintTarget

protected void repaintTarget()
Repaints the target node.


getScrollIncrement

public int getScrollIncrement(boolean up)
Gets the scroll increment for autoscrolling.

Specified by:
getScrollIncrement in class XDropTarget
Parameters:
up - True if scrolling up, false if scrolling down.

stopDrag

public void stopDrag()
Stops a drag.

Overrides:
stopDrag in class XDropTarget

dragExit

public void dragExit(java.awt.dnd.DropTargetEvent dte)
Handles drag exit events.

Specified by:
dragExit in interface java.awt.dnd.DropTargetListener
Overrides:
dragExit in class XDropTarget
Parameters:
dte - The event.

dragOver

public void dragOver(java.awt.dnd.DropTargetDragEvent dtde)
Handles drag over events.

Specified by:
dragOver in interface java.awt.dnd.DropTargetListener
Overrides:
dragOver in class XDropTarget
Parameters:
dtde - The event.

drop

public void drop(java.awt.dnd.DropTargetDropEvent dtde)
Handles drop events.

Specified by:
drop in interface java.awt.dnd.DropTargetListener
Specified by:
drop in class XDropTarget
Parameters:
dtde - The event.

targetIsValid

public abstract boolean targetIsValid(javax.swing.tree.TreePath path,
                                      java.util.List flavors)
Returns true if a target node is valid.

Parameters:
path - The target path.
flavors - A list of the data flavors in the drag source.
Returns:
True if target node is valid, false otherwise.

acceptDrop

public abstract void acceptDrop(javax.swing.tree.TreePath path,
                                java.awt.dnd.DropTargetDropEvent dtde)
Accepts a drop on a target.

Parameters:
path - The target path on which the drop occurred.
dtde - The drop target drop event.