[Jython-dev] Re: JFace/SWT application - trouble with jface.action.Action event handling
by Erin Seed other posts by this author
Sep 24 2003 9:18PM messages near this date
[Jython-dev] __getattr__ weirdness
|
Antw: [Jython-dev] serious problems with jythonc - several
bugs all in one (both 2.1 and 2.2a0)
Hi,
I thought that SWT enthusiasts might be interested in a problem that I =
am having when trying to build a "simple" JFace/SWT application scripted =
using Jython. (See below if SWT and JFace are new to you.) Any =
suggestions or help in interpreting the error messages would be =
gratefully received.
Thanks,
Ian
-------------------------------------------------------------------------=
-------------------------------------------------------------------------=
-------------------------------------------------------------------------=
-------------
Contents
a.. Discussion
b.. ApplicationJFaceSWT.java in Java
c.. ApplicationJFaceSWT.py in Jython
d.. Error messages
e.. SWT/JFace
f.. Modifying jython.bat to put swt and jface .jar files in the =
classpath
-------------------------------------------------------------------------=
-------------------------------------------------------------------------=
-------------------------------------------------------------------------=
-------------
Discussion
I have been trying to translate a SWT JFace application into Jython. =
This might not be what Jython is for or is good at? (I am new to all of =
Jython, Java, Eclipse and the SWT/JFace libraries.) The Java example =
works but the Jython does not. For the Jython example, the application =
window opens, but when a menu item is selected the application crashes. =
The Jython code generates the GUI without difficulty - it is the action =
event handling that fails to work.
I am using Jython 2.1, the latest Sun runtime j2re1.4.1_04, on a PC =
platform with Windows 2000.=20
(I edit Jy/CPython in Eclipse, using the pyeclipse plug in.)
-------------------------------------------------------------------------=
-------------------------------------------------------------------------=
-------------------------------------------------------------------------=
-------------
ApplicationJFaceSWT.java in Java
/*
* A simple application using JFace
* =
http://www.eclipse.org/documentation/html/plugins/org.eclipse.platform.do=
c.isv/doc/reference/api/org/eclipse/jface/window/Window.html
* =
http://www.eclipse.org/documentation/html/plugins/org.eclipse.platform.do=
c.isv/doc/reference/api/org/eclipse/jface/window/ApplicationWindow.html
* =
http://www.eclipse.org/documentation/html/plugins/org.eclipse.platform.do=
c.isv/doc/reference/api/org/eclipse/jface/action/Action.html
* =
http://www.eclipse.org/documentation/html/plugins/org.eclipse.platform.do=
c.isv/doc/reference/api/org/eclipse/jface/action/MenuManager.html
* http://www-106.ibm.com/developerworks/java/library/os-ecgui1/
*=20
* Must have boot, jface, runtime, swt and workbench .jar files on build =
path
* Part of the Eclipse distribution http://www.eclipse.org
*/
import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.GroupMarker;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.window.ApplicationWindow;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
public class ApplicationJFaceSWT extends ApplicationWindow {
public ApplicationJFaceSWT(Shell parentShell) {
super(parentShell);
}
public static void main(String[] args) {
Display display =3D new Display(); //swt.widgets
Shell shell =3D new Shell(display); //swt.widgets
ApplicationJFaceSWT application =3D new ApplicationJFaceSWT(shell);
application.createComponents();
application.open(); //jface.window.Window
display.close();
}
//-----------------------------------------------
// All application specific methods=20
// (i.e. those not overriding superclass methods)
//-----------------------------------------------
public void createComponents() {
// --- call ApplicationWindow methods
addMenuBar();
//addStatusLine();
//addToolBar(SWT.FLAT | SWT.WRAP);
// --- call Window method
setBlockOnOpen(true);
};
private MenuManager createMyMenu1Menu() {
MenuManager menu =3D new MenuManager("MyMenu1", "M01MI01");
menu.add(new GroupMarker("MyMenu1Item1"));
menu.add(createMyMenu1Item1Action());
return menu;
}
private Action createMyMenu1Item1Action() {
return new Action() { // Anonymous inner class
public String getText() {
return "Text for Menu 1 Item 1";
}
public void run() {
System.out.println("Implement menu item 1.1 consequence");
}
};
}
//-----------------------------------------------
// Methods that override superclass methods
//-----------------------------------------------
protected MenuManager createMenuManager() {
MenuManager menuManager =3D new MenuManager();
menuManager.add(createMyMenu1Menu());
return menuManager;
}
protected void handleShellCloseEvent() {
System.out.println("Thank you. Goodbye.");
setReturnCode(CANCEL);
close();
}
// createContents - extend or reimplement to create controls before =
window opens=20
protected Control createContents(Composite parent) {
Table composite =3D new Table(parent, SWT.BORDER);
return composite;
}
}
-------------------------------------------------------------------------=
-------------------------------------------------------------------------=
-------------------------------------------------------------------------=
-------------
ApplicationJFaceSWT.py in Jython
"""/
* A simple application using JFace
* See
* =
http://www.eclipse.org/documentation/html/plugins/org.eclipse.platform.do=
c.isv/doc/reference/api/org/eclipse/jface/window/Window.html
* =
http://www.eclipse.org/documentation/html/plugins/org.eclipse.platform.do=
c.isv/doc/reference/api/org/eclipse/jface/window/ApplicationWindow.html
* =
http://www.eclipse.org/documentation/html/plugins/org.eclipse.platform.do=
c.isv/doc/reference/api/org/eclipse/jface/action/Action.html
* =
http://www.eclipse.org/documentation/html/plugins/org.eclipse.platform.do=
c.isv/doc/reference/api/org/eclipse/jface/action/MenuManager.html
* http://www-106.ibm.com/developerworks/java/library/os-ecgui1/
*=20
* Must have boot, jface, runtime, swt and workbench .jar files on =
Jython classpath
* Part of the Eclipse distribution http://www.eclipse.org
"""
import java
import org.eclipse.jface
import org.eclipse.swt
#import org.eclipse.core
#import org.eclipse.ui
# This named class is in lieu of an anonymous inner class in Java
class MyMenu1Item1Action(org.eclipse.jface.action.Action):
def getText(self):
print("In getText")
return "Menu 1 Item 1"
def run(self):
print("In run")
pass
=20
class ApplicationJFaceSWT(org.eclipse.jface.window.ApplicationWindow):
#---------------------------------------------------
# Novel methods (not overridden superclass methods)
#---------------------------------------------------
def createComponents(self):
print("Creating components: menubar")
self.addMenuBar()
#addStatusLine();
#addToolBar(org.eclipse.swt.SWT.FLAT | =
org.eclipse.swt.SWT.WRAP);
self.setBlockOnOpen(1)
=20
def createMyMenu1Menu(self):
print("Creating MyMenu1 menu")
menumgr =3D org.eclipse.jface.action.MenuManager("MyMenu1", =
"M01MI01")
menumgr.add(org.eclipse.jface.action.GroupMarker("Menu1Item1"))
menumgr.add(self.createMyMenu1Item1Action())
return menumgr
def createMyMenu1Item1Action(self):
print("Creating MyMenu1Item1 action")
return MyMenu1Item1Action()=20
=20
#---------------------------------------------------
# Overridden superclass methods
#---------------------------------------------------
def createMenuManager(self):
print("Creating menu manager")
menuManager =3D org.eclipse.jface.action.MenuManager()
menuManager.add(self.createMyMenu1Menu())=20
return menuManager
=20
def handleShellCloseEvent(self):
print("Goodbye!")
self.setReturnCode(self.CANCEL)
self.close()
def createContents(self, parentComposite):
print("Creating contents")
childComposite =3D =
org.eclipse.swt.widgets.Table(parentComposite, =
org.eclipse.swt.SWT.BORDER)
return childComposite
if __name__ =3D=3D '__main__':
print("Starting __main__")
display =3D org.eclipse.swt.widgets.Display() =20
shell =3D org.eclipse.swt.widgets.Shell(display)
application =3D ApplicationJFaceSWT(shell)=20
application.createComponents()
application.open() =20
-------------------------------------------------------------------------=
-------------------------------------------------------------------------=
-------------------------------------------------------------------------=
-------------
Error messages
java.lang.NoClassDefFoundError: =
org/eclipse/core/internal/boot/DelegatingURLClassLoader
at org.eclipse.core.runtime.Platform.getDebugOption(Platform.java:272)
at =
org.eclipse.jface.action.ActionContributionItem.handleWidgetSelection(Act=
ionContributionItem.java:452)
at =
org.eclipse.jface.action.ActionContributionItem.handleWidgetEvent(ActionC=
ontributionItem.java:403)
at =
org.eclipse.jface.action.ActionContributionItem.access$0(ActionContributi=
onItem.java:397)
at =
org.eclipse.jface.action.ActionContributionItem$ActionListener.handleEven=
t(ActionContributionItem.java:72)
at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:81)
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:840)
at org.eclipse.swt.widgets.Display.runDeferredEvents(Display.java:1838)
at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:1545)
at org.eclipse.jface.window.Window.runEventLoop(Window.java:583)
at org.eclipse.jface.window.Window.open(Window.java:563)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at =
org.python.core.PyReflectedFunction.__call__(PyReflectedFunction.java)
at org.python.core.PyMethod.__call__(PyMethod.java)
at org.python.core.PyObject.__call__(PyObject.java)
at org.python.core.PyInstance.invoke(PyInstance.java)
at org.python.pycode._pyx0.f$0(ApplicationJFaceSWT.py:75)
at org.python.pycode._pyx0.call_function(ApplicationJFaceSWT.py)
at org.python.core.PyTableCode.call(PyTableCode.java)
at org.python.core.PyCode.call(PyCode.java)
at org.python.core.Py.runCode(Py.java)
at org.python.core.__builtin__.execfile_flags(__builtin__.java)
at org.python.util.PythonInterpreter.execfile(PythonInterpreter.java)
at org.python.util.jython.main(jython.java)
-------------------------------------------------------------------------=
-------------------------------------------------------------------------=
-------------------------------------------------------------------------=
-------------
SWT/JFace
The standard widget toolkit (SWT), developed by IBM and now open source =
is an alternative GUI framework to Sun's AWT/Swing and is used to build =
the Eclipse Java IDE and development framework. Both Swing and SWT have =
their advantages and disadvantages; and these are discussed at length on =
other newsgroups. SWT is useful for people who wish to write plug-ins =
for the Eclipse framework. On top of SWT lives JFace. From an IBM =
developworks article=20
http://www-106.ibm.com/developerworks/java/library/os-ecgui1/=20
I quote:
"Two of its major components are a graphical library called SWT and a =
matching utility framework called JFace. I'll concentrate on these =
components in this article. The Eclipse Technical Overview on the =
Eclipse Web site (see Resources later in this article) describes them =
like this:
a.. SWT is a widget set and graphics library integrated with the =
native window system but with an OS-independent API.=20
b.. JFace is a UI toolkit implemented using SWT that simplifies common =
UI programming tasks. JFace is window-system-independent in both its API =
and implementation, and is designed to work with SWT without hiding it."
The Java docs are here:
http://www.eclipse.org/documentation/html/plugins/org.eclipse.platform.do=
c.isv/doc/reference/api/index.html
See also:
http://www.eclipse.org/
http://www.eclipse.org/swt/
http://www-106.ibm.com/developerworks/java/library/os-ecgui1/
http://www.eclipse.org/documentation/html/plugins/org.eclipse.platform.do=
c.isv/doc/reference/api/org/eclipse/jface/window/Window.html
http://www.eclipse.org/documentation/html/plugins/org.eclipse.platform.do=
c.isv/doc/reference/api/org/eclipse/jface/window/ApplicationWindow.html
http://www.eclipse.org/documentation/html/plugins/org.eclipse.platform.do=
c.isv/doc/reference/api/org/eclipse/jface/action/Action.html
http://www.eclipse.org/documentation/html/plugins/org.eclipse.platform.do=
c.isv/doc/reference/api/org/eclipse/jface/action/MenuManager.html
=20
-------------------------------------------------------------------------=
-------------------------------------------------------------------------=
-------------------------------------------------------------------------=
-------------
Modifying jython.bat
So that jython can work with SWT JFace, I have modified my jython.bat =
file as follows. There may be a better way. Suggestions?
set PATH_SWT=3D D:\Program =
Files\eclipse\plugins\org.eclipse.core.boot_2.1.1\boot.jar
set PATH_SWT=3D%PATH_SWT%;D:\Program =
Files\eclipse\plugins\org.eclipse.jface_2.1.1\jface.jar
set PATH_SWT=3D%PATH_SWT%;D:\Program =
Files\eclipse\plugins\org.eclipse.core.runtime_2.1.1\runtime.jar
set PATH_SWT=3D%PATH_SWT%;D:\Program =
Files\eclipse\plugins\org.eclipse.swt.win32_2.1.1\ws\win32\swt.jar
set PATH_SWT=3D%PATH_SWT%;D:\Program =
Files\eclipse\plugins\org.eclipse.ui.workbench_2.1.1\workbench.jar
"D:\Program Files\Java\j2re1.4.1_04\bin\java.exe" =
"-Dpython.home=3DD:\Program Files\Jython21" -classpath "D:\Program =
Files\Jython21\jython.jar;%CLASSPATH%;%PATH_SWT%" org.python.util.jython =
%ARGS%
Attachments:
unknown1
|