01: package abbot.tester;
02:
03: import java.awt.*;
04: import java.awt.dnd.*;
05: import javax.swing.*;
06: import javax.swing.tree.*;
07:
08: import abbot.Log;
09:
10: public class DropTree extends JTree {
11: /** Target received drag. */
12: public volatile boolean dragEntered = false;
13: /** Target accepted the drop. */
14: public volatile boolean dropAccepted = false;
15: private DropTarget dropTarget = null;
16: private DropTargetListener dtl = null;
17: private int dropRow = -1;
18:
19: public DropTree() {
20: setName("DropTree");
21: setCellRenderer(new DefaultTreeCellRenderer() {
22: private Font originalFont;
23: private Color originalColor;
24:
25: public Component getTreeCellRendererComponent(JTree tree,
26: Object value, boolean sel, boolean exp,
27: boolean leaf, int row, boolean focus) {
28: Component c = super .getTreeCellRendererComponent(tree,
29: value, sel, exp, leaf, row, focus);
30: if (c instanceof JLabel) {
31: JLabel label = (JLabel) c;
32: if (originalFont == null) {
33: originalFont = label.getFont();
34: originalColor = label.getForeground();
35: }
36: if (row == dropRow) {
37: label.setForeground(Color.blue);
38: label.setFont(label.getFont().deriveFont(
39: Font.BOLD));
40: } else {
41: label.setForeground(originalColor);
42: label.setFont(originalFont);
43: }
44: }
45: return c;
46: }
47: });
48: dtl = new DropTargetListener() {
49: public void dragEnter(DropTargetDragEvent e) {
50: Log.debug("Drag enter (target) "
51: + DropTree.this .getName());
52: dragEntered = true;
53: Point where = e.getLocation();
54: int row = getRowForLocation(where.x, where.y);
55: dropRow = row;
56: if (row != -1)
57: paintImmediately(getRowBounds(row));
58: }
59:
60: public void dragOver(DropTargetDragEvent e) {
61: Log.debug("Drag over (target) "
62: + DropTree.this .getName());
63: e.acceptDrag(e.getDropAction());
64: }
65:
66: public void dragExit(DropTargetEvent e) {
67: Log.debug("Drag exit (target)"
68: + DropTree.this .getName());
69: if (dropRow != -1) {
70: int repaint = dropRow;
71: dropRow = -1;
72: paintImmediately(getRowBounds(repaint));
73: }
74: }
75:
76: public void dropActionChanged(DropTargetDragEvent e) {
77: Log.debug("Drop action changed (target)");
78: e.acceptDrag(e.getDropAction());
79: }
80:
81: public void drop(DropTargetDropEvent e) {
82: Log.debug("Drop accepted (target)");
83: e.acceptDrop(e.getDropAction());
84: e.dropComplete(true);
85: dropAccepted = true;
86: if (dropRow != -1) {
87: int repaint = dropRow;
88: dropRow = -1;
89: paintImmediately(getRowBounds(repaint));
90: }
91: }
92: };
93: dropTarget = new DropTarget(this ,
94: DnDConstants.ACTION_COPY_OR_MOVE, dtl, true);
95: }
96:
97: }
|