!******************************************************************************!
! Show problem with AWT and Swing not playing nice with each other in
! NeXtMidas 2.4.2 and prior. The SWING conversion of PLOT and PANEL
! in NeXtMidas 2.5.0 addresses these problems.
!
! @bug     720
! @author  Pete Perrino / Neon Ngo
! @since   NeXtMidas 2.5.0
!******************************************************************************!
startmacro
  switch INTERACTIVE interAct GET DEF=TRUE  SDEF=TRUE  ! for normal interactive test(s)

  call setDatDir

  if interAct then
    call setInstructions

    ! Build a Swing GUI with embedded Midas Displays
    call constructSwingGUI

    ! Must add the MidasDisplays to the WIN table so PLOTs know where to attach
    res reg.win {}
    res reg.win.win1 md1
    res reg.win.win2 md2
    res reg.win.win3 md3
    res reg.win.win4 md4
    res reg.win.win5 md5

    pipe on
      plot/id=plot1//win=win1/msgid=main ^{datDir}.world.prm options="+crosshairs"
      shellgui/win=win2
      plot/win=win3 ^{datDir}.world.prm
      plot/win=win4 ^{datDir}.alaska.prm
      plot/win=win5 ^{datDir}.apenny.prm

      panel/controls=gc/setup
      gcontrol label macro "MACRO"
      gcontrol menu main "Exit with Status = " "PASS,FAIL" /tleft
      set gc.main.itemColors {PASS=GREEN,FAIL=RED}
      gcontrol text info "Instructions" "^instr" 24 32 /title

      invoke ,, reg.plot1.setCrossHairsColor(red)        ! test for Bug 2347

    pipe off
  endif
endmacro

!******************************************************************************!
procedure processMessage m:msg
  !say "Msg ^msg.name = ^msg.data from ^msg.fid"
!  res reg.win
!  res msg.name

  if msg.name EQS "MAIN" then
    assert/text="Test Swing" msg.data eqss "PASS"
    invoke ,, frame.dispose()
    pipe stop
  else

  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 constructSwingGUI

  ! Create the frame
  new javax.swing.JFrame frame
  invoke fcp frame.getContentPane()
  invoke ,, frame.setSize(900,900)
  invoke ,, frame.setLocation(280,5)
  new java.awt.BorderLayout() layout
  invoke ,, frame.setLayout(layout)  ! Java 5 or later
! invoke ,, fcp.setLayout(layout)    ! Java 1.4.2

  ! Add some menus
  new javax.swing.JMenuBar jmb
  do i 1 8
    res menu "Menu ^i"
    new javax.swing.JMenu("^menu") jm
    do j 1 6
      res menuitem "Menu Item ^j"
      new javax.swing.JMenuItem("^menuitem") jmi
      invoke ,, jm.add(jmi)
    enddo
    invoke ,, jmb.add(jm)
  enddo
  invoke ,, frame.setJMenuBar(jmb)

  ! Add a MidasDisplay
  new nxm.sys.libg.MidasDisplay md1
  new java.awt.Dimension(900,300) dim
  invoke ,, md1.setPreferredSize(dim)
  invoke north java.awt.BorderLayout.NORTH()
  invoke ,, fcp.add(md1,north)

  ! Add a MidasDisplay
  new nxm.sys.libg.MidasDisplay md2
  invoke ,, md2.setPreferredSize(dim)
  invoke south java.awt.BorderLayout.SOUTH()
  invoke ,, fcp.add(md2,south)

  ! Add a JDesktopPane
  new javax.swing.JDesktopPane jdp
  invoke ,, jdp.setPreferredSize(dim)
  invoke center java.awt.BorderLayout.CENTER()
  invoke ,, fcp.add(jdp,center)

  ! Add JInternalFrames
  new javax.swing.JInternalFrame jif1
  invoke ,, jif1.setResizable(true)
  new nxm.sys.libg.MidasDisplay md3
  invoke ,, jif1.add(md3)
  invoke ,, jdp.add(jif1)
  invoke ,, jif1.setSize(250,200)
  invoke ,, jif1.setLocation(15,25)
  invoke ,, jif1.setVisible(true)

  ! Add JInternalFrames
  new javax.swing.JInternalFrame jif2
  invoke ,, jif2.setResizable(true)
  new nxm.sys.libg.MidasDisplay md4
  invoke ,, jif2.add(md4)
  invoke ,, jdp.add(jif2)
  invoke ,, jif2.setSize(250,200)
  invoke ,, jif2.setLocation(300,25)
  invoke ,, jif2.setVisible(true)

  ! Add JInternalFrames
  new javax.swing.JInternalFrame jif3
  invoke ,, jif3.setResizable(true)
  new nxm.sys.libg.MidasDisplay md5
  invoke ,, jif3.add(md5)
  invoke ,, jdp.add(jif3)
  invoke ,, jif3.setSize(250,200)
  invoke ,, jif3.setLocation(600,25)
  invoke ,, jif3.setVisible(true)

  ! Make it visible
  invoke ,, frame.setVisible(true)

return

!******************************************************************************!
! moved here from pipe section to avoid clutter
procedure setInstructions
  ! NXM-3845 since NeXtMidas 4.1.1
  set jarNote ""
  if ^{env.NMROOT} EQS "classpath:" then set jarNote "\n\nNOTE: When running this test from a jar file, &
                                                      a static crosshair may appear in the top plot."
  set instr  "Click on Menu 1 (it should be shown over the plot), then hover over each &
        subsequent menu, ending at Menu 8. &
        With one of the middle menus open, move the mouse up and down and the &
        appropriate menu item should be highlighted. &
        Move the three framed plots (in the middle) around. &
        They should refresh as they are moved. &
        MMB on one of the framed plots to bring up the PLOT menu. &
        Move the PLOT menu over the shellgui at the bottom. &
        Prior to the SWING conversion of PLOT and PANEL, moving the PLOT menu &
        would cause visual anomalies in the plots and the PLOT menu would be &
        hidden by the plot. &
        You should not see any partial border around the shellgui panel at the &
        bottom of the screen. &
        The top plot should have red crosshairs when the mouse is over it.^{jarNote}"

return
