Permalink
Browse files

Helper function for creating new data flavors. url-flavor.

  • Loading branch information...
daveray committed Sep 4, 2011
1 parent cab51d5 commit b96befb40e01c10338965891ec589d906f338086
Showing with 29 additions and 0 deletions.
  1. +25 −0 src/seesaw/dnd.clj
  2. +4 −0 test/seesaw/test/dnd.clj
View
@@ -19,12 +19,37 @@
[javax.swing TransferHandler
TransferHandler$TransferSupport]))
+(defn ^DataFlavor make-flavor
+ "Construct a new data flavor with the given mime-type and representation class.
+
+
+ Notes:
+
+ Run seesaw.dnd-explorer to experiment with flavors coming from
+ other apps.
+
+ Examples:
+
+ ; HTML as a reader
+ (make-flavor \"text/html\" java.io.Reader)
+ "
+ [mime-type ^java.lang.Class rep-class]
+ (DataFlavor.
+ (format "%s;class=%s"
+ mime-type
+ (.getCanonicalName rep-class))))
+
+(def url-flavor (make-flavor "application/x-java-url" java.net.URL))
+(def html-flavor (make-flavor "text/html" java.lang.String))
+
(defn ^DataFlavor to-flavor
[v]
(cond
(instance? DataFlavor v) v
(= v String) DataFlavor/stringFlavor
(= v java.io.File) DataFlavor/javaFileListFlavor
+ (= v java.net.URL) url-flavor
+ (= v java.io.File) DataFlavor/javaFileListFlavor
(= v java.awt.Image) DataFlavor/imageFlavor
(instance? java.awt.Image v) DataFlavor/imageFlavor
(class? v) (DataFlavor. (format "%s; class=%s" DataFlavor/javaJVMLocalObjectMimeType (.getName v)))
View
@@ -36,6 +36,10 @@
(= DataFlavor/javaFileListFlavor (to-flavor java.io.File)))
(it "returns a file list flavor for a file value"
(= DataFlavor/javaFileListFlavor (to-flavor (java.io.File. "."))))
+ (it "returns a url flavor for a URL class"
+ (= url-flavor (to-flavor java.net.URL)))
+ (it "returns a url flavor for a URL value"
+ (= url-flavor (to-flavor (java.net.URL. "http://example.com"))))
(it "returns an image flavor for an Image class"
(= DataFlavor/imageFlavor (to-flavor java.awt.Image)))
(it "returns an image flavor for an image value"

0 comments on commit b96befb

Please sign in to comment.