-
Notifications
You must be signed in to change notification settings - Fork 3
/
DragAndDropHandler.java
72 lines (59 loc) · 2.2 KB
/
DragAndDropHandler.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package Harvester;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JTable;
import javax.swing.TransferHandler;
/**
* Handles when user tries to drag files into the main table. If it's a file
* or folder, will accept the transfer and pass off the files to the underlying
* model, which then updates the view. Draws the table in green when the
* drop can be accepted
* @author Nick Dunn
* @date November 24, 2008
*
*/
public class DragAndDropHandler extends TransferHandler {
private PoliceModel model;
private PoliceView view;
private JTable table;
public DragAndDropHandler(PoliceModel model, PoliceView view, JTable table) {
this.model = model;
this.view = view;
this.table = table;
}
@Override
public boolean canImport(TransferHandler.TransferSupport data) {
if (!data.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) {
//table.setBackground(Color.RED);
return false;
}
//table.setBackground(Color.GREEN);
return true;
}
@Override
public boolean importData(TransferHandler.TransferSupport data) {
// Check to see if we can handle the file or files
if (!canImport(data)) {
return false;
}
Transferable t = data.getTransferable();
try {
List<File> files = (List<File>) t.getTransferData(DataFlavor.javaFileListFlavor);
model.addFiles(files);
} catch (UnsupportedFlavorException ex) {
Logger.getLogger(DragAndDropHandler.class.getName()).log(Level.SEVERE, null, ex);
view.logError(ex.getMessage());
} catch (IOException ex) {
Logger.getLogger(DragAndDropHandler.class.getName()).log(Level.SEVERE, null, ex);
view.logError(ex.getMessage());
}
// We know now that we can handle it. Take care of the transfer
return true;
}
}