jakarta-bsf-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Dmitry Goldenberg" <dmitry.goldenb...@weblayers.com>
Subject RE: A sample employing declareBean and interacting with it from JavaScript and others ... (Re: BSF integration sample code - ?
Date Tue, 02 May 2006 13:46:31 GMT
Rony,
 
This rocks!  I wonder if it may be worthwhile publishing this code (and the other sample)
on the BSF site ??

________________________________

From: Rony G. Flatscher [mailto:Rony.Flatscher@wu-wien.ac.at]
Sent: Mon 5/1/2006 6:15 PM
To: Bean Scripting Framework users
Subject: A sample employing declareBean and interacting with it from JavaScript and others
... (Re: BSF integration sample code - ?



Hi Dimitry,

Dmitry Goldenberg wrote:
> Thanks, Jaime.
> 
> Well, let's say I have a set of Java API's as follows:
>  
Why not use one of the samples coming with BSF(4Rexx) demonstrating what
you are looking for ?

After compiling the following Java-program ("javac ScriptedUI.java"),
you can invoke it by supplying the filename containing the script (which
could be in any of the BSF supported script engines, inferred from the
filetype), e.g.:

java ScriptedUI ui.js
java ScriptedUI ui.rex


----------------- cut here (ScriptedUI.java) -----------------
/* This example shows how a Java app can allow a script to customize
   a UI */

import java.awt.*;
import java.awt.event.*;
import java.io.*;

import org.apache.bsf.*;
import org.apache.bsf.util.*;

public class ScriptedUI {
  BSFManager mgr = new BSFManager ();

  public ScriptedUI (String fileName) {
    Frame f = new Frame ("Application's Main Frame");
    f.addWindowListener (new WindowAdapter () {
      public void windowClosing (WindowEvent e) {
        System.exit (0);
      }
    });

    Panel p = new Panel ();
    f.add ("Center", p);
    f.add ("North", new Button ("North Button"));
    f.add ("South", new Button ("South Button"));

    mgr.registerBean ("centerPanel", p);  // register panel (will be looked up by script)

    // exec script engine code to do its thing for this
    try {
      String language = BSFManager.getLangFromFilename (fileName);
      FileReader in = new FileReader (fileName);
      String script = IOUtils.getStringFromReader (in);

      mgr.exec (language, fileName, -1, -1, script);
    } catch (BSFException e) {
      System.err.println ("Ouch: " + e.getMessage ());
      e.printStackTrace ();
    } catch (IOException e) {
      System.err.println ("Ouch: " + e.getMessage ());
      e.printStackTrace ();
    }

    // now pack and show the frame
    f.pack ();
    f.show ();
  }

  public static void main (String[] args) throws Exception {
    if (args.length != 1) {
      System.err.println ("Usage: java ScriptedUI filename");
      System.err.println ("       where filename is the name of the script");
      System.exit (1);
    }
    new ScriptedUI (args[0]);
  }
}
----------------- cut here -----------------


This is the JavaScript version:

----------------- cut here (ui.js) -----------------
/* pick up the center panel bean */
p = bsf.lookupBean ("centerPanel");       /* get Panel object from the BSFRegistry */

/* set the layout manager to border */
p.setLayout (new java.awt.BorderLayout ());

/* add a few things */
p.add ("Center", new java.awt.Label ("Middle from JavaScript"));
p.add ("North", new java.awt.TextField ("north text from JavaScript"));
p.add ("South", new java.awt.TextField ("south text from JavaScript"));
p.add ("East", new java.awt.Button ("inner east from JavaScript"));
p.add ("West", new java.awt.Button ("inner west from JavaScript"));

/* configure p a bit */
p.setBackground (java.awt.Color.red);

/* configure the frame that p is in */
f = p.getParent ();
f.setTitle ("Hello from JavaScript (title reset from JavaScript)");
----------------- cut here -----------------


This is the ooRexx-version:

----------------- cut here (ui.rex) -----------------
p = .bsf~bsf.lookupBean("centerPanel")  -- reference the entry in BSF registry, put there
by
                                        -- the Java program "ScriptedUI.class"

/* set the layout manager to border */
p~setLayout(.bsf~new("java.awt.BorderLayout"))

/* add a few things */
p~add("Center", .bsf~new("java.awt.Label",     "Middle from Object Rexx"))
p~add("North",  .bsf~new("java.awt.TextField", "North text from Object Rexx"))
p~add("South",  .bsf~new("java.awt.TextField", "South text from Object Rexx"))
p~add("East",   .bsf~new("java.awt.Button",    "Inner east text from Object Rexx"))
p~add("West",   .bsf~new("java.awt.Button",    "Inner west text from Object Rexx"))

/* configure p a bit */
p~setBackground(.bsf~bsf.getStaticValue("java.awt.Color", "green"))

/* configure the frame that p is in */
f=p~getParent
f~setTitle("Hello from Object REXX (title reset from Object Rexx)")

::requires BSF.CLS    -- get Object Rexx wrapper support for BSF
----------------- cut here -----------------


Hope that helps,

---rony


---------------------------------------------------------------------
To unsubscribe, e-mail: bsf-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: bsf-user-help@jakarta.apache.org





Mime
View raw message