Friday, May 15, 2009

Rapid application development with Jython and JFace (part I)

In this series of articles I'll show you how to create applications with jython and jface. I assume that you know syntax of python if not please first visit this fine tutorial here or any other. We'll start by setting up environment needed for development and will create the simplest jface application which will be foundation for further development.

Jython

Jython, lest you do not know of it, is the most compelling weapon the Java platform has for its survival into the 21st century:-) [Sean McGrath, CTO, Propylon]

Download Jython

What are your waiting for? Go and download jython from here now! Installation is easy jest execute command java -jar jython_installer-2.5.0.jar and follow instructions.

SWT: The Standard Widget Toolkit

SWT is an open source widget toolkit for Java designed to provide efficient, portable access to the user-interface facilities of the operating systems on which it is implemented. I recommend using prepared SWT packages from your Eclipse installation. You are using Eclipse right?

Needed jar files are the following:

  • org.eclipse.swt.gtk.linux.x86_3.4.0.xxx.jar
  • org.eclipse.swt_3.4.0.xxx.jar
  • org.eclipse.jface_3.4.0.xxx.jar
  • org.eclipse.core.commands_3.4.0.xxx.jar
  • org.eclipse.osgi_3.4.0.xxx.jar
  • org.eclipse.equinox.common_3.4.0.xxx.jar

Setting environment

To be able to run Jython/SWT/JFace application you should properly set your classpath (or configure your Eclipse IDE). If your are using Linux operating system you should execute the following commands:

CLASSPATH=$CLASSPATH:"[path to jar]/org.eclipse.swt.gtk.linux.x86_3.4.0.xxx.jar"
CLASSPATH=$CLASSPATH:"[path to jar]/org.eclipse.swt_3.4.0.xxx.jar"
CLASSPATH=$CLASSPATH:"[path to jar]/org.eclipse.jface_3.4.0.xxx.jar"
CLASSPATH=$CLASSPATH:"[path to jar]/org.eclipse.core.commands_3.4.0.xxx.jar"
CLASSPATH=$CLASSPATH:"[path to jar]/org.eclipse.osgi_3.4.0.xxx.jar"
CLASSPATH=$CLASSPATH:"[path to jar]/org.eclipse.equinox.common_3.4.0.xxx.jar"
export CLASSPATH
For Windows execute the following command:
set CLASSPATH=.;[path to jar]\org.eclipse.swt.win32.win32.x86_3.4.xxx.jar;[path to jar]\org.eclipse.swt_3.4.xxx.jar;[path to jar]\org.eclipse.jface_3.4.xxx.jar;[path to jar]\org.eclipse.core.commands_3.4.xxx.jar;[path to jar]\org.eclipse.osgi_3.4.xxx.jar;[path to jar]\org.eclipse.equinox.common_3.4.xxx.jar

Code:
"""
Hello world application with Jython and JFace
GUID of this code snippet: cb4f368e-3b6b-42fe-ab1d-6f80e77ee3f5 
Author: Darius Kucinskas (c) 2008-2009
Email: d[dot]kucinskas[eta]gmail[dot]com
Blog: http://blog-of-darius.blogspot.com/
License: GPL
"""

from org.eclipse.swt import *
from org.eclipse.swt.SWT import *
from org.eclipse.swt.widgets import *
from org.eclipse.jface.window import *

class SWTApp(ApplicationWindow):
    """ Your first JFace application in jython """
    
    def __init__(self, shell):
        """ application constructor """
        ApplicationWindow.__init__(self, shell)
    
    def dispose(self):
        """ dispose resources here """
        pass
    
    def createContents(self, parent):
        """ 
        Creates the main window's contents
        parent - the main window
        return - control
        """ 
        self.getShell().text = 'First Jython & JFace App'
        self.label = Label(parent, SWT.CENTER)
        self.label.text = 'Hello, World'
        return self.label

if __name__ == "__main__":
    """ The application entry point """
    display = Display()
    shell = Shell(display)
    
    app = SWTApp(shell)
    try:
        app.setBlockOnOpen(True)
        app.open()
    finally:
        if app != None:
            app.dispose()
    
    display.dispose()

Save this code in file swtapp.py.

Execute your program:

Now execute your application: jython swtapp.py. There it is, your first jython and jface application.