Damn, that is just so cool.

Michael

Patrick LeBoutillier wrote:

Hi all,

I believe I finally found a working solution for this problem.
It basically follow the original design I posted a few days back:

Definitions:
- TCP: A Java thread that communicates with Perl. All Perl actions done
on Java objects obviously occur through a TCP.

New Interface (not final):
- The InlineJavaPerlCaller class has 2 new methods: StartCallbackLoop
and StopCallbackLoop that are used to control the callback loop.

New Rules:
- As mentionned previously, only a TCP is now allowed to CREATE
InlineJavaPerlCaller object.
- Only a TCP can invoke StartCallbackLoop.
- A call to StopCallbackLoop through any InlineJavaPerlCaller object
that was created by an ygiven TCP will interrupt that TCP's
StartCallbackLoop.
- The enforcement of these rules forced me to state that the constructor
for InlineJavaPerlCaller may now throw an InlineJavaException. The
StartCallbackLoop and StopCallbackLoop methods may also throw this
exception type.

In most cases these rules apply themselves automatically, but I must
specify them explicitely since the Inline::Java JVM server can deal with
many Perl scripts simultaneously (SHARED_MODE), and in this case these
rules are important.

So basically Mark's script (after modification) ends up looking like
this:

-----8<-----
#!/usr/bin/perl
use strict;
use warnings;

use Inline Java => "DATA";

my $cnt = 0 ;
my $greeter = MyButton->new();
$greeter->StartCallbackLoop() ;


###########################################



sub button_pressed { $cnt++ ; print "Button Pressed $cnt times (from perl)\n" ; if ($cnt >= 10){ $greeter->StopCallbackLoop() ; } }

__DATA__
__Java__

import java.util.*;
import org.perl.inline.java.*;
import javax.swing.*;
import java.awt.event.*;

public class MyButton extends    InlineJavaPerlCaller
                     implements ActionListener
{
 public MyButton() throws InlineJavaException
 {
   // create frame
   JFrame frame = new JFrame("MyButton");
   frame.setSize(200,200);

   // create button
   JButton button = new JButton("Click Me!");
   frame.getContentPane().add(button);

   // tell the button that when it's clicked, report it to
   // this class.
   button.addActionListener(this);

   // all done, everything added, just show it
   frame.show();
 }

 public void actionPerformed(ActionEvent e)
 {
   try
   {
     CallPerl("main", "button_pressed", new Object [] {});
   }
   catch (InlineJavaPerlException pe)  { }
   catch (InlineJavaException pe) { pe.printStackTrace() ;}
 }
}
-----8<-----


So basically very minor changes for the user.
I will put a trial version (minus documentation and test cases) up on http://inline.perl.org/java/download.html over the weekend.


Cheers,

Patrick





Reply via email to