From: Josh J. <jun...@gm...> - 2010-01-27 12:58:47
|
Rasjid/Marcos- This approach has also crossed my mind. I'd really like to implement something in the PlyJy project that would allow for one to create a Jython applet without worrying about the object factory design. I started an implementation some time ago, but was never really able to embed the resulting applet into a webpage and make it work. Perhaps with a little more time we can come up with a variant of my implementation that would make this approach usable. So far, I've created a "test" facade which is actually a Java applet class which invokes the PythonInterpreter from within the init() method. Basically, the Jython code is not really an applet in this approach, but rather just swing code that is added to this applet facade. In the end, the facade would be the embedded applet and it would call to the Jython code to create the actual GUI. The implementation I've started is not reusable and we'd have to take it a step further in order to make it so, but it is a start. AppletFacade.java - This is the actual (likely reusable at some point) applet that will become part of PlyJy. In this hard-coded implementation I am instantiating a Jython module named "Ouch" which simply places a button on the applet. Maybe this could be an abstract class in a reusable implementation. package org.plyjy.facade; import java.util.ArrayList; import java.util.List; import javax.swing.JApplet; import javax.swing.SwingUtilities; import javax.swing.JLabel; import org.plyjy.factory.JythonObjectFactory; import org.plyjy.factory.PySystemObjectFactory; import org.plyjy.interfaces.AppletType; public class AppletFacade extends JApplet { private String appletClass = "Ouch"; private List argList = new ArrayList(); //Called when this applet is loaded into the browser. @Override public void init() { // Add a reference to this applet into our list...this will be passed to the Jython code this.argList.add(this); //Execute a job on the event-dispatching thread; creating this applet's GUI. try { SwingUtilities.invokeAndWait(new Runnable() { public void run() { PySystemObjectFactory factory = new PySystemObjectFactory( AppletType.class, appletClass, appletClass); AppletType app = (AppletType) factory.createObject(); app.run_applet(argList); } }); } catch (Exception e) { System.err.println("createGUI didn't complete successfully " + e); } } public void setArgList(){ // Empty method to override...perhaps use to load values in reusable case? } } AppletType.java - This is the interface which all Jython GUI code will need to implement in our solution package org.plyjy.interfaces; import java.util.List; public interface AppletType { public void run_applet(List args); } Ouch.py - Adds a button to the applet. We can create a GUI here and simply add it to the applet object which is passed in via the argList. import javax.swing as swing from org.plyjy.interfaces import AppletType class Ouch (AppletType): def init(self): # Probably do something else here....just a test self.frame = None self.button = None def print_message(self, event): print "Ouch!" def run_applet(self, *args): arrayList = args[0] self.frame = arrayList.get(0) self.button=swing.JButton("Push Me!", actionPerformed=self.print_message) self.frame.contentPane.add(self.button) Like I said, this approach runs fine on my desktop, but it doesn't work when I embed it into an HTML page. Perhaps I just need to work a bit with it to make it happen. I haven't committed this code to PlyJy as I wanted to make it function properly first. However, perhaps it would be desirable for me to commit it so that others can work on it as well. :) Is this the idea you had in mind Marcos? Best Josh Juneau jun...@gm... http://jj-blogger.blogspot.com http://www.jythonpodcast.com Twitter ID: javajuneau 2010/1/26 Rasjid Wilcox <ra...@gm...> > 2010/1/27 Marcos Sánchez Provencio <msa...@gr...> > > But you can use a pythoninterpreter in your applet classes, can't you? >> You just have to include your py sources as resources. I have a test >> applet that adds 2+2 and show sys.version. In theory, you could add your >> jython java object factory to this mix... >> >> By the way, what is the minimal set of permissions for jython to work? I >> am testing with >> grant { >> permission java.security.AllPermission; >> }; >> >> Rasjid, is that close to what you wanted? >> >> > Yes, this approach occurred to me late last night. If you can share your > test applet, that would be great, since I'd be kind of working it out from > scratch. (I have minimal exposure to Java.) > > Cheers, Rasjid. > > |