1 // Copyright 2002 Adam Megacz, see the COPYING file for licensing [GPL]
11 * A helper class for properties of Box which require special
14 * To avoid excessive use of String.equals(), the Box.get() and
15 * Box.put() methods employ a Hash keyed on property names that
16 * require special handling. The value stored in the Hash is an
17 * instance of an anonymous subclass of SpecialBoxProperty, which knows
18 * how to handle get()s and put()s for that property name. There
19 * should be one anonymous subclass of SpecialBoxProperty for each
20 * specially-handled property on Box.
22 class SpecialBoxProperty {
24 SpecialBoxProperty() { }
25 private static final int NUMINTS = Box.NUMINTS;
26 private static final int dmax = Box.dmax;
27 private static final int dmin = Box.dmin;
28 private static final int cmin = Box.cmin;
29 private static final int abs = Box.abs;
30 private static final int pos = Box.pos;
31 private static final int size = Box.size;
32 private static final int oldpos = Box.oldpos;
33 private static final int oldsize = Box.oldsize;
34 private static final int pad = Box.pad;
36 /** stores instances of SpecialBoxProperty; keyed on property name */
37 static Hash specialBoxProperties = new Hash(200, 3);
39 /** this method defines the behavior when the property is get()ed from b */
40 Object get(Box b) { return null; }
42 /** this method defines the behavior when the property is put() to b */
43 void put(Box b, Object value) { }
45 /** this method defines the behavior when the property is put() to b, allows a single SpecialBoxProperty to serve multiple properties */
46 void put(String name, Box b, Object value) { put(b, value); }
48 // 'ye olde IBM CGA colours', as defined in the XWT reference
49 static final int black = 0xFF000000;
50 static final int blue = 0xFF000088;
51 static final int green = 0xFF008800;
52 static final int cyan = 0xFF008888;
53 static final int red = 0xFF880000;
54 static final int magenta = 0xFF880088;
55 static final int brown = 0xFF888800;
56 static final int lightGray = 0xFFBDBEBD;
57 static final int darkGray = 0xFF242424;
58 static final int brightBlue = 0xFF0000FF;
59 static final int brightGreen = 0xFF00FF00;
60 static final int brightCyan = 0xFF00FFFF;
61 static final int brightRed = 0xFFFF0000;
62 static final int pink = 0xFFFF00FF;
63 static final int yellow = 0xFFFFFF00;
64 static final int white = 0xFFFFFFFF;
68 specialBoxProperties.put("color", new SpecialBoxProperty() {
69 public Object get(Box b) {
70 if ((b.color & 0xFF000000) == 0) return null;
71 String red = Integer.toHexString((b.color & 0x00FF0000) >> 16);
72 String green = Integer.toHexString((b.color & 0x0000FF00) >> 8);
73 String blue = Integer.toHexString(b.color & 0x000000FF);
74 if (red.length() < 2) red = "0" + red;
75 if (blue.length() < 2) blue = "0" + blue;
76 if (green.length() < 2) green = "0" + green;
77 return "#" + red + green + blue;
79 public void put(Box b, Object value) {
80 int newcolor = b.color;
81 String s = value == null ? null : value.toString();
82 if (value == null) newcolor = 0x00000000;
83 else if (s.length() > 0 && s.charAt(0) == '#')
85 newcolor = 0xFF000000 |
86 (Integer.parseInt(s.substring(1, 3), 16) << 16) |
87 (Integer.parseInt(s.substring(3, 5), 16) << 8) |
88 Integer.parseInt(s.substring(5, 7), 16);
89 } catch (NumberFormatException e) {
90 Log.log(this, "invalid color " + s);
93 else if (s.equals("black")) newcolor = black;
94 else if (s.equals("blue")) newcolor = blue;
95 else if (s.equals("green")) newcolor = green;
96 else if (s.equals("cyan")) newcolor = cyan;
97 else if (s.equals("red")) newcolor = red;
98 else if (s.equals("magenta")) newcolor = magenta;
99 else if (s.equals("brown")) newcolor = brown;
100 else if (s.equals("lightGray")) newcolor = lightGray;
101 else if (s.equals("darkGray")) newcolor = darkGray;
102 else if (s.equals("brightBlue")) newcolor = brightBlue;
103 else if (s.equals("brightGreen")) newcolor = brightGreen;
104 else if (s.equals("brightCyan")) newcolor = brightCyan;
105 else if (s.equals("brightRed")) newcolor = brightRed;
106 else if (s.equals("pink")) newcolor = pink;
107 else if (s.equals("yellow")) newcolor = yellow;
108 else if (s.equals("white")) newcolor = white;
109 else if (Log.on) Log.log(this, "invalid color \"" + s + "\" at " + JS.getCurrentFunctionSourceName());
110 if (newcolor == b.color) return;
116 specialBoxProperties.put("textcolor", new SpecialBoxProperty() {
117 public Object get(Box b) {
118 if ((b.textcolor & 0xFF000000) == 0) return null;
119 String red = Integer.toHexString((b.textcolor & 0x00FF0000) >> 16);
120 String green = Integer.toHexString((b.textcolor & 0x0000FF00) >> 8);
121 String blue = Integer.toHexString(b.textcolor & 0x000000FF);
122 if (red.length() < 2) red = "0" + red;
123 if (blue.length() < 2) blue = "0" + blue;
124 if (green.length() < 2) green = "0" + green;
125 return "#" + red + green + blue;
127 public void put(Box b, Object value) {
128 int newtextcolor = b.color;
129 if (value == null) return;
130 String s = value.toString();
131 if (s.length() > 0 && s.charAt(0) == '#')
132 newtextcolor = 0xFF000000 |
133 (Integer.parseInt(s.substring(1, 3), 16) << 16) |
134 (Integer.parseInt(s.substring(3, 5), 16) << 8) |
135 Integer.parseInt(s.substring(5, 7), 16);
136 else if (s.equals("black")) newtextcolor = black;
137 else if (s.equals("blue")) newtextcolor = blue;
138 else if (s.equals("green")) newtextcolor = green;
139 else if (s.equals("cyan")) newtextcolor = cyan;
140 else if (s.equals("red")) newtextcolor = red;
141 else if (s.equals("magenta")) newtextcolor = magenta;
142 else if (s.equals("brown")) newtextcolor = brown;
143 else if (s.equals("lightGray")) newtextcolor = lightGray;
144 else if (s.equals("darkGray")) newtextcolor = darkGray;
145 else if (s.equals("brightBlue")) newtextcolor = brightBlue;
146 else if (s.equals("brightGreen")) newtextcolor = brightGreen;
147 else if (s.equals("brightCyan")) newtextcolor = brightCyan;
148 else if (s.equals("brightRed")) newtextcolor = brightRed;
149 else if (s.equals("pink")) newtextcolor = pink;
150 else if (s.equals("yellow")) newtextcolor = yellow;
151 else if (s.equals("white")) newtextcolor = white;
152 else if (Log.on) Log.log(this, "invalid color \"" + s + "\" at " + JS.getCurrentFunctionSourceName());
153 if (newtextcolor == b.textcolor) return;
154 b.textcolor = newtextcolor;
158 specialBoxProperties.put("text", new SpecialBoxProperty() {
159 public Object get(Box b) { return b.text; }
160 public void put(Box b, Object value) {
161 String t = value == null ? "null" : value.toString();
162 if (t.equals(b.text)) return;
164 for(int i=0; i<t.length(); i++)
165 if (Character.isISOControl(t.charAt(i))) {
166 if (Log.on) Log.log(this,
167 "ISO Control characters are not permitted in box text strings; offending character is ASCII " +
172 // FEATURE: try removing the following line; it appears to be redundant
179 specialBoxProperties.put("thisbox", new SpecialBoxProperty() {
180 public Object get(Box b) { return b; }
181 public void put(Box b, Object value) {
182 if (value == null) b.remove();
183 else if (value.equals("window") || value.equals("frame")) {
184 if (b.redirect != b && Log.on)
185 Log.log(this, "WARNING: you have created a surface whose root box's redirect is not itself " +
186 "-- this isn't usually a good idea");
187 Platform.createSurface(b, value.equals("frame"), true);
188 } else if (Log.on) Log.log(this, "put invalid value to 'thisbox' property: " + value);
192 specialBoxProperties.put("orient", new SpecialBoxProperty() {
193 public Object get(Box b) {
194 if (b.redirect == null) return "horizontal";
195 else if (b.redirect != b) return get(b.redirect);
196 else if (b.o == 1) return "vertical";
197 else return "horizontal";
199 public void put(Box b, Object value) {
200 if (value == null) return;
201 if (b.redirect == null) return;
202 if (b.redirect != b) {
203 put(b.redirect, value);
206 if (value.equals("vertical")) {
207 if (b.o == 1) return;
210 } else if (value.equals("horizontal")) {
211 if (b.o == 0) return;
215 Log.log(this, "invalid value put to orient property: " + value);
216 b.mark_for_prerender();
217 b.sync_cmin_to_children();
220 specialBoxProperties.put("static", new SpecialBoxProperty() {
221 public Object get(Box b) {
222 JS.Function cf = JS.getCurrentFunction();
223 String cfsn = cf.getSourceName();
224 for(int i=0; i<cfsn.length() - 1; i++)
225 if (cfsn.charAt(i) == '.' && (cfsn.charAt(i+1) == '_' || Character.isDigit(cfsn.charAt(i+1)))) {
226 cfsn = cfsn.substring(0, i);
229 return Static.getStatic(cfsn);
231 public void put(Box b, Object value) { }
234 specialBoxProperties.put("sizetoimage", new SpecialBoxProperty() {
235 public Object get(Box b) { return b.sizetoimage ? Boolean.TRUE : Boolean.FALSE; }
236 public void put(Box b, Object value) {
238 if (b.sizetoimage) return;
239 b.sizetoimage = true;
242 b.sizetoimage = false;
246 specialBoxProperties.put("fixedaspect", new SpecialBoxProperty() {
247 public Object get(Box b) { return b.sizetoimage ? Boolean.TRUE : Boolean.FALSE; }
248 public void put(Box b, Object value) {
249 boolean newval = stob(value);
250 if (newval == b.fixedaspect) return;
251 b.fixedaspect = newval;
255 specialBoxProperties.put("shrink", new SpecialBoxProperty() {
256 public Object get(Box b) { return (b.vshrink && b.hshrink) ? Boolean.TRUE : Boolean.FALSE; }
257 public void put(Box b, Object value) {
258 boolean newshrink = stob(value);
259 if (b.hshrink == newshrink && b.vshrink == newshrink) return;
260 b.hshrink = b.vshrink = newshrink;
261 b.mark_for_prerender();
264 specialBoxProperties.put("hshrink", new SpecialBoxProperty() {
265 public Object get(Box b) { return new Boolean(b.hshrink); }
266 public void put(Box b, Object value) {
267 boolean newshrink = stob(value);
268 if (b.hshrink == newshrink) return;
269 b.hshrink = newshrink;
270 b.mark_for_prerender();
274 specialBoxProperties.put("vshrink", new SpecialBoxProperty() {
275 public Object get(Box b) { return b.vshrink ? Boolean.TRUE : Boolean.FALSE; }
276 public void put(Box b, Object value) {
277 boolean newshrink = stob(value);
278 if (b.vshrink == newshrink) return;
279 b.vshrink = newshrink;
280 b.mark_for_prerender();
284 specialBoxProperties.put("x", new SpecialBoxProperty() {
285 public Object get(Box b) {
286 if (b.surface == null) return new Integer(0);
287 if (b.invisible) return new Integer(0);
288 return new Integer(b.abs(0));
290 public void put(Box b, Object value) {
291 if (b.getParent() == null && b.surface != null) {
292 b.surface.setLocation(stoi(value), b.abs(1));
293 b.surface.centerSurfaceOnRender = false;
295 b.set(abs, 0, stoi(value));
299 specialBoxProperties.put("y", new SpecialBoxProperty() {
300 public Object get(Box b) {
301 if (b.surface == null) return new Integer(0);
302 if (b.invisible) return new Integer(0);
303 return new Integer(b.abs(1));
305 public void put(Box b, Object value) {
306 if (b.getParent() == null && b.surface != null) {
307 b.surface.setLocation(b.abs(0), stoi(value));
308 b.surface.centerSurfaceOnRender = false;
310 b.set(abs, 1, stoi(value));
314 specialBoxProperties.put("width", new SpecialBoxProperty() {
315 public Object get(Box b) { return new Integer(b.size(0)); }
316 public void put(Box b, Object value) {
317 if (b.sizetoimage) return;
318 if (b.getParent() == null && b.surface != null) {
319 b.set(size, 0, Box.max(Surface.scarPicture.getWidth(), stoi(value)));
320 b.mark_for_prerender();
322 b.set(dmax, 0, stoi(value));
323 b.set(dmin, 0, stoi(value));
327 specialBoxProperties.put("height", new SpecialBoxProperty() {
328 public Object get(Box b) { return new Integer(b.size(1)); }
329 public void put(Box b, Object value) {
330 if (b.sizetoimage) return;
331 if (b.getParent() == null && b.surface != null) {
332 b.set(size, 1, Box.max(Surface.scarPicture.getHeight(), stoi(value)));
333 b.mark_for_prerender();
335 b.set(dmax, 1, stoi(value));
336 b.set(dmin, 1, stoi(value));
340 specialBoxProperties.put("flex", new SpecialBoxProperty() {
341 public Object get(Box b) { return new Double(b.flex); }
342 public void put(Box b, Object value) {
343 if (value == null) return;
344 int newflex = stoi(value);
345 if (newflex == b.flex) return;
347 b.mark_for_prerender();
350 specialBoxProperties.put("tile", new SpecialBoxProperty() {
351 public Object get(Box b) { return b.tile ? Boolean.TRUE : Boolean.FALSE; }
352 public void put(Box b, Object value) {
353 boolean newtile = stob(value);
354 if (newtile == b.tile) return;
359 specialBoxProperties.put("align", new SpecialBoxProperty() {
360 public Object get(Box b) {
361 if (b.align == -1) return "topleft";
362 else if (b.align == 1) return "bottomright";
363 else return "center";
365 public void put(Box b, Object value) {
366 String s = value == null ? "" : value.toString();
367 byte newalign = b.align;
368 if (s.equals("topleft")) newalign = -1;
369 else if (s.equals("bottomright")) newalign = 1;
370 else if (s.equals("center")) newalign = 0;
371 else if (Log.on) Log.log(this, "invalid value put to align property: " + value);
372 if (newalign == b.align) return;
374 if (b.getParent() != null) b.getParent().mark_for_prerender();
377 specialBoxProperties.put("invisible", new SpecialBoxProperty() {
378 public Object get(Box b) {
379 for (Box cur = b; cur != null; cur = cur.getParent()) { if (cur.invisible) return Boolean.TRUE; }
380 return Boolean.FALSE;
382 public void put(Box b, Object value) {
383 boolean newinvisible = stob(value);
384 if (newinvisible == b.invisible) return;
385 b.invisible = newinvisible;
386 if (b.getParent() == null) {
387 if (b.surface != null) b.surface.setInvisible(newinvisible);
390 b.getParent().mark_for_prerender();
391 b.getParent().sync_cmin_to_children();
392 b.getParent().dirty(b.pos(0), b.pos(1), b.size(0), b.size(1));
393 b.getParent().dirty(b.oldpos(0), b.oldpos(1), b.oldsize(0), b.oldsize(1));
397 specialBoxProperties.put("absolute", new SpecialBoxProperty() {
398 public Object get(Box b) { return b.absolute ? Boolean.TRUE : Boolean.FALSE; }
399 public void put(Box b, Object value) {
400 boolean newabsolute = stob(value);
401 if (newabsolute == b.absolute) return;
402 b.absolute = newabsolute;
403 if (b.getParent() != null) {
404 b.getParent().mark_for_prerender();
405 b.getParent().sync_cmin_to_children();
409 specialBoxProperties.put("image", new SpecialBoxProperty() {
410 public Object get(Box b) { return b.image == null ? null : Box.imageToNameMap.get(b.image); }
411 public void put(Box b, Object value) { b.setImage(value == null ? null : value.toString()); }
414 specialBoxProperties.put("border", new SpecialBoxProperty() {
415 public Object get(Box b) { return b.border == null ? null : Box.imageToNameMap.get(b.border); }
416 public void put(Box b, Object value) { b.setBorder(value == null ? null : value.toString()); }
419 specialBoxProperties.put("font", new SpecialBoxProperty() {
420 public Object get(Box b) { return b.font; }
421 public void put(Box b, Object value) {
422 b.font = value == null ? null : value.toString();
427 specialBoxProperties.put("globalx", new SpecialBoxProperty() {
428 public Object get(Box b) { return new Integer(b.getParent() == null || b.surface == null ? 0 : b.pos(0)); }
429 public void put(Box b, Object value) {
430 if (b.surface == null || b.getParent() == null) return;
431 b.put("x", new Integer(stoi(value) - stoi(get(b.getParent()))));
435 specialBoxProperties.put("globaly", new SpecialBoxProperty() {
436 public Object get(Box b) { return new Integer(b.getParent() == null || b.surface == null ? 0 : b.pos(1)); }
437 public void put(Box b, Object value) {
438 if (b.surface == null || b.getParent() == null) return;
439 b.put("y", new Integer(stoi(value) - stoi(get(b.getParent()))));
443 specialBoxProperties.put("cursor", new SpecialBoxProperty() {
444 public Object get(Box b) { return b.cursor; }
445 public void put(Box b, Object value) {
446 b.cursor = (String)value;
447 if (b.surface == null) return;
449 // see if we need to update the surface cursor
450 String tempcursor = b.surface.cursor;
451 b.Move(b.surface.mousex, b.surface.mousey, b.surface.mousex, b.surface.mousey);
452 if (b.surface.cursor != tempcursor) b.surface.syncCursor();
456 specialBoxProperties.put("mousex", new SpecialBoxProperty() {
457 public Object get(Box b) { return new Integer(b.surface == null ? 0 : b.surface.mousex - b.pos(0)); }
460 specialBoxProperties.put("mousey", new SpecialBoxProperty() {
461 public Object get(Box b) { return new Integer(b.surface == null ? 0 : b.surface.mousey - b.pos(1)); }
464 specialBoxProperties.put("xwt", new SpecialBoxProperty() {
465 public Object get(Box b) { return XWT.singleton; }
468 specialBoxProperties.put("mouseinside", new SpecialBoxProperty() {
469 public Object get(Box b) { return b.mouseinside ? Boolean.TRUE : Boolean.FALSE; }
472 specialBoxProperties.put("numchildren", new SpecialBoxProperty() {
473 public Object get(Box b) {
474 if (b.redirect == null) return new Integer(0);
475 if (b.redirect != b) return get(b.redirect);
476 return new Integer(b.numChildren());
479 SpecialBoxProperty mouseEventHandler = new SpecialBoxProperty() {
480 public void put(String name, Box b, Object value) {
481 if (b.surface == null) return;
482 for(Box c = b.prevSibling(); c != null; c = c.prevSibling()) {
483 Box siblingChild = c.whoIs(c.surface.mousex, c.surface.mousey);
484 if (siblingChild != null) {
485 siblingChild.put(name, value);
489 if (b.getParent() != null)
490 b.getParent().put(name, value);
493 specialBoxProperties.put("Press1", mouseEventHandler);
494 specialBoxProperties.put("Press2", mouseEventHandler);
495 specialBoxProperties.put("Press3", mouseEventHandler);
496 specialBoxProperties.put("Release1", mouseEventHandler);
497 specialBoxProperties.put("Release2", mouseEventHandler);
498 specialBoxProperties.put("Release3", mouseEventHandler);
499 specialBoxProperties.put("Click1", mouseEventHandler);
500 specialBoxProperties.put("Click2", mouseEventHandler);
501 specialBoxProperties.put("Click3", mouseEventHandler);
502 specialBoxProperties.put("DoubleClick1", mouseEventHandler);
503 specialBoxProperties.put("DoubleClick2", mouseEventHandler);
504 specialBoxProperties.put("DoubleClick3", mouseEventHandler);
506 specialBoxProperties.put("root", new SpecialBoxProperty() {
507 public Object get(Box b) {
508 if (b.surface == null) return null;
509 else if (b.getRoot() == null) return null;
510 else if (b.getParent() == null) return b;
511 else return b.getRoot().getRootProxy();
514 specialBoxProperties.put("Minimized", new SpecialBoxProperty() {
515 public Object get(Box b) {
516 if (b.getParent() == null && b.surface != null) return b.surface.minimized ? Boolean.TRUE : Boolean.FALSE;
519 public void put(Box b, Object value) {
520 if (b.surface == null) return;
521 boolean val = stob(value);
522 if (b.getParent() == null && b.surface.minimized != val) b.surface.setMinimized(val);
526 specialBoxProperties.put("Maximized", new SpecialBoxProperty() {
527 public Object get(Box b) {
528 if (b.getParent() == null && b.surface != null) return b.surface.maximized ? Boolean.TRUE : Boolean.FALSE;
531 public void put(Box b, Object value) {
532 if (b.surface == null) return;
533 boolean val = stob(value);
534 if (b.getParent() == null && b.surface.maximized != val) b.surface.setMaximized(val);
538 specialBoxProperties.put("toback", new SpecialBoxProperty() {
539 public void put(Box b, Object value) {
540 if (b.getParent() == null && stob(value) && b.surface != null) b.surface.toBack();
544 specialBoxProperties.put("tofront", new SpecialBoxProperty() {
545 public void put(Box b, Object value) {
546 if (b.getParent() == null && stob(value) && b.surface != null) b.surface.toFront();
550 specialBoxProperties.put("hscar", new SpecialBoxProperty() {
551 public void put(Box b, Object value) {
552 if (b.getParent() == null && b.surface != null) {
553 b.surface.hscar = stoi(value);
554 b.surface.dirty(0, 0, b.surface.width, b.surface.height);
560 specialBoxProperties.put("vscar", new SpecialBoxProperty() {
561 public void put(Box b, Object value) {
562 if (b.getParent() == null && b.surface != null) {
563 b.surface.vscar = stoi(value);
564 b.surface.dirty(0, 0, b.surface.width, b.surface.height);
570 specialBoxProperties.put("Close", new SpecialBoxProperty() {
571 public void put(Box b, Object value) {
572 if (b.getParent() == null && b.surface != null) b.surface.dispose(true);
576 // these are all do-nothings; just to prevent space from getting taken up in the params Hash.
577 specialBoxProperties.put("KeyPressed", new SpecialBoxProperty());
578 specialBoxProperties.put("KeyReleased", new SpecialBoxProperty());
579 specialBoxProperties.put("PosChange", new SpecialBoxProperty());
580 specialBoxProperties.put("SizeChange", new SpecialBoxProperty());
582 specialBoxProperties.put("hpad", new SpecialBoxProperty() {
583 public Object get(Box b) {
584 if (b.redirect == null) return new Integer(0);
585 if (b.redirect != b) return get(b.redirect);
586 return new Integer(b.pad(0));
588 public void put(Box b, Object value) {
589 if (b.redirect == null) return;
590 if (b.redirect != b) { put(b.redirect, value); return; }
591 int newval = stoi(value);
592 if (newval == b.pad(0)) return;
593 b.set(pad, 0, newval);
597 specialBoxProperties.put("vpad", new SpecialBoxProperty() {
598 public Object get(Box b) {
599 if (b.redirect == null) return new Integer(0);
600 if (b.redirect != b) return get(b.redirect);
601 return new Integer(b.pad(1));
603 public void put(Box b, Object value) {
604 if (b.redirect == null) return;
605 if (b.redirect != b) { put(b.redirect, value); return; }
606 int newval = stoi(value);
607 if (newval == b.pad(1)) return;
608 b.set(pad, 1, newval);
612 specialBoxProperties.put("indexof", new SpecialBoxProperty() {
613 public Object get(Box b) { return b.indexof(); }
616 specialBoxProperties.put("minwidth", new SpecialBoxProperty() {
617 public Object get(Box b) { return new Integer(b.dmin(0)); }
618 public void put(Box b, Object value) {
619 if (b.sizetoimage) return;
620 b.set(dmin, 0, stoi(value));
624 specialBoxProperties.put("maxwidth", new SpecialBoxProperty() {
625 public Object get(Box b) { return new Integer(b.dmax(0)); }
626 public void put(Box b, Object value) {
627 if (b.sizetoimage) return;
628 b.set(dmax, 0, stoi(value));
632 specialBoxProperties.put("minheight", new SpecialBoxProperty() {
633 public Object get(Box b) { return new Integer(b.dmin(1)); }
634 public void put(Box b, Object value) {
635 if (b.sizetoimage) return;
636 b.set(dmin, 1, stoi(value));
640 specialBoxProperties.put("maxheight", new SpecialBoxProperty() {
641 public Object get(Box b) { return new Integer(b.dmax(1)); }
642 public void put(Box b, Object value) {
643 if (b.sizetoimage) return;
644 b.set(dmax, 1, stoi(value));
648 specialBoxProperties.put("redirect", new SpecialBoxProperty() {
649 public void put(Box b, Object value) { }
650 public Object get(Box b) {
651 if (b.redirect == null) return null;
652 if (b.redirect == b) return Boolean.TRUE;
653 return get(b.redirect);
657 specialBoxProperties.put("apply", new SpecialBoxProperty() {
658 public void put(Box b, Object value) { }
659 public Object get(Box b) { return new Apply(b); }
662 specialBoxProperties.put("id", new SpecialBoxProperty() {
663 public void put(Box b, Object value) { }
664 public Object get(Box b) { return b.id; }
669 /** helper that converts a String to a boolean according to JavaScript coercion rules */
670 public static boolean stob(Object o) {
671 if (o == null) return false;
672 return Boolean.TRUE.equals(o) || "true".equals(o);
675 /** helper that converts a String to an int according to JavaScript coercion rules */
676 public static int stoi(Object o) {
677 if (o == null) return 0;
678 if (o instanceof Integer) return ((Integer)o).intValue();
681 if (!(o instanceof String)) s = o.toString();
684 try { return Integer.parseInt(s.indexOf('.') == -1 ? s : s.substring(0, s.indexOf('.'))); }
685 catch (NumberFormatException e) { return 0; }
688 /** helper that converts a String to a int according to JavaScript coercion rules */
689 public static class Apply extends JS.Function {
692 public Apply(Box b) { super(-1, "java", null, null); this.b = b; }
694 public Object _call(JS.Array args) throws JS.Exn {
697 if (args.elementAt(0) instanceof String) {
698 String templatename = (String)args.elementAt(0);
699 Template t = Template.getTemplate(templatename, null);
701 if (Log.on) Log.log(this, "template " + templatename + " not found at " + JS.getFileAndLine());
703 if (ThreadMessage.suspendThread()) try {
704 JS.Function callback = args.length() < 2 ? null : (Function)args.elementAt(1);
705 t.apply(b, null, null, callback, 0, t.numUnits());
707 ThreadMessage.resumeThread();
711 // apply a list of properties
712 } else if (args.elementAt(0) instanceof JS && !(args.elementAt(0) instanceof Box)) {
713 JS s = (JS)args.elementAt(0);
714 Object[] keys = s.keys();
715 for(int j=0; j<keys.length; j++) b.put(keys[j].toString(), s.get(keys[j]));