source: trunk/src/org/expeditee/gio/swing/SwingDragAndDropManager.java@ 1436

Last change on this file since 1436 was 1436, checked in by bnemhaus, 5 years ago

Added the ability to copy/paste recognised files (mostly images) into Expeditee. This functionality already existed when drag n dropping, but now also works when using copy/paste.

At this stage, recognised files get imported as a approapriate Text Item. For example, a image will create a @i Text Item and attach it to your cursor. Ideally this would be the image directly. Unfortunately, items attached to the cursor (FreeItems) are not currently parsed. This is something I will discuss with David as there are confounding issues to consider. For example: surrogates...

Attached to the bottom of this commit message are my current thoughts.

Also made it so when creating a @i through DND or Copy/Paste, that @i is as relative as possible. The users image directories setting is used to achieve this.


  • FreeItems need to be parsed...we would need to deal with primaries/surrogates in them...
  • Do we want FreeItems to be parsed? If I attach an item to my cursor and then enter surrogate mode, should it change?
  • My gut says no. Both because it would be work, and also because it kind of feels like a free win if we do not.
  • But we do want to replace @i with images etc. Therefore, we want an old style parse for FreeItems?
File size: 4.9 KB
Line 
1package org.expeditee.gio.swing;
2
3import java.awt.datatransfer.DataFlavor;
4import java.awt.datatransfer.UnsupportedFlavorException;
5import java.io.File;
6import java.io.IOException;
7import java.util.List;
8
9import javax.swing.TransferHandler;
10
11import org.expeditee.Util;
12import org.expeditee.core.Point;
13import org.expeditee.gio.DragAndDropManager;
14import org.expeditee.gui.DisplayController;
15import org.expeditee.gui.MessageBay;
16
17public 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}
Note: See TracBrowser for help on using the repository browser.