!******************************************************************************!
! Test the plotting of multiple Layers and Features (LayerDrawable).
!
! @bug      2792 Group Layers and Features
! @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)
  switch COREGRAPHICS coreGraphics GET DEF=FALSE SDEF=TRUE  ! for a quick check of graphics - more extreme tests

  if interAct then
    call setDatDir

    set instructions "In the Map... \n&
                      You should see a map of the world with Alaska, SV1, and SV2 plotted, as &
                      well as 100 red circle Features: \n\n&
                      1. Click the Toggle buttons to enable and disable the SPACE features Group &
                      and the Features group.\n\n&
                      2. Click on the Add/Remove features buttons and observe the PLOT.\n\n&
                      3. Click on the Toggle SV2/SV3 buttons and observe the PLOT.\n\n&
                      4. Click on the buttons to list the Layer and/or Feature Drawable items.\n&
                      \n&
                      In the Venn Diagram... \n&
                      1. Select in various places within the plot using LMB, &
                      the colors of the Venn diagram (RED/GREEN/BLUE) or the vertical YELLOW bar &
                      will be reported in the Shell. \n&
                      2. Toggle the visibility of the yellow bar, it should enable/disable.\n\n&
                      Final Step On both: Clear all layers."

    set hasSV2 true
    set hasSV3 false
    set vennClear true
    set isPlotClear false

    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
      env set KEYMAP {PLOT={LEFT="L",RIGHT="R",in=i,down=d,up=u,out=o}}

      plot/id=plot1 ^{datDir}.world.prm|^{datDir}.alaska.prm|&
                   {LT="Drawable",name="SPACE"}|&
                   {LT="Drawable",name="FEATGROUP"}|&
                   ^{datDir}.sv1.prm{LAYER={NAME="SV1",group="SPACE"}}|&
                   ^{datDir}.sv2.prm{LAYER={NAME="SV2",group="SPACE"}} view=latlon readout=+onAxis readoutext=+show
      plot/id=plot2 ^{datDir}.fftdemo.prm|{LT="Drawable",name="VENN"} OPTIONS=+DBuffer /MSGID=MAIN

      feature label=ftriangle1 table={NAME="TRIANGLE1",TYPE=SYMBOL,X=120,Y=50,&
                                  COLOR=YELLOW,LINE={SYMBOL="UTRI"}} plot=plot1
      feature label=ftriangle2 table={NAME="TRIANGLE2",TYPE=SYMBOL,X=120,Y=60,&
                                  COLOR=AQUA,LINE={SYMBOL="UTRI"}} plot=plot1

      set layVenn reg.plot2.layers.VENN
      call populateVenn

      shellgui/id=idshellgui/attach

      ! now add a bazillion features to the plot
      call addFeatures

      gcontrol button laysButt       "Enable/Disable SV Layer Group" "Toggle"         "" /TLeft
      gcontrol button featsButt      "Enable/Disable Features      " "Toggle"         "" /TLeft
      gcontrol button addFeat        "Add or Remove 10 Features    " "Add,Remove"     "" /TLeft
      gcontrol button addSV3         "Add or Remove SV3            " "Toggle"         "" /TLeft
      gcontrol button addSV2         "Add or Remove SV2            " "Toggle"         "" /TLeft
      gcontrol button listButt       "List Drawables               " "LAY,FEAT,ALL"   "" /TLeft
      gcontrol button visButt        "Yellow Bar Visibility        " "Toggle"         "" /TLeft
      gcontrol button clearAllPlot1  "Clear all Layers Map         " "Clear,Populate"      "" /TLeft
      gcontrol button clearAllPlot2  "Clear all Layers Venn        " "Clear,Populate" "" /TLeft
    pipe off
  endif

  ! Additional interactive tests, only run with /BASELINE
  if interAct and baseline then
    %test_plot_layerdrawable_continuous     ! NXM-3284
    %test_plot_layerdrawable_gak            ! NXM-3582
    %test_plot_layerdrawable_graph_features ! NXM-3500
    %test_plot_layerdrawable_pipe           ! Bug 2913
    %test_plot_layerdrawable_zoomkeep       ! NXM-3652

  ! If COREGRAPHICS run a short list of extreme tests that tend to be the ones to show graphical issues
  elseif coreGraphics then
    %test_plot_layerdrawable_continuous     ! NXM-3284
    %test_plot_layerdrawable_graph_features ! NXM-3500
  endif
