Re: Fruit basket program in Jython

  • From: "qubit" <lauraeaves@xxxxxxxxx>
  • To: <programmingblind@xxxxxxxxxxxxx>
  • Date: Thu, 30 Sep 2010 20:23:43 -0500

nice collection.
Thanks.
--le

----- Original Message ----- 
From: "Jamal Mazrui" <empower@xxxxxxxxx>
To: "programmingblind" <programmingblind@xxxxxxxxxxxxx>; "Program-l" 
<program-l@xxxxxxxxxxxxx>
Sent: Thursday, September 30, 2010 7:39 PM
Subject: Fruit basket program in Jython


http://EmpowermentZone.com/jvm_fruit.zip

This is an update to my collection of fruit basket programs that run on
the Java Virtual Machine (JVM).  It adds the Jython language, which
currently implements Python 2.5 on the JVM.

Jython syntax includes conveniences such as property initializers in the
call to a class constructor method.  The fact that functions are objects
enables them to be passed as a value where an event handler is expected.

Besides the additional Jython code, this distribution adds to the
description of these JVM languages and their ecosystem.  The main text
file is appended to the body of this message.

Jamal

----------

 From the archive
http://EmpowermentZone.com/jvm_fruit.zip

These fruit basket programs are written with languages that target the
cross-platform Java Virtual Machine (JVM).  They use the Standard Widget
Toolkit (SWT), a GUI library that wraps native controls of the operating
system, which implement its baseline accessibility API:  Microsoft
Active Accessibility on Windows, the Assistive Technology Service
Provider Interface on Linux/GNOME, and the Apple Accessibility Protocol
on the Mac.  On Windows, the Java Access Bridge is not needed for use
with a screen reader.  SWT is available from
http://eclipse.org/swt

Five different languages implement essentially the same fruit basket
program:  Java,
http://java.com

Groovy,
http://groovy.codehaus.org

Python
http://jython.org

Scala,
http://scala-lang.org

and
Ruby,
http://JRuby.org

Java and Scala are statically typed languages, whereas Groovy, Python,
and Ruby are dynamically typed.  Groovy is generally a superset of Java
syntax, adding syntactic sugar and making type declarations and
semicolon line endings to be generally optional.  Jython is the Python
interpreter on the JVM, supporting the same language as the original
C-based interpreter, as well as the .NET-based interpreter called
IronPython.  Similarly, JRuby is the Ruby interpreter on the JVM,
supporting the same language as the original C-based interpreter, as
well as the .NET-based interpreter called IronRuby.  Scala has strong
support for both object oriented and functional programming styles, and
although strongly typed, its use of type inference and other features
support script-like coding.  Groovy, Python, Ruby, and Scala are
distributed with interactive console environments that aid learning and
testing.  Java and Scala create JVM-based applications with comparable
performance, followed by JRuby, Groovy, and Jython (speed comparisons
vary, however, as better language compilers are developed over time).

Any text editor may be used to code in these languages, as free,
command-line compilers are included in their development kits.  The
Eclipse integrated development environment (IDE) has free plug-ins
available for all these languages -- search for downloads on
http://eclipse.org

The batch files buildJava.bat, buildGroovy.bat, buildPython.bat,
buildRuby.bat, and buildScala.bat compile and run the source code in
FruitBasket.java, FruitBasket.groovy, FruitBasket.py, FruitBasket.rb,
and FruitBasket.scala, respectively.  The appropriate bin subdirectories
are assumed to be on the search path of the operating system.  On
Windows, for example, the standard installer places the Java home
directory at a location like
c:\program Files\Java\jdk1.6.0_21

Other JVM languages could be installed at directories like
C:\Groovy
C:\Jython
C:\JRuby
C:\Scala

To minimize the chance of interoperability problems, it may also be
worth setting the following environment variables to appropriate
installation directories:  JAVA_HOME, GROOVY_HOME, JYTHON_HOME,
JRUBY_HOME, and SCALA_HOME.

This distribution additionally illustrates one, free way of building an
executable to launch a JVM application.  There are console and
Windows-mode versions of the otherwise, same executable:
FruitBasketC.exe and FruitBasketW.exe.  Note that these are not
self-standing executables.  They require a Java Runtime Environment
(JRE) to be installed in a standard location.  Code in .class and .jar
files remains in those separate files rather than being bundled into the
executable.  There are commercial builders, packagers, and installers
for JVM-based applications with many features.  This simple approach is
just a bit better than a batch file to run the JRE with a particular
classpath, main class, and other runtime parameters.  The pair of
executables is built with a free program called exeJ, available from the
following web page:

exeJ - Wraps the startup command line of your java application in an
executable
http://www.sureshotsoftware.com/exej/

