001: package abbot.tester;
002:
003: import java.awt.*;
004: import java.awt.dnd.*;
005: import javax.swing.*;
006: import javax.swing.table.*;
007:
008: import abbot.Log;
009:
010: public class DropTable extends JTable {
011: private static final String DATA[][] = {
012: { "one", "red", "basketball", },
013: { "two", "green", "football", },
014: { "three", "blue", "rugby", },
015: { "four", "yellow", "badminton", }, };
016: private static final String COLUMN_NAMES[] = { "numbers", "colors", };
017:
018: /** Target received drag. */
019: public volatile boolean dragEntered = false;
020: /** Target accepted the drop. */
021: public volatile boolean dropAccepted = false;
022: private DropTarget dropTarget = null;
023: private DropTargetListener dtl = null;
024: private int dropRow = -1;
025: private int dropCol = -1;
026:
027: public DropTable() {
028: super (DATA, COLUMN_NAMES);
029: setName("DropTable");
030: setDefaultRenderer(Object.class,
031: new DefaultTableCellRenderer() {
032: private Font originalFont;
033: private Color originalColor;
034:
035: public Component getTableCellRendererComponent(
036: JTable table, Object value, boolean sel,
037: boolean focus, int row, int col) {
038: Component c = super
039: .getTableCellRendererComponent(table,
040: value, sel, focus, row, col);
041: if (c instanceof JLabel) {
042: JLabel label = (JLabel) c;
043: if (originalFont == null) {
044: originalFont = label.getFont();
045: originalColor = label.getForeground();
046: }
047: if (row == dropRow && col == dropCol) {
048: label.setForeground(Color.blue);
049: label.setFont(label.getFont()
050: .deriveFont(Font.BOLD));
051: } else {
052: label.setForeground(originalColor);
053: label.setFont(originalFont);
054: }
055: }
056: return c;
057: }
058: });
059: dtl = new DropTargetListener() {
060: public void dragEnter(DropTargetDragEvent e) {
061: Log.debug("Drag enter (target) "
062: + DropTable.this .getName());
063: dragEntered = true;
064: dropRow = rowAtPoint(e.getLocation());
065: dropCol = columnAtPoint(e.getLocation());
066: if (dropRow != -1 && dropCol != -1)
067: paintImmediately(getCellRect(dropRow, dropCol,
068: false));
069: }
070:
071: public void dragOver(DropTargetDragEvent e) {
072: Log.debug("Drag over (target) "
073: + DropTable.this .getName());
074: e.acceptDrag(e.getDropAction());
075: }
076:
077: public void dragExit(DropTargetEvent e) {
078: Log.debug("Drag exit (target)"
079: + DropTable.this .getName());
080: if (dropRow != -1 && dropCol != -1) {
081: int row = dropRow;
082: int col = dropCol;
083: dropRow = -1;
084: dropCol = -1;
085: paintImmediately(getCellRect(row, col, false));
086: }
087: }
088:
089: public void dropActionChanged(DropTargetDragEvent e) {
090: Log.debug("Drop action changed (target)");
091: e.acceptDrag(e.getDropAction());
092: }
093:
094: public void drop(DropTargetDropEvent e) {
095: Log.debug("Drop accepted (target)");
096: e.acceptDrop(e.getDropAction());
097: e.dropComplete(true);
098: dropAccepted = true;
099: if (dropRow != -1 && dropCol != -1) {
100: int row = dropRow;
101: int col = dropCol;
102: dropRow = -1;
103: dropCol = -1;
104: paintImmediately(getCellRect(row, col, false));
105: }
106: }
107: };
108: dropTarget = new DropTarget(this ,
109: DnDConstants.ACTION_COPY_OR_MOVE, dtl, true);
110: }
111:
112: }
|