endmacro

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

  elseif msg.name EQS "VISBUTT" then
    set vennYellow.ENABLE "~Global"

  elseif msg.name EQS "LAYSBUTT" then
    invoke ,, laySpace.toggle()

  elseif msg.name EQS "FEATSBUTT" then
    invoke ,, layFeats.toggle()

  elseif msg.name eqs "ADDFEAT" then
    if msg.data eqs "ADD" then
      call addMoreFeats
    else
      call remFeats
    endif

  elseif msg.name eqs "ADDSV2" then
    if hasSV2 isTrue then
      invoke ,, laySpace.removeDrawable("SV2")
      set hasSV2 false
    else
      invoke ,, reg.plot1.openFile(^{datDir}.sv2.prm{LAYER={NAME="SV2",GROUP="SPACE"}})
      set hasSV2 true
    endif

  elseif msg.name eqs "ADDSV3" then
    if hasSV3 isTrue then
      !invoke ,, laySpace.removeDrawable("SV3")   ! alternate way of doing it
      invoke ,, reg.plot1.closeFile("SV3")
      set hasSV3 false
    else
      invoke ,, reg.plot1.openFile(^{datDir}.sv3.prm{LAYER={NAME="SV3",GROUP="SPACE"}})
      set hasSV3 true
    endif

  elseif msg.name eqs "LISTBUTT" then
    if msg.data eqs "LAY" then
      invoke t:layerObjs  laySpace.getDrawables("LAYERS",true)
      res t:layerObjs
    elseif msg.data eqs "FEAT"
      invoke t:featObjs layFeats.getDrawables("FEATURES",true)
      res t:featObjs
    else
      invoke t:featObjs layFeats.getDrawables("ALL",true)
      res t:featObjs
      invoke t:layObjs  laySpace.getDrawables("LAYERS",true)
      res t:layObjs
    endif
  elseif msg.name eqs "clearAllPlot1" then
    if msg.data eqs "clear" then
      if isPlotClear isFalse
        call clearPlot
      endif
    else
      if isPlotClear isTrue
        call repopulatePlot
      endif
    endif
  elseif msg.name eqs "clearAllPlot2" then
    if msg.data eqs "CLEAR" then
      if vennClear isFalse then !do not clear what is not there
        invoke ,, reg.plot2.clearData()
        set vennClear true
      endif
    elseif vennClear isTrue then ! do not add what is already there
      call populateVenn
    else
      say "nothing to do"
    endif
  elseif msg.fid EQS "PLOT2" then
    if msg.name EQS "MARK" then
      invoke matches REG.PLOT2.LAYERS.VENN.getDrawablesAt(msg.data)

      if (matches.size == 0) then
        say "Selected colors: <NONE>"
      else
        say/L=0 "Selected colors:"
        for f in matches
          say/L=0 " ^f.name"
        endfor
        say/L=1 ""
      endif
    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

