Re: J/Link listeners

*To*: mathgroup at smc.vnet.net*Subject*: [mg27225] Re: J/Link listeners*From*: tgayley at wolfram.com (Todd Gayley)*Date*: Mon, 12 Feb 2001 03:21:03 -0500 (EST)*Organization*: Wolfram Research, Inc.*References*: <9609h5$oft@smc.vnet.net>*Sender*: owner-wri-mathgroup at wolfram.com

On 9 Feb 2001 03:25:09 -0500, murphee <werner.schuster at netway.at> wrote: >yo Todd Gayley (and of course the rest of the group), > >the MathListener Util classes in J/Link make it very easy to write GUI >code, without writing (an deploying) classes; >but, as soon as you need a Listener not defined in J/Link, you have to >write Java Code again >(eg if you use some of the Swing Listeners like ChangeListener (for JSlider)); > >I thinks it is possible to use the JDK 1.3 Dynamic Proxy API to add this to >J/Link; > >I know that everyone could use the DynamicProxy things on their own, but >having a little Utility class that hides >all the ugly details would make J/Link even more useful; Murphee, This is a very clever suggestion. In fact, this feature has already been written and is slated to be included in J/Link 1.2. The basic idea, as you obviously understand, is to allow any Java interface to be implemented entirely in Mathematica code. This is a very nice feature made possible by the new Dynamic Proxy API in Java 1.3. For example, let's say you want an instance of a Java class that implements the Swing CaretListener interface via callbacks to Mathematica. (You want such an object because you have a JTextComponent and you want the Mathematica functions within highlighted text to appear in red.) The CaretListener interface has one method: public void caretUpdate(CaretEvent e); There is no MathCaretListener class built into J/Link, so in the current version of J/Link you have to write some Java code to create your own version of such a class. In the next release of J/Link, however, here is all you will need to do (the design is not finalized yet): obj = ImplementInterface["javax.swing.CaretListener", {"caretUpdate" -> "caretUpdateHandler"}] That's it. You now have a Java CaretListener object in Mathematica that will call the Mathematica function caretUpdateHandler when CaretEvents are fired. Not a single line of Java needed to be written to create this class and the obj instance (and in fact, just a single line of Mathematica code). Now write caretUpdateHandler: caretUpdateHandler[caretEvent_?JavaObjectQ] := (* Here you examine the caretEvent to determine the extent of the selection, then query the JTextComponent for that region of text, look for Mathematica functions, etc., etc. *) >something like MathGenericListener, with a setHandler method like the other >Math*Listeners; There is already such a base class in J/Link 1.1 to help people who want to write their own listener classes. It is called MathListener, and you can see how it is used by looking at the source code for any of the MathXXXListener classes in J/Link, which are all subclasses of it. MathListener makes it completely trivial to implement a listener class that calls back into Mathematica. You don't have to deal with the link at all. As discussed above, however, the next release of J/Link will make MathListener unnecessary, and the small amount of Java code you now have to write to create such a listener will go to zero. >one drawback would be that at least JDK 1.3 would be required, but that >shouldn't be too much of a problem >(except for the 4 or 5 MacOS 9 users ;) ); We're all looking forward to a fast and solid Java 1.3 implementation in Mac OS X. Todd Gayley Wolfram Research