KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > suberic > pooka > gui > dnd > DndUtils


1 package net.suberic.pooka.gui.dnd;
2
3 import java.awt.datatransfer.*;
4 import java.io.*;
5 import java.util.List JavaDoc;
6 import java.util.ArrayList JavaDoc;
7 import javax.swing.*;
8 import java.awt.*;
9
10 import net.suberic.pooka.FolderInfo;
11 import net.suberic.pooka.gui.*;
12
13 /**
14  * A set of utility methods to use with drag and drop.
15  */

16 public class DndUtils {
17
18   /**
19    * Indicates if it is safe to access the system clipboard. Once false,
20    * access will never be checked again.
21    */

22   private static boolean canAccessSystemClipboard = true;
23
24   /**
25    * Key used in app context to lookup Clipboard to use if access to
26    * System clipboard is denied.
27    */

28   private static Object JavaDoc SandboxClipboardKey = new Object JavaDoc();
29
30   static boolean isLinux = System.getProperty("os.name").startsWith("Linux");
31
32   /**
33    * Returns true if this set of DataFlavors might include a FileFlavor.
34    */

35   public static boolean hasFileFlavor(DataFlavor[] flavors) {
36     // first see if we're on linux.
37
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   /**
50    * Extracts a List of File objects from a Transferable.
51    */

52   public static List JavaDoc extractFileList(Transferable t) throws UnsupportedFlavorException, java.io.IOException JavaDoc {
53     DataFlavor[] availableFlavors = t.getTransferDataFlavors();
54     DataFlavor match = matchDataFlavor(new DataFlavor[] { DataFlavor.javaFileListFlavor }, availableFlavors);
55     if (match != null) {
56       return (java.util.List JavaDoc) t.getTransferData(DataFlavor.javaFileListFlavor);
57     } else if (isLinux) {
58       match = matchDataFlavor(new DataFlavor[] { DataFlavor.stringFlavor }, availableFlavors);
59       if (match != null) {
60         ArrayList JavaDoc returnValue = new ArrayList JavaDoc();
61         Reader urlReader = match.getReaderForText(t);
62         BufferedReader br = new BufferedReader(urlReader);
63         for (String JavaDoc line = br.readLine(); line != null && line.length() > 0; line = br.readLine()) {
64           try {
65             java.net.URI JavaDoc fileUri = new java.net.URI JavaDoc(line);
66             File currentFile = new File(fileUri);
67             returnValue.add(currentFile);
68           } catch (java.net.URISyntaxException JavaDoc e) {
69             e.printStackTrace();
70           }
71         }
72
73         return returnValue;
74       }
75     }
76
77     return null;
78   }
79
80   /**
81    * Finds the first acceptable DataFlavor match and returns it,
82    * or null if no match is found.
83    */

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   /**
98    * Creates a temporary file with the given name.
99    */

100   public static File createTemporaryFile(String JavaDoc fileName) throws java.io.IOException JavaDoc {
101     File returnValue = null;
102     File tmpDir = null;
103     String JavaDoc 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       // try creating a temporary file.
113
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       // oh well. just create a normal temporary file.
126
returnValue = File.createTempFile(fileName, null);
127     }
128
129     returnValue.deleteOnExit();
130
131     return returnValue;
132   }
133
134   /**
135    * Gets the FolderInfo from the given Component.
136    */

137   public static FolderInfo getFolderInfo(JComponent c) {
138     try {
139       if (c instanceof FolderDisplayPanel) {
140         return ((FolderDisplayPanel) c).getFolderInfo();
141       }
142
143       Object JavaDoc 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       // check for the folder tree.
149
o = SwingUtilities.getAncestorOfClass(Class.forName("net.suberic.pooka.gui.FolderPanel"), c);
150       if (o != null) {
151         Object JavaDoc 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 JavaDoc e) {
159       return null;
160     }
161   }
162
163   /**
164    * Returns the clipboard to use for cut/copy/paste.
165    */

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   /**
179    * Returns true if it is safe to access the system Clipboard.
180    * If the environment is headless or the security manager
181    * does not allow access to the system clipboard, a private
182    * clipboard is used.
183    */

184   public static boolean canAccessSystemClipboard() {
185     if (canAccessSystemClipboard) {
186       if (GraphicsEnvironment.isHeadless()) {
187         canAccessSystemClipboard = false;
188         return false;
189       }
190
191       SecurityManager JavaDoc sm = System.getSecurityManager();
192       if (sm != null) {
193         try {
194           sm.checkSystemClipboardAccess();
195           return true;
196         } catch (SecurityException JavaDoc se) {
197           canAccessSystemClipboard = false;
198           return false;
199         }
200       }
201       return true;
202     }
203     return false;
204   }
205
206   /**
207    * Clears the clipboard of its current selection.
208    */

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