exeJ is a command-line utility that takes a parameter indicating the
configuration file to use for building the console and Windows
executables, which are named

using_java.exe

and
using_javaw.exe

which one would then rename to something befitting of the particular
application.  The Jython version of this fruit basket program was used
to build the executables using the following command line:
exej.exe -cfg FruitBasket.cfg

The configuration file assumes that the jython.jar package is installed
in the directory
C:\Jython

That file would typically be distributed with a JVM application coded in
Jython.  This fruit basket distribution includes the current exeJ
package in the archive
exej.zip

Source code to these fruit basket programs is pasted below, as well as
in the separate FruitBasket.* files.  A large collection of text
tutorials on Java and other JVM languages is available at
http://EmpowermentZone.com/java_doc.zip

Jamal

----------

/*
Content of FruitBasket.java
Fruit basket program in Java with SWT
Public domain by Jamal Mazrui
September 6, 2010
*/

import org.eclipse.swt.*;
import org.eclipse.swt.events.*;
import org.eclipse.swt.graphics.*;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;

class FruitBasket {

public static void main(String[] args) {
Display display = new Display();
final Shell shell = new Shell(display);
shell.setText("Fruit Basket");

GridLayout lytGrid = new GridLayout();
lytGrid.numColumns = 3;
shell.setLayout(lytGrid);

Label lblFruit = new Label(shell, SWT.NONE);
lblFruit.setText("&Fruit:");

final Text txtFruit = new Text(shell, SWT.BORDER);

Button btnAdd = new Button(shell, SWT.PUSH);
btnAdd.setText("&Add");
shell.setDefaultButton(btnAdd);

Label lblBasket = new Label(shell, SWT.NONE);
lblBasket.setText("&Basket:");

final List lstBasket = new List(shell, SWT.BORDER | SWT.V_SCROLL);

Button btnDelete = new Button(shell, SWT.PUSH);
btnDelete.setText("&Delete");

btnAdd.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
String sFruit = txtFruit.getText().trim();
if (sFruit == "") {
ShowMessage(shell, "No fruit to add!", "Alert", SWT.OK);
}
else {
lstBasket.add(sFruit, 0);
lstBasket.select(0);
txtFruit.setText("");
}
txtFruit.setFocus();
}
});

btnDelete.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
int iIndex = lstBasket.getSelectionIndex();
if (iIndex == -1) {
ShowMessage(shell, "No fruit to delete!", "Alert", SWT.OK);
}
else {
lstBasket.remove(iIndex);
int iCount = lstBasket.getItemCount();
if (iIndex > iCount - 1) iIndex = iCount - 1;
lstBasket.select(iIndex);
}
lstBasket.setFocus();
}
});

shell.addShellListener(new ShellAdapter() {
public void shellClosed(ShellEvent e) {
if (ShowMessage(shell, "Exit program?", "Confirm", SWT.YES | SWT.NO |
SWT.CANCEL) != SWT.YES) e.doit = false;
}
});

shell.pack();
shell.open();
while(!shell.isDisposed()) {
if (!display.readAndDispatch()) display.sleep();
}
display.dispose();
} // main method

static int ShowMessage(Shell shell, String sMessage, String sText, int
iStyle) {
MessageBox mb = new MessageBox(shell, iStyle);
mb.setMessage(sMessage);
mb.setText(sText);
return mb.open();
} // showMessage method
} // FruitBasket class

----------

/*
Content of FruitBasket.groovy
Fruit basket program in Groovy with SWT
Public domain by Jamal Mazrui
September 6, 2010
*/

import org.eclipse.swt.*
import org.eclipse.swt.events.*
import org.eclipse.swt.graphics.*
import org.eclipse.swt.layout.*
import org.eclipse.swt.widgets.*

