

/* ========================================================================= */
::requires "BSF.CLS"                    -- java decimal format & gui elements
/* ========================================================================= */

/* ========================================================================= */
::routine chooseSheet                                                 public
/* ========================================================================= */
use arg sheets

   if sheets~items = 1 then RETURN sheets[1]     -- hobsons choice         -->|
   sheet = .bsf.dialog~inputBox("Which Sheet?",file,"plain",,sheets,sheets[1])

RETURN sheet                                                               -->|


/* ========================================================================= */
::routine chooseFile                                                  public
/* ========================================================================= */
-- Let the user choose the file to parse -------------------------------------
use arg filename

   pwd = directory()

   -- prepare a java file object pointing to current folder
      loc  = .bsf~new("java.io.File", pwd) -- where we start
      file = .bsf~new("java.io.File", filename)

   -- define our own file filter for xlsx files only
      ff  = BsfCreateRexxProxy(.jFileChooserXlsxFileFilter~new,    -
                              ,"javax.swing.filechooser.FileFilter")
   -- create and show filechooser in details view with our filter on downloads
      dlg = .bsf~new("javax.swing.JFileChooser")
      dlg~setPreferredSize(.bsf~new("java.awt.Dimension", 600, 400))
      dlg~setDialogTitle('Select xlsx file to get data from')
      dlg~setCurrentDirectory(loc)
      dlg~setSelectedFile(file)
      dlg~setFileFilter(ff)
   -- kudos to Rony Flatscher for getting the filechooser to open in details view without causing an exception
      clzActionEvent = bsf.import("java.awt.event.ActionEvent")
      ae     = clzActionEvent~new(dlg, clzActionEvent~action_performed, "viewTypeDetails")
      action = dlg~getActionMap~get("viewTypeDetails")
      action~actionPerformed(ae)

      res = dlg~showOpenDialog(.nil)

   call directory pwd -- restore the working directory

   if res \= dlg~APPROVE_OPTION then RETURN .nil    -- user pressed cancel -->|

   file = dlg~getSelectedFile~getAbsolutePath

RETURN file

/* ------------------------------------------------------------------------- */
::class jFileChooserXlsxFileFilter       -- supports JFileChooser
/* ========================================================================= */
::method 'getDescription'
RETURN 'xlsx files (*.xlsx)'
/* ------------------------------------------------------------------------- */
::method 'accept'
use arg file
   fn = lower(file~name)
   select
      when file~isDirectory then RETURN .true
      when fn~right(5) == '.xlsx'
                            then RETURN .true
      otherwise                  RETURN .false
   end /* select */
/* ========================================================================= */



