startmacro
local /M/

! turn on NextMidas right away
nm/on/bg

! we need the message queue up for later
message/nmq=16k up

! create the main frame
nm new nxm.sys.libxm.XPrimitive() xp
nm invoke ,, xp.setSize(600,600),
nm invoke ,, xp.setup()
nm invoke ,, xp.setVisible(true)
nm invoke ,, xp.setXS(2)
nm invoke ,, xp.mainFrame.setTitle("JAVA XMIDAS DEMO...loading")
nm invoke ,, xp.mainFrame.setEnabled(false)

! add the menu bar to the main frame
nm new javax.swing.JMenuBar() jmb
nm new javax.swing.JMenu("File") jmf
nm invoke l:VK_F java.awt.event.KeyEvent.VK_F
nm invoke ,, jmf.setMnemonic(VK_F)
nm new javax.swing.JMenuItem("Exit Macro") reg.jmi
nm invoke l:VK_X java.awt.event.KeyEvent.VK_X
nm invoke ,, reg.jmi.setMnemonic(VK_X)
nm invoke ,, jmf.addSeparator()
nm invoke ,, jmf.add(reg.jmi)
nm invoke ,, jmb.add(jmf)
nm invoke ,, xp.mainFrame.setJMenuBar(jmb)
nm new nxm.sys.libxm.XControl("JMI") xcon1
nm invoke ,, xcon1.addActionListener(1)


! use a tabbed pane for display
nm new javax.swing.JTabbedPane() jtp
nm invoke ,, xp.mainFrame.setContentPane(jtp)

! add the XMidas plot
nm new nxm.sys.libxm.XCanvas() xc1
nm invoke ,, jtp.addTab("X-Midas XPLOT",xc1)
nm invoke ,, nxm.sys.libxm.XPrimitive.getXinfo(3,xc1)
xplot/bg/xwin=3/xs=4 fftdemo
nm invoke ,, xc1.wrap(4)

! add the NextMidas plot
nm new nxm.sys.libg.MPanel reg.mp1
nm invoke ,, jtp.addTab("Next-Midas PLOT",reg.mp1)
nm/bg plot/win=mp1 nxm.sys.dat.world

! add the java controls
nm new javax.swing.JPanel jp1
nm invoke ,, jtp.addTab("Java Controls",jp1)

! adding the slider bar
nm invoke PAGE_AXIS javax.swing.BoxLayout.PAGE_AXIS
nm new javax.swing.BoxLayout(jp1,PAGE_AXIS) blay
nm invoke ,, jp1.setLayout(blay)
nm invoke theme nxm.sys.libg.Theme.global()
nm invoke col theme.getSystemTextColor()
nm invoke bf javax.swing.BorderFactory.createLineBorder(col,2)
nm invoke bft javax.swing.BorderFactory.createTitledBorder(bf,"Slider Example")
nm new javax.swing.JSlider(0,100,50) reg.js1
nm invoke ,, reg.js1.setMajorTickSpacing(25)
nm invoke ,, reg.js1.setMinorTickSpacing(5)
nm invoke ,, reg.js1.setPaintTicks(true)
nm invoke ,, reg.js1.setPaintLabels(true)
nm invoke ,, reg.js1.setBorder(bft)
nm invoke ,, jp1.add(reg.js1)
nm new nxm.sys.libxm.XControl("JS1") xcon2
nm invoke ,, xcon2.addChangeListener(1)

! add the boring button
nm new javax.swing.JButton("VERY BORING BUTTON") reg.jb1
nm invoke CENTER_ALIGNMENT java.awt.Component.CENTER_ALIGNMENT
nm invoke ,, reg.jb1.setAlignmentX(CENTER_ALIGNMENT)
nm invoke ,, jp1.add(reg.jb1)
nm new nxm.sys.libxm.XControl("JB1") xcon3
nm invoke ,, xcon3.addActionListener(1)

! add the xpipe display button
nm new javax.swing.JButton("HIDE XPIPE WINDOW") reg.jb2
nm invoke ,, reg.jb2.setAlignmentX(CENTER_ALIGNMENT)
nm invoke ,, jp1.add(reg.jb2)
nm new nxm.sys.libxm.XControl("JB2") xcon3
nm invoke ,, xcon3.addActionListener(1)
res xp 1

! add the msglog
nm new javax.swing.JPanel jp2
nm new java.awt.Dimension(650,300) d1
nm invoke ,, jp2.setPreferredSize(d1)
nm new java.awt.BorderLayout blay
nm invoke ,, jp2.setLayout(blay)
nm invoke ,, jp1.add(jp2)
nm new nxm.sys.libxm.XCanvas() xc2
nm invoke ,, jp2.add(xc2)
nm invoke ,, nxm.sys.libxm.XPrimitive.getXinfo(5,xc2)
nm invoke ,, jp2.setBorder(bf)
xmsglog/bg/id=2/xs=6/xwin=5/info/lookback=0
nm invoke ,, xc2.wrap(6)

! Let the user start to interact
nm invoke ,, xp.mainFrame.setTitle("JAVA XMIDAS DEMO")
nm invoke ,, xp.mainFrame.setEnabled(true)

! turn on the xpipe (make sure the controls are enabled)
xpipe/controls/msglog=2 on
xpipe off

nm/off
message down

endmacro

startcontrols
  ! our only xmidas control is needed to listen for java messages
  xcontrol/lab=PROCESS_MESSAGES MSMONITOR 1
endcontrols

label PROCESS_MESSAGES
  message get /M/
  if "^M.name" eqs "ACTION"
    if "^M.val1" eqs "JMI"
      stop
    endif
  elseif "^M.name" eqs "BUTTON" 
    if "^M.val1" eqs "JB1" 
      callp logmsg "You pushed the very boring button" 3
    elseif "^M.val1" eqs "JB2"
      if xp eq 1
        nm invoke ,, nxm.sys.libxm.XPrimitive.unmap_window(1)
        nm res reg.jb2.text "SHOW XPIPE WINDOW"
	res xp 0
      else
        nm invoke ,, nxm.sys.libxm.XPrimitive.map_window(1)
        nm res reg.jb2.text "HIDE XPIPE WINDOW"
	res xp 1      
      endif	 
    endif
  elseif "^M.name" eqs "CHANGE"
    if "^M.val1" eqs "JS1" 
      nm res xm.sval reg.js1.value
      callp logmsg "Slider has a value of ^sval" 3
    endif
  endif
return

procedure logmsg a:msgstr n:sev[3]
  timecode/time=n/quiet ,,, ta:p_acqtime
  sedit p_acqtime p_acqtime range 1 24  ! /msglog/ wants 24 chars
  message send /MSGLOG/ 2 0 sev p_acqtime "Macro" msgstr
return
