!******************************************************************************!
! Test that Feature Groups
! FeatureGroup adds the ability to group features and set attributes
! across all featurs in a group
!
! @bug      NXM-3471 Add Feature Group ability
! @since    3.9.3
! @author:  Kevin Esterheld
!******************************************************************************!
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 "Test that the features can be moved from group to group.\n&
                      All the features from one group should move to the other &
                      when the 'Move Features' button is clicked\n\n&
                      Click on one of the colors and all the features should &
                      follow the color change."

    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 12 /title
       plot/id=idplot ^{datDir}.world.prm

      call generateFeatures 1 50 "CIRCLE" "A"
      call generateFeatures 51 100 "TRIANGLE" "C"

      invoke featuregroup reg.idplot.featureGroups
      invoke groupa featureGroup.get("A")
      invoke sizeA groupa.getSize()
      invoke groupb featureGroup.get("B")
      if groupb ISNULL
        set sizeB 0
      else
        invoke sizeB groupb.getSize()
      endif
      gcontrol text VALA "" "Size of Group A = ^{sizeA}"
      gcontrol text VALB "" "Size of Group B = ^{sizeB}"
      gcontrol/nc=1 button moveFeatures "" "Move Features"
      gcontrol/nc=3 button setColor  "" "RED,GREEN,WHITE"
      invoke groupa.setLine({Color="GREEN"})
      invoke groupa.setFeatureAttributes({type="Circle",line={Color="YELLOW"}, DX=10, DY=10})

    pipe off

  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 "MOVEFEATURES"
    call moveFeatures
  elseif msg.name eqs "SETCOLOR"
    call setColor ^msg.data
  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

!******************************************************************************!
procedure generateFeatures l:start l:end s:name s:group
  do i start end
    calc xval ^i 180 MOD/T
    calc yval ^i 90 MOD/T
    feature table={NAME="^{name}^{i}",TYPE=SYMBOL,X=^xval,Y=^yval,&
                   LINE={SYMBOL="UTRI"},GROUP=^group} plot=idplot

  enddo

return

!******************************************************************************!
! Move features from A -> B or B -> A depending on which group the features are in
!******************************************************************************!
procedure moveFeatures
  invoke groupa featureGroup.get("A")
  invoke sizeA groupa.getSize()
  if sizeA eq 0
    invoke reg.idplot.moveAllFeatures("B","A")
  else
    invoke reg.idplot.moveAllFeatures("A","B")
  endif
  invoke groupb featureGroup.get("B")
  invoke sizeA groupa.getSize()
  invoke sizeB groupb.getSize()
  invoke gc.vala.setValue("Size of Group A = ^{sizeA}")
  invoke gc.valb.setValue("Size of Group B = ^{sizeB}")
return

!******************************************************************************!
! Set the color of the features based on the message data
!******************************************************************************!
procedure setColor s:color
  invoke groupa featureGroup.get("A")
  invoke groupc featureGroup.get("C")
  invoke sizeA groupa.getSize()
  if sizeA eq 0
    invoke groupToChange featureGroup.get("B")
  else
    set groupToChange groupa
  endif
  invoke groupToChange.setLine({color="^{color}"})
  invoke groupc.setLine({color="^{color}"})

return