#         Portable Batch System (PBS) Software License
# 
# Copyright (c) 1999, MRJ Technology Solutions.
# All rights reserved.
# 
# Acknowledgment: The Portable Batch System Software was originally developed
# as a joint project between the Numerical Aerospace Simulation (NAS) Systems
# Division of NASA Ames Research Center and the National Energy Research
# Supercomputer Center (NERSC) of Lawrence Livermore National Laboratory.
# 
# Redistribution of the Portable Batch System Software and use in source
# and binary forms, with or without modification, are permitted provided
# that the following conditions are met:
# 
# - Redistributions of source code must retain the above copyright and
#   acknowledgment notices, this list of conditions and the following
#   disclaimer.
# 
# - Redistributions in binary form must reproduce the above copyright and 
#   acknowledgment notices, this list of conditions and the following
#   disclaimer in the documentation and/or other materials provided with the
#   distribution.
# 
# - All advertising materials mentioning features or use of this software must
#   display the following acknowledgment:
# 
#   This product includes software developed by NASA Ames Research Center,
#   Lawrence Livermore National Laboratory, and MRJ Technology Solutions.
# 
#         DISCLAIMER OF WARRANTY
# 
# THIS SOFTWARE IS PROVIDED BY MRJ TECHNOLOGY SOLUTIONS ("MRJ") "AS IS" WITHOUT 
# WARRANTY OF ANY KIND, AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
# BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS
# FOR A PARTICULAR PURPOSE, AND NON-INFRINGEMENT ARE EXPRESSLY DISCLAIMED.
# 
# IN NO EVENT, UNLESS REQUIRED BY APPLICABLE LAW, SHALL MRJ, NASA, NOR
# THE U.S. GOVERNMENT BE LIABLE FOR ANY DIRECT DAMAGES WHATSOEVER,
# NOR ANY INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
# USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
# 
# This license will be governed by the laws of the Commonwealth of Virginia,
# without reference to its choice of law rules.
################################################################################
# res: defines the layout and functions of the select resources attributes
#	criteria dialog box.
proc res {} {
  global LABELFONT resDialogBox activeWindow selv selv_res selv_op \
	 selv_val jobResourceList

  busy_cursor
## Bring up the standard Dialog box ##
  set resDialogBox ".res"
  set dbox [popupDialogBox $resDialogBox "Select Resource Attributes Criteria" 1 "" "" 1]
  set activeWindow($resDialogBox) 1
  set dbox_top    [lindex $dbox 0]
  set dbox_bottom [lindex $dbox 1]

## BOTTOM PART ##
  set cmdButtons \
  [buildCmdButtons $dbox_bottom {{{ok ok} {help help}}} x 2m 18 3]

  set cmdFrame  [lindex $cmdButtons 0]
  set cmdOk     [lindex $cmdButtons 1]
  set cmdHelp   [lindex $cmdButtons 2]

  $cmdHelp configure -command {xpbs_help select.resources $resDialogBox}

  $cmdOk configure -command {

    switch -exact -- $selv(res_option) {
      res {
	set size 0
	if [info exists selv_res] { 
	  set size [array size selv_res]
        }
        for {set i 0} {$i < $size} {incr i} {

          if {[string compare $selv_res($i) ""] == 0 && \
              [string compare $selv_val($i) ""] == 0} {
	     continue
          }
          if {([string compare $selv_res($i) ""] == 0 && \
               [string compare $selv_val($i) ""] != 0) || \
	      ([string compare $selv_res($i) ""] != 0 && \
               [string compare $selv_val($i) ""] == 0) } {
             popupInfoBox $resDialogBox "Resource List entry $i is incomplete"
             return
          }
          set operator [oper $selv_op($i)]
	  if { [string compare $operator ""] == 0 } {
	    popupInfoBox $resDialogBox "Please specify the operator for entry $i"
            return
          }
          set sel_resources($i) "$selv_res($i)$operator$selv_val($i)"
        }
        set res_args [construct_array_args sel_resources ","]
        if {[string compare $res_args ""] == 0} {
          popupInfoBox $resDialogBox "Please specify a resource list argument"
          return
        } else {
          set selv(reslist) "Resources: $res_args"
        }
	catch {unset sel_resources}
      }
      ANY {set selv(reslist) "Resources: -ANY-"}
    }
    destroy $resDialogBox
  }

## TOP PART ##
  frame $dbox_top.header -height 10m -width 10m
  frame $dbox_top.footer -height 10m -width 10m

  frame $dbox_top.r
  frame $dbox_top.r.main
  frame $dbox_top.r.main.r -relief ridge -borderwidth 2
  frame $dbox_top.r.dummy -relief groove -borderwidth 3

  set selv(res_frame) $dbox_top.r.main.r

  box $selv(res_frame) -title "" \
	-entrylabels [list [list "resource" MENU_ENTRY $jobResourceList] \
	 [list "op" MENU_ENTRY "{ {} {=}} {{} {!=}} {{} {>=}} {{} {>}} {{} {<=}} {{} {<}}"] "value"] \
	-lboxwidths [list 8 3 30] -lboxheights [list 3 3 3] -orient y \
	-lboxlabels [list " " " " "Resources:"] \
	-array 0 selv_res -array 1 selv_op -array 2 selv_val \
        -key [list 0 1 2] -cleanstrExceptChar 1 "=" -grabBox $resDialogBox

  set radioList [list   [concat  list \"\" $selv(res_frame)] \
                          {a "ANY"} ]
  set rbox [buildRadioboxes $dbox_top.r.main [list $radioList] \
                            column 12 ""]
  set rboxFrame    [lindex $rbox 0]
  set rboxRes      [lindex $rbox 1]
  set rboxANY      [lindex $rbox 2]

  $rboxRes configure -variable selv(res_option) -value res -command {
    box $selv(res_frame) enable
  }

  $rboxANY configure -variable selv(res_option) -value ANY -command {
    box $selv(res_frame) disable
  }

## PACK the FRAMES
  label $dbox_top.r.l -text "Resource Attribute" -font $LABELFONT

  pack $dbox_top.header $dbox_top.r $dbox_top.footer -padx 5m
  pack $dbox_top.r.l $dbox_top.r.dummy -side left -padx 5 -anchor nw
  pack $rboxFrame -in $dbox_top.r.dummy -padx 3 -pady 3
  raise $rboxFrame $dbox_top.r.dummy
  pack $dbox_top.r

## set DEFAULT 
  set res_arg [lindex $selv(reslist) 1]
  switch -regexp -- $res_arg {
    "-ANY-"       { $rboxANY invoke }
    default       { $rboxRes invoke
		  load_argstr $res_arg {,} {selv_res selv_op selv_val} {\\. \\.}
		  foreach o [array names selv_op] {
			set selv_op($o) [oper_invert $selv_op($o)]
		  }
   }
  }

  register_default_action $resDialogBox $cmdOk
  tkwait visibility $resDialogBox
  boxLoad $selv(res_frame) 0
  remove_busy_cursor
  catch {tkwait window $resDialogBox}
  boxesUnset
  set activeWindow($resDialogBox) 0
}
