diff --git a/js/rhino/docs/rhino15R5.html b/js/rhino/docs/rhino15R5.html index 27567bc2bc7b..0e367bc2105c 100644 --- a/js/rhino/docs/rhino15R5.html +++ b/js/rhino/docs/rhino15R5.html @@ -15,24 +15,39 @@ This is a log of significant changes in Rhino 1.5 Release 5.
-Rhino allows to pass a JavaScript function to a Java method expecting an interface with a single method. The JavaScript function will be called whenever interface's method is called from Java. It allows to simplify code that previously had to create explicit JavaAdapter objects. +Rhino allows to pass a JavaScript function to a Java method expecting an interface which either has a single method or all its methods have the same number of parameters and each corresponding parameter has the same type. +The JavaScript function will be called whenever interface's method is called from Java. The function will receive all Java arguments properly converted into JS types and as the last parameter Rhino will pass interface method's name.
-For example, one can write now: +The feature allows to simplify code that previously had to create explicit JavaAdapter objects. For example, one can write now:
var button = new javax.swing.JButton("My Button"); button.addActionListener(function(e) { - java.lang.System.out.println("Button click:"+e); + java.lang.System.out.println("Button click:"+e); + }); + var frame = new javax.swing.JFrame("My Frame"); + frame.addWindowListener(function(e, methodName) { + java.lang.System.out.println("Window event:"+e); + if (methodName == "windowClosing") { + java.lang.System.exit(0); + } });instead of
var button = new javax.swing.JButton("My Button"); - button.addActionListener(new java.awt.event.ActionListener({ - actionPerformed : function(e) { - java.lang.System.out.println("Button click:"+e); - } + button.addActionListener(new java.awt.event.WindowListener({ + windowClosing : function(e) { + java.lang.System.out.println("Window event:"+e); + java.lang.System.exit(0); + }, + windowActivated : function(e) { + java.lang.System.out.println("Window event:"+e); + }, + // similar code for the rest of WindowListener methods }); + var frame = new javax.swing.JFrame("My Frame"); + frame.addWindowListener(function(e, methodName) {which was necessary in the previous version of Rhino. See Bugzilla 223435. diff --git a/js/rhino/docs/scriptjava.html b/js/rhino/docs/scriptjava.html index 205e8a332642..2c9335aaa442 100644 --- a/js/rhino/docs/scriptjava.html +++ b/js/rhino/docs/scriptjava.html @@ -168,7 +168,7 @@ Rhino actually creates a new Java class that implements ActionListener and forwards calls from that class to the JavaScript object. So when you click on the button, the printDate method is called.
-Starting from the release 1.5R5 Rhino allows to pass JavaScript functions directly to Java methods if the corresponding argument is Java interface with single method. It allows to pass printDate directly to addActionListener and simplifies example: +Starting from the release 1.5R5 Rhino allows to pass JavaScript functions directly to Java methods if the corresponding argument is Java interface and it either has the single method or all its methods has the same number of arguments and corresponding arguments has the same types. It allows to pass printDate directly to addActionListener and simplifies example:
$ java org.mozilla.javascript.tools.shell.Main js> importPackage(java.awt); js> frame = new Frame("JavaScript") diff --git a/js/rhino/examples/SwingApplication.js b/js/rhino/examples/SwingApplication.js index 0a14ed605910..8c9b3b89e77c 100644 --- a/js/rhino/examples/SwingApplication.js +++ b/js/rhino/examples/SwingApplication.js @@ -16,7 +16,10 @@ function createComponents() { var button = new JButton("I'm a Swing button!"); button.mnemonic = KeyEvent.VK_I; // Since Rhino 1.5R5 JS functions can be passed to Java method if - // corresponding argument type is Java interface with single method. + // corresponding argument type is Java interface with single method + // or all its methods have the same number of arguments and the + // corresponding arguments has the same type. See also comments for + // frame.addWindowListener bellow button.addActionListener(function() { numClicks += 1; label.setText(labelPrefix + numClicks); @@ -48,12 +51,18 @@ try { var frame = new JFrame("SwingApplication"); frame.getContentPane().add(createComponents(), BorderLayout.CENTER); -//Finish setting up the frame, and show it. -frame.addWindowListener(new WindowAdapter({ - windowClosing : function() { - java.lang.System.exit(0); +// Pass JS function as implementation of WindowListener. It is allowed since +// all methods in WindowListener have the same signature. To distinguish +// between methods Rhino passes to JS function the name of corresponding +// method as the last argument +frame.addWindowListener(function(event, methodName) { +print(event + " "+methodName); + if (methodName == "windowClosing") { + java.lang.System.exit(0); } -}) ); +}); + +//Finish setting up the frame, and show it. frame.pack(); frame.setVisible(true);