kotlin-swing can be found in its own repository: https://github.com/kotlin-projects/kotlin-swing
This commit is contained in:
@@ -1,11 +0,0 @@
|
||||
## Kotlin Swing
|
||||
|
||||
The Kotlin Swing library provides some helper functions and extensions for creating Swing user interfaces.
|
||||
|
||||
To try the sample run
|
||||
|
||||
cd kotlin/libraries/kotlin-swing
|
||||
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
|
||||
@@ -1,114 +0,0 @@
|
||||
<?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>0.1-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>kotlin-swing</artifactId>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.jetbrains.kotlin</groupId>
|
||||
<artifactId>kotlin-stdlib</artifactId>
|
||||
<version>${project.version}</version>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<sourceDirectory>src/main/kotlin</sourceDirectory>
|
||||
<testSourceDirectory>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.DemoPackage</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.DemoPackage"/>
|
||||
</target>
|
||||
</configuration>
|
||||
<goals>
|
||||
<goal>run</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</profile>
|
||||
</profiles>
|
||||
</project>
|
||||
@@ -1,27 +0,0 @@
|
||||
package kotlin.swing
|
||||
|
||||
import java.awt.event.ActionEvent
|
||||
import javax.swing.AbstractAction
|
||||
import javax.swing.Action
|
||||
import javax.swing.Action.*
|
||||
import javax.swing.Icon
|
||||
|
||||
/**
|
||||
* Helper method to create an action from a function
|
||||
*/
|
||||
fun action(text: String, description: String? = null, mnemonic: Int? = null, icon: Icon? = null, fn: (ActionEvent) -> Unit): Action {
|
||||
val answer = object: AbstractAction(text, icon) {
|
||||
public override fun actionPerformed(e: ActionEvent) {
|
||||
val event: ActionEvent = if (e != null) {
|
||||
e
|
||||
} else {
|
||||
// lets create a dummy event
|
||||
ActionEvent(this, ActionEvent.ACTION_PERFORMED, text)
|
||||
}
|
||||
(fn)(event)
|
||||
}
|
||||
}
|
||||
if (description != null) answer.putValue(SHORT_DESCRIPTION, description)
|
||||
if (mnemonic != null) answer.putValue(MNEMONIC_KEY, mnemonic)
|
||||
return answer
|
||||
}
|
||||
@@ -1,90 +0,0 @@
|
||||
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)
|
||||
}
|
||||
@@ -1,18 +0,0 @@
|
||||
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
|
||||
}
|
||||
@@ -1,11 +0,0 @@
|
||||
package kotlin.swing
|
||||
|
||||
import javax.swing.*
|
||||
import java.awt.event.*
|
||||
import java.awt.*
|
||||
|
||||
fun button(text: String, action: (ActionEvent) -> Unit): JButton {
|
||||
val result = JButton(text)
|
||||
result.addActionListener(action)
|
||||
return result
|
||||
}
|
||||
@@ -1,59 +0,0 @@
|
||||
package kotlin.swing
|
||||
|
||||
import java.awt.Dimension
|
||||
import java.awt.Component
|
||||
import javax.swing.JComponent
|
||||
|
||||
var JComponent.description: String?
|
||||
get() {
|
||||
return getAccessibleContext()?.getAccessibleDescription()
|
||||
}
|
||||
set(value) {
|
||||
getAccessibleContext()?.setAccessibleDescription(value)
|
||||
}
|
||||
|
||||
var JComponent.preferredWidth: Int
|
||||
get() = getPreferredSize().width()
|
||||
set(value) {
|
||||
setPreferredSize(getPreferredSize().width(value))
|
||||
}
|
||||
var JComponent.preferredHeight: Int
|
||||
get() = getPreferredSize().height()
|
||||
set(value) {
|
||||
setPreferredSize(getPreferredSize().height(value))
|
||||
}
|
||||
|
||||
var Component.minimumWidth: Int
|
||||
get() = getMinimumSize().width()
|
||||
set(value) {
|
||||
setMinimumSize(getMinimumSize().width(value))
|
||||
}
|
||||
var Component.minimumHeight: Int
|
||||
get() = getMinimumSize().height()
|
||||
set(value) {
|
||||
setMinimumSize(getMinimumSize().height(value))
|
||||
}
|
||||
|
||||
var Component.maximumWidth: Int
|
||||
get() = getMaximumSize().width()
|
||||
set(value) {
|
||||
setMaximumSize(getMaximumSize().width(value))
|
||||
}
|
||||
var Component.maximumHeight: Int
|
||||
get() = getMaximumSize().height()
|
||||
set(value) {
|
||||
setMaximumSize(getMaximumSize().height(value))
|
||||
}
|
||||
|
||||
var Component.width: Int
|
||||
get() = getSize().width()
|
||||
set(value) {
|
||||
setSize(getSize().width(value))
|
||||
}
|
||||
var Component.height: Int
|
||||
get() = getSize().height()
|
||||
set(value) {
|
||||
setSize(getSize().height(value))
|
||||
}
|
||||
|
||||
|
||||
@@ -1,32 +0,0 @@
|
||||
package kotlin.swing
|
||||
|
||||
import java.awt.Dimension
|
||||
import java.awt.Component
|
||||
import javax.swing.JComponent
|
||||
|
||||
/**
|
||||
* Returns the width of the dimension or zero if its null
|
||||
*/
|
||||
fun Dimension?.width(): Int {
|
||||
return if (this == null) 0 else this.width
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the height of the dimension or zero if its null
|
||||
*/
|
||||
fun Dimension?.height(): Int {
|
||||
return if (this == null) 0 else this.height
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a new dimension with the same height and the given width
|
||||
*/
|
||||
fun Dimension?.width(newValue: Int): Dimension {
|
||||
return Dimension(newValue, height())
|
||||
}
|
||||
/**
|
||||
* Returns a new dimension with the same width and the given height
|
||||
*/
|
||||
fun Dimension?.height(newValue: Int): Dimension {
|
||||
return Dimension(width(), newValue)
|
||||
}
|
||||
@@ -1,55 +0,0 @@
|
||||
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()!!
|
||||
set(t) {
|
||||
setTitle(t)
|
||||
}
|
||||
|
||||
var JFrame.size: Pair<Int, Int>
|
||||
get() = Pair(getSize()!!.getWidth().toInt(), getSize()!!.getHeight().toInt())
|
||||
set(dim) {
|
||||
setSize(Dimension(dim.first, dim.second))
|
||||
}
|
||||
|
||||
var JFrame.height: Int
|
||||
get() = getSize()!!.getHeight().toInt()
|
||||
set(h) {
|
||||
setSize(width, h)
|
||||
}
|
||||
|
||||
var JFrame.width: Int
|
||||
get() = getSize()!!.getWidth().toInt()
|
||||
set(w) {
|
||||
setSize(height, w)
|
||||
}
|
||||
|
||||
var JFrame.jmenuBar: JMenuBar?
|
||||
get() = getJMenuBar()
|
||||
set(value) {
|
||||
setJMenuBar(value)
|
||||
}
|
||||
@@ -1,37 +0,0 @@
|
||||
package kotlin.swing
|
||||
|
||||
import java.awt.GridBagConstraints
|
||||
|
||||
/**
|
||||
* Helper function to create a [GridBagConstraints]
|
||||
*/
|
||||
fun gridBagContraints(gridx: Int? = null, gridy: Int? = null, fill: Int? = null, anchor: Int? = null,
|
||||
gridwidth: Int? = null, gridheight: Int? = null,
|
||||
weightx: Double? = null, weighty: Double? = null): GridBagConstraints {
|
||||
val answer = GridBagConstraints()
|
||||
if (gridx != null) {
|
||||
answer.gridx = gridx
|
||||
}
|
||||
if (gridy != null) {
|
||||
answer.gridy = gridy
|
||||
}
|
||||
if (fill != null) {
|
||||
answer.fill = fill
|
||||
}
|
||||
if (anchor != null) {
|
||||
answer.anchor = anchor
|
||||
}
|
||||
if (gridwidth != null) {
|
||||
answer.gridwidth = gridwidth
|
||||
}
|
||||
if (gridheight != null) {
|
||||
answer.gridheight = gridheight
|
||||
}
|
||||
if (weightx != null) {
|
||||
answer.weightx = weightx
|
||||
}
|
||||
if (weighty != null) {
|
||||
answer.weighty = weighty
|
||||
}
|
||||
return answer
|
||||
}
|
||||
@@ -1,124 +0,0 @@
|
||||
package kotlin.swing
|
||||
|
||||
import javax.swing.*
|
||||
|
||||
public fun JMenuBar.menu(text: String, init: JMenu.() -> Unit): JMenu {
|
||||
val answer = JMenu(text)
|
||||
answer.init()
|
||||
add(answer)
|
||||
return answer;
|
||||
}
|
||||
|
||||
public fun JPopupMenu.menu(text: String, init: JMenu.() -> Unit): JMenu {
|
||||
val answer = JMenu(text)
|
||||
answer.init()
|
||||
add(answer)
|
||||
return answer;
|
||||
}
|
||||
|
||||
var JMenu.mnemonic: Int
|
||||
get() {
|
||||
return getMnemonic()
|
||||
}
|
||||
set(value) {
|
||||
setMnemonic(value)
|
||||
}
|
||||
|
||||
var JMenu.accelerator: KeyStroke?
|
||||
get() {
|
||||
return getAccelerator()
|
||||
}
|
||||
set(value) {
|
||||
setAccelerator(value)
|
||||
}
|
||||
|
||||
fun JMenu.item(vararg actions: Action): Unit {
|
||||
for (action in actions) {
|
||||
val answer = JMenuItem(action)
|
||||
add(answer)
|
||||
}
|
||||
}
|
||||
|
||||
fun JMenu.item(text: String, description: String? = null, mnemonic: Char? = null, accelerator: KeyStroke? = null): JMenuItem {
|
||||
val answer = menuItem(text, description, mnemonic, accelerator)
|
||||
add(answer)
|
||||
return answer
|
||||
}
|
||||
|
||||
fun JMenu.checkBoxItem(text: String, description: String? = null, mnemonic: Char? = null, accelerator: KeyStroke? = null): JCheckBoxMenuItem {
|
||||
val answer = checkBoxMenuItem(text, description, mnemonic, accelerator)
|
||||
add(answer)
|
||||
return answer
|
||||
}
|
||||
|
||||
fun JMenu.radioButtonItem(text: String, description: String? = null, mnemonic: Char? = null, accelerator: KeyStroke? = null): JRadioButtonMenuItem {
|
||||
val answer = radioButtonMenuItem(text, description, mnemonic, accelerator)
|
||||
add(answer)
|
||||
return answer
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper method to create a new [JMenuItem]
|
||||
*/
|
||||
public fun menuItem(text: String, description: String? = null, mnemonic: Char? = null, accelerator: KeyStroke? = null): JMenuItem {
|
||||
return configureMenuItem(JMenuItem(text), description, mnemonic, accelerator)
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper method to create a new [JCheckBoxMenuItem]
|
||||
*/
|
||||
public fun checkBoxMenuItem(text: String, description: String? = null, mnemonic: Char? = null, accelerator: KeyStroke? = null): JCheckBoxMenuItem {
|
||||
return configureMenuItem(JCheckBoxMenuItem(text), description, mnemonic, accelerator)
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper method to create a new [JRadioButtonMenuItem]
|
||||
*/
|
||||
public fun radioButtonMenuItem(text: String, description: String? = null, mnemonic: Char? = null, accelerator: KeyStroke? = null): JRadioButtonMenuItem {
|
||||
return configureMenuItem(JRadioButtonMenuItem(text), description, mnemonic, accelerator)
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper method to create a new [JMenuItem]
|
||||
*/
|
||||
fun <T: JMenuItem> configureMenuItem(answer: T, description: String?, mnemonic: Char?, accelerator: KeyStroke?): T {
|
||||
if (description != null) {
|
||||
answer.getAccessibleContext()?.setAccessibleDescription(description)
|
||||
}
|
||||
if (mnemonic != null) {
|
||||
answer.setMnemonic(mnemonic)
|
||||
}
|
||||
if (accelerator != null) {
|
||||
answer.setAccelerator(accelerator)
|
||||
}
|
||||
return answer
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper function to create a [KeyStroke]
|
||||
*/
|
||||
public fun keyStroke(keyChar: Char, modifiers: Int?): KeyStroke? {
|
||||
return if (modifiers != null) {
|
||||
KeyStroke.getKeyStroke(keyChar, modifiers)
|
||||
} else {
|
||||
KeyStroke.getKeyStroke(keyChar)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a [JMenuBar] from a list of [JMenu] objects
|
||||
*/
|
||||
public fun menuBar(init: JMenuBar.() -> Unit): JMenuBar {
|
||||
val answer = JMenuBar()
|
||||
answer.init()
|
||||
return answer
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a [JPopupMenu] from a list of [JMenu] objects
|
||||
*/
|
||||
public fun popupMenu(init: JPopupMenu.() -> Unit): JPopupMenu {
|
||||
val answer = JPopupMenu()
|
||||
answer.init()
|
||||
return answer
|
||||
}
|
||||
@@ -1,53 +0,0 @@
|
||||
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()
|
||||
|
||||
val aboutAction = action("About") {
|
||||
println("Show about window")
|
||||
}
|
||||
val propertiesAction = action("Properties") {
|
||||
println("Show Properties window")
|
||||
}
|
||||
|
||||
jmenuBar = menuBar{
|
||||
menu("File") {
|
||||
add(aboutAction)
|
||||
}
|
||||
menu("Help") {
|
||||
add(propertiesAction)
|
||||
}
|
||||
}
|
||||
|
||||
size = Pair(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)
|
||||
}
|
||||
@@ -98,7 +98,6 @@
|
||||
|
||||
<module>kunit</module>
|
||||
<module>kotlin-jdbc</module>
|
||||
<module>kotlin-swing</module>
|
||||
|
||||
<module>examples/kotlin-java-example</module>
|
||||
<module>examples/js-example</module>
|
||||
|
||||
Reference in New Issue
Block a user