|
1
|
|
package abbot.tester;
|
|
2
|
|
|
|
3
|
|
import java.awt.*;
|
|
4
|
|
import java.awt.datatransfer.*;
|
|
5
|
|
import java.awt.dnd.*;
|
|
6
|
|
import javax.swing.*;
|
|
7
|
|
import javax.swing.table.*;
|
|
8
|
|
|
|
9
|
|
import abbot.Log;
|
|
10
|
|
|
|
11
|
|
public class DropTable extends JTable {
|
|
12
|
|
private static final String DATA[][] = {
|
|
13
|
|
{ "one", "red", "basketball", },
|
|
14
|
|
{ "two", "green", "football", },
|
|
15
|
|
{ "three", "blue", "rugby", },
|
|
16
|
|
{ "four", "yellow", "badminton", },
|
|
17
|
|
};
|
|
18
|
|
private static final String COLUMN_NAMES[] = {
|
|
19
|
|
"numbers", "colors",
|
|
20
|
|
};
|
|
21
|
|
|
|
22
|
|
|
|
23
|
|
public volatile boolean dragEntered = false;
|
|
24
|
|
|
|
25
|
|
public volatile boolean dropAccepted = false;
|
|
26
|
|
private DropTarget dropTarget = null;
|
|
27
|
|
private DropTargetListener dtl = null;
|
|
28
|
|
private int dropRow = -1;
|
|
29
|
|
private int dropCol = -1;
|
|
30
|
2
|
public DropTable() {
|
|
31
|
2
|
super(DATA, COLUMN_NAMES);
|
|
32
|
2
|
setName("DropTable");
|
|
33
|
2
|
setDefaultRenderer(Object.class, new DefaultTableCellRenderer() {
|
|
34
|
|
private Font originalFont;
|
|
35
|
|
private Color originalColor;
|
|
36
|
32
|
public Component getTableCellRendererComponent(JTable table,
|
|
37
|
|
Object value,
|
|
38
|
|
boolean sel,
|
|
39
|
|
boolean focus,
|
|
40
|
|
int row, int col) {
|
|
41
|
32
|
Component c = super.
|
|
42
|
|
getTableCellRendererComponent(table, value, sel, focus,
|
|
43
|
|
row, col);
|
|
44
|
32
|
if (c instanceof JLabel) {
|
|
45
|
32
|
JLabel label = (JLabel)c;
|
|
46
|
32
|
if (originalFont == null) {
|
|
47
|
2
|
originalFont = label.getFont();
|
|
48
|
2
|
originalColor = label.getForeground();
|
|
49
|
|
}
|
|
50
|
32
|
if (row == dropRow && col == dropCol) {
|
|
51
|
2
|
label.setForeground(Color.blue);
|
|
52
|
2
|
label.setFont(label.getFont().deriveFont(Font.BOLD));
|
|
53
|
|
}
|
|
54
|
|
else {
|
|
55
|
30
|
label.setForeground(originalColor);
|
|
56
|
30
|
label.setFont(originalFont);
|
|
57
|
|
}
|
|
58
|
|
}
|
|
59
|
32
|
return c;
|
|
60
|
|
}
|
|
61
|
|
});
|
|
62
|
2
|
dtl = new DropTargetListener() {
|
|
63
|
2
|
public void dragEnter(DropTargetDragEvent e) {
|
|
64
|
2
|
Log.debug("Drag enter (target) "
|
|
65
|
|
+ DropTable.this.getName());
|
|
66
|
2
|
dragEntered = true;
|
|
67
|
2
|
dropRow = rowAtPoint(e.getLocation());
|
|
68
|
2
|
dropCol = columnAtPoint(e.getLocation());
|
|
69
|
2
|
if (dropRow != -1 && dropCol != -1)
|
|
70
|
2
|
paintImmediately(getCellRect(dropRow, dropCol, false));
|
|
71
|
|
}
|
|
72
|
12
|
public void dragOver(DropTargetDragEvent e) {
|
|
73
|
12
|
Log.debug("Drag over (target) "
|
|
74
|
|
+ DropTable.this.getName());
|
|
75
|
12
|
e.acceptDrag(e.getDropAction());
|
|
76
|
|
}
|
|
77
|
0
|
public void dragExit(DropTargetEvent e) {
|
|
78
|
0
|
Log.debug("Drag exit (target)"
|
|
79
|
|
+ DropTable.this.getName());
|
|
80
|
0
|
if (dropRow != -1 && dropCol != -1) {
|
|
81
|
0
|
int row = dropRow;
|
|
82
|
0
|
int col = dropCol;
|
|
83
|
0
|
dropRow = -1;
|
|
84
|
0
|
dropCol = -1;
|
|
85
|
0
|
paintImmediately(getCellRect(row, col, false));
|
|
86
|
|
}
|
|
87
|
|
}
|
|
88
|
0
|
public void dropActionChanged(DropTargetDragEvent e) {
|
|
89
|
0
|
Log.debug("Drop action changed (target)");
|
|
90
|
0
|
e.acceptDrag(e.getDropAction());
|
|
91
|
|
}
|
|
92
|
2
|
public void drop(DropTargetDropEvent e) {
|
|
93
|
2
|
Log.debug("Drop accepted (target)");
|
|
94
|
2
|
e.acceptDrop(e.getDropAction());
|
|
95
|
2
|
e.dropComplete(true);
|
|
96
|
2
|
dropAccepted = true;
|
|
97
|
2
|
if (dropRow != -1 && dropCol != -1) {
|
|
98
|
2
|
int row = dropRow;
|
|
99
|
2
|
int col = dropCol;
|
|
100
|
2
|
dropRow = -1;
|
|
101
|
2
|
dropCol = -1;
|
|
102
|
2
|
paintImmediately(getCellRect(row, col, false));
|
|
103
|
|
}
|
|
104
|
|
}
|
|
105
|
|
};
|
|
106
|
2
|
dropTarget = new DropTarget(this, DnDConstants.ACTION_COPY_OR_MOVE,
|
|
107
|
|
dtl, true);
|
|
108
|
|
}
|
|
109
|
|
|
|
110
|
|
}
|
|
111
|
|
|
|
112
|
|
|