BBjFileChooser Example 2

For getActiveFileFilter, getControlButtonsAreShown, getCurrentDirectory, getFileFilterContents, getFileFilternames, getSelectedFiles, removeFileFilter, rescanCurrentDirectory, setControlButtonsAreShown, and setCurrentDirectory

rem 'Open SYSGUI channel

sg = unt
open(sg)"X0"

rem 'Get BBj objects
api! = BBjAPI()
sg! = api!.getSysGui()

rem 'Create main window
window! = sg!.addWindow(100, 100, 800, 300, "File Chooser Demo", $00010083$, $$)
stBar! = window!.addStatusBar(100, $$)

rem 'Create new file chooser with appropriate flags
pwd$ = DIR("")
fc! = window!.addFileChooser(101, 0, 0, 500, 250, pwd$, flags$)
fc!.setAcceptAllFileFilterUsed(0)

rem 'Create description area
description! = window!.addStaticText(102, 500, 0, 300, 200, $$)
description!.setFont(sg!.makeFont("Arial", 18, sg!.BOLD))

rem 'Setup constants for actionButton!
W_ACTION$ = "Finish Distribution"
S_ACTION$ = "Create Widget"
G_ACTION$ = "Create Sprocket"
A_ACTION$ = "Create Gadget/Doodad"

rem 'Create actionButton! which will change based on the active file filter
actionButton! = window!.addButton(103, 510, 230, 140, 25, A_ACTION$)

rem 'Create delete button, which can occur at any time.
delButton! = window!.addButton(104, 660, 230, 140, 25, "Delete All Parts")

rem 'If control buttons are shown, hide them.
if (fc!.getControlButtonsAreShown())
    fc!.setControlButtonsAreShown(0)
endif

rem 'Setup file filter name constants
WIDGETS$ = "Widgets"
SPROCKETS$ = "Sprockets"
G_AND_D$ = "Gadgets and Doodads"
PARTS$ = "All Parts"

rem 'Set some fictional file type filters.
fc!.addFileFilter(WIDGETS$, "*.wgt")
fc!.addFileFilter(SPROCKETS$, "*.skt")

rem 'Gadgets and Doodads go together to make up Sprockets
vec! = api!.makeVector()
vec!.add("*.ggt")
vec!.add("*.ddd")
fc!.addFileFilter(G_AND_D$, vec!)

rem 'All parts except part distributions
vec! = api!.makeVector()
vec!.add("*.ggt")
vec!.add("*.ddd")
vec!.add("*.skt")
vec!.add("*.wgt")
fc!.addFileFilter(PARTS$, vec!)

rem 'Set the active filter to be the part distribution.
fc!.setActiveFileFilter(PARTS$)

rem 'Set the callback for the close box on the window
window!.setCallback(api!.ON_CLOSE, "DO_CLOSE")

rem 'Set the callback for when the filechooser selection changes
fc!.setCallback(api!.ON_FILECHOOSER_CHANGE, "SELECTION")

rem 'Set the callback for the actionButton!
actionButton!.setCallback(api!.ON_BUTTON_PUSH, "DO_ACTION")

rem 'Set the callback for the delButton!
delButton!.setCallback(api!.ON_BUTTON_PUSH, "DELETE_ALL_FILES")

rem 'Create the timer to update the preview
api!.createTimer("timer", .5, "UPDATE_DESCRIPTION")
process_events

rem 'Callback for window! ON_CLOSE
DO_CLOSE:
release

