added an initial spike of a swing library for kotlin to help create swing UIs using nice kotlin builder syntax

This commit is contained in:
James Strachan
2012-04-05 13:02:52 +01:00
parent ed8836d170
commit 698c6cc7bc
9 changed files with 330 additions and 1 deletions
+1
View File
@@ -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")
}
}
+10
View File
@@ -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
+114
View File
@@ -0,0 +1,114 @@
<?xml version="1.0" encoding="UTF-8"?>
<project
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-project</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>kotlin-swing</artifactId>
<dependencies>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>stdlib</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
<build>
<sourceDirectory>${project.basedir}/src/main/kotlin</sourceDirectory>
<testSourceDirectory>${project.basedir}/src/test/kotlin</testSourceDirectory>
<plugins>
<plugin>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-plugin</artifactId>
<version>${project.version}</version>
<executions>
<execution>
<id>compile</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
<execution>
<id>test-compile</id>
<phase>test-compile</phase>
<goals>
<goal>test-compile</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>${exec-maven-plugin.version}</version>
<executions>
<execution>
<goals>
<goal>java</goal>
</goals>
</execution>
</executions>
<configuration>
<mainClass>demo.namespace</mainClass>
<classpathScope>test</classpathScope>
<systemProperties>
<systemProperty>
<key>java.awt.headless</key>
<value>false</value>
</systemProperty>
</systemProperties>
</configuration>
</plugin>
</plugins>
</build>
<profiles>
<!-- runs the Swing sample UI -->
<profile>
<id>ui</id>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.7</version>
<executions>
<execution>
<id>run</id>
<phase>test</phase>
<configuration>
<target>
<java fork="true" classpathref="maven.test.classpath" classname="demo.namespace"/>
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>
@@ -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)
}
@@ -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
}
@@ -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)
}
@@ -0,0 +1,35 @@
package demo
import javax.swing.*
import kotlin.swing.*
fun main(args: Array<String>): 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)
}
+2
View File
@@ -25,6 +25,7 @@
<maven-jar-plugin.version>2.4</maven-jar-plugin.version>
<pegdown.version>1.1.0</pegdown.version>
<surefire-version>2.5</surefire-version>
<exec-maven-plugin.version>1.2.1</exec-maven-plugin.version>
</properties>
<modules>
@@ -36,6 +37,7 @@
<module>kdoc</module>
<module>kotlin-jdbc</module>
<module>kotlin-java</module>
<module>kotlin-swing</module>
<module>website</module>
<module>examples</module>
</modules>
+1 -1
View File
@@ -36,7 +36,7 @@
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<version>${exec-maven-plugin.version}</version>
<executions>
<execution>
<goals>