1 | package org.expeditee.gio.swing;
|
---|
2 |
|
---|
3 | import java.awt.datatransfer.DataFlavor;
|
---|
4 | import java.awt.datatransfer.UnsupportedFlavorException;
|
---|
5 | import java.io.File;
|
---|
6 | import java.io.IOException;
|
---|
7 | import java.util.List;
|
---|
8 |
|
---|
9 | import javax.swing.TransferHandler;
|
---|
10 |
|
---|
11 | import org.expeditee.Util;
|
---|
12 | import org.expeditee.core.Point;
|
---|
13 | import org.expeditee.gio.DragAndDropManager;
|
---|
14 | import org.expeditee.gui.DisplayController;
|
---|
15 | import org.expeditee.gui.MessageBay;
|
---|
16 |
|
---|
17 | public class SwingDragAndDropManager extends DragAndDropManager {
|
---|
18 |
|
---|
19 | /** Singleton instance. */
|
---|
20 | private static SwingDragAndDropManager _instance = null;
|
---|
21 |
|
---|
22 | /** Singleton instantiator. */
|
---|
23 | public static SwingDragAndDropManager getInstance()
|
---|
24 | {
|
---|
25 | if (_instance == null) _instance = new SwingDragAndDropManager();
|
---|
26 |
|
---|
27 | return _instance;
|
---|
28 | }
|
---|
29 |
|
---|
30 | // GNOME and KDE desktops have a specialized way of DNDing files
|
---|
31 | private DataFlavor _URIListDataflavorString;
|
---|
32 |
|
---|
33 | private DataFlavor _URIListDataflavorCharArray;
|
---|
34 |
|
---|
35 | private ExpediteeTransferHandler _transferHandler;
|
---|
36 |
|
---|
37 | private SwingDragAndDropManager()
|
---|
38 | {
|
---|
39 | // Wire up the transfer handler
|
---|
40 | if (Util.isMinimumVersion6()) {
|
---|
41 | _transferHandler = new ExpediteeTransferHandler();
|
---|
42 | SwingMiscManager.getIfUsingSwingGraphicsManager().setTransferHandler(_transferHandler);
|
---|
43 | } else {
|
---|
44 | System.err.println("Upgrade to a (minimum) of Java 1.6 to enable drag and drop support in Expeditee");
|
---|
45 | }
|
---|
46 |
|
---|
47 | try {
|
---|
48 | _URIListDataflavorString = new DataFlavor("text/uri-list;class=java.lang.String");
|
---|
49 | _URIListDataflavorCharArray = new DataFlavor("text/uri-list;class=\"[C\"");
|
---|
50 |
|
---|
51 | // This would never happen, java.lang.String is always present
|
---|
52 | } catch (ClassNotFoundException e) {
|
---|
53 | e.printStackTrace();
|
---|
54 | _URIListDataflavorString = null;
|
---|
55 | }
|
---|
56 | assert (_URIListDataflavorString != null);
|
---|
57 | assert (_URIListDataflavorCharArray != null);
|
---|
58 | }
|
---|
59 |
|
---|
60 | private class ExpediteeTransferHandler extends TransferHandler {
|
---|
61 |
|
---|
62 | /** Auto-generated serialisation ID#. */
|
---|
63 | private static final long serialVersionUID = 6552099431345591756L;
|
---|
64 |
|
---|
65 | @Override
|
---|
66 | public boolean canImport(TransferSupport support) {
|
---|
67 |
|
---|
68 | if (!support.isDrop()) {
|
---|
69 | return false;
|
---|
70 | }
|
---|
71 |
|
---|
72 | // we only import Strings
|
---|
73 | if ( support.isDataFlavorSupported(DataFlavor.stringFlavor) ||
|
---|
74 | support.isDataFlavorSupported(DataFlavor.javaFileListFlavor) ||
|
---|
75 | support.isDataFlavorSupported(_URIListDataflavorString) ||
|
---|
76 | support.isDataFlavorSupported(_URIListDataflavorCharArray))
|
---|
77 | {
|
---|
78 | // check if the source actions (a bitwise-OR of supported actions)
|
---|
79 | // contains the COPY action
|
---|
80 | boolean copySupported = (COPY & support.getSourceDropActions()) == COPY;
|
---|
81 | if (copySupported) {
|
---|
82 | support.setDropAction(COPY);
|
---|
83 | return true;
|
---|
84 | }
|
---|
85 | }
|
---|
86 |
|
---|
87 | // Reject transfer
|
---|
88 | return false;
|
---|
89 | }
|
---|
90 |
|
---|
91 | @Override
|
---|
92 | public boolean importData(TransferSupport support)
|
---|
93 | {
|
---|
94 | if (!canImport(support) || DisplayController.getCurrentFrame() == null) return false;
|
---|
95 |
|
---|
96 | // Get the location of where to drop the import
|
---|
97 | DropLocation location = support.getDropLocation();
|
---|
98 |
|
---|
99 | // Convert it into expeditee space
|
---|
100 | Point expediteeDropPoint = SwingConversions.fromSwingPoint(location.getDropPoint());
|
---|
101 |
|
---|
102 | try {
|
---|
103 |
|
---|
104 | // The list of data flavors are ordered by most rich to least ..
|
---|
105 | // keep trying until first
|
---|
106 | // data flavor recognized.
|
---|
107 | DataFlavor[] flavors = support.getTransferable().getTransferDataFlavors();
|
---|
108 | for (DataFlavor df : flavors) {
|
---|
109 |
|
---|
110 | System.out.println(df);
|
---|
111 |
|
---|
112 | if (df == DataFlavor.stringFlavor) { // import as text item
|
---|
113 |
|
---|
114 | String str = (String) support.getTransferable().getTransferData(DataFlavor.stringFlavor);
|
---|
115 |
|
---|
116 | if (str != null && str.length() > 0) {
|
---|
117 | importString(str, expediteeDropPoint, false);
|
---|
118 | return true;
|
---|
119 | }
|
---|
120 |
|
---|
121 | // Usually Windows and MAC enviroments
|
---|
122 | // Windows has other random types...
|
---|
123 | } else if (df == DataFlavor.javaFileListFlavor || df.getSubType().equals("x-java-file-list")) {
|
---|
124 |
|
---|
125 | List<? extends File> files = (List<? extends File>) support.getTransferable().getTransferData(DataFlavor.javaFileListFlavor);
|
---|
126 |
|
---|
127 | importFileList(files, expediteeDropPoint, false);
|
---|
128 |
|
---|
129 | return true;
|
---|
130 |
|
---|
131 | // Usually GNOME and KDE enviroments
|
---|
132 | } else if (df.equals(_URIListDataflavorString)) {
|
---|
133 |
|
---|
134 | String data = (String) support.getTransferable().getTransferData(_URIListDataflavorString);
|
---|
135 |
|
---|
136 | List<File> files = textURIListToFileList(data);
|
---|
137 |
|
---|
138 | importFileList(files, expediteeDropPoint, false);
|
---|
139 |
|
---|
140 | return true;
|
---|
141 |
|
---|
142 | } else if (df.equals(_URIListDataflavorCharArray)) {
|
---|
143 |
|
---|
144 | char[] data = (char[]) support.getTransferable().getTransferData(_URIListDataflavorCharArray);
|
---|
145 |
|
---|
146 | String uriString = new String(data);
|
---|
147 |
|
---|
148 | List<File> files = textURIListToFileList(uriString);
|
---|
149 |
|
---|
150 | importFileList(files, expediteeDropPoint, false);
|
---|
151 |
|
---|
152 | return true;
|
---|
153 | }
|
---|
154 | }
|
---|
155 |
|
---|
156 | } catch (UnsupportedFlavorException e) {
|
---|
157 | MessageBay.displayMessage("Drag and drop for that type of data is not supported");
|
---|
158 | } catch (IOException e) {
|
---|
159 | e.printStackTrace();
|
---|
160 | MessageBay.displayMessage("Failed to import data in Expeditee");
|
---|
161 | }
|
---|
162 |
|
---|
163 | return false;
|
---|
164 | }
|
---|
165 |
|
---|
166 | }
|
---|
167 |
|
---|
168 | }
|
---|