rem 'Callback for actionButton!
DO_ACTION:
    dir$ = fc!.getCurrentDirectory()
    text$ = actionButton!.getText()

    rem 'Each action produces a larger part from smaller parts.
    rem 'Randomly create a doodad or a gadget.
    if (text$ = A_ACTION$)
        I = 1
        GADGET = RND(2)

        rem 'Iterate until we can create a new file.
        while 1
            if (GADGET)
                fileName$ = dir$ + "/gadget" + STR(I) + ".ggt"
            else
                fileName$ = dir$ + "/doodad" + STR(I) + ".ddd"
            endif
            STRING fileName$,ERR=*NEXT; BREAK
            I = I + 1
        wend
        PRINT "Created " + fileName$
    endif

    rem 'Create a sprocket out of the selected gadgets and doodads.
    if (text$ = G_ACTION$)
        I = 1

        rem 'Iterate until we can create a new file.
        while 1
            fileName$ = dir$ + "/sprocket" + STR(I) + ".skt"
            STRING fileName$,ERR=NEXT_SPROCKET

            rem 'Once we create a new file, add the selected files
            rem 'representing gadgets and doodads in the sprocket file
            PRINT "Created " + fileName$

            rem 'Get current set of selected files from the file chooser.
            vec! = fc!.getSelectedFiles()
            fChan = UNT
            OPEN(fChan,ERR=*NEXT)fileName$
            FOR I = 0 TO vec!.size() - 1
                WRITE(fChan,ERR=*BREAK)vec!.get(I)
            NEXT I
            CLOSE(fChan,ERR=*NEXT)

            rem 'Done creating the file, now break the while 1
            break

NEXT_SPROCKET:
    I = I + 1
wend

rem 'Once we've created the sprocket, we'll want to make
rem 'a widget out of sprockets, so show all the sprockets.
fc!.setActiveFileFilter(SPROCKETS$)
endif

rem 'Create a widget out of the selected sprockets
if (text$ = S_ACTION$)
    I = 1
    while 1
        fileName$ = dir$ + "/widget" + STR(I) + ".wgt"
        STRING fileName$,ERR=NEXT_WIDGET

        rem 'Once we create a new file, add the selected files
        rem 'representing sprockets in the new widget file
        PRINT "Created " + fileName$

        rem 'Get current set of selected files from the file chooser.
        vec! = fc!.getSelectedFiles()
        fChan = UNT
        OPEN(fChan,ERR=*NEXT)fileName$
        FOR I = 0 TO vec!.size() - 1
            WRITE(fChan,ERR=*BREAK)vec!.get(I)
        NEXT I
        CLOSE(fChan,ERR=*NEXT)

        rem 'Done creating the file, now break the while 1
        break

NEXT_WIDGET:
    I = I + 1
wend
fc!.setActiveFileFilter(WIDGETS$)
endif

rem 'Widgets go in a parts distribution file.
if (text$ = W_ACTION$)
    I = 1
    while 1
        fileName$ = dir$ + "/distribution" + STR(I) + ".pds"
        STRING fileName$,ERR=NEXT_DIST

        rem 'Add the selected widget files to the parts distribution file
        PRINT "Created " + fileName$

        rem 'Get current set of selected files from the file chooser.
        vec! = fc!.getSelectedFiles()
        fChan = UNT
        OPEN(fChan,ERR=*NEXT)fileName$
        FOR I = 0 TO vec!.size() - 1
            WRITE(fChan,ERR=*BREAK)vec!.get(I)
        NEXT I
        CLOSE(fChan,ERR=*NEXT)

        rem 'Done creating file, now break the while 1
        break

NEXT_DIST:
    I = I + 1
wend

rem 'Add the parts distribution file type to the parts filter, now
rem 'that we've created at least one
filters! = fc!.getFileFilterNames()
FOR I = 0 TO filters!.size() - 1
    filterName$ = filters!.get(I)
    if (filterName$ = PARTS$)
        filterStrings! = fc!.getFileFilterContents(filterName$)
        filterStrings!.add("*.pds")
        fc!.removeFileFilter(PARTS$)
        fc!.addFileFilter(PARTS$, filterStrings!)
    endif
NEXT I
endif

rem 'Rescan the directory so we can see the changes with new files.
fc!.rescanCurrentDirectory()

return

rem 'Callback for a change in the file selection
SELECTION:
    ev! = BBjAPI().getLastEvent()
    vec! = ev!.getSelectedFiles()

    rem 'If we have a selected file, put the name in the status bar,
    if vec!.size() > 0
        status$ = "Selected File: " + vec!.get(0)
        stBar!.setText(status$)

        rem 'A selected file allows us to use the action button.
        actionButton!.setEnabled(1)

        rem 'otherwise, clear it
    else
        rem 'We require selected files unless creating a gadget or doodad.
        if (actionButton!.getText() = W_ACTION$)
            actionButton!.setEnabled(1)
        else
            actionButton!.setEnabled(0)
        endif

        rem 'Clear the selected file text.
        stBar!.setText($$)
    endif
