Hi wip siva...
need a small favour frm u ... or anyone who can answer my query ...
In case of drag n drop... whenever i try to drag n drop multiple elements within the same root node.. it doesn't reflect the multiple subnodes..
and at the same time ...
when i delete a node and try to drag another node in his position .. it doesn't reflect .. it does change the icon frm dot to directory with + but doesn't reflect the element within that.
am attaching the sample code of mine.. do let me know whts missing...
am using TreeModel .. not DefaultTreeModel..
If anyone of u can help me it would be great...
Thanks in advance
public void dragGestureRecognized(DragGestureEvent e) {
//Get the selected node
AdapterNode dragNode =(AdapterNode)newPath.getLastPathComponent();;
if (dragNode != null) {
//Get the Transferable Object
Transferable transferable = (Transferable) dragNode.getUserObject();
/* ********************** CHANGED ********************** */
//Select the appropriate cursor;
Cursor cursor = DragSource.DefaultCopyNoDrop;
int action = e.getDragAction();
if (action == DnDConstants.ACTION_MOVE)
cursor = DragSource.DefaultMoveNoDrop;
//In fact the cursor is set to NoDrop because once an action is rejected
// by a dropTarget, the dragSourceListener are no more invoked.
// Setting the cursor to no drop by default is so more logical, because
// when the drop is accepted by a component, then the cursor is changed by the
// dropActionChanged of the default DragSource.
/* ****************** END OF CHANGE ******************** */
//begin the drag
dragSource.startDrag(e, cursor, transferable, this);
}
}
public void dragDropEnd(DragSourceDropEvent dsde) {
}
/** DragSourceListener interface method */
public void dragEnter(DragSourceDragEvent dsde) {
/* ********************** CHANGED ********************** */
/* ****************** END OF CHANGE ******************** */
}
/** DragSourceListener interface method */
public void dragOver(DragSourceDragEvent dsde) {
/* ********************** CHANGED ********************** */
/* ****************** END OF CHANGE ******************** */
}
/** DragSourceListener interface method */
public void dropActionChanged(DragSourceDragEvent dsde) {
}
/** DragSourceListener interface method */
public void dragExit(DragSourceEvent dsde) {
}
public void drop(DropTargetDropEvent e) {
try {
Transferable tr = e.getTransferable();
//cast into appropriate data type
AdapterNode childInfo =
(AdapterNode) tr.getTransferData( AdapterNode.INFO_FLAVOR );
//get new parent node
Point loc = e.getLocation();
//SwingGUI.tree.
TreePath destinationPath = documentTree.getPathForLocation(loc.x, loc.y);
AdapterNode newParent = (AdapterNode)destinationPath.getLastPathComponent();
try {
newParent.addNode(childInfo, document);
// e.rejectDrop();
}
catch (java.lang.IllegalStateException ils) {
System.out.println("Into catch");
e.rejectDrop();
}
// e.getDropTargetContext().dropComplete(true);
System.out.println("full ppath"+destinationPath);
//documentTree.repaint();
}
catch (IOException io) {
System.out.println("Into io exception");
e.rejectDrop(); }
catch (UnsupportedFlavorException ufe) {
System.out.println("Into ufe exception");
e.rejectDrop();}
} //end of method
/** DropTaregetListener interface method */
public void dragEnter(DropTargetDragEvent e) {
}
/** DropTaregetListener interface method */
public void dragExit(DropTargetEvent e) {
}
public void dragOver(DropTargetDragEvent e) {
/* ********************** CHANGED ********************** */
//set cursor location. Needed in setCursor method
Point cursorLocationBis = e.getLocation();
//SwingGUI.tree.
TreePath destinationPath =
documentTree.getPathForLocation(cursorLocationBis.x, cursorLocationBis.y);
// if destination path is okay accept drop...
if (testDropTarget(destinationPath, newPath) == null){
e.acceptDrag(DnDConstants.ACTION_COPY_OR_MOVE ) ;
}
// ...otherwise reject drop
else {
e.rejectDrag() ;
}
/* ****************** END OF CHANGE ******************** */
}
/** DropTaregetListener interface method */
public void dropActionChanged(DropTargetDragEvent e) {
}
private
String testDropTarget(TreePath destination, TreePath dropper) {
//Typical Tests for dropping
//Test 1.
boolean destinationPathIsNull = destination == null;
if (destinationPathIsNull)
return "Invalid drop location.";
//Test 2.
AdapterNode node = (AdapterNode) destination.getLastPathComponent();
// if ( !node.getAllowsChildren() )
// return "This node does not allow children";
if (destination.equals(dropper))
return "Destination cannot be same as source";
//Test 3.
if ( dropper.isDescendant(destination))
return "Destination node cannot be a descendant.";
//Test 4.
if ( dropper.getParentPath().equals(destination))
return "Destination node cannot be a parent.";
return null;
}