Fruit baskets on the Java Virtual Machine

  • From: Jamal Mazrui <empower@xxxxxxxxx>
  • To: programmingblind <programmingblind@xxxxxxxxxxxxx>, Program-l <program-l@xxxxxxxxxxxxx>
  • Date: Mon, 06 Sep 2010 18:26:18 -0400

From the archive at
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

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

Groovy,
http://groovy.codehaus.org

Scala,
http://scala-lang.org

and Ruby,
http://JRuby.org

Java and Scala are statically typed languages, whereas Groovy 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. Scala has strong support for both object oriented and functional programming styles. 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. Ruby, Groovy, and Scala are distributed with interactive console environments that aid learning. Java and Scala have comparable performance, followed by JRuby, and then Groovy.

The batch files BuildGroovy.bat, BuildJava.bat, BuildRuby.bat, and BuildScala.bat compile and run the source code in FruitBasket.groovy, FruitBasket.java, 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:\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, JRUBY_HOME, and SCALA_HOME.

Source code to these fruit basket programs is also pasted below. A large collection of text tutorials on Java and other JVM languages is 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.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

Other related posts: