Created
December 5, 2011 20:34
-
-
Save flengyel/1435235 to your computer and use it in GitHub Desktop.
Java Buttondemo translated from Java to Clojure
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns buttondemo.core | |
(:gen-class) | |
(:import [javax.swing AbstractButton JButton JPanel JFrame ImageIcon] | |
[java.awt.event ActionEvent KeyEvent ActionListener]) | |
(:use [clojure.contrib.swing-utils])) | |
(defn create-image-icon [path] | |
; (prn (. System getProperty "user.dir")) | |
; (prn (. System getProperty "java.class.path")) | |
(if-let [img-url (clojure.java.io/resource path)] | |
(ImageIcon. img-url) | |
(.println System/err (str "File not found:" path)))) | |
(defn enable-buttons [button-flags] | |
(doseq [[button flag] button-flags] (.setEnabled button flag))) | |
(defn initialize-buttons [b1 b2 b3] | |
(doto b1 | |
(add-action-listener | |
(fn [_] (enable-buttons [[b1 false][b2 false][b3 true]]))) | |
(.setVerticalTextPosition AbstractButton/CENTER) | |
(.setHorizontalTextPosition AbstractButton/LEADING) | |
(.setToolTipText "I can disable the middle button.") | |
(.setMnemonic KeyEvent/VK_D)) | |
(doto b2 | |
(add-action-listener (fn [_] (prn "I told you not to click me."))) | |
(.setVerticalTextPosition AbstractButton/BOTTOM) | |
(.setHorizontalTextPosition AbstractButton/CENTER) | |
(.setToolTipText "Don't click me.") | |
(.setMnemonic KeyEvent/VK_M)) | |
(doto b3 | |
(add-action-listener | |
(fn [_] (enable-buttons [[b1 true][b2 true][b3 false]]))) | |
(.setMnemonic KeyEvent/VK_E) | |
(.setToolTipText "I can enable the middle button.") | |
(.setEnabled false)) | |
) | |
(defn -main [] | |
(let [[b1 b2 b3 :as buttons] (for [[title file] | |
; [["<html><center><b><u>D</u>isable</b><br><font color=#ffffdd>middle button</font>" "right.gif"] | |
[["Disable middle button" "right.gif"] | |
["Middle button" "middle.gif"] | |
["Enable middle button" "left.gif"]]] | |
(JButton. title (create-image-icon file))) | |
panel (doto (JPanel.) (.setOpaque true))] | |
(doseq [b buttons] (doto panel (.add b))) | |
(initialize-buttons b1 b2 b3) | |
(do-swing-and-wait | |
(doto (JFrame. "Button Demo") | |
(.setDefaultCloseOperation JFrame/EXIT_ON_CLOSE) | |
(.setContentPane panel) | |
(.pack) | |
(.setVisible true)))) | |
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defproject buttondemo "1.0.0-SNAPSHOT" | |
:description "Translation of the abysmal java button demo into clojure." | |
:dependencies [[org.clojure/clojure "1.2.0"] | |
[org.clojure/clojure-contrib "1.2.0"]] | |
; :resources-path "resources/" | |
:main buttondemo.core | |
:license {:url "http://bit.ly/igfXCC" | |
:comments "Derived in April 2011 from the above url."} | |
) |
The graphics files left.gif, middle.gif and right.gif, can be obtained from http://docs.oracle.com/javase/tutorial/uiswing/examples/components/ (cf. ButtonDemo) and go under the resources directory of the Leiningen project.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
buttondemo
Translation of Oracle ButtonDemo example, using Leiningen
Usage
lein run
License
Copyright (c) 1995, 2008, Oracle and/or its affiliates. All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
Neither the name of Oracle or the names of its
contributors may be used to endorse or promote products derived
from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
The rest is Copyright (C) 2011 Florian Lengyel Creative Commons Attribution Non-Commercial
Share and Share Alike
Distributed under the Eclipse Public License, the same as Clojure.