class FruitBasket {

static void main(args) {
def display = new Display()
def shell = new Shell(display)
shell.text = "Fruit Basket"

def lytGrid = new GridLayout()
lytGrid.numColumns = 3
shell.layout = lytGrid

def lblFruit = new Label(shell, SWT.NONE)
lblFruit.text = "&Fruit:"

def txtFruit = new Text(shell, SWT.BORDER)

def btnAdd = new Button(shell, SWT.PUSH)
btnAdd.text = "&Add"
shell.defaultButton = btnAdd

def lblBasket = new Label(shell, SWT.NONE)
lblBasket.text = "&Basket:"

def lstBasket = new List(shell, SWT.BORDER | SWT.V_SCROLL)

def btnDelete = new Button(shell, SWT.PUSH)
btnDelete.text = "&Delete"

btnAdd.addSelectionListener(new SelectionAdapter() {
void widgetSelected(SelectionEvent e) {
def sFruit = txtFruit.text.trim()
if(sFruit == "") {
ShowMessage(shell, "No fruit to add!", "Alert", SWT.OK)
}
else {
lstBasket.add(sFruit, 0)
lstBasket.select(0)
txtFruit.text = ""
}
txtFruit.setFocus()
}
})

btnDelete.addSelectionListener(new SelectionAdapter() {
void widgetSelected(SelectionEvent e) {
def iIndex = lstBasket.selectionIndex
if(iIndex == -1) {
ShowMessage(shell, "No fruit to delete!", "Alert", SWT.OK)
}
else {
lstBasket.remove(iIndex)
def iCount = lstBasket.itemCount
if(iIndex > iCount - 1) iIndex = iCount - 1
lstBasket.select(iIndex)
}
lstBasket.setFocus()
}
})

shell.addShellListener(new ShellAdapter() {
void shellClosed(ShellEvent e) {
if(ShowMessage(shell, "Exit program?", "Confirm", SWT.YES | SWT.NO |
SWT.CANCEL) != SWT.YES) e.doit = false
}
})

shell.pack()
shell.open()
while(!shell.isDisposed()) {
if(!display.readAndDispatch()) display.sleep()
}
display.dispose()
} // main method

static int ShowMessage(shell, sMessage, sText, iStyle) {
def mb = new MessageBox(shell, iStyle)
mb.message = sMessage
mb.text = sText
return mb.open()
} // showMessage method
} // FruitBasket class

----------

"""
Content of FruitBasket.py
Fruit basket program in Jython with SWT
Public domain by Jamal Mazrui
September 30, 2010
"""

from org.eclipse.swt import *
from org.eclipse.swt.events import *
from org.eclipse.swt.graphics import *
from org.eclipse.swt.layout import *
from org.eclipse.swt.widgets import *

def showMessage(shell, sMessage, sText, iStyle):
mb = MessageBox(shell, iStyle, message=sMessage, text=sText)
return mb.open()
# showMessage method

def addFruit(e):
sFruit = txtFruit.text.strip()
if not sFruit: showMessage(shell, 'No fruit to add!', 'Alert', SWT.OK)
else:
lstBasket.add(sFruit, 0)
lstBasket.select(0)
txtFruit.text = ''

txtFruit.setFocus()
# addFruit method

def deleteFruit(e):
iIndex = lstBasket.selectionIndex
if iIndex == -1: showMessage(shell, 'No fruit to delete!', 'Alert', SWT.OK)
else:
lstBasket.remove(iIndex)
iCount = lstBasket.itemCount
if iIndex > iCount - 1: iIndex = iCount - 1
lstBasket.select(iIndex)

lstBasket.setFocus()
# deleteFruit method

def exitProgram(e):
if showMessage(shell, 'Exit program?', 'Confirm', SWT.YES | SWT.NO |
SWT.CANCEL) != SWT.YES: e.doit = False
# exitProgram method

display = Display()
shell = Shell(display, text='Fruit Basket', shellClosed=exitProgram)
lytGrid = GridLayout(numColumns=3)
shell.setLayout(lytGrid)

lblFruit = Label(shell, SWT.NONE, text='&Fruit:')
txtFruit = Text(shell, SWT.BORDER)
btnAdd = Button(shell, SWT.PUSH, text='&Add', widgetSelected=addFruit)
shell.defaultButton = btnAdd

lblBasket = Label(shell, SWT.NONE, text='&Basket:')
lstBasket = List(shell, SWT.BORDER | SWT.V_SCROLL)
btnDelete = Button(shell, SWT.PUSH, text='&Delete',
widgetSelected=deleteFruit)

shell.pack()
shell.open()

while not shell.isDisposed():
if not display.readAndDispatch(): display.sleep()

display.dispose()

----------

/*
Content of FruitBasket.rb
Fruit basket program in JRuby with SWT
Public domain by Jamal Mazrui
September 6, 2010
*/

module FB
require 'java'
require 'swt.jar'
include_package 'org.eclipse.swt'
include_package 'org.eclipse.swt.events'
include_package 'org.eclipse.swt.graphics'
include_package 'org.eclipse.swt.layout'
include_package 'org.eclipse.swt.widgets'

def showMessage(shell, sMessage, sText, iStyle)
mb = MessageBox.new(shell, iStyle)
mb.message = sMessage
mb.text = sText
return mb.open
end # showMessage method
end # FB module

include FB
module FB
display = Display.new
shell = Shell.new(display)
shell.text = "Fruit Basket"
lytGrid = GridLayout.new
lytGrid.numColumns = 3
shell.layout = lytGrid

lblFruit = Label.new(shell, SWT::NONE)
lblFruit.text = "&Fruit:"

