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: REM Get the BBjFileChooserChangeEvent ev! = BBjAPI().getLastEvent()
PRINT "Current Filter: " + ev!.getFileChooser().getActiveFilter()
REM Get the vector from the change event 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) FI 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) FI
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) FI 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
|