- Timestamp:
- 05/10/18 16:04:51 (6 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/expeditee/importer/FrameDNDTransferHandler.java
r919 r1102 19 19 package org.expeditee.importer; 20 20 21 import java.awt.Point;22 21 import java.awt.datatransfer.DataFlavor; 23 22 import java.awt.datatransfer.UnsupportedFlavorException; … … 32 31 import javax.swing.TransferHandler; 33 32 33 import org.expeditee.core.Point; 34 import org.expeditee.gio.swing.SwingConversions; 34 35 import org.expeditee.gui.DisplayIO; 35 36 import org.expeditee.gui.FrameGraphics; … … 65 66 } 66 67 67 private FrameDNDTransferHandler() {68 68 private FrameDNDTransferHandler() 69 { 69 70 // Add standard file importers - order from most ideal to last resort 70 71 // (if competing) … … 74 75 _standardFileImporters.add(new pdfImporter()); 75 76 _standardFileImporters.add(new TextImporter()); 76 _standardFileImporters.add(new FilePathImporter()); // Filepath importer 77 // as last resort 77 _standardFileImporters.add(new FilePathImporter()); // Filepath importer as last resort 78 78 79 79 try { 80 _URIListDataflavorString = new DataFlavor( 81 "text/uri-list;class=java.lang.String"); 82 _URIListDataflavorCharArray = new DataFlavor( 83 "text/uri-list;class=\"[C\""); 84 } catch (ClassNotFoundException e) { // This would never happen, 85 // java.lang.String is always 86 // present 80 _URIListDataflavorString = new DataFlavor("text/uri-list;class=java.lang.String"); 81 _URIListDataflavorCharArray = new DataFlavor("text/uri-list;class=\"[C\""); 82 83 // This would never happen, java.lang.String is always present 84 } catch (ClassNotFoundException e) { 87 85 e.printStackTrace(); 88 86 _URIListDataflavorString = null; … … 104 102 * if importer is null. 105 103 */ 106 public void addCustomFileImporter(FileImporter importer) { 107 if (importer == null) 108 throw new NullPointerException("importer"); 109 if (!_customFileImporters.contains(importer)) 104 public void addCustomFileImporter(FileImporter importer) 105 { 106 if (importer == null) throw new NullPointerException("importer"); 107 108 if (!_customFileImporters.contains(importer)) { 110 109 _customFileImporters.add(importer); 110 } 111 111 112 112 } … … 121 121 * if importer is null. 122 122 */ 123 public void removeCustomFileImporter(FileImporter importer) { 124 if (importer == null) 125 throw new NullPointerException("importer"); 123 public void removeCustomFileImporter(FileImporter importer) 124 { 125 if (importer == null) throw new NullPointerException("importer"); 126 126 127 _customFileImporters.remove(importer); 127 128 } … … 135 136 136 137 // we only import Strings 137 if ( support.isDataFlavorSupported(DataFlavor.stringFlavor)138 || support.isDataFlavorSupported(DataFlavor.javaFileListFlavor)139 || support.isDataFlavorSupported(_URIListDataflavorString)140 || support.isDataFlavorSupported(_URIListDataflavorCharArray)) {141 138 if ( support.isDataFlavorSupported(DataFlavor.stringFlavor) || 139 support.isDataFlavorSupported(DataFlavor.javaFileListFlavor) || 140 support.isDataFlavorSupported(_URIListDataflavorString) || 141 support.isDataFlavorSupported(_URIListDataflavorCharArray)) 142 { 142 143 // check if the source actions (a bitwise-OR of supported actions) 143 144 // contains the COPY action … … 154 155 155 156 @Override 156 public boolean importData(TransferSupport support) { 157 158 if (!canImport(support) || DisplayIO.getCurrentFrame() == null) 159 return false; 160 161 // Get the drop location of where to lot the import 157 public boolean importData(TransferSupport support) 158 { 159 if (!canImport(support) || DisplayIO.getCurrentFrame() == null) return false; 160 161 // Get the location of where to drop the import 162 162 DropLocation location = support.getDropLocation(); 163 163 164 // Co vert it into expeditee space165 Point expediteeDropPoint = location.getDropPoint();164 // Convert it into expeditee space 165 Point expediteeDropPoint = SwingConversions.fromSwingPoint(location.getDropPoint()); 166 166 167 167 try { … … 170 170 // keep trying until first 171 171 // data flavor recognized. 172 for (DataFlavor df : support.getTransferable() 173 .getTransferDataFlavors()) { 172 for (DataFlavor df : support.getTransferable().getTransferDataFlavors()) { 174 173 175 174 System.out.println(df); … … 177 176 if (df == DataFlavor.stringFlavor) { // import as text item 178 177 179 String str = (String) support.getTransferable() 180 .getTransferData(DataFlavor.stringFlavor); 178 String str = (String) support.getTransferable().getTransferData(DataFlavor.stringFlavor); 181 179 182 180 if (str != null && str.length() > 0) { … … 185 183 } 186 184 187 // Usually Windows and MAC enviroments 188 } else if (df == DataFlavor.javaFileListFlavor 189 || df.getSubType().equals("x-java-file-list")) { // Windows 190 // has 191 // other 192 // random 193 // types... 194 195 List<? extends File> files = (List<? extends File>) support 196 .getTransferable().getTransferData( 197 DataFlavor.javaFileListFlavor); 185 // Usually Windows and MAC enviroments 186 // Windows has other random types... 187 } else if (df == DataFlavor.javaFileListFlavor || df.getSubType().equals("x-java-file-list")) { 188 189 List<? extends File> files = (List<? extends File>) support.getTransferable().getTransferData(DataFlavor.javaFileListFlavor); 198 190 199 191 importFileList(files, expediteeDropPoint); … … 201 193 return true; 202 194 203 195 // Usually GNOME and KDE enviroments 204 196 } else if (df.equals(_URIListDataflavorString)) { 205 197 206 String data = (String) support.getTransferable() 207 .getTransferData(_URIListDataflavorString); 198 String data = (String) support.getTransferable().getTransferData(_URIListDataflavorString); 208 199 209 200 List<File> files = textURIListToFileList(data); … … 215 206 } else if (df.equals(_URIListDataflavorCharArray)) { 216 207 217 char[] data = (char[]) support.getTransferable() 218 .getTransferData(_URIListDataflavorCharArray); 208 char[] data = (char[]) support.getTransferable().getTransferData(_URIListDataflavorCharArray); 219 209 220 210 String uriString = new String(data); … … 229 219 230 220 } catch (UnsupportedFlavorException e) { 231 MessageBay 232 .displayMessage("Drag and drop for that type of data is not supported"); 221 MessageBay.displayMessage("Drag and drop for that type of data is not supported"); 233 222 } catch (IOException e) { 234 223 e.printStackTrace(); … … 246 235 * 247 236 * @param expediteeDropPoint 248 * The location in the current e cpeditee frame of where to drop237 * The location in the current expeditee frame of where to drop 249 238 * the text item. 250 239 */ 251 public static Text importString(String text, Point expediteeDropPoint) {252 240 public static Text importString(String text, Point expediteeDropPoint) 241 { 253 242 assert (DisplayIO.getCurrentFrame() != null); 254 243 assert (text != null && text.length() > 0); 255 244 256 Text importedTextItem = new Text(DisplayIO.getCurrentFrame() 257 .getNextItemID(), text); 245 Text importedTextItem = new Text(DisplayIO.getCurrentFrame().getNextItemID(), text); 258 246 importedTextItem.setPosition(expediteeDropPoint); 259 247 … … 264 252 } 265 253 266 public void importFileList(List<? extends File> files, 267 Point expediteeDropPoint) throws IOException{268 269 Point currentPoint = expediteeDropPoint.getLocation(); 270 271 for (File fileToImport : files) { // import files one by one254 public void importFileList(List<? extends File> files, Point expediteeDropPoint) throws IOException 255 { 256 Point currentPoint = new Point(expediteeDropPoint); 257 258 // import files one by one 259 for (File fileToImport : files) { 272 260 273 261 Item lastItem = importFile(fileToImport, currentPoint); … … 296 284 * @throws IOException 297 285 */ 298 public Item importFile(File f, Point expediteeDropPoint) throws IOException { 286 public Item importFile(File f, Point expediteeDropPoint) throws IOException 287 { 299 288 assert (f != null); 300 289 … … 302 291 // importing routines... 303 292 Item lastCreatedItem; 304 if (null == (lastCreatedItem = performFileImport(_customFileImporters, 305 f, expediteeDropPoint))) { 306 293 if (null == (lastCreatedItem = performFileImport(_customFileImporters, f, expediteeDropPoint))) { 307 294 // Standard file importing 308 lastCreatedItem = performFileImport(_standardFileImporters, f, 309 expediteeDropPoint); 310 311 } 295 lastCreatedItem = performFileImport(_standardFileImporters, f, expediteeDropPoint); 296 } 297 312 298 return lastCreatedItem; 313 299 } 314 300 315 private Item performFileImport(List<FileImporter> importers, File f, 316 Point expediteeDropPoint) throws IOException { 317 301 private Item performFileImport(List<FileImporter> importers, File f, Point expediteeDropPoint) throws IOException 302 { 318 303 for (FileImporter fi : importers) { 319 304 Item lastCreated = fi.importFile(f, expediteeDropPoint); 320 if (lastCreated != null)321 305 306 if (lastCreated != null) return lastCreated; 322 307 } 323 308 … … 326 311 327 312 /** 313 * Converts a string formatted as a list of URIs into a list of Files. 314 * 328 315 * Code adopted from SUN - java BUG ID 4899516 workaround for KDE/GNOME 329 316 * Desktops … … 334 321 * @return The list of FILES in the uriListString. Never null. 335 322 */ 336 private List<File> textURIListToFileList(String uriListString) {337 323 private List<File> textURIListToFileList(String uriListString) 324 { 338 325 List<File> fileList = new LinkedList<File>(); 339 326 340 for (StringTokenizer st = new StringTokenizer(uriListString, "\r\n"); st 341 .hasMoreTokens();) { 327 for (StringTokenizer st = new StringTokenizer(uriListString, "\r\n"); st.hasMoreTokens();) { 342 328 343 329 String s = st.nextToken();
Note:
See TracChangeset
for help on using the changeset viewer.