1 package net.suberic.pooka.gui.dnd; 2 3 import java.awt.datatransfer.*; 4 import java.io.*; 5 import java.util.List ; 6 import java.util.ArrayList ; 7 import javax.swing.*; 8 import java.awt.*; 9 10 import net.suberic.pooka.FolderInfo; 11 import net.suberic.pooka.gui.*; 12 13 16 public class DndUtils { 17 18 22 private static boolean canAccessSystemClipboard = true; 23 24 28 private static Object SandboxClipboardKey = new Object (); 29 30 static boolean isLinux = System.getProperty("os.name").startsWith("Linux"); 31 32 35 public static boolean hasFileFlavor(DataFlavor[] flavors) { 36 if (flavors != null) { 38 for (int i = 0; i < flavors.length; i++) { 39 if (flavors[i]!= null && flavors[i].isFlavorJavaFileListType()) 40 return true; 41 else if (isLinux && flavors[i] != null && flavors[i].isFlavorTextType()) 42 return true; 43 } 44 } 45 46 return false; 47 } 48 49 52 public static List extractFileList(Transferable t) throws UnsupportedFlavorException, java.io.IOException { 53 DataFlavor[] availableFlavors = t.getTransferDataFlavors(); 54 DataFlavor match = matchDataFlavor(new DataFlavor[] { DataFlavor.javaFileListFlavor }, availableFlavors); 55 if (match != null) { 56 return (java.util.List ) t.getTransferData(DataFlavor.javaFileListFlavor); 57 } else if (isLinux) { 58 match = matchDataFlavor(new DataFlavor[] { DataFlavor.stringFlavor }, availableFlavors); 59 if (match != null) { 60 ArrayList returnValue = new ArrayList (); 61 Reader urlReader = match.getReaderForText(t); 62 BufferedReader br = new BufferedReader(urlReader); 63 for (String line = br.readLine(); line != null && line.length() > 0; line = br.readLine()) { 64 try { 65 java.net.URI fileUri = new java.net.URI (line); 66 File currentFile = new File(fileUri); 67 returnValue.add(currentFile); 68 } catch (java.net.URISyntaxException e) { 69 e.printStackTrace(); 70 } 71 } 72 73 return returnValue; 74 } 75 } 76 77 return null; 78 } 79 80 84 public static DataFlavor matchDataFlavor(DataFlavor[] acceptableFlavors, DataFlavor[] availableFlavors) { 85 if (acceptableFlavors != null && availableFlavors != null) { 86 for (int i = 0; i < availableFlavors.length; i++) { 87 for (int j = 0; j < acceptableFlavors.length; j++) { 88 if (availableFlavors[i] != null && availableFlavors[i].match(acceptableFlavors[j])) 89 return availableFlavors[i]; 90 } 91 } 92 } 93 94 return null; 95 } 96 97 100 public static File createTemporaryFile(String fileName) throws java.io.IOException { 101 File returnValue = null; 102 File tmpDir = null; 103 String tmpDirString = System.getProperty("java.io.tmpdir"); 104 if (tmpDirString != null && tmpDirString.length() > 0) { 105 File firstType = new File(tmpDirString); 106 if (firstType != null && firstType.exists() && firstType.isDirectory() && firstType.canWrite()) { 107 tmpDir = firstType; 108 } 109 } 110 111 if (tmpDir == null) { 112 File tempfile = File.createTempFile("pooka", "tmp"); 114 tmpDir = tempfile.getParentFile(); 115 } 116 117 if (tmpDir != null) { 118 File testMe = new File(tmpDir, fileName); 119 if (! testMe.exists()) { 120 returnValue = testMe; 121 } 122 } 123 124 if (returnValue == null) { 125 returnValue = File.createTempFile(fileName, null); 127 } 128 129 returnValue.deleteOnExit(); 130 131 return returnValue; 132 } 133 134 137 public static FolderInfo getFolderInfo(JComponent c) { 138 try { 139 if (c instanceof FolderDisplayPanel) { 140 return ((FolderDisplayPanel) c).getFolderInfo(); 141 } 142 143 Object o = SwingUtilities.getAncestorOfClass(Class.forName("net.suberic.pooka.gui.FolderDisplayPanel"), c); 144 if (o != null) { 145 return ((net.suberic.pooka.gui.FolderDisplayPanel) o).getFolderInfo(); 146 } 147 148 o = SwingUtilities.getAncestorOfClass(Class.forName("net.suberic.pooka.gui.FolderPanel"), c); 150 if (o != null) { 151 Object selected = ((net.suberic.pooka.gui.FolderPanel) o).getSelectedNode(); 152 if (selected instanceof FolderNode) { 153 return ((FolderNode) selected).getFolderInfo(); 154 } 155 } 156 157 return null; 158 } catch (Exception e) { 159 return null; 160 } 161 } 162 163 166 public static Clipboard getClipboard(JComponent c) { 167 if (canAccessSystemClipboard()) { 168 return c.getToolkit().getSystemClipboard(); 169 } 170 Clipboard clipboard = (Clipboard)sun.awt.AppContext.getAppContext().get(SandboxClipboardKey); 171 if (clipboard == null) { 172 clipboard = new Clipboard("Sandboxed Component Clipboard"); 173 sun.awt.AppContext.getAppContext().put(SandboxClipboardKey, clipboard); 174 } 175 return clipboard; 176 } 177 178 184 public static boolean canAccessSystemClipboard() { 185 if (canAccessSystemClipboard) { 186 if (GraphicsEnvironment.isHeadless()) { 187 canAccessSystemClipboard = false; 188 return false; 189 } 190 191 SecurityManager sm = System.getSecurityManager(); 192 if (sm != null) { 193 try { 194 sm.checkSystemClipboardAccess(); 195 return true; 196 } catch (SecurityException se) { 197 canAccessSystemClipboard = false; 198 return false; 199 } 200 } 201 return true; 202 } 203 return false; 204 } 205 206 209 public static void clearClipboard(JComponent c) { 210 Clipboard cb = getClipboard(c); 211 if (cb != null) { 212 cb.setContents(new StringSelection(""),null); 213 } 214 } 215 } 216 | Popular Tags |