Myna. Swing

Helper function for creating Swing based UI’s

Overview

The Myna.Swing library contains functions for interacting with javax.swing.* and java.awt.*.  This is primarily intended for use with the Myna commandline.

The core of this library is Myna.Swing.build.  This function takes a config object and returns a dynamically extended instance of the requested class.  This allows for a more representative definition of the GUI hierarchy.

Summary
Myna. SwingHelper function for creating Swing based UI’s
Functions
getColorreturns a java.awt.Color object from an RGB value
getDimensionreturns a java.awt.Dimension object from a width,height value
getConstreturns the value of the passed AWT/Swing constant Value.
alertCreates an alert dialog
getInputDisplays an input dialog, similar to window.prompt() in the browser
confirmDisplays a confirmation dialog, similar to window.confirm() in the browser
showDialogCreates a dialog with arbitrary fields
buildCreates Swing component from an object config
Properties
BorderFactoryshortcut to the javax.swing.BorderFactory class

Functions

getColor

getColor:function(r,
g,
b)

returns a java.awt.Color object from an RGB value

Parameters

rinteger “red” value
binteger “blue” value
ginteger “green” value

getDimension

getDimension:function(w,
h)

returns a java.awt.Dimension object from a width,height value

Parameters

winteger “width” value in pixels
hinteger “heigth” value in pixels

getConst

getConst:function(name)

returns the value of the passed AWT/Swing constant Value.

Parameters

namename of AWT/Swing constant

Example

var MS = Myna.Swing;
...
layoutPos:{
anchor:MS.getConst("GridBagConstraints.WEST"),
gridwidth:MS.getConst("GridBagConstraints.REMAINDER"), //end row
fill:MS.getConst("GridBagConstraints.HORIZONTAL"), //expand horizontally
weightx:1.0, // expand to fill extra space
},
...

alert

alert:function(text,
title,
type)

Creates an alert dialog

Parameters

textText to display in the alert box
titleOptional, default “Alert” Title of the alert window
type*Optional, default “info” The possible types are “info”,”warning”, and “error”.  This selects the icon displayed next to text

getInput

getInput:function(prompt,
options,
defaultIndex)

Displays an input dialog, similar to window.prompt() in the browser

Parameters

promptText to display above the input control
optionsOptional, default “” Either a string, or Array of string values to display.  If Array, a drop down or scrolling select box will be displayed, depending on the number of items in this array.  If this is a string, this will be the initial value of the text box
defaultIndexOptional, default 0 Only applies when options is an Array.  This is the option in the array to pre-select

Returns

text selected/entered by the user, or null if the dialog is canceled

confirm

confirm:function(prompt,
buttons,
defaultIndex)

Displays a confirmation dialog, similar to window.confirm() in the browser

Parameters

promptText to display
buttonsOptional, default [“OK”,”Cancel”] Array of text button labels to display.
defaultIndexOptional, default 0 Button to make the default option

Returns

True, if the “OK” button is pressed false, if canceled

showDialog

showDialog:function(fields,
buttonLabels,
defaultIndex)

Creates a dialog with arbitrary fields

Parameters

fieldsArray of field configuration objects.  See build
titleOptional, default “” Title of dialog
buttonLabelsOptional, default [“OK”,”Cancel”]
defaultIndexOptional, default 0 Button to make the default option

Returns

Array of generated fields, or null if canceled.  If the config objects in fields include the name property, then a property of that name will also be in the returned array.  This means that fields can be referenced by name or index

Example

var result = showDialog([{
cls:"JLabel",
text:"Username:"
},{
cls:"JTextField",
name:"username"
},{
cls:"JLabel",
text:"Password:"
},{
cls:"JPasswordField",
name:"password"
}],"SVN Authentication")

if (result){
p.username = result.username.getText()
//getPassword returns a char[], so we filter it through a native Java string
p.password = new java.lang.String(result.password.getPassword()).toString()
svn = new WebSvn().connect(p.username,p.password);
}

build

build:function(config)

Creates Swing component from an object config

Parameters

configObject configuration, see “Config” below.  This config object will be stored in a “config” property in the returned object

Config

clsString name of a class in Swing or AWT, like “JLabel” or “JTextField”
itemsOptional, default [] An array of component configs that represent children of this component
defaultsOptional, default {} An object representing a template for objects in items.  Every property in this object will be applied to each item unless that property is already defined. if defaults appears as a property in defaults then that will apply to the grandchildren items, etc.  This is useful for setting default properties for a group of fields
handlerOptional Function that handles the default action for this component.  This will create an ActionListener to this component
listenersOptional, default {} Allows the creation of component listeners for this component, by defining an event property and function handler.  The following events can be monitored: componentResized, componentShown, componentHidden, and componentMoved.  See http://download.oracle.com- /javase- /6- /docs- /api- /java- /awt- /event- /ComponentListener.html
layoutPosOptional A Java options object appropriate to use as the second parameter to java.awt.Contrainer.add().  See http://download.oracle.com- /javase- /1.5.0- /docs- /api- /java- /awt- /Container.html#add%28java.awt.Component%2C%20java.lang.Object%29 Alternatively, this can be a config object that describes an options object.
[*]Optional Any other property.  If this property has a match as a setter in cls then that setter will be called with the value of this property.  If the calue of this property has a cls property, build() will be called against it before passing it to the setter

