FileTransferHandler.java

00001 package edu.stanford.hci.r3.tools.design.acrobat;
00002 
00003 import java.awt.Desktop;
00004 import java.awt.datatransfer.*;
00005 import java.io.File;
00006 import java.io.IOException;
00007 import java.util.ArrayList;
00008 import java.util.List;
00009 
00010 import javax.swing.*;
00011 
00023 public class FileTransferHandler extends TransferHandler {
00024 
00025         private static final DataFlavor FILE_FLAVOR = DataFlavor.javaFileListFlavor;
00026 
00033         public static boolean hasFileFlavor(DataFlavor[] transferFlavors) {
00034                 for (DataFlavor f : transferFlavors) {
00035                         if (FILE_FLAVOR.equals(f)) {
00036                                 return true;
00037                         }
00038                 }
00039                 return false;
00040         }
00041 
00042         public FileTransferHandler() {
00043         }
00044 
00049         @Override
00050         public boolean canImport(JComponent comp, DataFlavor[] transferFlavors) {
00051                 if (hasFileFlavor(transferFlavors)) {
00052                         return true;
00053                 }
00054                 return false;
00055         }
00056 
00064         @SuppressWarnings("unchecked")
00065         public List<File> getOnlyPDFs(JComponent comp, Transferable t) {
00066                 final List<File> okFiles = new ArrayList<File>();
00067                 if (!canImport(comp, t.getTransferDataFlavors())) {
00068                         return okFiles;
00069                 }
00070                 if (hasFileFlavor(t.getTransferDataFlavors())) {
00071                         try {
00072                                 List<File> files = (List<File>) t.getTransferData(FILE_FLAVOR);
00073                                 for (File f : files) {
00074                                         // TODO: Is this OS Specific? OS X doesn't require PDF extensions...
00075                                         // But, all cool people use extensions anyways. :)
00076                                         if (f.getName().toLowerCase().endsWith(".pdf")) {
00077                                                 okFiles.add(f);
00078                                         }
00079                                 }
00080                                 return okFiles;
00081                         } catch (UnsupportedFlavorException e) {
00082                                 e.printStackTrace();
00083                         } catch (IOException e) {
00084                                 e.printStackTrace();
00085                         }
00086 
00087                 }
00088                 return okFiles;
00089         }
00090 
00094         @Override
00095         public int getSourceActions(JComponent c) {
00096                 System.out.println("Getting Source Actions");
00097                 return COPY_OR_MOVE;
00098         }
00099 
00103         @Override
00104         public Icon getVisualRepresentation(Transferable t) {
00105                 System.out.println("Get Visual Representation");
00106                 return super.getVisualRepresentation(t);
00107         }
00108 
00116         @SuppressWarnings("unchecked")
00117         @Override
00118         public boolean importData(JComponent comp, Transferable t) {
00119                 if (!canImport(comp, t.getTransferDataFlavors())) {
00120                         return false;
00121                 }
00122                 if (hasFileFlavor(t.getTransferDataFlavors())) {
00123                         final List<File> onlyPDFs = getOnlyPDFs(comp, t);
00124                         if (onlyPDFs.size() == 0) {
00125                                 return false;
00126                         }
00127 
00128                         // start the acrobat communication server if it hasn't already started...
00129                         AcrobatCommunicationServer.startServer();
00130 
00131                         System.out.println("Opening... " + onlyPDFs);
00132                         for (File f : onlyPDFs) {
00133                                 try {
00134                                         Desktop.getDesktop().open(f);
00135                                 } catch (IOException e) {
00136                                         e.printStackTrace();
00137                                 }
00138                         }
00139 
00140                         return true;
00141                 }
00142                 return false;
00143         }
00144 
00148         public String toString() {
00149                 return "PDF File Transfer Handler";
00150         }
00151 
00152 }

Generated on Sat Apr 14 18:21:34 2007 for R3 Paper Toolkit by  doxygen 1.4.7