#         OpenPBS (Portable Batch System) v2.3 Software License
# 
# Copyright (c) 1999-2000 Veridian Information Solutions, Inc.
# All rights reserved.
# 
# ---------------------------------------------------------------------------
# For a license to use or redistribute the OpenPBS software under conditions
# other than those described below, or to purchase support for this software,
# please contact Veridian Systems, PBS Products Department ("Licensor") at:
# 
#    www.OpenPBS.org  +1 650 967-4675                  sales@OpenPBS.org
#                        877 902-4PBS (US toll-free)
# ---------------------------------------------------------------------------
# 
# This license covers use of the OpenPBS v2.3 software (the "Software") at
# your site or location, and, for certain users, redistribution of the
# Software to other sites and locations.  Use and redistribution of
# OpenPBS v2.3 in source and binary forms, with or without modification,
# are permitted provided that all of the following conditions are met.
# After December 31, 2001, only conditions 3-6 must be met:
# 
# 1. Commercial and/or non-commercial use of the Software is permitted
#    provided a current software registration is on file at www.OpenPBS.org.
#    If use of this software contributes to a publication, product, or
#    service, proper attribution must be given; see www.OpenPBS.org/credit.html
# 
# 2. Redistribution in any form is only permitted for non-commercial,
#    non-profit purposes.  There can be no charge for the Software or any
#    software incorporating the Software.  Further, there can be no
#    expectation of revenue generated as a consequence of redistributing
#    the Software.
# 
# 3. Any Redistribution of source code must retain the above copyright notice
#    and the acknowledgment contained in paragraph 6, this list of conditions
#    and the disclaimer contained in paragraph 7.
# 
# 4. Any Redistribution in binary form must reproduce the above copyright
#    notice and the acknowledgment contained in paragraph 6, this list of
#    conditions and the disclaimer contained in paragraph 7 in the
#    documentation and/or other materials provided with the distribution.
# 
# 5. Redistributions in any form must be accompanied by information on how to
#    obtain complete source code for the OpenPBS software and any
#    modifications and/or additions to the OpenPBS software.  The source code
#    must either be included in the distribution or be available for no more
#    than the cost of distribution plus a nominal fee, and all modifications
#    and additions to the Software must be freely redistributable by any party
#    (including Licensor) without restriction.
# 
# 6. All advertising materials mentioning features or use of the Software must
#    display the following acknowledgment:
# 
#     "This product includes software developed by NASA Ames Research Center,
#     Lawrence Livermore National Laboratory, and Veridian Information
#     Solutions, Inc.
#     Visit www.OpenPBS.org for OpenPBS software support,
#     products, and information."
# 
# 7. DISCLAIMER OF WARRANTY
# 
# THIS SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. 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 SHALL VERIDIAN CORPORATION, ITS AFFILIATED COMPANIES, OR THE
# U.S. GOVERNMENT OR ANY OF ITS AGENCIES BE LIABLE FOR ANY DIRECT OR 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
}