Returns

An extended instance of cls with these extra properties:

configThe original config object
getCmpA function that takes a component name and searches this component and all child components and returns a reference to the named component, or null if not found

Example

var MS = Myna.Swing;
var config = {
//Special Property: Swing class to create
cls:"JFrame",
//any property that has setter is set on the resulting object
title:"HelloWorldSwing",
//getConst returns the value of the passed constant Value.
defaultCloseOperation:$server.isCommandline?MS.getConst("JFrame.EXIT_ON_CLOSE"):MS.getConst("WindowConstants.DISPOSE_ON_CLOSE"),
//any property not already defined by the class, will be added to the resulting object
center:function(){
this.setLocationRelativeTo(null)
},
//Special Property: elements to add to this Swing object
items:[{
cls:"JPanel",

//Special Property: layout specific positioning hint for this control
layoutPos:MS.getConst("BorderLayout.CENTER"),

//this is equivalent to setLayout(new java.awt.GridBagLayout())
layout:{
cls:"GridBagLayout"
},
//MS.BorderFactory is a shortcut to java.swing.BorderFactory
border:MS.BorderFactory.createCompoundBorder(
MS.BorderFactory.createTitledBorder("Text Fields"),
MS.BorderFactory.createEmptyBorder(5,5,5,5)
),
//Special Property: properties of "defaults" are copied to each item in "items".
//Nested "defaults" will applied to nested "items"
defaults:{
cls:"JLabel",
layoutPos:{
cls:"GridBagConstraints",
anchor:MS.getConst("GridBagConstraints.WEST"),
gridwidth:MS.getConst("GridBagConstraints.RELATIVE"), //next-to-last
fill:MS.getConst("GridBagConstraints.NONE"), //reset to default
weightx:0, // don't expand to fill space
}
},
items:[{
//all other properties are coming from "defaults" above
text:"JTextField:"
},{
cls:"JTextField",
preferredSize:MS.getDimension(100,22),
layoutPos:{
anchor:MS.getConst("GridBagConstraints.EAST"),
gridwidth:MS.getConst("GridBagConstraints.REMAINDER"), //end row
fill:MS.getConst("GridBagConstraints.HORIZONTAL"), //expand horizontally
weightx:1.0, // expand to fill extra space
},
handler:function(event){
var src =event.getSource();
//getCmp is custom extension for each component that searches
// recusively through its children looking for a component by name
src.getParent().getCmp("OutputField").setText("JTextField set to: " + src.getText())
}
},{
text:"JPasswordField:",
},{
cls:"JPasswordField",
layoutPos:{
anchor:MS.getConst("GridBagConstraints.EAST"),
gridwidth:MS.getConst("GridBagConstraints.REMAINDER"), //end row
fill:MS.getConst("GridBagConstraints.HORIZONTAL"), //expand horizontally
weightx:1.0, // expand to fill extra space
},
handler:function(event){
var src =event.getSource();
src.getParent().getCmp("OutputField").setText("JPasswordField set to: " + src.getText())
}
},{
text:"JFormattedTextField:",
},{
cls:"JFormattedTextField",
value:new java.util.Date(new Date().getTime()),
layoutPos:{
anchor:MS.getConst("GridBagConstraints.EAST"),
gridwidth:MS.getConst("GridBagConstraints.REMAINDER"), //end row
fill:MS.getConst("GridBagConstraints.HORIZONTAL"), //expand horizontally
weightx:1.0, // expand to fill extra space
},
//Special Property: automatically adds an ActionListener for this component
handler:function(event){
var src =event.getSource();

src.getParent().getCmp("OutputField").setText("JFormattedTextField set to: " + src.getText())
}
},{
text:"Type text in a field and press Enter.",
border:MS.BorderFactory.createEmptyBorder(10,0,0,0),
name:"OutputField",//this is how we find this component later with getCmp(name)
layoutPos:{
anchor:MS.getConst("GridBagConstraints.WEST"),
gridwidth:MS.getConst("GridBagConstraints.REMAINDER"), //end row
fill:MS.getConst("GridBagConstraints.HORIZONTAL"), //expand horizontally
weightx:1.0, // expand to fill extra space
},
}]
}]


}

var panel = new MS.build(config);

panel.center();
panel.show()

Properties

BorderFactory

shortcut to the javax.swing.BorderFactory class

getColor:function(r,
g,
b)
returns a java.awt.Color object from an RGB value
getDimension:function(w,
h)
returns a java.awt.Dimension object from a width,height value
getConst:function(name)
returns the value of the passed AWT/Swing constant Value.
alert:function(text,
title,
type)
Creates an alert dialog
getInput:function(prompt,
options,
defaultIndex)
Displays an input dialog, similar to window.prompt() in the browser
confirm:function(prompt,
buttons,
defaultIndex)
Displays a confirmation dialog, similar to window.confirm() in the browser
showDialog:function(fields,
buttonLabels,
defaultIndex)
Creates a dialog with arbitrary fields
build:function(config)
Creates Swing component from an object config