[Thinlet-development] thinlet/src/java/thinlet/examples/demo Calculator.java,NONE,1.1 Demo.java,NONE
Brought to you by:
bajzat
Update of /cvsroot/thinlet/thinlet/src/java/thinlet/examples/demo
In directory sc8-pr-cvs1:/tmp/cvs-serv29772/src/java/thinlet/examples/demo
Added Files:
Calculator.java Demo.java calculator.xml demo.xml
demodialog.xml manifest.mf
Log Message:
New CVS layout and build infrastructure. Contributed by Campbell Boucher-Burnet. Many thanks\!
--- NEW FILE: Calculator.java ---
package thinlet.examples.demo;
import thinlet.*;
public class Calculator extends Thinlet {
public Calculator() {
try {
add(parse("calculator.xml"));
} catch (Exception exc) { exc.printStackTrace(); }
}
public void calculate(String number1, String number2, Object result) { // Widget result
try {
int i1 = Integer.parseInt(number1);
int i2 = Integer.parseInt(number2);
setString(result, "text", String.valueOf(i1 + i2));
// result.set("text", String.valueOf(i1 + i2));
} catch (NumberFormatException nfe) {
getToolkit().beep();
}
}
public static void main(String[] args) {
new FrameLauncher("Calculator", new Calculator(), 320, 240);
}
}
--- NEW FILE: Demo.java ---
package thinlet.examples.demo;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import java.util.*;
import thinlet.*;
/**
*
*/
public class Demo extends Thinlet {
/**
*
*/
public Demo() {
try {
add(parse("demo.xml"));
} catch (Exception exc) { exc.printStackTrace(); }
}
/**
*
*/
public static void main(String[] args) {
new FrameLauncher("Demo", new Demo(), 320, 320);
}
boolean textinit;
boolean valueinit;
/**
*
*/
public void loadText(Object textarea) {
try {
InputStream inputstream = null;
try {
inputstream = getClass().getResourceAsStream("demodialog.xml");
} catch (Throwable e) {}
BufferedReader reader = new BufferedReader(new InputStreamReader(inputstream));
StringBuffer text = new StringBuffer();
for (int c = reader.read(); c != -1; c = reader.read()) {
if (((c > 0x1f) && (c < 0x7f)) ||
((c > 0x9f) && (c < 0xffff)) || (c == '\n')) {
text.append((char) c);
}
else if (c == '\t') {
text.append(" ");
}
}
reader.close();
setString(textarea, "text", text.toString());
textinit = true;
} catch (Exception exc) { getToolkit().beep(); }
}
public void actionTheme(String idx) {
int index = idx.charAt(1) - '0';
switch (index) {
case 0: //xp
setColors(0xece9d8, 0x000000, 0xf5f4f0,
0x919b9a, 0xb0b0b0, 0xededed, 0xb9b9b9, 0xff899a, 0xc5c5dd);
break;
case 1: //gray
setColors(0xe6e6e6, 0x000000, 0xffffff,
0x909090, 0xb0b0b0, 0xededed, 0xb9b9b9, 0x89899a, 0xc5c5dd);
break;
case 2: //yellow
setColors(0xeeeecc, 0x000000, 0xffffff,
0x999966, 0xb0b096, 0xededcb, 0xcccc99, 0xcc6600, 0xffcc66);
break;
case 3: //blue
setColors(0x6375d6, 0xffffff, 0x7f8fdd,
0xd6dff5, 0x9caae5, 0x666666, 0x003399, 0xff3333, 0x666666);
break;
}
}
/**
*
*/
public void changeEditable(boolean editable, Object textarea) {
setBoolean(textarea, "editable", editable);
}
/**
*
*/
public void changeEnabled(boolean enabled, Object textarea) {
setBoolean(textarea, "enabled", enabled);
}
public void changeBorder(boolean enabled, Object textarea) {
setBoolean(textarea, "border", enabled);
}
Object dialog;
/**
*
*/
public void showDialog() {
try {
if (dialog == null) {
dialog = parse("demodialog.xml");
}
add(dialog);
} catch (Exception exc) { exc.printStackTrace(); }
}
/**
*
*/
public void findText(Object combobox, String what,
boolean match, boolean down) {
closeDialog();
if (what.length() == 0) { return; }
boolean cacheditem = false;
for (int i = getCount(combobox) - 1; i >= 0; i--) {
String choicetext = getString(getItem(combobox, i), "text");
if (what.equals(choicetext)) { cacheditem = true; break; }
}
if (!cacheditem) {
Object choice = create("choice");
setString(choice, "text", what);
add(combobox, choice);
}
Object textarea = find("textarea");
int end = getInteger(textarea, "end");
String text = getString(textarea, "text");
if (!match) {
what = what.toLowerCase();
text = text.toLowerCase();
}
int index = text.indexOf(what, down ? end : 0);
if (!down && (index != -1) && (index >= end)) { index = -1; }
if (index != -1) {
setInteger(textarea, "start", index);
setInteger(textarea, "end", index + what.length());
requestFocus(textarea);
}
else {
getToolkit().beep();
}
}
/**
*
*/
public void closeDialog() {
remove(dialog);
}
/**
*
*/
public void insertList(Object list) {
Object item = create("item");
setString(item, "text", "New item");
setIcon(item, "icon", getIcon("/icons/bookmarks.gif"));
add(list, item, 0);
}
/**
*
*/
public void deleteList(Object delete, Object list) {
for (int i = getCount(list) - 1; i >= 0; i--) {
Object item = getItem(list, i);
if (getBoolean(item, "selected")) {
remove(item);
}
}
setBoolean(delete, "enabled", false);
}
/**
*
*/
public void changeSelection(Object list, Object delete) {
setBoolean(delete, "enabled", getSelectedIndex(list) != -1);
}
/**
*
*/
public void setSelection(Object list, String selection, Object delete) {
for (int i = getCount(list) - 1; i >= 0; i--) {
setBoolean(getItem(list, i), "selected", false);
}
setChoice(list, "selection", selection);
setBoolean(delete, "enabled", false);
}
/**
*
*/
public void sliderChanged(int value, Object spinbox) {
setString(spinbox, "text", String.valueOf(value));
hsbChanged();
}
/**
*
*/
public void spinboxChanged(String text, Object slider) {
try {
int value = Integer.parseInt(text);
if ((value >= 0) && (value <= 255)) {
setInteger(slider, "value", value);
hsbChanged();
}
} catch (NumberFormatException nfe) { getToolkit().beep(); }
}
private Object sl_red, sl_green, sl_blue;
private Object tf_hue, tf_saturation, tf_brightness;
private Object pb_hue, pb_saturation, pb_brightness;
private Object rgb_label;
/**
*
*/
public void storeWidgets(Object sl_red, Object sl_green, Object sl_blue,
Object tf_hue, Object tf_saturation, Object tf_brightness,
Object pb_hue, Object pb_saturation, Object pb_brightness,
Object rgb_label) {
this.sl_red = sl_red;
this.sl_green = sl_green;
this.sl_blue = sl_blue;
this.tf_hue = tf_hue;
this.tf_saturation = tf_saturation;
this.tf_brightness = tf_brightness;
this.pb_hue = pb_hue;
this.pb_saturation = pb_saturation;
this.pb_brightness = pb_brightness;
this.rgb_label = rgb_label;
actionTheme("t0");
}
/**
*
*/
private void hsbChanged() {
int red = getInteger(sl_red, "value");
int green = getInteger(sl_green, "value");
int blue = getInteger(sl_blue, "value");
float[] hsb = Color.RGBtoHSB(red, green, blue, null);
setColor(rgb_label, "background", new Color(red, green, blue));
setString(tf_hue, "text", String.valueOf(hsb[0]));
setString(tf_saturation, "text", String.valueOf(hsb[1]));
setString(tf_brightness, "text", String.valueOf(hsb[2]));
setInteger(pb_hue, "value", (int) (100f * hsb[0]));
setInteger(pb_saturation, "value", (int) (100f * hsb[1]));
setInteger(pb_brightness, "value", (int) (100f * hsb[2]));
}
}
--- NEW FILE: calculator.xml ---
<panel gap="4" top="4" left="4">
<textfield name="number1" columns="4" />
<label text="+" />
<textfield name="number2" columns="4" />
<button text="=" action="calculate(number1.text, number2.text, result)" />
<textfield name="result" editable="false" />
</panel>
--- NEW FILE: demo.xml ---
<?xml version="1.0" encoding="ISO-8859-1"?>
<panel columns="1" gap="4">
<menubar weightx="1">
<menu text="File" mnemonic="0">
<menuitem text="New" icon="/icons/new.gif" mnemonic="0" />
<menuitem text="Open..." icon="/icons/open.gif" />
<menuitem text="Save" icon="/icons/save.gif" />
<menuitem text="Save As..." icon="/icons/saveas.gif" />
<separator />
<menuitem text="Page Setup" icon="/icons/pagesetup.gif" />
<menuitem text="Print" icon="/icons/print.gif" />
<separator />
<menuitem text="Exit" />
</menu>
<menu text="Edit">
<menuitem text="Undo" icon="/icons/undo.gif" />
<separator />
<menuitem text="Cut" icon="/icons/cut.gif" />
<menuitem text="Copy" icon="/icons/copy.gif" />
<menuitem text="Paste" icon="/icons/paste.gif" />
<menuitem text="Delete" icon="/icons/delete.gif" />
<separator />
<menuitem text="Select All" />
</menu>
<menu text="Search">
<menuitem text="Find..." icon="/icons/find.gif" />
<menuitem text="Find Next" icon="/icons/findagain.gif" />
</menu>
<menu text="Theme">
<checkboxmenuitem text="XP" name="t0" group="theme" action="actionTheme(this.name)" selected="true"/>
<checkboxmenuitem text="Gray" name="t1" group="theme" action="actionTheme(this.name)"/>
<checkboxmenuitem text="Sandstone" name="t2" group="theme" action="actionTheme(this.name)"/>
<checkboxmenuitem text="Ocean" name="t3" group="theme" action="actionTheme(this.name)"/>
</menu>
<menu text="Help">
<menuitem text="Help Topics" icon="/icons/help.gif" />
<separator />
<menuitem text="About" icon="/icons/about.gif" />
</menu>
</menubar>
<tabbedpane selected="1" weightx="1" weighty="1">
<tab text="Texts">
<panel columns="2" top="4" left="4" bottom="4" right="4" gap="4">
<panel columns="1" gap="2">
<label text="Find in the text:" mnemonic="10" />
<button name="b_finddialog" text="Search" tooltip="Search..."
icon="/icons/find.gif" mnemonic="1" action="showDialog()" />
</panel>
<panel columns="2" left="10" gap="4">
<checkbox name="cb_editable" text="Editable" mnemonic="4"
selected="true" action="changeEditable(this.selected, textarea)" />
<checkbox name="cb_enabled" text="Enabled" selected="true"
action="changeEnabled(this.selected, textarea)" />
<checkbox name="cb_border" text="Border" selected="true"
action="changeBorder(this.selected, textarea)" />
</panel>
<textarea name="textarea" init="loadText(this)" colspan="2" halign="fill" weightx="1" weighty="1" />
</panel>
</tab>
<tab text="Lists" mnemonic="0">
<panel columns="1" top="4" left="4" bottom="4" right="4" gap="4">
<panel gap="4">
<label text="Update list:" />
<button icon="/icons/new.gif" tooltip="Add new item" action="insertList(list)" />
<button name="delete" icon="/icons/delete.gif"
tooltip="Delete selected items" action="deleteList(this, list)" />
<label text=" & selection:" />
<combobox name="selection" selected="2" editable="false"
valign="center" action="setSelection(list, this.text, delete)">
<choice text="single" />
<choice text="interval" />
<choice text="multiple" />
</combobox>
</panel>
<splitpane orientation="vertical" divider="100" weightx="1" weighty="1">
<splitpane divider="120">
<list name="list" selection="multiple" action="changeSelection(this, delete)"
colspan="4" weightx="1" weighty="1">
<popupmenu>
<menuitem text="one"/>
<menuitem text="two"/>
<separator/>
<menuitem text="three"/>
</popupmenu>
<item text="List item A" />
<item text="List item B" icon="/icons/bookmarks.gif" selected="true" />
<item text="List item C" enabled="false" />
<item text="List item D" icon="/icons/bookmarks.gif" />
<item text="List item E" />
<item text="List item F" />
<item text="List item G" />
</list>
<tree selection="multiple">
<popupmenu>
<menuitem text="one"/>
<menuitem text="two"/>
<separator/>
<menuitem text="three"/>
</popupmenu>
<node text="Tree node A" icon="/icons/open.gif">
<node text="Tree node" icon="/icons/open.gif" selected="true">
<node text="Tree node" icon="/icons/new.gif" />
</node>
</node>
<node text="Tree node B" enabled="false" icon="/icons/new.gif" />
<node text="Tree node C" icon="/icons/open.gif" expanded="false">
<node text="Tree node C1" icon="/icons/new.gif" />
</node>
</tree>
</splitpane>
<table selection="multiple">
<popupmenu>
<menuitem text="one"/>
<menuitem text="two"/>
<separator/>
<menuitem text="three"/>
</popupmenu>
<header>
<column text="Column" icon="/icons/bookmarks.gif" width="120" />
<column text="Column" alignment="center" />
<column text="Column" alignment="center" />
</header>
<row>
<cell text="Cell" />
<cell text="Cell" />
<cell text="Cell" icon="/icons/bookmarks.gif" />
</row>
<row selected="true">
<cell text="Cell" icon="/icons/bookmarks.gif" />
<cell text="Cell" />
<cell text="Cell" />
</row>
<row>
<cell text="Cell" />
<cell text="Cell" icon="/icons/bookmarks.gif" enabled="false" />
<cell text="Cell" />
</row>
</table>
</splitpane>
</panel>
</tab>
<tab text="Values">
<panel columns="3" top="4" left="4" gap="4"
init="storeWidgets(sl_red, sl_green, sl_blue, tf_hue, tf_saturation, tf_brightness, pb_hue, pb_saturation, pb_brightness, rgb_label)">
<label text="Red, green, and blue values" colspan="3" />
<label text="Red:" alignment="right" />
<slider name="sl_red" maximum="255" valign="center"
action="sliderChanged(this.value, sb_red)" />
<spinbox name="sb_red" maximum="255" text="0" columns="3"
action="spinboxChanged(this.text, sl_red)" />
<label text="Green:" alignment="right" />
<slider name="sl_green" maximum="255" valign="center"
action="sliderChanged(this.value, sb_green)" />
<spinbox name="sb_green" maximum="255" text="0" columns="3"
action="spinboxChanged(this.text, sl_green)" />
<label text="Blue:" alignment="right" />
<slider name="sl_blue" maximum="255" valign="center"
action="sliderChanged(this.value, sb_blue)" />
<spinbox name="sb_blue" maximum="255" text="0" columns="3"
action="spinboxChanged(this.text, sl_blue)" />
<separator colspan="3" />
<label text="Hue, saturation, and brightness values" colspan="3" />
<label text="Hue:" alignment="right" />
<textfield name="tf_hue" text="0.0" editable="false" />
<progressbar name="pb_hue" valign="center" />
<label text="Saturation:" alignment="right" />
<textfield name="tf_saturation" text="0.0" editable="false" />
<progressbar name="pb_saturation" valign="center" />
<label text="Brightness:" alignment="right" />
<textfield name="tf_brightness" text="0.0" editable="false" />
<progressbar name="pb_brightness" valign="center" />
<separator colspan="3"/>
<label colspan="3" name="rgb_label" valign="fill" weighty="1" background="#000000"/>
</panel>
</tab>
<tab text="Other">
<panel halign="fill" weightx="1" valign="fill" weighty="1" gap="4" top="4" columns="3">
<panel halign="fill" weightx="1" columns="1" gap="2">
<label text="Sidebar widget:"/>
<tabbedpane placement="stacked" halign="fill" weightx="1" valign="fill" weighty="1">
<popupmenu>
<menuitem text="one"/>
<menuitem text="two"/>
<separator/>
<menuitem text="three"/>
</popupmenu>
<tab text="One" property="c=123;v=345">
<panel columns="1" gap="4">
<textarea valign="fill" weighty="1" weightx="1" halign="fill" border="false" wrap="true" editable="false" text="Popup menus are supported in many components. Try to right-click on the tab name. Try it on lists, trees and tables."/>
</panel>
</tab>
<tab text="Two" font="bold 14">
<panel scrollable="true" columns="1" gap="4">
<label text="This is a scrollable panel."/>
<label text="Custom colors/fonts:"/>
<button text="abcdef" font="18" background="#888888"/>
<button text="abcdef" font="Courier 17" background="#882288"/>
<button text="abcdef" font="Serif 16" background="#228888"/>
<button text="abcdef" font="bold 15" background="#888822"/>
<button text="abcdef" font="italic 14" background="#448844"/>
<button text="abcdef" font="bold italic 13" background="#444488"/>
<button text="abcdef" font="12" background="#884444"/>
<button text="abcdef" font="11" background="#2288ff"/>
<button text="abcdef" font="10" background="#ff2288"/>
</panel>
</tab>
<tab text="Three" font="bold" foreground="#0000ff">
<panel columns="1" gap="4">
<menubar halign="fill" weightx="1">
<menu text="File">
<menuitem text="one"/>
</menu>
</menubar>
<textarea wrap="true" valign="fill" weighty="1" halign="fill" weightx="1" border="false" editable="false" text="Menus can be placed on any panel or dialog."/>
</panel>
</tab>
<tab text="Four" font="Serif bold 14" background="#8888ff" foreground="#ffffff">
<panel columns="1" gap="4">
<textarea valign="fill" weighty="1" weightx="1" halign="fill" border="false" wrap="true" editable="false" font="Serif italic bold 14" foreground="#88ff88" background="#446600" text="Most components support custom background / foreground colors and font settings"/>
</panel>
</tab>
</tabbedpane>
</panel>
<separator/>
<panel weighty="1" weightx="1" halign="fill" valign="fill" columns="1" gap="2">
<textarea halign="fill" weightx="1" border="false" editable="false" text="Border-less textareas can be used as multiline labels." wrap="true"/>
<label text="Link widget:"/>
<button type="link" tooltip="Link widget" halign="left" text="www.thinlet.com"/>
</panel>
<separator colspan="3"/>
<panel colspan="3" halign="fill" weightx="1">
<menubar placement="top">
<menu text="Start" font="bold 13" tooltip="Click here to begin">
<menuitem text="Thinlet Home"/>
<separator/>
<menu text="Programs">
<menu text="Accessories">
<menuitem text="Thinlet!"/>
<menuitem text="Thinlet!"/>
<menuitem text="Thinlet!"/>
</menu>
<menuitem text="Amazon Browser"/>
<menuitem text="Calculator"/>
</menu>
<menu text="Documents">
<menuitem text="one"/>
<menuitem text="two"/>
<menuitem text="three"/>
</menu>
<menu text="Settings">
<menuitem text="Control Panel"/>
<separator/>
<menu text="Network & Dialup">
<menuitem text="Thinlet!"/>
</menu>
<menuitem text="Printers"/>
<menuitem text="Taskbar & Start Menu..."/>
</menu>
<menu text="Search">
<menuitem text="one"/>
</menu>
<menuitem text="Help"/>
<menuitem text="Run..."/>
<separator/>
<menuitem text="Shut Down..."/>
</menu>
</menubar>
<separator/>
<label text=" Unfold-up menus are supported."/>
</panel>
</panel>
</tab>
</tabbedpane>
</panel>
--- NEW FILE: demodialog.xml ---
<?xml version="1.0" encoding="ISO-8859-1"?>
<dialog text="Find" icon="/icons/find.gif" modal="true"
columns="4" top="4" left="4" bottom="4" right="4" gap="4">
<label text="Find what:" />
<combobox name="ch_what" colspan="2" valign="center" />
<button name="b_find" text="Find next"
action="findText(ch_what, ch_what.text, cb_match.selected, rb_down.selected)" />
<label text="Direction:" alignment="right" />
<checkbox name="rb_up" text="Up" group="direction" />
<checkbox name="cb_match" text="Match case" selected="true" />
<button name="b_cancel" text="Cancel" action="closeDialog" />
<label />
<checkbox name="rb_down" text="Down" group="direction" selected="true" />
</dialog>
--- NEW FILE: manifest.mf ---
Main-Class: thinlet.examples.demo.Demo
|