/* Sandals - a JavaScript/Rhino version of _why's Shoes library
* for Ruby that uses Java's Swing library
* Copyright (C) 2008, Rudolf Olah
*/
importPackage(java.awt);
importPackage(javax.swing);
importPackage(javax.swing.text);
importPackage(Packages.beachhouse);
importPackage(Packages.beachhouse.util);
/*** STYLE STUFF ***/
function inherit_style (parent, child) {
var name = '';
var merged = new Object();
for (name in parent)
merged[name] = parent[name];
for (name in child)
merged[name] = child[name];
return merged;
}
function has_style (args, position) {
var x = position >= 0 ? args[position] : args[args.length + position];
if (x instanceof Object)
return x;
return false;
}
function extract_style (default_style, args, position) {
var user_style = has_style(args, position);
if (user_style)
return inherit_style(default_style, user_style);
return default_style;
}
function js_to_beach_style (js, bs) {
if ("color" in js)
bs.setColor(js.color);
if ("background" in js)
bs.setBackground(js.background);
bs.setResizable("resizable" in js && js.resizable ? true : false);
bs.setBold("bold" in js && js.bold ? true : false);
bs.setItalic("italic" in js && js.italic ? true : false);
bs.setUnderline("underline" in js && js.underline ? true : false);
bs.setFontSize("size" in js ? js.size : 12);
}
/*** STYLED TEXT ***/
function StyledText (text) {
if (arguments.length == 0 || arguments.length > 2)
throw Error("Too many arguments.");
this.style = (arguments.length == 2 ? arguments[1] : new Object() );
if (text instanceof StyledText) {
this.text = text.text;
this.style = inherit_style(this.style, text.style);
} else {
this.text = text;
}
}
/*** SANDAL STUFF ***/
var Sandals = new Object();
Sandals.RELEASE_NAME = "Rootbeer";
Sandals.RELEASE_ID = 1;
/*
Sandals.app = function () {
if (arguments.length == 0)
throw Error("Sandals requires style, or things to display");
this.style = extract_style({ width: 500, height: 600, resizable: true,
background: "grey", color: "black" },
arguments, 0);
this.window = new AppWindow();
js_to_beach_style(this.style, this.window.style);
var blah = function () {
var app = {
get name() { return this.window.getTitle(); },
set name(x) { this.window.setTitle(x); },
location: java.lang.System.getProperty("user.dir")
};
arguments[0]();
this.window.go();
print("Walking on to the beach.");
};
*/
try {
if (app.started)
print("Sandals application already running.");
} catch (ReferenceError) {
/* There will only ever be one Sandals application running at a time. */
var app = new Object;
}
Sandals.go = function (body) {
this.app_window = new AppWindow();
app.__defineGetter__("name", function () { return Sandals.app_window.name; });
app.__defineSetter__("name", function (x) { Sandals.app_window.name = x; });
app.location = this.app_window.getCurrentDirectory();
print("Slipping feet into Sandals.");
body();
this.app_window.go();
print("Walking on to the beach.");
}
/*** LAYOUT STUFF ***/
Sandals.stack = function () {
var layout = new Stack();
for (var i = 0; i < arguments.length; i++)
layout.add(arguments[i]);
return layout;
};
Sandals.flow = function () {
var layout = new Flow();
for (var i = 0; i < arguments.length; i++)
layout.add(arguments[i]);
return layout;
};
/*** TEXT BLOCKS STUFF ***/
Sandals.para = function () {
print("Arguments to para: " + arguments[0]);
var style = extract_style({ background: "white", color: "black" },
arguments, -1);
var text = TextBlock.Para("");
for each (var x in arguments) {
text.appendText(x);
}
js_to_beach_style(style, text.style);
this.app_window.add(text);
};
Sandals.title = function () {
return TextBlock.Title(arguments[0]);
};
Sandals.caption = function () {
var style = extract_style({ size: 14 }, arguments, -1);
return this.para();
};
Sandals.strong = function (text) {
return new StyledText(text, extract_style({ bold: true }, arguments, -1));
};
Sandals.em = function (text) {
return new StyledText(text, extract_style({ italic: true }, arguments, -1));
};
Sandals.code = function (text) {
return new StyledText(text, extract_style({ code: true }, arguments, -1));
};
Sandals.ins = function (text) {
return new StyledText(text, extract_style({ underline: true }, arguments, -1));
};