BBjSpinner Example

rem 'Spinner sample

USE java.util.HashMap

rem 'Obtain the instance of the BBjAPI object
let myAPI!=BBjAPI()

rem 'Open the SysGui device
SYSGUI=UNT
OPEN (SYSGUI)"X0"

rem 'Obtain the instance of the BBjSysGui object
let mySysGui!=myAPI!.getSysGui()

rem 'Set addWindow param values
X=200
Y=200
WIDTH=350
HEIGHT=250
TITLE$="Programming Languages"

rem 'Set the current context
mySysGui!.setContext(0)

rem 'Create a window
myWindow! = mySysGui!.addWindow(X,Y,WIDTH,HEIGHT,TITLE$,$00010083$)

rem 'Create programming language data
myLangList! = myAPI!.makeVector()
myDescMap! = new HashMap()
dummy = fnAddLanguage(myLangList!,
:                     myDescMap!,
:                     "BBx",
:                     "A Business BASIC developed by BASIS International, Ltd.")
dummy = fnAddLanguage(myLangList!,
:                     myDescMap!,
:                     "Java",
:                     "An Object-Oriented C++ derivative developed by Sun Microsystems")
dummy = fnAddLanguage(myLangList!,
:                     myDescMap!,
:                     "C++",
:                     "A low-level object oriented language developed by Bjarne Stroustrup")

rem 'Mask for inpute's
mask$ = "XXXXXXXXXXXXXXXXXXX"

rem 'Create the spinner
myLangSpinner! = myWindow!.addInputESpinner(101, 10, 10, 96, 25, $$, mask$, " ", myLangList!)

rem 'Create the inpute for the name
myLangName! = myWindow!.addInputE(103, 10, 45, 80, 25, $$, mask$, " ", myLangList!.get(0))

rem 'Create the CEDIT for the description
firstDesc$ = myDescMap!.get(myLangList!.get(0))
myLangDesc! = myWindow!.addCEdit(104, 10, 80, 240, 160, firstDesc$)
myLangDesc!.setLineWrap(1)

rem 'Create a button for controlling the slideshow
mySlideshowButton! = myWindow!.addButton(102, 220, 10, 120, 25, "Start Slideshow")

rem 'Create a button for adding new languages
myAddButton! = myWindow!.addButton(105, 260, 80, 80, 25, "Add")

rem 'Create a button for deleting existing languages
myRemoveButton! = myWindow!.addButton(106, 260, 115, 80, 25, "Remove")

rem 'Set callbacks
myWindow!.setCallback(myWindow!.ON_CLOSE,"APP_CLOSE")
myLangSpinner!.setCallback(myLangSpinner!.ON_SPIN, "SPIN")
myAddButton!.setCallback(myAddButton!.ON_BUTTON_PUSH, "ADD_LANGUAGE")
myRemoveButton!.setCallback(myRemoveButton!.ON_BUTTON_PUSH, "REMOVE_LANGUAGE")
mySlideshowButton!.setCallback(myRemoveButton!.ON_BUTTON_PUSH, "SLIDESHOW")
process_events

rem 'Callback for the ON_SPIN event
SPIN:
    rem 'Get the BBjSpinEvent
    ev! = myAPI!.getLastEvent()

    rem 'Get the text from the event and associated description
    currentValue$ = ev!.getText()
    currentDesc$ = myDescMap!.get(currentValue$)

    rem 'Set the name and description in the inpute and CEdit
    myLangName!.setText(currentValue$)
    myLangDesc!.setText(currentDesc$)
return

rem 'Callback for myAddButton! ON_BUTTON_PUSH
ADD_LANGUAGE:
    rem 'Check for existence of language, no duplicates
    langName$ = myLangName!.getText().trim()
    if (myDescMap!.containsKey(langName$))
        A = MSGBOX("Already have a language " + langName$)
    else
        rem 'Otherwise, add it to the list
        langDesc$ = myLangDesc!.getText()

        rem 'Get a copy of the current list, and add the new langauge
        myLangList! = myLangSpinner!.getSpinList()
        dummy = fnAddLanguage(myLangList!, myDescMap!, langName$, langDesc$)

        rem 'Set the modified list to be the current list
        myLangSpinner!.setSpinList(myLangList!)

        rem 'Find the new item's index and set the spinner on the new value
        index = myLangList!.indexOf(langName$)
        myLangSpinner!.setListIndex(index)
    endif
return

REMOVE_LANGUAGE:
    rem 'Get a copy of the list and the current item's index
    myLangList! = myLangSpinner!.getSpinList()
    index = myLangSpinner!.getListIndex()

    rem 'Remove that index from the spin list
    myLangList!.remove(index)

    rem 'set the modified spin list on the spinner
    myLangSpinner!.setSpinList(myLangList!)
    value$ = myLangSpinner!.getText()
    myLangName!.setText(value$)
    myLangDesc!.setText(myDescMap!.get(value$))
return

SLIDESHOW:
    rem 'If we show "Start", start the slide show
    if mySlideshowButton!.getText().startsWith("Start")
        rem 'Set the opposite action text on the button
        mySlideshowButton!.setText("Stop Slideshow")

        rem 'Disable the editing buttons
        myAddButton!.setEnabled(0)
        myRemoveButton!.setEnabled(0)

        rem 'Add the timer for the slideshow
        myAPI!.createTimer("timer", 3, "SLIDESHOW_TIMER")
    else
        rem 'Set the opposite action text on the button
        mySlideshowButton!.setText("Start Slideshow")

        rem 'Remove the timer
        myAPI!.removeTimer("timer")

        rem 'Re-enable the editing buttons
        myAddButton!.setEnabled(1)
        myRemoveButton!.setEnabled(1)
    endif
return

SLIDESHOW_TIMER:
    rem 'Ignore leftover timer event
    if (! myAddButton!.isEnabled())
        rem 'If at the end
        if (myLangSpinner!.getListIndex() = myLangSpinner!.getSpinList().size() - 1)
            rem 'roll around to the top
            myLangSpinner!.setListIndex(0)
        else
            rem 'Otherwise, spin up
            myLangSpinner!.spin(1)
        endif

        rem 'Note: the spin() method does not fire an event
        rem 'Must synchronize the fields manually
        myLangName$ = myLangSpinner!.getText()
        myLangName!.setText(myLangName$)
        myLangDesc!.setText(myDescMap!.get(myLangName$))
    endif
return

APP_CLOSE:
release

rem 'Utility function to add a language to the list and the description to
rem 'the map.
DEF fnAddLanguage(p_list!, p_map!, p_lang$, p_langDesc$)
p_list!.add(p_lang$)
p_map!.put(p_lang$, p_langDesc$)
return 0
FNEND