Scripting for Pnuts 
by John H. McCoy


Example 1: 

(function(){catch(FileNotFoundException, function(e) error(e))
    if (!f.exists()){
      throw(FileNotFoundException(f.getPath()+" not found"))}
})()

Example 2: 

b1=JButton("Test")
bind(b1,"actionPerformed",function(e) println("Press to test, Release to detonate"))


Listing One
//JugglerBook.pnut. Implements a tear-off toolbar with radio-button selection 
//  of plaf, creates two instances of the Duke juggler animation--one 
//  controlled by start/stop buttons and the other with a slider.
// Usage:  Assumes all scripts/class files are unzipped into apps/juggler.
//          Load script and run.  Doesn't catch window closing events.
//  John H. McCoy
//  csc_jhm@shsu.edu
//  Sam Houston State University

import("javax.swing.*")
import("javax.swing.border.*")
import("java.awt.*")
import("javax.swing.event.*")
import("pnuts.awt.*")
load("apps/juggler/LNF")
load("apps/juggler/StartStopJuggler")
load("apps/juggler/SliderJuggler")
UIManager::setLookAndFeel(UIManager::getSystemLookAndFeelClassName())
jtb=JToolBar()
jtb.addSeparator()
jtb.setFloatable(true)
page=JPanel()
page.setBorder(BevelBorder(BevelBorder::RAISED))
layout(page,[PnutsLayout,"cols=1,halign=fill,valign=fill,,expand=xy",
                                                        SSJ::page,SJ::page])
jf=JFrame("Goobers Demo")
LNF::addLNFChooser(jtb, jf.getContentPane())
jf.getContentPane().add(jtb,BorderLayout::NORTH)
jf.getContentPane().add(page,BorderLayout::CENTER)
jf.getContentPane().setBorder(BevelBorder(BevelBorder::RAISED))
jf.setSize(500,360)
jf.setVisible(true)

Listing Two
//  LNF.pnut. Essentially a script implementation of the look-and-feel
//    from the Swing demos. Creates a radio button group and adds
//    it to a specified buttonBox container.
//  Usage:  load("path/LNF")
//          LNF::addLNFChooser(buttonBox, UIRoot)
//    The button group selection is initialized to lnf in effect
//    Selecting a button updates the UItree at UIRoot with the
//    corresponding lnf.
//  John H. McCoy
//  csc_jhm@shsu.edu
//  Sam Houston State University

import("java.awt.*")
import("java.awt.event.*")
import("javax.swing.*")
import("Pnuts.lang.*")
package ("LNF")
function myRadioListener(e){
  lnfName = e.getActionCommand()
  UIManager::setLookAndFeel(lnfName)
  SwingUtilities::updateComponentTreeUI(UIRoot)}
function addLNFChooser(buttonBox, UIRoot){
   LNF::UIRoot=UIRoot
   metal= "Metal"
   metalClassName = "javax.swing.plaf.metal.MetalLookAndFeel"
   motif = "Motif"
   motifClassName = "com.sun.java.swing.plaf.motif.MotifLookAndFeel"
   windows = "Windows"
   windowsClassName = "com.sun.java.swing.plaf.windows.WindowsLookAndFeel"
   // Create the buttons.
   metalButton = JRadioButton(metal)
   metalButton.setActionCommand(metalClassName)
   motifButton = JRadioButton(motif)
   motifButton.setActionCommand(motifClassName)
   windowsButton = JRadioButton(windows)
   windowsButton.setActionCommand(windowsClassName)
   // Group the radio buttons.
   group = ButtonGroup()
   group.add(metalButton)
   group.add(motifButton)
   group.add(windowsButton)
   // Register a listener with the buttons.
   bind(metalButton,"actionPerformed",myRadioListener)
   bind(motifButton,"actionPerformed",myRadioListener)
   bind(windowsButton,"actionPerformed",myRadioListener)
   lnfName = UIManager::getLookAndFeel().getClass().getName()
   if (lnfName.indexOf(metal) >= 0) {
      metalButton.setSelected(true)
   } else if (lnfName.indexOf(windows) >= 0) {
      windowsButton.setSelected(true)
   } else if (lnfName.indexOf(motif) >= 0) {
      motifButton.setSelected(true)
   } else {
      System.err.println("Attempting to use an unknown L&F: " + lnfName);
   }
   //Add the button to the specified container
   buttonBox.add(metalButton)
   buttonBox.add(motifButton)
   buttonBox.add(windowsButton)
}

Listing Four
//  StartStopJuggler.pnut. Script that creates a lightweight panel and 
//    displays Duke Juggler applet adding start/stop buttons to control 
//    the juggling
//  John H. McCoy
//  csc_jhm@shsu.edu
//  Sam Houston State University
//  Assumes the Juggler applet is in Java package apps.juggler and scripts
//  are in apps/juggler folder.
//  Usage:  load("apps/juggler/SSJ")
//          <component name>.add(SSJ::page)

import("java.awt.*")
import("javax.swing.*")
import("javax.swing.border.*")
import("pnuts.awt.*")
package("SSJ")
page=JPanel()
page.setBorder(BevelBorder(BevelBorder::RAISED))
juggler=class apps.juggler.Juggler()
juggler.setSize(juggler.getPreferredSize())
start=JButton("Start Juggler")
start.setBackground(Color::green)
stop=JButton("Stop Juggler")
stop.setBackground(Color::red)
layout(page,[FlowLayout,[],start,juggler,stop])
bind(start,"actionPerformed",function(e) juggler.start())
bind(stop,"actionPerformed", function(e) juggler.stop())

package("")

//  Remove "//" in last line to test as a standalone frame
(function testSSJ(){
   jf=JFrame("Test Start Stop Juggler")
   jf.getContentPane().add(SSJ::page)
   jf.setSize (400,400)
   jf.setVisible(true)
})//()

Listing Five
//  SliderJugger.pnut. Script that creates a lightweight panel and then 
//    instantiates and displays Duke Juggler applet. Slider is used to control
//    the juggling rate
//  John H. McCoy
//  csc_jhm@shsu.edu
//  Sam Houston State University
//  Assumes the Juggler applet is in Java package apps.juggler and scripts
//  are in apps/juggler folder.
//  Usage:  load("apps/juggler/SJ")
//          <component name>.add(SJ::page)

import("javax.swing.*")
import("javax.swing.event.*")
import("javax.swing.border.*")

package("SJ")
page=JPanel()
page.setBorder(BevelBorder(BevelBorder::RAISED))
jsl=JSlider(JSlider::HORIZONTAL,10,100,50)
jsl.setInverted(true)
tb=TitledBorder("  slower        SPEED        faster  ")
tb.setTitleJustification(TitledBorder::CENTER)
tb.setTitlePosition(TitledBorder::BOTTOM)
jsl.setBorder(tb)
juggler=class apps.juggler.Juggler()
juggler.setSize(juggler.getPreferredSize())
juggler.setAnimationRate(jsl.getValue())
registerEventListener(ChangeListener)
bind(jsl,"stateChanged",function(e)
      juggler.setAnimationRate(jsl.getValue()))
page.add(juggler)
page.add(jsl)
juggler.start()

package("")

//  Remove "//" in last line to test as a standalone frame
(function testSJ(){
   jf=JFrame("Test Slider Juggler")
   jf.getContentPane().add(SJ::page)
   jf.setSize (400,200)
   jf.setVisible(true)
})//()





4