txtFruit = Text.new(shell, SWT::BORDER)

btnAdd = Button.new(shell, SWT::PUSH)
btnAdd.text = "&Add"
shell.defaultButton = btnAdd

lblBasket = Label.new(shell, SWT::NONE)
lblBasket.text = "&Basket:"

lstBasket = List.new(shell, SWT::BORDER | SWT::V_SCROLL)

btnDelete = Button.new(shell, SWT::PUSH)
btnDelete.text = "&Delete"

btnAdd.addSelectionListener do
sFruit = txtFruit.text.strip
if sFruit == ""
showMessage(shell, "No fruit to add!", "Alert", SWT::OK)
else
lstBasket.add(sFruit, 0)
lstBasket.select(0)
txtFruit.text = ""
end # if
txtFruit.setFocus
end # do

btnDelete.addSelectionListener do
iIndex = lstBasket.selectionIndex
if iIndex == -1
showMessage(shell, "No fruit to delete!", "Alert", SWT::OK)
else
lstBasket.remove(iIndex)
iCount = lstBasket.itemCount
iIndex = iCount - 1 if iIndex > iCount - 1
lstBasket.select(iIndex)
end # if
lstBasket.setFocus
end # do

class ClosedEvent < ShellAdapter
def initialize(shell)
@shell = shell
super()
end # initialize method

def shellClosed(e)
e.doit = false if showMessage(@shell, "Exit program?", "Confirm",
SWT::YES | SWT::NO | SWT::CANCEL) != SWT::YES
end # shellClosed methodd
end # ClosedEvent class

shell.addShellListener ClosedEvent.new(shell)

shell.pack
shell.open

display.sleep unless display.readAndDispatch while !shell.isDisposed
display.dispose
end # FB module

----------

/*
Content of FruitBasket.scala
Fruit basket program in Scala with SWT
Public domain by Jamal Mazrui
September 6, 2010
*/

import org.eclipse.swt._
import org.eclipse.swt.events._
import org.eclipse.swt.graphics._
import org.eclipse.swt.layout._
import org.eclipse.swt.widgets._

object FruitBasket extends Application {
val display = new Display()
val shell = new Shell(display)
shell.setText("Fruit Basket")

val lytGrid = new GridLayout()
lytGrid.numColumns = 3
shell.setLayout(lytGrid)

val lblFruit = new Label(shell, SWT.NONE)
lblFruit.setText("&Fruit:")

val txtFruit = new Text(shell, SWT.BORDER)

val btnAdd = new Button(shell, SWT.PUSH)
btnAdd.setText("&Add")
shell.setDefaultButton(btnAdd)

val lblBasket = new Label(shell, SWT.NONE)
lblBasket.setText("&Basket:")

val lstBasket = new List(shell, SWT.BORDER | SWT.V_SCROLL)

val btnDelete = new Button(shell, SWT.PUSH)
btnDelete.setText("&Delete")

btnAdd.addSelectionListener(new SelectionAdapter() {
override def widgetSelected(e: SelectionEvent) = {
val sFruit = txtFruit.getText().trim()
if(sFruit == "") {
ShowMessage(shell, "No fruit to add!", "Alert", SWT.OK)
}
else {
lstBasket.add(sFruit, 0)
lstBasket.select(0)
txtFruit.setText("")
}
txtFruit.setFocus()
}
})

btnDelete.addSelectionListener(new SelectionAdapter() {
override def widgetSelected(e: SelectionEvent) = {
var iIndex = lstBasket.getSelectionIndex()
if(iIndex == -1) {
ShowMessage(shell, "No fruit to delete!", "Alert", SWT.OK)
}
else {
lstBasket.remove(iIndex)
val iCount = lstBasket.getItemCount()
if(iIndex > iCount - 1) iIndex = iCount - 1
lstBasket.select(iIndex)
}
lstBasket.setFocus()
}
})

shell.addShellListener(new ShellAdapter() {
override def shellClosed(e: ShellEvent) = {
if(ShowMessage(shell, "Exit program?", "Confirm", SWT.YES | SWT.NO |
SWT.CANCEL) != SWT.YES) e.doit = false
}
})

shell.pack()
shell.open()
while(!shell.isDisposed()) {
if(!display.readAndDispatch()) display.sleep()
}
display.dispose()

def ShowMessage(shell: Shell, sMessage: String, sText : String, iStyle:
Int): Int = {
val mb = new MessageBox(shell, iStyle)
mb.setMessage(sMessage)
mb.setText(sText)
return mb.open
} // showMessage method
} // FruitBasket class


__________
View the list's information and change your settings at
//www.freelists.org/list/programmingblind

__________
View the list's information and change your settings at 
//www.freelists.org/list/programmingblind

Other related posts: