-// Copyright 2002 Adam Megacz, see the COPYING file for licensing [GPL]
-package org.xwt;
-
-import java.util.*;
-import java.net.*;
-import java.text.*;
-import org.xwt.js.*;
-import org.xwt.util.*;
-
-/**
- * A helper class for properties of Box which require special
- * handling.
- *
- * To avoid excessive use of String.equals(), the Box.get() and
- * Box.put() methods employ a Hash keyed on property names that
- * require special handling. The value stored in the Hash is an
- * instance of an anonymous subclass of SpecialBoxProperty, which knows
- * how to handle get()s and put()s for that property name. There
- * should be one anonymous subclass of SpecialBoxProperty for each
- * specially-handled property on Box.
- */
-class SpecialBoxProperty {
-
- SpecialBoxProperty() { }
- private static final int NUMINTS = Box.NUMINTS;
- private static final int dmax = Box.dmax;
- private static final int dmin = Box.dmin;
- private static final int cmin = Box.cmin;
- private static final int abs = Box.abs;
- private static final int pos = Box.pos;
- private static final int size = Box.size;
- private static final int oldpos = Box.oldpos;
- private static final int oldsize = Box.oldsize;
- private static final int pad = Box.pad;
-
- /** stores instances of SpecialBoxProperty; keyed on property name */
- static Hash specialBoxProperties = new Hash(200, 3);
-
- /** this method defines the behavior when the property is get()ed from b */
- Object get(Box b) { return null; }
-
- /** this method defines the behavior when the property is put() to b */
- void put(Box b, Object value) { }
-
- /** this method defines the behavior when the property is put() to b, allows a single SpecialBoxProperty to serve multiple properties */
- void put(String name, Box b, Object value) { put(b, value); }
-
- // 'ye olde IBM CGA colours', as defined in the XWT reference
- static final int black = 0xFF000000;
- static final int blue = 0xFF000088;
- static final int green = 0xFF008800;
- static final int cyan = 0xFF008888;
- static final int red = 0xFF880000;
- static final int magenta = 0xFF880088;
- static final int brown = 0xFF888800;
- static final int lightGray = 0xFFBDBEBD;
- static final int darkGray = 0xFF242424;
- static final int brightBlue = 0xFF0000FF;
- static final int brightGreen = 0xFF00FF00;
- static final int brightCyan = 0xFF00FFFF;
- static final int brightRed = 0xFFFF0000;
- static final int pink = 0xFFFF00FF;
- static final int yellow = 0xFFFFFF00;
- static final int white = 0xFFFFFFFF;
-
- static {
- specialBoxProperties.put("color", new SpecialBoxProperty() {
- public Object get(Box b) {
- if ((b.color & 0xFF000000) == 0) return null;
- String red = Integer.toHexString((b.color & 0x00FF0000) >> 16);
- String green = Integer.toHexString((b.color & 0x0000FF00) >> 8);
- String blue = Integer.toHexString(b.color & 0x000000FF);
- if (red.length() < 2) red = "0" + red;
- if (blue.length() < 2) blue = "0" + blue;
- if (green.length() < 2) green = "0" + green;
- return "#" + red + green + blue;
- }
- public void put(Box b, Object value) {
- int newcolor = b.color;
- String s = value == null ? null : value.toString();
- if (value == null) newcolor = 0x00000000;
- else if (s.length() > 0 && s.charAt(0) == '#')
- try {
- newcolor = 0xFF000000 |
- (Integer.parseInt(s.substring(1, 3), 16) << 16) |
- (Integer.parseInt(s.substring(3, 5), 16) << 8) |
- Integer.parseInt(s.substring(5, 7), 16);
- } catch (NumberFormatException e) {
- Log.log(this, "invalid color " + s);
- return;
- }
- else if (s.equals("black")) newcolor = black;
- else if (s.equals("blue")) newcolor = blue;
- else if (s.equals("green")) newcolor = green;
- else if (s.equals("cyan")) newcolor = cyan;
- else if (s.equals("red")) newcolor = red;
- else if (s.equals("magenta")) newcolor = magenta;
- else if (s.equals("brown")) newcolor = brown;
- else if (s.equals("lightGray")) newcolor = lightGray;
- else if (s.equals("darkGray")) newcolor = darkGray;
- else if (s.equals("brightBlue")) newcolor = brightBlue;
- else if (s.equals("brightGreen")) newcolor = brightGreen;
- else if (s.equals("brightCyan")) newcolor = brightCyan;
- else if (s.equals("brightRed")) newcolor = brightRed;
- else if (s.equals("pink")) newcolor = pink;
- else if (s.equals("yellow")) newcolor = yellow;
- else if (s.equals("white")) newcolor = white;
- else if (Log.on) Log.logJS(this, "invalid color \"" + s + "\"");
- if (newcolor == b.color) return;
- b.color = newcolor;
- b.dirty();
- } });
-
-
- specialBoxProperties.put("textcolor", new SpecialBoxProperty() {
- public Object get(Box b) {
- if ((b.textcolor & 0xFF000000) == 0) return null;
- String red = Integer.toHexString((b.textcolor & 0x00FF0000) >> 16);
- String green = Integer.toHexString((b.textcolor & 0x0000FF00) >> 8);
- String blue = Integer.toHexString(b.textcolor & 0x000000FF);
- if (red.length() < 2) red = "0" + red;
- if (blue.length() < 2) blue = "0" + blue;
- if (green.length() < 2) green = "0" + green;
- return "#" + red + green + blue;
- }
- public void put(Box b, Object value) {
- int newtextcolor = b.color;
- if (value == null) return;
- String s = value.toString();
- if (s.length() > 0 && s.charAt(0) == '#')
- newtextcolor = 0xFF000000 |
- (Integer.parseInt(s.substring(1, 3), 16) << 16) |
- (Integer.parseInt(s.substring(3, 5), 16) << 8) |
- Integer.parseInt(s.substring(5, 7), 16);
- else if (s.equals("black")) newtextcolor = black;
- else if (s.equals("blue")) newtextcolor = blue;
- else if (s.equals("green")) newtextcolor = green;
- else if (s.equals("cyan")) newtextcolor = cyan;
- else if (s.equals("red")) newtextcolor = red;
- else if (s.equals("magenta")) newtextcolor = magenta;
- else if (s.equals("brown")) newtextcolor = brown;
- else if (s.equals("lightGray")) newtextcolor = lightGray;
- else if (s.equals("darkGray")) newtextcolor = darkGray;
- else if (s.equals("brightBlue")) newtextcolor = brightBlue;
- else if (s.equals("brightGreen")) newtextcolor = brightGreen;
- else if (s.equals("brightCyan")) newtextcolor = brightCyan;
- else if (s.equals("brightRed")) newtextcolor = brightRed;
- else if (s.equals("pink")) newtextcolor = pink;
- else if (s.equals("yellow")) newtextcolor = yellow;
- else if (s.equals("white")) newtextcolor = white;
- else if (Log.on) Log.logJS(this, "invalid color \"" + s + "\"");
- if (newtextcolor == b.textcolor) return;
- b.textcolor = newtextcolor;
- b.dirty();
- } });
-
- specialBoxProperties.put("text", new SpecialBoxProperty() {
- public Object get(Box b) { return b.text; }
- public void put(Box b, Object value) {
- String t = value == null ? "null" : value.toString();
- if (t.equals(b.text)) return;
-
- for(int i=0; i<t.length(); i++)
- if (Character.isISOControl(t.charAt(i))) {
- if (Log.on) Log.log(this,
- "ISO Control characters are not permitted in box text strings; offending character is ASCII " +
- ((int)t.charAt(i)));
- return;
- }
-
- // FEATURE: try removing the following line; it appears to be redundant
- b.dirty();
- b.text = t;
- b.textupdate();
- b.dirty();
- } });
-
- specialBoxProperties.put("thisbox", new SpecialBoxProperty() {
- public Object get(Box b) { return b; }
- public void put(Box b, Object value) {
- if (value == null) b.remove();
- else if (value.equals("window") || value.equals("frame")) {
- if (b.redirect != b && Log.on)
- Log.log(this, "WARNING: you have created a surface whose root box's redirect is not itself " +
- "-- this isn't usually a good idea");
- Platform.createSurface(b, value.equals("frame"), true);
- } else if (Log.on) Log.log(this, "put invalid value to 'thisbox' property: " + value);
- }
- });
-
- specialBoxProperties.put("orient", new SpecialBoxProperty() {
- public Object get(Box b) {
- if (b.redirect == null) return "horizontal";
- else if (b.redirect != b) return get(b.redirect);
- else if (b.o == 1) return "vertical";
- else return "horizontal";
- }
- public void put(Box b, Object value) {
- if (value == null) return;
- if (b.redirect == null) return;
- if (b.redirect != b) {
- put(b.redirect, value);
- return;
- }
- if (value.equals("vertical")) {
- if (b.o == 1) return;
- b.o = 1;
- b.xo = 0;
- } else if (value.equals("horizontal")) {
- if (b.o == 0) return;
- b.o = 0;
- b.xo = 1;
- } else if (Log.on)
- Log.log(this, "invalid value put to orient property: " + value);
- b.mark_for_prerender();
- b.sync_cmin_to_children();
- } });
-
- specialBoxProperties.put("static", new SpecialBoxProperty() {
- public Object get(Box b) {
- String cfsn = JS.Thread.fromJavaThread(Thread.currentThread()).getCurrentCompiledFunction().getSourceName();
- for(int i=0; i<cfsn.length() - 1; i++)
- if (cfsn.charAt(i) == '.' && (cfsn.charAt(i+1) == '_' || Character.isDigit(cfsn.charAt(i+1)))) {
- cfsn = cfsn.substring(0, i);
- break;
- }
- return Static.getStatic(cfsn);
- }
- public void put(Box b, Object value) { }
- });
-
- specialBoxProperties.put("sizetoimage", new SpecialBoxProperty() {
- public Object get(Box b) { return b.sizetoimage ? Boolean.TRUE : Boolean.FALSE; }
- public void put(Box b, Object value) {
- if (stob(value)) {
- if (b.sizetoimage) return;
- b.sizetoimage = true;
- b.syncSizeToImage();
- } else {
- b.sizetoimage = false;
- }
- } });
-
- specialBoxProperties.put("fixedaspect", new SpecialBoxProperty() {
- public Object get(Box b) { return b.sizetoimage ? Boolean.TRUE : Boolean.FALSE; }
- public void put(Box b, Object value) {
- boolean newval = stob(value);
- if (newval == b.fixedaspect) return;
- b.fixedaspect = newval;
- b.dirty();
- } });
-
- specialBoxProperties.put("shrink", new SpecialBoxProperty() {
- public Object get(Box b) { return (b.vshrink && b.hshrink) ? Boolean.TRUE : Boolean.FALSE; }
- public void put(Box b, Object value) {
- boolean newshrink = stob(value);
- if (b.hshrink == newshrink && b.vshrink == newshrink) return;
- b.hshrink = b.vshrink = newshrink;
- b.mark_for_prerender();
- } });
-
- specialBoxProperties.put("hshrink", new SpecialBoxProperty() {
- public Object get(Box b) { return new Boolean(b.hshrink); }
- public void put(Box b, Object value) {
- boolean newshrink = stob(value);
- if (b.hshrink == newshrink) return;
- b.hshrink = newshrink;
- b.mark_for_prerender();
- }
- });
-
- specialBoxProperties.put("vshrink", new SpecialBoxProperty() {
- public Object get(Box b) { return b.vshrink ? Boolean.TRUE : Boolean.FALSE; }
- public void put(Box b, Object value) {
- boolean newshrink = stob(value);
- if (b.vshrink == newshrink) return;
- b.vshrink = newshrink;
- b.mark_for_prerender();
- }
- });
-
- specialBoxProperties.put("x", new SpecialBoxProperty() {
- public Object get(Box b) {
- if (b.surface == null) return new Integer(0);
- if (b.invisible) return new Integer(0);
- return new Integer(b.abs(0));
- }
- public void put(Box b, Object value) {
- if (b.getParent() == null && b.surface != null) {
- b.surface.setLocation(stoi(value), b.abs(1));
- b.surface.centerSurfaceOnRender = false;
- }
- b.set(abs, 0, stoi(value));
- }
- });
-
- specialBoxProperties.put("y", new SpecialBoxProperty() {
- public Object get(Box b) {
- if (b.surface == null) return new Integer(0);
- if (b.invisible) return new Integer(0);
- return new Integer(b.abs(1));
- }
- public void put(Box b, Object value) {
- if (b.getParent() == null && b.surface != null) {
- b.surface.setLocation(b.abs(0), stoi(value));
- b.surface.centerSurfaceOnRender = false;
- }
- b.set(abs, 1, stoi(value));
- }
- });
-
- specialBoxProperties.put("width", new SpecialBoxProperty() {
- public Object get(Box b) { return new Integer(b.size(0)); }
- public void put(Box b, Object value) {
- if (b.sizetoimage) return;
- if (b.getParent() == null && b.surface != null) {
- b.set(size, 0, Box.max(Surface.scarPicture.getWidth(), stoi(value)));
- b.mark_for_prerender();
- } else {
- b.set(dmax, 0, stoi(value));
- b.set(dmin, 0, stoi(value));
- }
- } });
-
- specialBoxProperties.put("height", new SpecialBoxProperty() {
- public Object get(Box b) { return new Integer(b.size(1)); }
- public void put(Box b, Object value) {
- if (b.sizetoimage) return;
- if (b.getParent() == null && b.surface != null) {
- b.set(size, 1, Box.max(Surface.scarPicture.getHeight(), stoi(value)));
- b.mark_for_prerender();
- } else {
- b.set(dmax, 1, stoi(value));
- b.set(dmin, 1, stoi(value));
- }
- } });
-
- specialBoxProperties.put("flex", new SpecialBoxProperty() {
- public Object get(Box b) { return new Double(b.flex); }
- public void put(Box b, Object value) {
- if (value == null) return;
- int newflex = stoi(value);
- if (newflex == b.flex) return;
- b.flex = newflex;
- b.mark_for_prerender();
- } });
-
- specialBoxProperties.put("tile", new SpecialBoxProperty() {
- public Object get(Box b) { return b.tile ? Boolean.TRUE : Boolean.FALSE; }
- public void put(Box b, Object value) {
- boolean newtile = stob(value);
- if (newtile == b.tile) return;
- b.tile = newtile;
- b.dirty();
- } });
-
- specialBoxProperties.put("align", new SpecialBoxProperty() {
- public Object get(Box b) {
- if (b.align == -1) return "topleft";
- else if (b.align == 1) return "bottomright";
- else return "center";
- }
- public void put(Box b, Object value) {
- String s = value == null ? "" : value.toString();
- byte newalign = b.align;
- if (s.equals("topleft")) newalign = -1;
- else if (s.equals("bottomright")) newalign = 1;
- else if (s.equals("center")) newalign = 0;
- else if (Log.on) Log.log(this, "invalid value put to align property: " + value);
- if (newalign == b.align) return;
- b.align = newalign;
- if (b.getParent() != null) b.getParent().mark_for_prerender();
- } });
-
- specialBoxProperties.put("invisible", new SpecialBoxProperty() {
- public Object get(Box b) {
- for (Box cur = b; cur != null; cur = cur.getParent()) { if (cur.invisible) return Boolean.TRUE; }
- return Boolean.FALSE;
- }
- public void put(Box b, Object value) {
- boolean newinvisible = stob(value);
- if (newinvisible == b.invisible) return;
- b.invisible = newinvisible;
- if (b.getParent() == null) {
- if (b.surface != null) b.surface.setInvisible(newinvisible);
- } else {
- b.dirty();
- b.getParent().mark_for_prerender();
- b.getParent().sync_cmin_to_children();
- b.getParent().dirty(b.pos(0), b.pos(1), b.size(0), b.size(1));
- b.getParent().dirty(b.oldpos(0), b.oldpos(1), b.oldsize(0), b.oldsize(1));
- }
- }});
-
- specialBoxProperties.put("absolute", new SpecialBoxProperty() {
- public Object get(Box b) { return b.absolute ? Boolean.TRUE : Boolean.FALSE; }
- public void put(Box b, Object value) {
- boolean newabsolute = stob(value);
- if (newabsolute == b.absolute) return;
- b.absolute = newabsolute;
- if (b.getParent() != null) {
- b.getParent().mark_for_prerender();
- b.getParent().sync_cmin_to_children();
- }
- } });
-
- specialBoxProperties.put("image", new SpecialBoxProperty() {
- public Object get(Box b) { return b.image == null ? null : Box.imageToNameMap.get(b.image); }
- public void put(Box b, Object value) { b.setImage(value == null ? null : value.toString()); }
- });
-
- specialBoxProperties.put("border", new SpecialBoxProperty() {
- public Object get(Box b) { return b.border == null ? null : Box.imageToNameMap.get(b.border); }
- public void put(Box b, Object value) { b.setBorder(value == null ? null : value.toString()); }
- });
-
- specialBoxProperties.put("font", new SpecialBoxProperty() {
- public Object get(Box b) { return b.font; }
- public void put(Box b, Object value) {
- b.font = value == null ? null : value.toString();
- b.fontChanged();
- b.dirty();
- } });
-
- specialBoxProperties.put("globalx", new SpecialBoxProperty() {
- public Object get(Box b) { return new Integer(b.getParent() == null || b.surface == null ? 0 : b.pos(0)); }
- public void put(Box b, Object value) {
- if (b.surface == null || b.getParent() == null) return;
- b.put("x", new Integer(stoi(value) - stoi(get(b.getParent()))));
- }
- });
-
- specialBoxProperties.put("globaly", new SpecialBoxProperty() {
- public Object get(Box b) { return new Integer(b.getParent() == null || b.surface == null ? 0 : b.pos(1)); }
- public void put(Box b, Object value) {
- if (b.surface == null || b.getParent() == null) return;
- b.put("y", new Integer(stoi(value) - stoi(get(b.getParent()))));
- }
- });
-
- specialBoxProperties.put("cursor", new SpecialBoxProperty() {
- public Object get(Box b) { return b.cursor; }
- public void put(Box b, Object value) {
- b.cursor = (String)value;
- if (b.surface == null) return;
-
- // see if we need to update the surface cursor
- String tempcursor = b.surface.cursor;
- b.Move(b.surface.mousex, b.surface.mousey, b.surface.mousex, b.surface.mousey);
- if (b.surface.cursor != tempcursor) b.surface.syncCursor();
- }
- });
-
- specialBoxProperties.put("mousex", new SpecialBoxProperty() {
- public Object get(Box b) { return new Integer(b.surface == null ? 0 : b.surface.mousex - b.pos(0)); }
- });
-
- specialBoxProperties.put("mousey", new SpecialBoxProperty() {
- public Object get(Box b) { return new Integer(b.surface == null ? 0 : b.surface.mousey - b.pos(1)); }
- });
-
- specialBoxProperties.put("xwt", new SpecialBoxProperty() {
- public Object get(Box b) { return XWT.singleton; }
- });
-
- specialBoxProperties.put("mouseinside", new SpecialBoxProperty() {
- public Object get(Box b) { return b.mouseinside ? Boolean.TRUE : Boolean.FALSE; }
- });
-
- specialBoxProperties.put("numchildren", new SpecialBoxProperty() {
- public Object get(Box b) {
- if (b.redirect == null) return new Integer(0);
- if (b.redirect != b) return get(b.redirect);
- return new Integer(b.numChildren());
- } });
-
- SpecialBoxProperty mouseEventHandler = new SpecialBoxProperty() {
- public void put(String name, Box b, Object value) {
- if (b.surface == null) return;
- for(Box c = b.prevSibling(); c != null; c = c.prevSibling()) {
- Box siblingChild = c.whoIs(c.surface.mousex, c.surface.mousey);
- if (siblingChild != null) {
- siblingChild.put(name, value);
- return;
- }
- }
- if (b.getParent() != null)
- b.getParent().put(name, value);
- }};
-
- specialBoxProperties.put("Press1", mouseEventHandler);
- specialBoxProperties.put("Press2", mouseEventHandler);
- specialBoxProperties.put("Press3", mouseEventHandler);
- specialBoxProperties.put("Release1", mouseEventHandler);
- specialBoxProperties.put("Release2", mouseEventHandler);
- specialBoxProperties.put("Release3", mouseEventHandler);
- specialBoxProperties.put("Click1", mouseEventHandler);
- specialBoxProperties.put("Click2", mouseEventHandler);
- specialBoxProperties.put("Click3", mouseEventHandler);
- specialBoxProperties.put("DoubleClick1", mouseEventHandler);
- specialBoxProperties.put("DoubleClick2", mouseEventHandler);
- specialBoxProperties.put("DoubleClick3", mouseEventHandler);
-
- specialBoxProperties.put("root", new SpecialBoxProperty() {
- public Object get(Box b) {
- if (b.surface == null) return null;
- else if (b.getRoot() == null) return null;
- else if (b.getParent() == null) return b;
- else return b.getRoot().getRootProxy();
- } });
-
- specialBoxProperties.put("Minimized", new SpecialBoxProperty() {
- public Object get(Box b) {
- if (b.getParent() == null && b.surface != null) return b.surface.minimized ? Boolean.TRUE : Boolean.FALSE;
- else return null;
- }
- public void put(Box b, Object value) {
- if (b.surface == null) return;
- boolean val = stob(value);
- if (b.getParent() == null && b.surface.minimized != val) b.surface.setMinimized(val);
- }
- });
-
- specialBoxProperties.put("Maximized", new SpecialBoxProperty() {
- public Object get(Box b) {
- if (b.getParent() == null && b.surface != null) return b.surface.maximized ? Boolean.TRUE : Boolean.FALSE;
- else return null;
- }
- public void put(Box b, Object value) {
- if (b.surface == null) return;
- boolean val = stob(value);
- if (b.getParent() == null && b.surface.maximized != val) b.surface.setMaximized(val);
- }
- });
-
- specialBoxProperties.put("toback", new SpecialBoxProperty() {
- public void put(Box b, Object value) {
- if (b.getParent() == null && stob(value) && b.surface != null) b.surface.toBack();
- }
- });
-
- specialBoxProperties.put("tofront", new SpecialBoxProperty() {
- public void put(Box b, Object value) {
- if (b.getParent() == null && stob(value) && b.surface != null) b.surface.toFront();
- }
- });
-
- specialBoxProperties.put("hscar", new SpecialBoxProperty() {
- public void put(Box b, Object value) {
- if (b.getParent() == null && b.surface != null) {
- b.surface.hscar = stoi(value);
- b.surface.dirty(0, 0, b.surface.width, b.surface.height);
- b.surface.Refresh();
- }
- }
- });
-
- specialBoxProperties.put("vscar", new SpecialBoxProperty() {
- public void put(Box b, Object value) {
- if (b.getParent() == null && b.surface != null) {
- b.surface.vscar = stoi(value);
- b.surface.dirty(0, 0, b.surface.width, b.surface.height);
- b.surface.Refresh();
- }
- }
- });
-
- specialBoxProperties.put("Close", new SpecialBoxProperty() {
- public void put(Box b, Object value) {
- if (b.getParent() == null && b.surface != null) b.surface.dispose(true);
- }
- });
-
- // these are all do-nothings; just to prevent space from getting taken up in the params Hash.
- specialBoxProperties.put("KeyPressed", new SpecialBoxProperty());
- specialBoxProperties.put("KeyReleased", new SpecialBoxProperty());
- specialBoxProperties.put("PosChange", new SpecialBoxProperty());
- specialBoxProperties.put("SizeChange", new SpecialBoxProperty());
-
- specialBoxProperties.put("hpad", new SpecialBoxProperty() {
- public Object get(Box b) {
- if (b.redirect == null) return new Integer(0);
- if (b.redirect != b) return get(b.redirect);
- return new Integer(b.pad(0));
- }
- public void put(Box b, Object value) {
- if (b.redirect == null) return;
- if (b.redirect != b) { put(b.redirect, value); return; }
- int newval = stoi(value);
- if (newval == b.pad(0)) return;
- b.set(pad, 0, newval);
- }
- });
-
- specialBoxProperties.put("vpad", new SpecialBoxProperty() {
- public Object get(Box b) {
- if (b.redirect == null) return new Integer(0);
- if (b.redirect != b) return get(b.redirect);
- return new Integer(b.pad(1));
- }
- public void put(Box b, Object value) {
- if (b.redirect == null) return;
- if (b.redirect != b) { put(b.redirect, value); return; }
- int newval = stoi(value);
- if (newval == b.pad(1)) return;
- b.set(pad, 1, newval);
- }
- });
-
- specialBoxProperties.put("indexof", new SpecialBoxProperty() {
- public Object get(Box b) { return b.indexof(); }
- });
-
- specialBoxProperties.put("minwidth", new SpecialBoxProperty() {
- public Object get(Box b) { return new Integer(b.dmin(0)); }
- public void put(Box b, Object value) {
- if (b.sizetoimage) return;
- b.set(dmin, 0, stoi(value));
- }
- });
-
- specialBoxProperties.put("maxwidth", new SpecialBoxProperty() {
- public Object get(Box b) { return new Integer(b.dmax(0)); }
- public void put(Box b, Object value) {
- if (b.sizetoimage) return;
- b.set(dmax, 0, stoi(value));
- }
- });
-
- specialBoxProperties.put("minheight", new SpecialBoxProperty() {
- public Object get(Box b) { return new Integer(b.dmin(1)); }
- public void put(Box b, Object value) {
- if (b.sizetoimage) return;
- b.set(dmin, 1, stoi(value));
- }
- });
-
- specialBoxProperties.put("maxheight", new SpecialBoxProperty() {
- public Object get(Box b) { return new Integer(b.dmax(1)); }
- public void put(Box b, Object value) {
- if (b.sizetoimage) return;
- b.set(dmax, 1, stoi(value));
- }
- });
-
- specialBoxProperties.put("redirect", new SpecialBoxProperty() {
- public void put(Box b, Object value) { }
- public Object get(Box b) {
- if (b.redirect == null) return null;
- if (b.redirect == b) return Boolean.TRUE;
- return get(b.redirect);
- }
- });
-
- specialBoxProperties.put("apply", new SpecialBoxProperty() {
- public void put(Box b, Object value) { }
- public Object get(final Box b) { return new Apply(b); }
- });
-
- specialBoxProperties.put("id", new SpecialBoxProperty() {
- public void put(Box b, Object value) { }
- public Object get(Box b) { return b.id; }
- });
- }
-
-
- /** helper that converts a String to a boolean according to JavaScript coercion rules */
- public static boolean stob(Object o) {
- if (o == null) return false;
- return Boolean.TRUE.equals(o) || "true".equals(o);
- }
-
- /** helper that converts a String to an int according to JavaScript coercion rules */
- public static int stoi(Object o) {
- if (o == null) return 0;
- if (o instanceof Integer) return ((Integer)o).intValue();
-
- String s;
- if (!(o instanceof String)) s = o.toString();
- else s = (String)o;
-
- try { return Integer.parseInt(s.indexOf('.') == -1 ? s : s.substring(0, s.indexOf('.'))); }
- catch (NumberFormatException e) { return 0; }
- }
-
- private static class Apply extends JS.Callable {
- Box b;
- public Apply(Box b) { this.b = b; }
- public Object call(JS.Array args) throws JS.Exn {
- if (args.elementAt(0) instanceof String) {
- String templatename = (String)args.elementAt(0);
- Template t = Template.getTemplate(templatename, null);
- if (t == null) {
- if (Log.on) Log.logJS(this, "template " + templatename + " not found");
- } else {
- if (ThreadMessage.suspendThread()) try {
- JS.Callable callback = args.length() < 2 ? null : (Callable)args.elementAt(1);
- t.apply(b, null, null, callback, 0, t.numUnits());
- } finally {
- ThreadMessage.resumeThread();
- }
- }
-
- } else if (args.elementAt(0) instanceof JS && !(args.elementAt(0) instanceof Box)) {
- JS s = (JS)args.elementAt(0);
- Object[] keys = s.keys();
- for(int j=0; j<keys.length; j++) b.put(keys[j].toString(), s.get(keys[j]));
- }
-
- return b;
- }
- }
-}
-
-
-