Hi, ich habe folgendes Problem mit meinen Transferhandler!
Um innerhalb meines Baumes einen Knoten abzulegen nutze ich insertNodeInto!
Löschen will ich dann mit draggedNode.removeFromParent.
Das Problem ist, das teilweise beim droppen er quasi einen neuen Root Knoten anlegt ( keine Ahnung warum ) und
damit funktioniert natürlich auch nicht mehr removeFromParent.
Hat jemand ne Idee woran das liegen könnte?
Um innerhalb meines Baumes einen Knoten abzulegen nutze ich insertNodeInto!
Löschen will ich dann mit draggedNode.removeFromParent.
Das Problem ist, das teilweise beim droppen er quasi einen neuen Root Knoten anlegt ( keine Ahnung warum ) und
damit funktioniert natürlich auch nicht mehr removeFromParent.
Hat jemand ne Idee woran das liegen könnte?
Code:
import java.awt.*;
import javax.swing.*;
import javax.swing.tree.*;
import java.awt.dnd.*;
import java.awt.datatransfer.*;
import java.awt.image.*;
import java.awt.geom.*;
public class DndTreeTransferHandler implements DragGestureListener,
DragSourceListener, DropTargetListener {
private DndTree tree;
private DragSource dragSource; // dragsource
private DropTarget dropTarget; //droptarget
private static VDM_TreeNode draggedNode;
private VDM_TreeNode draggedNodeParent;
private static BufferedImage image = null; //buff image
private Rectangle rect2D = new Rectangle();
private boolean drawImage;
protected DndTreeTransferHandler(DndTree tree, int action, boolean drawIcon) {
this.tree = tree;
drawImage = drawIcon;
dragSource = new DragSource();
dragSource.createDefaultDragGestureRecognizer(tree, action, this);
dropTarget = new DropTarget(tree, action, this);
}
/* Methods for DragSourceListener */
public void dragDropEnd(DragSourceDropEvent dsde) {
draggedNode.getRoot();
draggedNode.getLevel();
System.out.println(draggedNode.getRoot());
System.out.println(draggedNode.getLevel());
if (dsde.getDropAction() == DnDConstants.ACTION_MOVE
&& draggedNodeParent != null) {
draggedNode.removeFromParent();
((DefaultTreeModel) tree.getModel())
.nodeStructureChanged(draggedNodeParent);
} else if (dsde.getDropAction() == DnDConstants.ACTION_MOVE
&& draggedNodeParent == null) {
((DefaultTreeModel) tree.getModel())
.nodeStructureChanged(draggedNodeParent);
}
}
public final void dragEnter(DragSourceDragEvent dsde) {
int action = dsde.getDropAction();
if (action == DnDConstants.ACTION_COPY) {
dsde.getDragSourceContext().setCursor(DragSource.DefaultCopyDrop);
} else {
if (action == DnDConstants.ACTION_MOVE) {
dsde.getDragSourceContext().setCursor(
DragSource.DefaultMoveDrop);
} else {
dsde.getDragSourceContext().setCursor(
DragSource.DefaultMoveNoDrop);
}
}
}
public final void dragOver(DragSourceDragEvent dsde) {
int action = dsde.getDropAction();
if (action == DnDConstants.ACTION_COPY) {
dsde.getDragSourceContext().setCursor(DragSource.DefaultCopyDrop);
} else {
if (action == DnDConstants.ACTION_MOVE) {
dsde.getDragSourceContext().setCursor(
DragSource.DefaultMoveDrop);
} else {
dsde.getDragSourceContext().setCursor(
DragSource.DefaultMoveNoDrop);
}
}
}
public final void dropActionChanged(DragSourceDragEvent dsde) {
int action = dsde.getDropAction();
if (action == DnDConstants.ACTION_COPY) {
dsde.getDragSourceContext().setCursor(DragSource.DefaultCopyDrop);
} else {
if (action == DnDConstants.ACTION_MOVE) {
dsde.getDragSourceContext().setCursor(
DragSource.DefaultMoveDrop);
} else {
dsde.getDragSourceContext().setCursor(
DragSource.DefaultMoveNoDrop);
}
}
}
public final void dragExit(DragSourceEvent dse) {
dse.getDragSourceContext().setCursor(DragSource.DefaultMoveNoDrop);
}
/* Methods for DragGestureListener */
public final void dragGestureRecognized(DragGestureEvent dge) {
TreePath path = tree.getSelectionPath();
if (path != null) {
draggedNode = (VDM_TreeNode) path.getLastPathComponent();
draggedNodeParent = (VDM_TreeNode) draggedNode.getParent();
if (drawImage) {
Rectangle pathBounds = tree.getPathBounds(path); //getpathbounds
// of
// selectionpath
JComponent lbl = (JComponent) tree.getCellRenderer()
.getTreeCellRendererComponent(
tree,
draggedNode,
false,
tree.isExpanded(path),
((DefaultTreeModel) tree.getModel())
.isLeaf(path.getLastPathComponent()),
0, false);//returning the label
lbl.setBounds(pathBounds);//setting bounds to lbl
image = new BufferedImage(lbl.getWidth(), lbl.getHeight(),
java.awt.image.BufferedImage.TYPE_INT_ARGB_PRE);//buffered
// image
// reference
// passing
// the
// label's
// ht
// and
// width
Graphics2D graphics = image.createGraphics();//creating the
// graphics for
// buffered image
graphics.setComposite(AlphaComposite.getInstance(
AlphaComposite.SRC_OVER, 0.5f)); //Sets the Composite
// for the Graphics2D
// context
lbl.setOpaque(false);
lbl.paint(graphics); //painting the graphics to label
graphics.dispose();
}
dragSource.startDrag(dge, DragSource.DefaultMoveNoDrop, image,
new Point(0, 0), new TransferableNode(draggedNode), this);
}
}
/* Methods for DropTargetListener */
public final void dragEnter(DropTargetDragEvent dtde) {
Point pt = dtde.getLocation();
int action = dtde.getDropAction();
if (drawImage) {
paintImage(pt);
}
if (canPerformAction(tree, draggedNode, action, pt)) {
dtde.acceptDrag(action);
} else {
dtde.rejectDrag();
}
}
public final void dragExit(DropTargetEvent dte) {
if (drawImage) {
clearImage();
}
}
public final void dragOver(DropTargetDragEvent dtde) {
Point pt = dtde.getLocation();
int action = dtde.getDropAction();
if (drawImage) {
paintImage(pt);
}
if (canPerformAction(tree, draggedNode, action, pt)) {
dtde.acceptDrag(action);
} else {
dtde.rejectDrag();
}
}
public final void dropActionChanged(DropTargetDragEvent dtde) {
Point pt = dtde.getLocation();
int action = dtde.getDropAction();
if (drawImage) {
paintImage(pt);
}
if (canPerformAction(tree, draggedNode, action, pt)) {
dtde.acceptDrag(action);
} else {
dtde.rejectDrag();
}
}
public final void drop(DropTargetDropEvent dtde) {
try {
if (drawImage) {
clearImage();
}
int action = dtde.getDropAction();
Transferable transferable = dtde.getTransferable();
Point pt = dtde.getLocation();
if (transferable
.isDataFlavorSupported(TransferableNode.NODE_FLAVOR)
&& canPerformAction(tree, draggedNode, action, pt)) {
TreePath pathTarget = tree
.getClosestPathForLocation(pt.x, pt.y);
VDM_TreeNode node = (VDM_TreeNode) transferable
.getTransferData(TransferableNode.NODE_FLAVOR);
VDM_TreeNode newParentNode = (VDM_TreeNode) pathTarget
.getLastPathComponent();
if (executeDrop(tree, node, newParentNode, action)) {
dtde.acceptDrop(DnDConstants.ACTION_MOVE);
dtde.getDropTargetContext().dropComplete(true);
return;
}
}
dtde.rejectDrop();
dtde.dropComplete(false);
} catch (Exception e) {
System.out.println(e);
dtde.rejectDrop();
dtde.dropComplete(false);
}
}
private final void paintImage(Point pt) {
tree.paintImmediately(rect2D.getBounds());
rect2D.setRect((int) pt.getX(), (int) pt.getY(), image.getWidth(),
image.getHeight());
tree.getGraphics().drawImage(image, (int) pt.getX(), (int) pt.getY(),
tree);
}
private final void clearImage() {
tree.paintImmediately(rect2D.getBounds());
}
public boolean canPerformAction(DndTree target, VDM_TreeNode draggedNode,
int action, Point location) {
TreePath pathTarget = target.getPathForLocation(location.x, location.y);
if (pathTarget == null) {
target.setSelectionPath(null);
return (false);
} else if (action == DnDConstants.ACTION_MOVE) {
VDM_TreeNode parentNode = (VDM_TreeNode) pathTarget
.getLastPathComponent();
if (parentNode == draggedNode.getParent()
&& draggedNodeParent != null) {
return (false);
} else {
return (true);
}
} else {
return (false);
}
}
public boolean executeDrop(DndTree target, VDM_TreeNode draggedNode,
VDM_TreeNode newParentNode, int action) {
if (action == DnDConstants.ACTION_MOVE) {
((DefaultTreeModel) target.getModel()).insertNodeInto(draggedNode,
newParentNode, newParentNode.getChildCount());
TreePath treePath = new TreePath(draggedNode.getPath());
target.scrollPathToVisible(treePath);
target.setSelectionPath(treePath);
return (true);
}
return (false);
}
}