| How to create a component which is similar in usage as a standard Thinlet widget? |
|
Hover your mouse over the 's
to see the tips. |
Java code for the component |
package ui.component.numpicker;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Method;
import java.util.Hashtable;
import sbox.StaticBox;
import ui.component.numpad.NumpadCtrl;
import xkit.XKit;
import cize.ComponentBox;
/**
* @author Koen Roevens
*/
public class Numpicker {
// Register the component
static {
try {
ComponentBox cbox = (ComponentBox)StaticBox.getTheBox().lookUp(ComponentBox.LOOK_UP_NAME);
Hashtable properties = new Hashtable();
properties.put(String.class, new String[]{"value"});
properties.put(Method.class, new Object[] {"action", "enter"});
cbox.registerComponentType("numpicker",
Numpicker.class,
properties,
"panel" );
} catch (IllegalArgumentException iae) {
System.err.println(Numpicker.class.getName() + ".static: "+ iae.getMessage());
}
}
/////////////////////////////////////////////
/**
* Constructor
*/
private XKit x;
private Object parent;
private ComponentBox c;
public Numpicker(XKit x, ComponentBox c, Object parent, Object handler) throws IOException {
this.x = x;
this.c = c;
this.parent = parent;
if (c != null) {
c.parseComponent(getStream(), this, parent, handler);
} else {
x.add(x.parse(getStream(), this));
}
}
private InputStream getStream () throws IOException {
return getClass().getResourceAsStream("numpicker.xml");
}
public void showDialog() {
if (x.find("numdialog") == null) {
new NumpadCtrl(x, parent);
Object[] events = new Object[] {
new String[] {"zero","action"},
new String[] {"one","action"},
new String[] {"two","action"},
new String[] {"three","action"},
new String[] {"four","action"},
new String[] {"five","action"},
new String[] {"six","action"},
new String[] {"seven","action"},
new String[] {"eight","action"},
new String[] {"nine","action"},
new String[] {"komma","action"},
new String[] {"back","action"},
new String[] {"clear","action"}
};
c.registerDynamicEvents(parent , "action" , events );
}
}
}
|
|
Java2html
|
|
|
|
Xml for the dialog |
<?xml version="1.0"
encoding="ISO-8859-1"?>
<panel name="numpicker">
<button icon="/ui/component/numpicker/numpicker.png"
alignment="left" tooltip="NumPicker..."
action="showDialog()"/>
</panel>
|
|
Java code of the dialog handler. |
package ui.component.numpad;
import java.io.InputStream;
import xkit.XKit;
/**
* @author Koen Roevens
*/
public class NumpadCtrl {
private XKit x;
private Object parent;
/**
*
*/
public NumpadCtrl(XKit x, Object parent) {
this.x = x;
this.parent = parent;
try {
InputStream stream = getClass().getResourceAsStream("numdialog.xml");
x.add(x.parse(stream, this));
} catch (Exception exc) { exc.printStackTrace(); }
}
public void close(Object dialog) {
x.remove(dialog);
}
public void addDigit(String notTrimmedDigit) {
String oldValue = x.getString(parent,"value");
if (oldValue == null) {
oldValue = "";
}
String digit = notTrimmedDigit.trim();
if (digit.equals(".")) {
if (oldValue == null ||
oldValue.indexOf(".") == -1) {
x.setString(parent, "value", oldValue + digit.trim());
}
} else if (digit.equals("0")) {
if (oldValue != null &&
!oldValue.equals("")) {
x.setString(parent, "value", oldValue + digit.trim());
}
} else {
x.setString(parent, "value", oldValue + digit.trim());
}
}
public void clear() {
x.setString(parent, "value", null);
}
public void back() {
String oldValue = x.getString(parent,"value");
if (oldValue == null) {
oldValue = "";
}
if (oldValue != null &&
oldValue.length() > 0) {
x.setString(parent, "value", oldValue.substring(0,oldValue.length()-1));
}
}
}
|
|
Java2html
|
|
Xml for the dialog |
<?xml version="1.0"
encoding="UTF-8"?>
<dialog icon="/ui/component/numpicker/numpicker.png"
name="numdialog" text="Numpad" columns="2"
gap="1"
top="1" left="1" right="1"
bottom="1" border="true" closable="true">
<panel name="top" columns="3"
gap="1">
<button name="seven"
text="
7 "
action="addDigit(this.text)"/>
<button name="eight"
text="
8 "
action="addDigit(this.text)"/>
<button name="nine"
text="
9 "
action="addDigit(this.text)"/>
</panel>
<button name="back"
text="
B "
action="back()"/>
<panel name="middle" columns="3"
gap="1">
<button name="four"
text="
4 "
action="addDigit(this.text)"/>
<button name="five"
text="
5 "
action="addDigit(this.text)"/>
<button name="six"
text="
6 "
action="addDigit(this.text)"/>
</panel>
<button name="clear"
text="
C "
action="clear()"/>
<panel name="bottom" columns="3"
gap="1">
<button name="one"
text="
1 "
action="addDigit(this.text)"/>
<button name="two"
text="
2 "
action="addDigit(this.text)"/>
<button name="three"
text="
3 "
action="addDigit(this.text)"/>
<button name="zero"
text="
0 " colspan="2"
action="addDigit(this.text)"/>
<button name="komma"
text="
. "
action="addDigit(this.text)"/>
</panel>
<button name="enter"
text=" E "
action="close(numdialog)"/>
</dialog> |
|
|
Java code for the application using the component |
package ui.demo.numpicker;
import java.io.IOException;
import java.io.InputStream;
import listener.ListenerBox;
import sbox.StaticBox;
import xkit.Box;
import xkit.XKit;
import cize.ComponentBox;
import cize.Container;
/**
* @author Koen Roevens
*/
public class NumpickerDemo implements Container {
static {
Class clazz = xthinlet.XThinlet.class;
clazz = cize.simple.ComponentBoxImpl.class;
clazz = listener.thinlet.ListenerBoxImpl.class;
clazz = ui.component.menu.Menu.class;
clazz = ui.component.numpicker.Numpicker.class;
}
/////////////////////////////////////////////
private XKit x;
private ComponentBox c;
private ListenerBox l;
public NumpickerDemo(XKit x, ComponentBox c, ListenerBox l) throws IOException{
this.x = x;
this.l = l;
this.c = c;
parse();
}
/**
* Loads the xml file
*/
private void parse() throws IOException {
InputStream stream = getClass().getResourceAsStream("demo.xml");
x.add(x.parse(stream,this));
c.registerListeners();
}
public void addComponent(Object parent) {
String type = (String)x.getProperty(parent,"widget");
c.addComponent(type, parent, this);
}
public void changeValue(Object dest, Object numpicker) {
x.setString(dest, "text", x.getString(numpicker,"value"));
}
///////////////////////////////////////////////
public static void main(String[] args) {
Box box = StaticBox.getTheBox();
try {
ComponentBox c = (ComponentBox)box.lookUp(ComponentBox.LOOK_UP_NAME);
ListenerBox l = (ListenerBox)box.lookUp(ListenerBox.LOOK_UP_NAME);
new NumpickerDemo(box.getXKit(),c, l);
box.launch("NumpickerDemo - ThinCLet", 240, 320);
} catch (IOException e) {
e.printStackTrace();
} catch (Exception exc) {
exc.printStackTrace();
}
}
}
|
|
Java2html
|
|
|
|
Xml for the application using the component |
<panel name="demo"
columns="1">
<menu weightx="1"
description="Demo
of ThinCLet componentization"
author="Koen
Roevens"
email="koenroevens@users.sourceforge.net"/>
<panel name="form" columns="3"
top="10" left="10" right="10"
bottom="10" gap="2">
<textfield name="dest"
editable="false"/>
<numpicker name="picker"
left="5" action="changeValue(dest,this)"/>
</panel>
</panel>
|
|
|