return

rem 'Callback for delButton!
DELETE_ALL_FILES:
    rem 'Delete all the parts files in the current directory.
    dir$ = fc!.getCurrentDirectory()
    dirCh = UNT
    OPEN(dirCh)dir$
    while(dirCh)
        READRECORD(dirCh,END=*BREAK)fileName$
        str! = fileName$

        rem 'Use Java string methods
        doErase = str!.endsWith(".wgt")
        doErase = doErase OR str!.endsWith(".skt")
        doErase = doErase OR str!.endsWith(".ggt")
        doErase = doErase OR str!.endsWith(".ddd")
        doErase = doErase OR str!.endsWith(".pds")

        rem 'If it's a parts file by extension, delete it.
        if doErase
            ERASE dir$ + "/" + fileName$,ERR=*PROCEED
        endif
    wend

    rem 'After deleting the files, rescan the directory to see the changes
    fc!.rescanCurrentDirectory()
return

rem 'Callback for the timer
UPDATE_DESCRIPTION:
    filter$ = fc!.getActiveFileFilter()

    rem 'If we can see all the parts, we can create gadgets or doodads
    if filter$ = PARTS$
        fc!.setMultiSelectionEnabled(0)
        actionButton!.setText(A_ACTION$)
        msg$ = "Create gadgets (pointy objects) "
        msg$ = msg$ + "and doodads (rounded objects) "
        msg$ = msg$ + "for sprockets, or change the file view"
        actionButton!.setEnabled(1)
    endif

    rem 'If we can see widgets, we can build parts distributions.
    if filter$ = WIDGETS$
        rem 'Can only put one widget in a part distribution
        fc!.setMultiSelectionEnabled(0)
        msg$ = "Add a widget to a part distribution "
        msg$ = msg$ + "to finish a distribution, "
        msg$ = msg$ + "or change the file view"

        rem 'Set the action when widgets are viewable
        actionButton!.setText(W_ACTION$)

        rem 'Can only create parts distributions with one widget
        if fc!.getSelectedFiles().size() = 0
            actionButton!.setEnabled(0)
        else
            actionButton!.setEnabled(1)
        endif
    ENDIF

    rem 'If we can see sprockets, we can make widgets
    if filter$ = SPROCKETS$
        rem 'Can create a widget with multiple sprockets
        fc!.setMultiSelectionEnabled(1)
        msg$ = "Add sprockets to a widget, or change the file view"

        rem 'Set the action when sprockets are viewable
        actionButton!.setText(S_ACTION$)

        rem 'Can only create widgets with one or more sprockets
        if fc!.getSelectedFiles().size() = 0
            actionButton!.setEnabled(0)
        else
            actionButton!.setEnabled(1)
        endif
    ENDIF

    rem 'If we can see gadgets and doodads, we can make sprockets
    if filter$ = G_AND_D$
        rem 'Can create a sprocket with multiple gadgets and doodads
        fc!.setMultiSelectionEnabled(1)
        msg$ = "Add gadgets and doodads to a sprocket, or change the file
        view"

        rem 'Set the action when gadgets and doodads are visible
        actionButton!.setText(G_ACTION$)

        rem 'Can only create sprockets with one or more gadgets/doodads
        if fc!.getSelectedFiles().size() = 0
            actionButton!.setEnabled(0)
        else
            actionButton!.setEnabled(1)
        endif
    ENDIF

    rem 'Set the text of the description area
    description!.setText(msg$)

    rem 'Don't allow going above the starting directory
    rem 'First massage the current directory
    dir$ = fc!.getCurrentDirectory()
    if (dir$(LEN(dir$), 1) <> "/")
        dir$ = dir$ + "/"
    endif

    rem 'then check against the original. The original must
    rem 'be a substring in the current directory
    if (POS(pwd$=dir$) = 0)
        stBar!.setText("Cannot go above " + pwd$)
        fc!.setCurrentDirectory(pwd$)
    endif

    rem 'All done
return