!******************************************************************************!
! Test popping out plots and adding features
!
! @bug      42 GPanel / MWindow: UnPopped MAP area needs to disappear
! @author:  JC4 / J Bauer
!******************************************************************************!

startmacro

  call setDatDir

  calc L:featureCount 0
  set pauseTime 0.5
  set instructions "A new yellow diamond should be added to the map every &
      ^{pauseTime} seconds. \n\nUsing the button below the plot, you should be able to pop the map out to its &
      own window and then push it back in again. \n\n&
      The GPanel should shrink when the plot gets popped.\n\nAt all times points &
      should continue to be added to the plot."

  pipe on
    panel/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 9 /title

      ! create a GLabel and put a plot on it
      gcontrol label plotLabel ""
        gcontrol panel  gplot  "Locations"    200   250
        plot/id=gplot/nopushpop ^{datDir}.world.shp{LAYER={LABEL=NAME,OPAQ=0}} options=+BSTORE|+DBUFFER

      ! Add a button to pop the plot and to display the number of diamond features
      gcontrol label controlsLabel "Controls"
        gcontrol button pop    ""             "Pop" 0    /nostate/nodepress
        gcontrol line newerLine
        gcontrol/noedit lval featCounter "Number of Diamonds" 0 0 1e9

      ! timer to add a new feature every .5 seconds
      set this.timer(0) ^{pauseTime}
  pipe off

endmacro

!******************************************************************************!
! Main process message for this macro.
procedure processMessage m:msg
  if msg.name eqs "MAIN" then
    assert/text="^reg.main.name: User selected " msg.data eqss "PASS"
    assert/text="^reg.main.name Added feature count matches PLOT feature count." featCnt eq featureCount
    pipe stop
  elseif msg.name eqs "TIMER"
    call addPoint
    call verifyFeatureCounts
  elseif msg.name eqs "POP" then
    if msg.data eqs "POP" then
      message send gplot ,, "WINDOW" ,, "POP"
      set gc.pop.items "Push"
    else
      message send gplot ,, "WINDOW" ,, "PUSH"
      set gc.pop.items "Pop"
    endif
  else
    say "Unhandled message ^msg.name = ^msg.data from ^msg.fid"
  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

!**************************************************************************************************!
! Add a feature to the plot at a random point on the map
procedure addPoint
  calc xVal random 360 * 180 -
  calc yVal random 180 *  90 -
  calc L:featureCount featureCount 1 +

  res label "POINT_^{featureCount}"

  feature label="^{label}" plot=gplot &
          table={NAME="^{label}",TYPE=SYMBOL,X=^xVal,Y=^yVal,COLOR=YELLOW,&
                 LINE={SYMBOL="DIAMOND",SYMBOLFILL="YES",SYMBOLSIZE=5}}

  set gc.featCounter.value featureCount
return

!**************************************************************************************************!
! Make sure the feature got added
procedure verifyFeatureCounts
  set l:featCnt 0

  foreach key intable reg.gplot.features
    calc featCnt featCnt 1 +
  endfor

  ! Only assert if the assert will fail
  if featCnt neq featureCount then
    assert/text="^reg.main.name Added feature count does not match PLOT feature count." pass eqs fail
  endif

return
