!******************************************************************************!
! Test using drag and drop in Plot, List, and List2
!
! @bug      2120 PLOT: Add support for Drag -N- Drop
! @bug      1984 Add drag/drop between MidasDisplays and other parts of a Swing GUI
! @author:  J Bauer
!******************************************************************************!
startmacro
  switch INTERACTIVE interAct GET DEF=TRUE  SDEF=TRUE  ! for normal interactive test(s)
  switch BASELINE    baseline GET DEF=FALSE SDEF=TRUE  ! for defects or more obscure test case(s)

  call setDatDir

  if interAct then
    set instructions "You should see two LIST2s, a LIST, and a PLOT. \n\n&
                      Select a row or rows in the LIST and/or LIST2. \n\n&
                      You should be able to DRAG and DROP the selected row(s) &
                      onto the other LISTs and the PLOT. \n\n&
                      NOTES: \n&
                      1) You cannot DRAG from the Plot.\n&
                      2) To drag from a LIST2 select one of the cells (not the entire row)\n&
                      3) To drag from LIST select the entire row\n&
                      4) When dragging into LIST2 make sure to drag into an existing row &
                       (part of the LIST2 is highlighted)"

    ! create empty LIST file (type 6000)
    headermod/create ram:lfile SR=(F1/3A,R1/3A)

    ! create row to add to LIST later
    set t:myrow {F1="^{datDir}.sv1.prm",R1="What day is today?"}

    ! template to use for the initial LIST2 table
    set t:templ {}
    set templ._GLOBAL_ {AUTOHIDE="FALSE",AUTOORDER="TRUE",WIDTH=12}
    set templ.FNAME {WIDTH=25}
    set templ.TEXT  {WIDTH=20}

    ! Data for the first LIST2 table (contains the filename that plot will open from the drag/drop)
    set t:myTable {}
    set myTable.ROW1 {FNAME="^{datDir}.cities.prm",TEXT="Have a nice day!"}
    set myTable.ROW2 {FNAME="^{datDir}.sv3.prm",TEXT="Have a crummy night!"}
    !set myTable.ROW3 {A="A3",C="C3"}

    ! Data for the second LIST2 table (contains the filename that plot will open from the drag/drop)
    set t:myTable2 {}
    set myTable2.ROW1 {FNAME="^{datDir}.sv2.prm",TEXT="How is your day?"}

    pipe on
      panel/msgid=MAIN/setup/controls=gc
      gcontrol label macro "Test MACRO"
      gcontrol menu  main "Exit with Status =" "PASS,FAIL" TABLE={ItemColors={PASS="GREEN",FAIL="RED"}} /tleft

      gcontrol text  idtext "Instructions" instructions 24 26 /title

      list2/id=list2_1/msgid=main mytable template=templ /DRAGDROP
      list2/id=list2_2/msgid=main mytable2 template=templ /DRAGDROP
      list/id=list1/msgid=main ram:lfile /DRAGDROP=table  ! /dragtype=table /droptype=table
      plot/id=plot1  ^{datDir}.world.prm /DRAGDROP

      ! Add row to the LIST
      invoke ,, reg.list1.gs.addRow(^myrow)

    pipe off

  endif
endmacro

!******************************************************************************!
! Main process message for this macro. Should be right after the ENDMACRO.
!******************************************************************************!
procedure processMessage m:msg
  !say "Got msg ^msg data= ^msg.data "
  if msg.name eqs "MAIN" then
    assert/text="^reg.main.name: User indicated tests" msg.data eqss "PASS"
    pipe stop

  elseif msg.name eqs "DROPDATA" then
    res t:tdata ^msg.data
    if msg.fid eqs "PLOT1" then
      foreach row intab tdata
        res rowdata tdata.^row
        call mergeListData rowdata
        res s:fname rowdata.fname
        message send plot1 name="OPENFILE" info=1 data="^fname"
      endfor

    elseif msg.fid eqss "LIST" then
     foreach row intab tdata
       res rowdata tdata.^row
       call mergeListData rowdata
       invoke ,, reg.^{msg.fid}.addRow(rowdata)
     endfor

    endif

  else
    !say "Unhandled message name: ^msg.name from fid: ^msg.fid data: ^msg.data"
  endif
return

!******************************************************************************!
! If an unexpected exception is caught, mark test as failed, unless it is expected
!******************************************************************************!
procedure processException m:emsg
  if expectedException REXISTS then
    assert/text="^reg.main.name: Expected ERROR starts with: ^{expectedException}" expectedException EQSS emsg.data.message
    remove expectedException
  else
    assert/text="^reg.main.name: Caught unexpected exception ^{emsg.data}" "FALSE" isTrue
    if "Trace" SUBS ENV.DEBUG then invoke ,, emsg.data.printStackTrace()
  endif
return

!******************************************************************************!
! Set the dat directory
!******************************************************************************!
procedure setDatDir
  invoke nxmMajor env.nmversion.subString(0,1)
  invoke major java.lang.Integer.valueOf(nxmMajor)
  if major GE 4 then
    invoke s:datDir           nxm.sys.test.DatLocator.getDatDirLocation()
    invoke s:datDirSlash      nxm.sys.test.DatLocator.getDatDirSlashLocation()
    invoke z:sysDatFull       nxm.sys.test.DatLocator.sysDatFull()
    invoke z:sysTestDatExists nxm.sys.test.DatLocator.sysTestDatExists()
  else
    set s:datDir           "nxm.sys.dat"
    set s:datDirSlash      "nxm/sys/dat"
    set z:sysDatFull       true
    set z:sysTestDatExists false
  endif
return

!******************************************************************************!
! Merge data between LIST and LIST template structures for easier drop later
!******************************************************************************!
procedure mergeListData t:input

  if input contains "F1" OR input contains "R1"         ! LIST to LIST2
    if input contains "F1"
      set input.FNAME rowdata.F1
    endif
    if input contains "R1"
      set input.TEXT rowdata.R1
    endif

  elseif input contains "FNAME" OR input contains "TEXT"   ! LIST2 to LIST
    if input contains "FNAME"
      set input.F1 rowdata.FNAME
    endif
    if input contains "TEXT"
      set input.R1 rowdata.TEXT
    endif

  endif

return
