#         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.
################################################################################
# after_depend: defines the layout and functions of the concurrency set, after
#	after_dependency, and before after_dependency dialog box. This returns input focus
#	back to 'callerDialogBox' when the after_depend dialog box is closed. 
proc after_depend {callerDialogBox {qalter 0} } {
  global LABELFONT activeWindow def after_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 ${ARR}_after_parent_id ${ARR}_afterok_parent_id \
	 ${ARR}_afternotok_parent_id ${ARR}_afterany_parent_id \
	 ${ARR}_before_parent_id ${ARR}_beforeok_parent_id \
	 ${ARR}_beforenotok_parent_id ${ARR}_beforeany_parent_id \
	 jobinfo JOBS_LISTBOX_KEY afterJob

  busy_cursor
## Bring up the standard Dialog box ##
  set after_dependDialogBox ".after_depend"
  set dbox [popupDialogBox $after_dependDialogBox "After Dependency" 1 "" $callerDialogBox]
  set activeWindow($after_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 $after_dependDialogBox
  }

  if {$qalter} {
    $cmdCancel configure -width 18 -command {
	set_default_qalter_after
    }
  } else {
    $cmdCancel configure -width 18 -command {
	set_default_qsub_after
    }
  }

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

  frame $dbox_top.on
  frame $dbox_top.on.count
  frame $dbox_top.lchild
  frame $dbox_top.rchild
  frame $dbox_top.afterbox
  frame $dbox_top.afterbuttons
  frame $dbox_top.afterbuttons.after
  frame $dbox_top.afterbuttons.afterok
  frame $dbox_top.afterbuttons.afternotok
  frame $dbox_top.afterbuttons.afterany
 
  label $dbox_top.on.label -text "THIS JOB is dependent on" \
	-font $LABELFONT -padx 1 -pady 1

  set spinBox [buildSpinbox $dbox_top.on.count 2 \
                            1-99 ${ARR} oncount "other job(s)." right]
  set spinCountFrame [lindex $spinBox 0]
  set ${ARR}(spinCountEntry) [lindex $spinBox 1]

  register_default_action [set ${ARR}(spinCountEntry)] $cmdOk

## ARRANGEMENT ON SCREEN

### AFTER ####

  message $dbox_top.afterboxlabel -anchor c -font $LABELFONT -aspect 1000 \
	-text "THIS JOB should be scheduled for
execution AFTER the following job(s):"
  set listBoxInfo [buildFullListbox $dbox_top.afterbox 28x10 "(select one or more) " yscroll 1 0]
  set jobidframe   [lindex $listBoxInfo 0]
  set ${ARR}(selButton) [lindex $listBoxInfo 2]
  set ${ARR}(afterjobidBox) [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}(afterjobidBox)] insert end $entry
        incr i
      }
    }
  }
  bind_depend after [set ${ARR}(afterjobidBox)]
  [set ${ARR}(selButton)] configure -textvariable ${ARR}(afterSelMode) \
    -command {
	if { [string compare [set ${ARR}(afterSelMode)]  "Select All"] == 0 } {
		if { $tk_version < 4.0 } {
			[set ${ARR}(afterjobidBox)] select from 0
			[set ${ARR}(afterjobidBox)] select to end
		} else {
			[set ${ARR}(afterjobidBox)] select anchor 0
			[set ${ARR}(afterjobidBox)] select set anchor end
		}
		set ${ARR}(afterSelMode) "Deselect All"
		set afterJob [get_keyvals [set ${ARR}(afterjobidBox)] \
							{0} "" ":" "select"]

	} else {
                if {$tk_version < 4.0} {
			[set ${ARR}(afterjobidBox)] select clear
		} else {
			[set ${ARR}(afterjobidBox)] select clear 0 end
		}
			set ${ARR}(afterSelMode) "Select All"
			set afterJob ""
	}
  }


  set entrybox [buildFullEntrybox $dbox_top.afterbuttons.after 11 "after:" 45 \
			"" right 0 left]
  set ${ARR}(afterJobidLabel) [lindex $entrybox 1]
  set ${ARR}(afterJobid) [lindex $entrybox 2]
  set ${ARR}(afterJobidScroll) [lindex $entrybox 3]
  [set ${ARR}(afterJobid)] configure -textvariable ${ARR}(afterJobs)
  register_default_action [set ${ARR}(afterJobid)] $cmdOk

  set entrybox [buildFullEntrybox $dbox_top.afterbuttons.afterok 11 "afterok:" \
			45 "" right 0 left]
  set ${ARR}(afterokJobidLabel) [lindex $entrybox 1]
  set ${ARR}(afterokJobid) [lindex $entrybox 2]
  set ${ARR}(afterokJobidScroll) [lindex $entrybox 3]
  [set ${ARR}(afterokJobid)] configure -textvariable ${ARR}(afterokJobs)
  register_default_action [set ${ARR}(afterokJobid)] $cmdOk

  set entrybox [buildFullEntrybox $dbox_top.afterbuttons.afternotok 11 \
			"afternotok:" 45 "" right 0 left]
  set ${ARR}(afternotokJobidLabel) [lindex $entrybox 1]
  set ${ARR}(afternotokJobid) [lindex $entrybox 2]
  set ${ARR}(afternotokJobidScroll) [lindex $entrybox 3]
  [set ${ARR}(afternotokJobid)] configure -textvariable ${ARR}(afternotokJobs)
  register_default_action [set ${ARR}(afternotokJobid)] $cmdOk

  set entrybox [buildFullEntrybox $dbox_top.afterbuttons.afterany 11 \
			"afterany:" 45 "" right 0 left]
  set ${ARR}(afteranyJobidLabel) [lindex $entrybox 1]
  set ${ARR}(afteranyJobid) [lindex $entrybox 2]
  set ${ARR}(afteranyJobidScroll) [lindex $entrybox 3]
  [set ${ARR}(afteranyJobid)] configure -textvariable ${ARR}(afteranyJobs)
  register_default_action [set ${ARR}(afteranyJobid)] $cmdOk

  set chkboxList \
      [list [concat after \"started execution.\" $dbox_top.afterbuttons.after] \
            [concat afterOk \"terminated without errors." \
					$dbox_top.afterbuttons.afterok] \
            [concat afterNotok \"terminated with errors.\" \
                $dbox_top.afterbuttons.afternotok] \
            [concat afterAny \"terminated with or without errors.\" \
                $dbox_top.afterbuttons.afterany] \
      ]	

  set cmdbox [buildCmdButtons $dbox_top.afterbuttons [list $chkboxList] \
                 y 2m 35 0 0 0  10m "(click the appropriate button)" 0]
  set ${ARR}(cmdboxAfter) [lindex $cmdbox 1]
  set ${ARR}(cmdboxAfterok) [lindex $cmdbox 2]
  set ${ARR}(cmdboxAfternotok) [lindex $cmdbox 3]
  set ${ARR}(cmdboxAfterany) [lindex $cmdbox 4]

  [set ${ARR}(cmdboxAfter)] configure -command {
          enable_fullentry [set ${ARR}(afterJobidLabel)] \
                           [set ${ARR}(afterJobid)] \
                           [set ${ARR}(afterJobidScroll)]
	  set ${ARR}(afterJobs) $afterJob
  }
  [set ${ARR}(cmdboxAfterok)] configure -command {
          enable_fullentry [set ${ARR}(afterokJobidLabel)] \
                           [set ${ARR}(afterokJobid)] \
                           [set ${ARR}(afterokJobidScroll)]
	  set ${ARR}(afterokJobs) $afterJob
  }
  [set ${ARR}(cmdboxAfternotok)] configure -command {
          enable_fullentry [set ${ARR}(afternotokJobidLabel)] \
                           [set ${ARR}(afternotokJobid)] \
                           [set ${ARR}(afternotokJobidScroll)]
	  set ${ARR}(afternotokJobs) $afterJob
  }
  [set ${ARR}(cmdboxAfterany)] configure -command {
          enable_fullentry [set ${ARR}(afteranyJobidLabel)] \
                           [set ${ARR}(afteranyJobid)] \
                           [set ${ARR}(afteranyJobidScroll)]
	  set ${ARR}(afteranyJobs) $afterJob
  }


### PACK the FRAMES
  pack $dbox_top.afterboxlabel $dbox_top.afterbox -side top \
		-in $dbox_top.lchild -anchor nw
  label $dbox_top.dummy -height 3
  pack $dbox_top.dummy $dbox_top.afterbuttons -side top -in $dbox_top.rchild
  pack $dbox_top.lchild $dbox_top.rchild -side left -anchor nw -padx 2m 
  pack $dbox_top.on.label $dbox_top.on.count -side left -anchor nw
  pack $dbox_top.on -side bottom -before $dbox_top.lchild -anchor nw -padx 2m

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