Simple windowed application: Difference between revisions

Content added Content deleted
No edit summary
(→‎{{header|ooRexx}}: significantly shortened)
Line 1,154:
 
=={{header|ooRexx}}==
<lang oorexx>/* REXX ***************************************************************************************
<lang oorexx>#!/usr/bin/rexx
* 18.06.2014 Walter Pachl shortened from Rony Flatscher's bsf4oorexx (see sourceforge) samples
/*
* Look there Name:for "ShowCount.rxj"
* bsf4oorexx lets the ooRexx program use Java classes
 
**********************************************************************************************/
Invoke: "rexx Showcount.rxj"
or
"rexxj Showcount.rxj" (Windows)
or
"rexxj.sh Showcount.rxj" (Unix)
 
Purpose: create a little resizable window with a push button to count and reveal
the number of times the button was pressed
 
Needs: Apache's "Bean Scripting Framework" (BSF), part of the BSF4ooRexx distribution
 
Date: 2001-04-30 Rony Flatscher presented this at the 12th International Rexx Symposium
Changed: 2005-12-28, added Apache license
2008-08-23, ---rgf, if using BSF.CLS, then do not use BSF() directly (or
remove the first three chars from its result string)
2009-07-03, ---Walter Pachl add help and improve a little bit
2010-05-16, rgf, - adapting example to take full advantage of BSF4ooRexx
- replacing bsf.addEventListener[WithEventObject] with a Rexx event handler
2012-06-09, rgf, - inhibit callbacks from Java after Rexx ends (if Rexx loaded Java)
2014-06-17 Walter Pachl copied from bsf4oorexx samples to rosettacode.org
*/
 
If arg(1)='?' Then Do
Parse Source source
Say source
Say 'This program displays a window with a button at its bottom'
Say 'In the middle of this window you will be told'
Say ' how often you have pressed this very button.'
Exit
End
 
hi=BsfInvokedBy()
Select
When hi=1 then say "This Rexx program was invoked by Java!"
When hi=2 then say "This Rexx program was invoked by Rexx, JVM loaded by Rexx!"
Otherwise Do; say "No JVM present, we got troubles ..."
Exit
End
End
 
 
-- Create a userData directory
userData=.directory~new -- a directory which will be passed to Rexx with the event
 
Line 1,207 ⟶ 1,166:
rexxCloseEH =.RexxCloseAppEventHandler~new -- Rexx event handler
-- Create Java RexxProxy for the Rexx event handler
rpCloseEH=BsfCreateRexxProxy(rexxCloseEH, , "java.awt.event.WindowListener" )
win~addWindowListener(rpCloseEH) -- add RexxProxy event handler
 
-- create a Java push button
but=.bsf~new("java.awt.Button", "Press me!")
-- Create a RexxProxy for the button Rexx event handler
rp=BsfCreateRexxProxy(.RexxShowCountEventHandler~new, userData, "java.awt.event.ActionListener")
but~addActionListener(rp) -- add RexxProxy event handler
 
lab=.bsf~new("java.awt.Label") -- create a Java label, set it to show the text centered
userData~label=lab -- save label object for later use in event handler
lab=.bsf~new("java.awt.Label")
lab~setAlignment(lab~center) -- set alignment to center
userData~label=lab -- save label object for later use in event handler
lab~setText("Button was not yet pressed") -- assign ainitial text to the label
lab~setAlignment(lab~center) -- set alignment to center
lab~setText("Button was not yet pressed") -- assign a text to the label
 
win ~~add("Center", lab) ~~add("South", but) -- add the label and the button to the frame
win ~~pack -- now calculate all widget dimensions
call enlargeWidth win,but,90120 -- enlarge the width of the frame and the button
 
win ~~setVisible(.true) ~~toFront -- make frame visible and move it to the front
 
userData~i=0 -- set counter to 0
 
rexxCloseEH~waitForExit -- wait until we are allowed to end the program
 
-- if Java was loaded by Rexx, then terminate Java's RexxEngine to inhibit callbacks from Java
call BSF.terminateRexxEngine
 
 
::requires BSF.CLS -- get Java support
Line 1,240 ⟶ 1,197:
/* enlarge the width of the frame and of the button without using a layout manager */
::routine enlargeWidth
use arg win, but, addToWidth
winDim=win~getSize -- get frame's dimension
 
winDim~width+=addToWidth -- increase width
say "---"
win~setSize(winDim) -- set frame's dimension
say "win_size (standard):" win~getSize~toString
say "but_size (standard):" but~getSize~toString
 
winDim=win~getSize -- get frame's dimension
winDim~width+=addToWidth -- increase width
win~setSize(winDim) -- set frame's dimension
 
say "win_size (enlarged):" win~getSize~toString
say "but_size (enlarged):" but~getSize~toString
say "---"
 
 
/* ------------------------------------------------------------------------ */
/* Rexx event handler to set "close app" indicator */
::class RexxCloseAppEventHandler
::method init -- constructor
expose closeApp
closeApp = .false -- if set to .true, then it is safe to close the app
 
::attribute closeApp -- indicates whether app should be closed
 
::method unknown -- intercept unhandled events, do nothing
 
::method windowClosing -- event method (from WindowListener)
expose closeApp
closeApp=.true -- indicate that the app should close
 
::method waitForExit -- method blocks until attribute is set to .true
expose closeApp
guard on when closeApp=.true
 
 
/* ------------------------------------------------------------------------ */
Line 1,279 ⟶ 1,225:
::class RexxShowCountEventHandler
::method actionPerformed
use arg eventObject, slotDir
call showCount slotDir~userData
 
 
/* ------------------------------------------------------------------------ */
::routine ShowCount -- queryincrement andcounter displayand currentshow sizetext
use arg userData
userData~i+=i1 -- save currentincrement valuecounter in directory object
 
i=userData~i+1Select -- get and increment counter, save in local var "i" -- construct text part
When userData~i=21 Then texthow_often='twiceonce'
userData~i=i -- save current value in directory object
When userData~i=32 Then texthow_often='three timestwice'
Select
When userData~i=13 Then texthow_often='oncethree times'
Otherwise texthow_often=userData~i 'times'
When i=2 Then text='twice'
When i=3 Then text='three times'
Otherwise text=i 'times'
End
userData~label~setText("Button was pressed" texthow_often) -- display text</lang>
Output:
<pre>rexx ShowCount.rxj
This Rexx program was invoked by Rexx, JVM loaded by Rexx!
---
win_size (standard): java.awt.Dimension[width=180,height=91]
but_size (standard): java.awt.Dimension[width=162,height=23]
win_size (enlarged): java.awt.Dimension[width=270,height=91]
but_size (enlarged): java.awt.Dimension[width=252,height=23]
---</pre>
 
=={{header|Oz}}==