Package ca.cgjennings.ui.dnd
Class DragManager<T>
- java.lang.Object
-
- ca.cgjennings.ui.dnd.DragManager<T>
-
public final class DragManager<T> extends java.lang.Object
The drag manager is the core of the draggable token framework. This is a small set of classes that can be used to implement drag-and-drop functionality independently of AWT/Swing drag support. It can therefore display drag operations in a way that is consistent across platforms, and also avoids platform-specific drag bugs. However, unlike AWT/Swing, this framework does not interact with the native platform's drag support. It is only intended to be used to drag objects within a single virtual machine.To use the framework, you must implement a
DragHandler
andDropHandler
to provide the logic necessary to select an object when a drag starts and do something with it when it is dropped. (Both can be implemented in a single object usingAbstractDragAndDropHandler
.) Then create a manager for the handlers and add the source components that be dragged from and the target components that can be dragged to. In the simplest case, you simply want the user to be able to drag objects from one component and drop them on another. In this case, the drag handler detects the object being dragged and packages it as aDragToken
, and the drop handler fetches the object from the drag token and adds it to the target container.- Since:
- 3.0
- Author:
- Chris Jennings
-
-
Constructor Summary
Constructors Constructor Description DragManager(AbstractDragAndDropHandler<T> handler)
DragManager(DragHandler<T> dragHandler, DropHandler<T> dropHandler)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
addDragSource(javax.swing.JComponent source)
Adds a new source component in which the user can initiate drags.void
addDropTarget(javax.swing.JComponent target)
Adds a new target that the user can drag dragged objects upon.void
cancelDrag()
Cancels the active drag operation, if any.java.awt.Cursor
getDropCursor()
Returns the cursor displayed for the token when a drag is allowed.java.awt.Cursor
getNoDropCursor()
Returns the cursor displayed for the token when a drag is not allowed.boolean
isTokenVisible()
Returnstrue
if drag token images are displayed.void
removeDragSource(javax.swing.JComponent source)
Removes a component from the set of drag sources.void
removeDropTarget(javax.swing.JComponent target)
Removes a component from the set of drop targets.void
setDropCursor(java.awt.Cursor dropCursor)
Sets the cursor displayed for the token when a drag is allowed.void
setNoDropCursor(java.awt.Cursor noDropCursor)
Sets the cursor displayed for the token when a drag is not allowed.void
setTokenVisible(boolean visible)
Sets whether drag token images are displayed.java.lang.String
toString()
-
-
-
Constructor Detail
-
DragManager
public DragManager(AbstractDragAndDropHandler<T> handler)
-
DragManager
public DragManager(DragHandler<T> dragHandler, DropHandler<T> dropHandler)
-
-
Method Detail
-
addDragSource
public void addDragSource(javax.swing.JComponent source)
Adds a new source component in which the user can initiate drags.- Parameters:
source
- the new drag source to add
-
removeDragSource
public void removeDragSource(javax.swing.JComponent source)
Removes a component from the set of drag sources.- Parameters:
source
- the source to remove
-
addDropTarget
public void addDropTarget(javax.swing.JComponent target)
Adds a new target that the user can drag dragged objects upon.- Parameters:
target
- the target component to add
-
removeDropTarget
public void removeDropTarget(javax.swing.JComponent target)
Removes a component from the set of drop targets.- Parameters:
target
- the target to remove
-
getDropCursor
public java.awt.Cursor getDropCursor()
Returns the cursor displayed for the token when a drag is allowed.- Returns:
- the drop cursor
-
setDropCursor
public void setDropCursor(java.awt.Cursor dropCursor)
Sets the cursor displayed for the token when a drag is allowed.- Parameters:
dropCursor
- the new drop cursor to use
-
getNoDropCursor
public java.awt.Cursor getNoDropCursor()
Returns the cursor displayed for the token when a drag is not allowed.- Returns:
- the drop cursor
-
setNoDropCursor
public void setNoDropCursor(java.awt.Cursor noDropCursor)
Sets the cursor displayed for the token when a drag is not allowed.- Parameters:
noDropCursor
- the new cursor to use;null
for default cursor
-
isTokenVisible
public boolean isTokenVisible()
Returnstrue
if drag token images are displayed.- Returns:
true
if the images associated with tokens are visible
-
setTokenVisible
public void setTokenVisible(boolean visible)
Sets whether drag token images are displayed. Some platforms may not support changing the token's visibility while a drag is active. On these platforms, the method will only affect subsequent tokens and not the currently displayed token.- Parameters:
visible
-true
if the images associated with tokens are visible
-
cancelDrag
public void cancelDrag()
Cancels the active drag operation, if any.
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
-
-