Skip to content
This repository
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 106 lines (94 sloc) 3.806 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
; Copyright (c) Dave Ray, 2011. All rights reserved.

; The use and distribution terms for this software are covered by the
; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
; which can be found in the file epl-v10.html at the root of this
; distribution.
; By using this software in any fashion, you are agreeing to be bound by
; the terms of this license.
; You must not remove this notice, or any other, from this software.

(ns seesaw.test.examples.dnd
  (:use seesaw.core
        seesaw.test.examples.example)
  (:require [seesaw.dnd :as dnd]))

; Set up a few targets for different data flavors.

; A list box that imports and exports strings
(defn string-target []
  (listbox
    :model ["A String"]
    :drag-enabled? true
    :drop-mode :insert
    :transfer-handler
      [:import [dnd/string-flavor (fn [{:keys [target data]}]
                                    (.. target getModel (addElement data)))]
       :export {
         :actions (constantly :copy)
         :start (fn [c] [dnd/string-flavor (selection c)])
         ; No :finish needed
        }]))


; A list box that imports and exports files, like with finder or explorer
(defn file-target []
  (listbox
    :model []
    :drag-enabled? true
    :drop-mode :insert
    :transfer-handler
      (dnd/default-transfer-handler
        :import [dnd/file-list-flavor (fn [{:keys [target data]}]
                                        ; data is always List<java.io.File>
                                        (doseq [file data]
                                          (.. target getModel (addElement file))))]
        :export {
          :actions (constantly :copy)
          :start (fn [c]
                     (let [file (selection c)]
                       [dnd/file-list-flavor [file]]))
          ; No :finish needed
        })))

; A list box that imports and exports URIs, like with a browser
(defn url-target []
  (listbox
    :model [(java.net.URI. "http://github.com/daveray/seesaw")]
    :drag-enabled? true
    :drop-mode :insert
    :transfer-handler
      (dnd/default-transfer-handler
        :import [dnd/uri-list-flavor (fn [{:keys [target data]}]
                                      ; data is seq of java.net.URI
                                      (doseq [url data]
                                        (.. target getModel (addElement url))))]
        :export {
          :actions (constantly :copy)
          :start (fn [c]
                     (let [url (selection c)]
                       [dnd/uri-list-flavor [url] ]))
          ; No :finish needed
        })))

; A list box that imports and exports images, like with a browser
(defn image-target []
  (let [icon-label (label)]
    (left-right-split
      (scrollable
        (listbox
          :listen [:selection (fn [e] (config! icon-label :icon (selection e)))]
          :selection-mode :single
          :model []
          :drag-enabled? true
          :drop-mode :insert
          :transfer-handler
            (dnd/default-transfer-handler
              :import [dnd/image-flavor (fn [{:keys [target data]}]
                                        (.. target getModel (addElement data)))])))
      (scrollable icon-label)
      :divider-location 1/2)))

(defexample []
  (frame
    :title "Seesaw Drag-n-Drop Example"
    :content
      (vertical-panel
        :items [
          (border-panel :border "Drag and Drop Text here" :center (scrollable (string-target)))
          (border-panel :border "Drag and Drop Files here" :center (scrollable (file-target)))
          (border-panel :border "Drag and Drop URIs here" :center (scrollable (url-target)))
          (border-panel :border "Drag and Drop Images here" :center (image-target))
          ])))

;(run :dispose)

Something went wrong with that request. Please try again.