From 698c6cc7bc62b0feb4fbd2653953e53e5472c6c9 Mon Sep 17 00:00:00 2001 From: James Strachan Date: Thu, 5 Apr 2012 13:02:52 +0100 Subject: [PATCH] added an initial spike of a swing library for kotlin to help create swing UIs using nice kotlin builder syntax --- libraries/kdoc/ApiDocsModule.kt | 1 + libraries/kotlin-swing/ReadMe.md | 10 ++ libraries/kotlin-swing/pom.xml | 114 ++++++++++++++++++ .../main/kotlin/kotlin/swing/BorderPanel.kt | 90 ++++++++++++++ .../src/main/kotlin/kotlin/swing/Builders.kt | 28 +++++ .../src/main/kotlin/kotlin/swing/Frames.kt | 49 ++++++++ .../kotlin/test/kotlin/swing/SwingSample.kt | 35 ++++++ libraries/pom.xml | 2 + libraries/stdlib/pom.xml | 2 +- 9 files changed, 330 insertions(+), 1 deletion(-) create mode 100644 libraries/kotlin-swing/ReadMe.md create mode 100644 libraries/kotlin-swing/pom.xml create mode 100644 libraries/kotlin-swing/src/main/kotlin/kotlin/swing/BorderPanel.kt create mode 100644 libraries/kotlin-swing/src/main/kotlin/kotlin/swing/Builders.kt create mode 100644 libraries/kotlin-swing/src/main/kotlin/kotlin/swing/Frames.kt create mode 100644 libraries/kotlin-swing/src/test/kotlin/test/kotlin/swing/SwingSample.kt diff --git a/libraries/kdoc/ApiDocsModule.kt b/libraries/kdoc/ApiDocsModule.kt index 137cc38e438..35e364a92b5 100644 --- a/libraries/kdoc/ApiDocsModule.kt +++ b/libraries/kdoc/ApiDocsModule.kt @@ -7,5 +7,6 @@ fun project() { addSourceFiles("../stdlib/src") addSourceFiles("../kunit/src/main/kotlin") addSourceFiles("../kotlin-jdbc/src/main/kotlin") + //addSourceFiles("../kotlin-swing/src/main/kotlin") } } \ No newline at end of file diff --git a/libraries/kotlin-swing/ReadMe.md b/libraries/kotlin-swing/ReadMe.md new file mode 100644 index 00000000000..35b6b5639d4 --- /dev/null +++ b/libraries/kotlin-swing/ReadMe.md @@ -0,0 +1,10 @@ +## Kotlin Swing + +The Kotlin Swing library provides some helper functions and extensions for creating Swing user interfaces. + +To try the sample run + + mvn test -Pui + +Or browse the [source of the sample](https://github.com/JetBrains/kotlin/blob/master/libraries/kotlin-swing/src/test/kotlin/test/kotlin/swing/SwingSample.kt) +to see the kinds of features available \ No newline at end of file diff --git a/libraries/kotlin-swing/pom.xml b/libraries/kotlin-swing/pom.xml new file mode 100644 index 00000000000..447d36b8df9 --- /dev/null +++ b/libraries/kotlin-swing/pom.xml @@ -0,0 +1,114 @@ + + + + 4.0.0 + + + org.jetbrains.kotlin + kotlin-project + 1.0-SNAPSHOT + + + kotlin-swing + + + + org.jetbrains.kotlin + stdlib + ${project.version} + + + + + + ${project.basedir}/src/main/kotlin + ${project.basedir}/src/test/kotlin + + + + org.jetbrains.kotlin + kotlin-maven-plugin + ${project.version} + + + + compile + compile + + compile + + + + + test-compile + test-compile + + test-compile + + + + + + + org.codehaus.mojo + exec-maven-plugin + ${exec-maven-plugin.version} + + + + java + + + + + demo.namespace + test + + + java.awt.headless + false + + + + + + + + + + + + + + ui + + false + + + + + org.apache.maven.plugins + maven-antrun-plugin + 1.7 + + + run + test + + + + + + + run + + + + + + + + + diff --git a/libraries/kotlin-swing/src/main/kotlin/kotlin/swing/BorderPanel.kt b/libraries/kotlin-swing/src/main/kotlin/kotlin/swing/BorderPanel.kt new file mode 100644 index 00000000000..7956abb49a6 --- /dev/null +++ b/libraries/kotlin-swing/src/main/kotlin/kotlin/swing/BorderPanel.kt @@ -0,0 +1,90 @@ +package kotlin.swing + +import javax.swing.* +import java.awt.event.* +import java.awt.* + + +// TODO ideally should use BorderPanel as only that class should have +// the north / south / east west properties +fun borderPanel(init: JPanel.() -> Unit): JPanel { + val p = JPanel() + p.init() + return p +} + + +/** + TODO compile error +class BorderPanel(layout: BorderLayout = BorderLayout(), init: BorderPanel.() -> Unit): JPanel(layout) { + { + this.init() + } + + var south: JComponent + get() = throw UnsupportedOperationException() + set(comp) { + add(comp, BorderLayout.SOUTH) + } + + var north: JComponent + get() = throw UnsupportedOperationException() + set(comp) { + add(comp, BorderLayout.NORTH) + } + + var east: JComponent + get() = throw UnsupportedOperationException() + set(comp) { + add(comp, BorderLayout.EAST) + } + + var west: JComponent + get() = throw UnsupportedOperationException() + set(comp) { + add(comp, BorderLayout.WEST) + } + + var center: JComponent + get() = throw UnsupportedOperationException() + set(comp) { + add(comp, BorderLayout.CENTER) + } + +} + +*/ + + + + +// TODO remove these properties when we can zap +var Container.south: JComponent + get() = throw UnsupportedOperationException() + set(comp) { + add(comp, BorderLayout.SOUTH) + } + +var Container.north: JComponent + get() = throw UnsupportedOperationException() + set(comp) { + add(comp, BorderLayout.NORTH) + } + +var Container.east: JComponent + get() = throw UnsupportedOperationException() + set(comp) { + add(comp, BorderLayout.EAST) + } + +var Container.west: JComponent + get() = throw UnsupportedOperationException() + set(comp) { + add(comp, BorderLayout.WEST) + } + +var Container.center: JComponent + get() = throw UnsupportedOperationException() + set(comp) { + add(comp, BorderLayout.CENTER) + } diff --git a/libraries/kotlin-swing/src/main/kotlin/kotlin/swing/Builders.kt b/libraries/kotlin-swing/src/main/kotlin/kotlin/swing/Builders.kt new file mode 100644 index 00000000000..88347fa25e1 --- /dev/null +++ b/libraries/kotlin-swing/src/main/kotlin/kotlin/swing/Builders.kt @@ -0,0 +1,28 @@ +package kotlin.swing + +import javax.swing.* +import java.awt.event.* +import java.awt.* + + +fun frame(title : String, init : JFrame.() -> Unit) : JFrame { + val result = JFrame(title) + result.init() + return result +} + +fun panel(init: JPanel.() -> Unit): JPanel { + val p = JPanel() + p.init() + return p +} + +fun button(text: String, action: (ActionEvent) -> Unit): JButton { + val result = JButton(text) + result.addActionListener(object : ActionListener { + public override fun actionPerformed(e: ActionEvent?) { + action(e!!) + } + }) + return result +} diff --git a/libraries/kotlin-swing/src/main/kotlin/kotlin/swing/Frames.kt b/libraries/kotlin-swing/src/main/kotlin/kotlin/swing/Frames.kt new file mode 100644 index 00000000000..3738bf673cc --- /dev/null +++ b/libraries/kotlin-swing/src/main/kotlin/kotlin/swing/Frames.kt @@ -0,0 +1,49 @@ +package kotlin.swing + +import javax.swing.* +import java.awt.event.* +import java.awt.* + + +fun JFrame.exitOnClose(): Unit { + // TODO causes compile error + //defaultCloseOperation = JFrame.EXIT_ON_CLOSE + defaultCloseOperation = 3 +} + +var JFrame.defaultCloseOperation: Int + get() = getDefaultCloseOperation() + set(def) { + setDefaultCloseOperation(def) + } + + +var JFrame.contentPane: Container? + get() = getContentPane() + set(value) { + setContentPane(value) + } + +var JFrame.title: String + get() = getTitle().sure() + set(t) { + setTitle(t) + } + +var JFrame.size: #(Int, Int) + get() = #(getSize().sure().getWidth().toInt(), getSize().sure().getHeight().toInt()) + set(dim) { + setSize(Dimension(dim._1, dim._2)) + } + +var JFrame.height: Int + get() = getSize().sure().getHeight().toInt() + set(h) { + setSize(width, h) + } + +var JFrame.width: Int + get() = getSize().sure().getWidth().toInt() + set(w) { + setSize(height, w) + } diff --git a/libraries/kotlin-swing/src/test/kotlin/test/kotlin/swing/SwingSample.kt b/libraries/kotlin-swing/src/test/kotlin/test/kotlin/swing/SwingSample.kt new file mode 100644 index 00000000000..7b4579a1f90 --- /dev/null +++ b/libraries/kotlin-swing/src/test/kotlin/test/kotlin/swing/SwingSample.kt @@ -0,0 +1,35 @@ +package demo + +import javax.swing.* +import kotlin.swing.* + +fun main(args: Array): Unit { + + val greeting = """ +Welcome to Kotlin + +Enter some text here! +""" + + val f = frame("Kool Kotlin Swing Demo") { + exitOnClose() + size = #(500, 300) + + val textArea = JTextArea(greeting) + + center = textArea + south = borderPanel { + west = button("Clear") { + textArea.setText("") + println("Cleared text!") + } + east = button("Restore") { + textArea.setText(greeting) + println("Restored text!") + } + } + } + + f.setLocationRelativeTo(null) + f.setVisible(true) +} \ No newline at end of file diff --git a/libraries/pom.xml b/libraries/pom.xml index d4a33c58bd7..93bffa9dd6e 100644 --- a/libraries/pom.xml +++ b/libraries/pom.xml @@ -25,6 +25,7 @@ 2.4 1.1.0 2.5 + 1.2.1 @@ -36,6 +37,7 @@ kdoc kotlin-jdbc kotlin-java + kotlin-swing website examples diff --git a/libraries/stdlib/pom.xml b/libraries/stdlib/pom.xml index b05b34f6099..cb814ed0479 100644 --- a/libraries/stdlib/pom.xml +++ b/libraries/stdlib/pom.xml @@ -36,7 +36,7 @@ org.codehaus.mojo exec-maven-plugin - 1.2.1 + ${exec-maven-plugin.version}