!******************************************************************************!
! Populate venn diagram
!******************************************************************************!
procedure populateVenn 
  if vennClear isTrue then
    feature ,, vennRed    {NAME="RED",   LINE={COLOR="#66FF0000"},LAYER=layVenn,TYPE=BOX|FILLED, X=1.5e6,Y=1.0E6,DX=1.5e6,DY=1.0e6} PLOT2
    feature ,, vennGreen  {NAME="GREEN", LINE={COLOR="#6600FF00"},LAYER=layVenn,TYPE=BOX|FILLED, X=2.0e6,Y=0.75E6,DX=1.5e6,DY=1.0e6} PLOT2
    feature ,, vennBlue   {NAME="BLUE",  LINE={COLOR="#660000FF"},LAYER=layVenn,TYPE=BOX|FILLED, X=2.5e6,Y=1.0E6,DX=1.5e6,DY=1.0e6} PLOT2
    feature ,, vennYellow {NAME="YELLOW",LINE={COLOR="#99FFFF00"},LAYER=layVenn,TYPE=VBAR|FILLED,X=4.5e6,Y=1.0E6,DX=0.5e6,DY=1.5e6} PLOT2
    feature ,, vennWhite  {NAME="WHITE", LINE={COLOR="#66AAAAAA"},LAYER=layVenn,TYPE=CIRC|FILLED,X=3e6,Y=0.75E6,DX=0.5e6,DY=0.5e6} PLOT2
    !feature ,, vennPink {NAME="PINK",LINE={COLOR=PINK},LAYER=layVenn,TYPE=ELPS|FILLED,X=3.5e6,Y=0.25E6,DX=0.5e6,DY=0.5e6,SEMIMAJOR=1e6,SEMIMINOR=1e6,TILT=30} PLOT2 !Add if BUG 2940 is worked
    set vennClear false
  endif
return
!******************************************************************************!
! Make a bazillion features and add them to the plot
!******************************************************************************!
procedure addFeatures

  ! initial values for the feature
  set featX -170
  set featY -80

  ! create variables to easily access LayerDrawables Layers
  set layFeats reg.plot1.layers.featGroup
  set laySpace reg.plot1.layers.space

  ! Add 100 features to the feature group layerDrawable
  do featNum 1 100
    feature label=f_^{featNum} table={NAME="FEAT_^{FEATNUM}",LAYER=layFeats,TYPE="CIRC",X=featX,Y=featY,DX=5,COLOR="RED"} plot=plot1
    calc featX featX 3 +
    calc featY featY 1 +
  enddo

return

!******************************************************************************!
! Make a few (10) more features and add them to the plot
!******************************************************************************!
procedure addMoreFeats

  do addSome 1 10 1
    calc L:featNum featNum 1 +
    feature label=f_^{featNum} table={NAME="FEAT_^{FEATNUM}",LAYER=layFeats,TYPE="CIRC",X=featX,Y=featY,DX=5,COLOR="RED"} plot=plot1
    calc featX featX 3 +
    calc featY featY 1 +
  enddo

return

!******************************************************************************!
! remove some (10) of the features
!******************************************************************************!
procedure remFeats
  do remSome 1 10 1
    if featNum gt -1 then
      !invoke ,, layFeats.removeDrawable("FEAT_^{featNum}")   ! alternate way of doing it
      invoke ,, reg.plot1.removeFeature("FEAT_^{featNum}")
      calc L:featNum featNum 1 -

      ! the lines below  prevent the features position from creeping off the screen
      calc featX featX 3 -
      calc featY featY 1 -
    endif
  enddo
return

!******************************************************************************!
! repopulate the plot after it is cleared
!******************************************************************************!
procedure repopulatePlot
  message func=send ID=plot1 NAME=OPENFILE DATA="^{datDir}.world.prm|^{datDir}.alaska.prm"
  invoke ,, reg.plot1.openFile(^{datDir}.sv1.prm{LAYER={NAME="SV1",GROUP="SPACE"}})
  invoke ,, reg.plot1.openFile(^{datDir}.sv2.prm{LAYER={NAME="SV2",GROUP="SPACE"}})
  call addFeatures
  set isPlotClear false
  set hasSV2 true
  set hasSV3 false
  set isClear false
return

!******************************************************************************!
! clear the plot
!******************************************************************************!
procedure clearPlot
  invoke ,, reg.plot1.clearData()
  set hasSV3 false
  set hasSV2 false
  set featX -170
  set featY -80
      set isPlotClear true
return
