#         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.
################################################################################
# before_depend: defines the layout and functions of the concurrency set, before
#	before_dependency, and before before_dependency dialog box. This returns input focus
#	back to 'callerDialogBox' when the before_depend dialog box is closed. 
proc before_depend {callerDialogBox {qalter 0} } {
  global LABELFONT activeWindow def before_dependDialogBox

  if {!$qalter} {
    global qsubv ARR def_qsub
    set ARR "qsubv"
    set def def_qsub
  } else {
    global qalterv ARR def_qalter
    set ARR "qalterv"
    set def def_qalter
  }
  global jobinfo JOBS_LISTBOX_KEY beforeJob

  busy_cursor
## Bring up the standard Dialog box ##
  set before_dependDialogBox ".before_depend"
  set dbox [popupDialogBox $before_dependDialogBox "Before Dependency" 1 "" $callerDialogBox]
  set activeWindow($before_dependDialogBox) 1
  set dbox_top    [lindex $dbox 0]
  set dbox_bottom [lindex $dbox 1]

## BOTTOM PART ##
  set cmdButtons \
  [buildCmdButtons $dbox_bottom {{{ok ok} {cancel "reset to default"} {help help}}} x 9m 16 3]

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

  $cmdOk configure -command {

    destroy $before_dependDialogBox
  }

  if {$qalter} {
    $cmdCancel configure -width 18 -command {
	set_default_qalter_before
    }
  } else {
    $cmdCancel configure -width 18 -command {
	set_default_qsub_before
    }
  }

  $cmdHelp configure -command {xpbs_help before_depend $before_dependDialogBox}
## TOP PART ##
  frame $dbox_top.header -height 10m -width 10m
  frame $dbox_top.footer -height 10m -width 10m

  frame $dbox_top.lchild
  frame $dbox_top.rchild
  frame $dbox_top.beforebox
  frame $dbox_top.beforebuttons
  frame $dbox_top.beforebuttons.before
  frame $dbox_top.beforebuttons.beforeok
  frame $dbox_top.beforebuttons.beforenotok
  frame $dbox_top.beforebuttons.beforeany
 
## ARRANGEMENT ON SCREEN

### BEFORE ####

  message $dbox_top.beforeboxlabel -anchor c -font $LABELFONT -aspect 1000 \
	-text "BEFORE scheduling the
following job(s) for execution:"
  set listBoxInfo [buildFullListbox $dbox_top.beforebox 28x10 "(select one or more) " yscroll 1 0]
  set jobidframe   [lindex $listBoxInfo 0]
  set ${ARR}(selButton) [lindex $listBoxInfo 2]
  set ${ARR}(beforejobidBox) [lindex $listBoxInfo 3]

  if [info exists jobinfo] {
    foreach el [array names jobinfo] {
      set vallist [list $jobinfo($el)]

      set i 0
      foreach val [lindex $vallist $i] {
        set entry [strget_keyvals $val $JOBS_LISTBOX_KEY "@"]
        [set ${ARR}(beforejobidBox)] insert end $entry
        incr i
      }
    }
  }
  bind_depend before [set ${ARR}(beforejobidBox)]
  [set ${ARR}(selButton)] configure -textvariable ${ARR}(beforeSelMode) \
    -command {
	if { [string compare [set ${ARR}(beforeSelMode)]  "Select All"] == 0 } {
		if { $tk_version < 4.0 } {
			[set ${ARR}(beforejobidBox)] select from 0
			[set ${ARR}(beforejobidBox)] select to end
		} else {
			[set ${ARR}(beforejobidBox)] select anchor 0
			[set ${ARR}(beforejobidBox)] select set anchor end
		}
		set ${ARR}(beforeSelMode) "Deselect All"
		set beforeJob [get_keyvals [set ${ARR}(beforejobidBox)] \
							{0} "" ":" "select"]

	} else {
                if {$tk_version < 4.0} {
			[set ${ARR}(beforejobidBox)] select clear
		} else {
			[set ${ARR}(beforejobidBox)] select clear 0 end
		}
			set ${ARR}(beforeSelMode) "Select All"
			set beforeJob ""
	}
  }


  set entrybox [buildFullEntrybox $dbox_top.beforebuttons.before 11 "before:" \
			45 "" right 0 left]
  set ${ARR}(beforeJobidLabel) [lindex $entrybox 1]
  set ${ARR}(beforeJobid) [lindex $entrybox 2]
  set ${ARR}(beforeJobidScroll) [lindex $entrybox 3]
  [set ${ARR}(beforeJobid)] configure -textvariable ${ARR}(beforeJobs)
  register_default_action [set ${ARR}(beforeJobid)] $cmdOk

  set entrybox [buildFullEntrybox $dbox_top.beforebuttons.beforeok 11 \
			"beforeok:" 45 "" right 0 left]
  set ${ARR}(beforeokJobidLabel) [lindex $entrybox 1]
  set ${ARR}(beforeokJobid) [lindex $entrybox 2]
  set ${ARR}(beforeokJobidScroll) [lindex $entrybox 3]
  [set ${ARR}(beforeokJobid)] configure -textvariable ${ARR}(beforeokJobs)
  register_default_action [set ${ARR}(beforeokJobid)] $cmdOk

  set entrybox [buildFullEntrybox $dbox_top.beforebuttons.beforenotok 11 \
			"beforenotok:" 45 "" right 0 left]
  set ${ARR}(beforenotokJobidLabel) [lindex $entrybox 1]
  set ${ARR}(beforenotokJobid) [lindex $entrybox 2]
  set ${ARR}(beforenotokJobidScroll) [lindex $entrybox 3]
  [set ${ARR}(beforenotokJobid)] configure -textvariable ${ARR}(beforenotokJobs)
  register_default_action [set ${ARR}(beforenotokJobid)] $cmdOk

  set entrybox [buildFullEntrybox $dbox_top.beforebuttons.beforeany 11 \
			"beforeany:" 45 "" right 0 left]
  set ${ARR}(beforeanyJobidLabel) [lindex $entrybox 1]
  set ${ARR}(beforeanyJobid) [lindex $entrybox 2]
  set ${ARR}(beforeanyJobidScroll) [lindex $entrybox 3]
  [set ${ARR}(beforeanyJobid)] configure -textvariable ${ARR}(beforeanyJobs)
  register_default_action [set ${ARR}(beforeanyJobid)] $cmdOk

  set chkboxList \
      [list [concat before \"THIS JOB must have started execution.\" $dbox_top.beforebuttons.before] \
            [concat beforeOk \"THIS JOB must have terminated without errors." \
					$dbox_top.beforebuttons.beforeok] \
            [concat beforeNotok \"THIS JOB must have terminated with errors.\" \
                $dbox_top.beforebuttons.beforenotok] \
            [concat beforeAny \"THIS JOB must have terminated with or without errors.\" \
                $dbox_top.beforebuttons.beforeany] \
      ]	

  set cmdbox [buildCmdButtons $dbox_top.beforebuttons [list $chkboxList] \
                 y 2m 56 0 0 0 10m "(click the appropriate button)" 0]
  set ${ARR}(cmdboxBefore) [lindex $cmdbox 1]
  set ${ARR}(cmdboxBeforeok) [lindex $cmdbox 2]
  set ${ARR}(cmdboxBeforenotok) [lindex $cmdbox 3]
  set ${ARR}(cmdboxBeforeany) [lindex $cmdbox 4]

  [set ${ARR}(cmdboxBefore)] configure -command {
          enable_fullentry [set ${ARR}(beforeJobidLabel)] \
                           [set ${ARR}(beforeJobid)] \
                           [set ${ARR}(beforeJobidScroll)]
	  set ${ARR}(beforeJobs) $beforeJob
  }
  [set ${ARR}(cmdboxBeforeok)] configure -command {
          enable_fullentry [set ${ARR}(beforeokJobidLabel)] \
                           [set ${ARR}(beforeokJobid)] \
                           [set ${ARR}(beforeokJobidScroll)]
	  set ${ARR}(beforeokJobs) $beforeJob
  }
  [set ${ARR}(cmdboxBeforenotok)] configure -command {
          enable_fullentry [set ${ARR}(beforenotokJobidLabel)] \
                           [set ${ARR}(beforenotokJobid)] \
                           [set ${ARR}(beforenotokJobidScroll)]
	  set ${ARR}(beforenotokJobs) $beforeJob
  }
  [set ${ARR}(cmdboxBeforeany)] configure -command {
          enable_fullentry [set ${ARR}(beforeanyJobidLabel)] \
                           [set ${ARR}(beforeanyJobid)] \
                           [set ${ARR}(beforeanyJobidScroll)]
	  set ${ARR}(beforeanyJobs) $beforeJob
  }


### PACK the FRAMES
  pack $dbox_top.beforeboxlabel $dbox_top.beforebox -side top \
		-in $dbox_top.lchild -anchor nw
  label $dbox_top.dummy -height 3
  pack $dbox_top.dummy $dbox_top.beforebuttons -side top -in $dbox_top.rchild
  pack $dbox_top.lchild $dbox_top.rchild -side left -anchor nw -padx 2m

  remove_busy_cursor
  catch {tkwait window $before_dependDialogBox}
  set activeWindow($before_dependDialogBox) 0
  focus $callerDialogBox
  catch {grab $callerDialogBox}
}
