001: package thinlet.demo;
002:
003: import java.awt.*;
004: import java.io.*;
005:
006: import thinlet.*;
007:
008: /**
009: * Simple demonstration of widgets and events
010: */
011: public class Demo extends Thinlet {
012:
013: /**
014: * Loads the xml file
015: */
016: public Demo() throws Exception {
017: add(parse("demo.xml"));
018: }
019:
020: /**
021: * Creates a frame including this thinlet demo
022: */
023: public static void main(String[] args) throws Exception {
024: new FrameLauncher("Demo", new Demo(), 320, 320);
025: }
026:
027: /**
028: * Called if the demo.xml was loaded,
029: * it fills the textarea from a resource file
030: */
031: public void loadText(Object textarea) throws Exception {
032: BufferedReader reader = new BufferedReader(
033: new InputStreamReader(getClass().getResourceAsStream(
034: "demodialog.xml")));
035: StringBuffer text = new StringBuffer();
036: for (int c = reader.read(); c != -1; c = reader.read()) {
037: if (((c > 0x1f) && (c < 0x7f))
038: || ((c > 0x9f) && (c < 0xffff)) || (c == '\n')) {
039: text.append((char) c);
040: } else if (c == '\t') {
041: text.append(" ");
042: }
043: }
044: reader.close();
045: setString(textarea, "text", text.toString());
046: }
047:
048: /**
049: * Updates textarea's editable property depending on a checkbox state
050: */
051: public void changeEditable(boolean editable, Object textarea) {
052: setBoolean(textarea, "editable", editable);
053: }
054:
055: /**
056: * Updates textarea's enabled property
057: */
058: public void changeEnabled(boolean enabled, Object textarea) {
059: setBoolean(textarea, "enabled", enabled);
060: }
061:
062: Object dialog;
063:
064: /**
065: * Shows the modal find dialog, creates only one dialog instance
066: */
067: public void showDialog() throws Exception {
068: if (dialog == null) {
069: dialog = parse("demodialog.xml");
070: }
071: add(dialog);
072: }
073:
074: /**
075: * Updates the textarea's selection range,
076: * and add the search string to the history
077: */
078: public void findText(Object combobox, String what, boolean match,
079: boolean down) {
080: closeDialog();
081: if (what.length() == 0) {
082: return;
083: }
084:
085: boolean cacheditem = false;
086: for (int i = getCount(combobox) - 1; i >= 0; i--) {
087: String choicetext = getString(getItem(combobox, i), "text");
088: if (what.equals(choicetext)) {
089: cacheditem = true;
090: break;
091: }
092: }
093: if (!cacheditem) {
094: Object choice = create("choice");
095: setString(choice, "text", what);
096: add(combobox, choice);
097: }
098:
099: Object textarea = find("textarea");
100: int end = getInteger(textarea, "end");
101: String text = getString(textarea, "text");
102:
103: if (!match) {
104: what = what.toLowerCase();
105: text = text.toLowerCase();
106: }
107:
108: int index = text.indexOf(what, down ? end : 0);
109: if (!down && (index != -1) && (index >= end)) {
110: index = -1;
111: }
112: if (index != -1) {
113: setInteger(textarea, "start", index);
114: setInteger(textarea, "end", index + what.length());
115: requestFocus(textarea);
116: } else {
117: getToolkit().beep();
118: }
119: }
120:
121: /**
122: * Closes the dialog
123: */
124: public void closeDialog() {
125: remove(dialog);
126: }
127:
128: /**
129: * Insert a new item into the list
130: */
131: public void insertList(Object list) {
132: Object item = create("item");
133: setString(item, "text", "New item");
134: setIcon(item, "icon", getIcon("/icon/library.gif"));
135: add(list, item, 0);
136: // System.out.println("> click " + System.currentTimeMillis());
137: // try { Thread.sleep(5000); } catch (InterruptedException ie) {}
138: }
139:
140: /**
141: * Removes the selected items from the list
142: */
143: public void deleteList(Object delete, Object list) {
144: for (int i = getCount(list) - 1; i >= 0; i--) {
145: Object item = getItem(list, i);
146: if (getBoolean(item, "selected")) {
147: remove(item);
148: }
149: }
150: setBoolean(delete, "enabled", false);
151: }
152:
153: /**
154: * Delete button's state depends on the list selection
155: */
156: public void changeSelection(Object list, Object delete) {
157: setBoolean(delete, "enabled", getSelectedIndex(list) != -1);
158: }
159:
160: /**
161: * Clears list selection and updates the selection model
162: */
163: public void setSelection(Object list, String selection,
164: Object delete) {
165: for (int i = getCount(list) - 1; i >= 0; i--) {
166: setBoolean(getItem(list, i), "selected", false);
167: }
168: setChoice(list, "selection", selection);
169: setBoolean(delete, "enabled", false);
170: }
171:
172: /**
173: *
174: */
175: public void sliderChanged(int value, Object spinbox) {
176: setString(spinbox, "text", String.valueOf(value));
177: hsbChanged();
178: }
179:
180: /**
181: *
182: */
183: public void spinboxChanged(String text, Object slider) {
184: try {
185: int value = Integer.parseInt(text);
186: if ((value >= 0) && (value <= 255)) {
187: setInteger(slider, "value", value);
188: hsbChanged();
189: }
190: } catch (NumberFormatException nfe) {
191: getToolkit().beep();
192: }
193: }
194:
195: private Object sl_red, sl_green, sl_blue;
196: private Object tf_hue, tf_saturation, tf_brightness;
197: private Object pb_hue, pb_saturation, pb_brightness;
198: private Object rgb_label;
199:
200: /**
201: *
202: */
203: public void storeWidgets(Object sl_red, Object sl_green,
204: Object sl_blue, Object tf_hue, Object tf_saturation,
205: Object tf_brightness, Object pb_hue, Object pb_saturation,
206: Object pb_brightness, Object rgb_label) {
207: this .sl_red = sl_red;
208: this .sl_green = sl_green;
209: this .sl_blue = sl_blue;
210: this .tf_hue = tf_hue;
211: this .tf_saturation = tf_saturation;
212: this .tf_brightness = tf_brightness;
213: this .pb_hue = pb_hue;
214: this .pb_saturation = pb_saturation;
215: this .pb_brightness = pb_brightness;
216: this .rgb_label = rgb_label;
217: }
218:
219: /**
220: *
221: */
222: private void hsbChanged() {
223: int red = getInteger(sl_red, "value");
224: int green = getInteger(sl_green, "value");
225: int blue = getInteger(sl_blue, "value");
226:
227: float[] hsb = Color.RGBtoHSB(red, green, blue, null);
228:
229: setString(tf_hue, "text", String.valueOf(hsb[0]));
230: setString(tf_saturation, "text", String.valueOf(hsb[1]));
231: setString(tf_brightness, "text", String.valueOf(hsb[2]));
232:
233: setInteger(pb_hue, "value", (int) (100f * hsb[0]));
234: setInteger(pb_saturation, "value", (int) (100f * hsb[1]));
235: setInteger(pb_brightness, "value", (int) (100f * hsb[2]));
236:
237: setColor(rgb_label, "background", new Color(red, green, blue));
238: setColor(rgb_label, "foreground", new Color(255 - red,
239: 255 - green, 255 - blue));
240: }
241: }
|