#
#  Module Traps.tcl
#
#   $Log: Traps.tcl,v $
#   Revision 1.3  2006/10/26 09:39:17  vfom
#   Added code for netstate server query
#
#   Revision 1.2  2006/10/19 08:40:26  vfom
#   .
#
#   Revision 1.1.1.1  2006/09/21 10:57:02  vfom
#   Loading TkNetmon to CVS
#
#   Revision 1.1  2004/11/05 11:50:39  vfom
#   ??
#
#
namespace eval ::Traps {
variable	linkTrap_exitFlag	0 \
		viewTraps_exitFlag	0 \
		editTrap_exitFlag	0 \
		tList	[list]    \
		err_msg		{}
array set	tBodyArray [list]
}
#
#
#---------------------------------------------------------------------------
proc EditTrap_GUIconstructor { } {
    set w .vtraps.editTrap
    set t $w.textFrame
    set b $w.mFrame
#
    toplevel $w -class Dialog
    wm transient $w [winfo toplevel [winfo parent $w]]	
    wm withdraw $w
#
    frame $t
    pack $t -side top -padx 5 -pady 5 -expand on -fill both 
    grid rowconfig    $t 0 -weight 1 -minsize 0 
    grid columnconfig $t 0 -weight 1 -minsize 0 
    set curr_row 0
    label $t.lb -text [msgcat::mc "Trap name:"]
    grid $t.lb -in $t \
	-row $curr_row -column 0 -columnspan 1 -rowspan 1 -sticky e
    entry $t.emname -width 48 \
	-validatecommand { 
	    if { [string equal [%W cget -state] "normal"] && 
		[lsearch $::Traps::tList [%W get]]>=0 } { return false }
	    return true
	    } \
	-invalidcommand  {
	    if { [string equal [%W cget -state] "normal"] } {
		set tmp_mname [%W get]
		selection clear
		tk_dialog .vtraps.editTrap.error [msgcat::mc "Error"] \
		    [msgcat::mc "Such a Trap exist."]\
		    error 0 [msgcat::mc "Ok"]		
	    }	
	    focus %W	
	}
    grid $t.emname -in $t \
	-row $curr_row -column 1 -columnspan 1 -rowspan 1 -sticky w
    incr curr_row
    label $t.lent -text [msgcat::mc "Enterprise OID:"]
    grid $t.lent -in $t \
	-row $curr_row -column 0 -columnspan 1 -rowspan 1 -sticky e
    entry $t.eent -width 48 
    grid $t.eent -in $t \
	-row $curr_row -column 1 -columnspan 1 -rowspan 1 -sticky w
    incr curr_row	
    label $t.lspc -text [msgcat::mc "Specific:"]
    grid $t.lspc -in $t \
	-row $curr_row -column 0 -columnspan 1 -rowspan 1 -sticky e
    entry $t.espc -width 8 
    grid $t.espc -in $t \
	-row $curr_row -column 1 -columnspan 1 -rowspan 1 -sticky w
	
    frame $b
    pack $b -side bottom -padx 5 -pady 5 -expand off -anchor s
    button $b.ok -text [msgcat::mc "Ok"] -command {
	if { [ .vtraps.editTrap.textFrame.emname get ] == "" } {
	    tk_dialog .vtraps.editTrap.error [msgcat::mc "Error"] \
		[msgcat::mc "Enter non-empty method name." ]\
		error 0 [msgcat::mc "Ok"]
	    focus .vtraps.editTrap.textFrame.emname	
	    return 
	}
	if { ![ .vtraps.editTrap.textFrame.emname validate] } { 
	    return 
	}
	if { ![::IsValidOID [ .vtraps.editTrap.textFrame.eent get ] ] } {
	    tk_dialog .vtraps.editTrap.error [msgcat::mc "Error"] \
		[msgcat::mc "Invalid Enterprise OID." ]\
		error 0 [msgcat::mc "Ok"]
	    focus .vtraps.editTrap.textFrame.eent	
	    return 
	}	
	set spc [ .vtraps.editTrap.textFrame.espc get ]
	regsub -all -- {[0-9]+} $spc {} num2
    	if { $spc == "" || $num2 != "" } {
	    tk_dialog .vtraps.editTrap.error [msgcat::mc "Error"] \
		[msgcat::mc "Invalid Specific ID." ]\
		error 0 [msgcat::mc "Ok"]
	    focus .vtraps.editTrap.textFrame.espc	
	    return 
	}	

	set ::Traps::editTrap_exitFlag 1
    }
    button $b.cancel -text [msgcat::mc "Cancel"] -command "set Traps::editTrap_exitFlag 2"
    pack $b.ok $b.cancel -side left -padx 5 -pady 5

    update idletasks
    set x [expr {[winfo screenwidth $w]/2 - [winfo reqwidth $w]/2 \
    	- [winfo vrootx [winfo parent $w]]}]
    set y [expr {[winfo screenheight $w]/2 - [winfo reqheight $w]/2 \
    	- [winfo vrooty [winfo parent $w]]}]
    wm geom $w +$x+$y
    #	
    bind $w <Destroy> "set ::Traps::editTrap_exitFlag 2"
    bind $w <Escape>  "set ::Traps::editTrap_exitFlag 2"
}
#
proc EditTrap { act el } {
    if { [string equal $act "add"] } {
	set mname ""
    } else {
	set pos  [$el curselection] 
	if { $pos != "" } {
	    set mname [lindex $::Traps::tList $pos ]    
	    if { [string equal $act "delete" ] } {
		if { [lsearch $::trapMethodsInternal $mname] >=0 } { return }
		if {[info exists ::Traps::tBodyArray($mname)] } {
		    unset ::Traps::tBodyArray($mname)
		}
		$el delete $pos
		return
	    }
	} else { return }
    }
    #    
    set w .vtraps.editTrap
    set t $w.textFrame
    set b $w.mFrame
    if { ![winfo exists $w] } {
    	EditTrap_GUIconstructor 
    }
#    
#  Load actual data
#    
    if {[string equal $act "edit"]} {
	wm title $w [msgcat::mc "Edit Trap"]
	$t.emname insert 0 $mname
	$t.emname configure -state disabled
	if { [lsearch $::trapMethodsInternal $mname] >=0 } {
	    $t.eent insert 0 [msgcat::mc "<<internal trap: not editable>>"]
	    $t.eent configure -state disabled	    
	} else {
	    if {[info exists ::Traps::tBodyArray($mname)]} {
		set Ent [lindex $::Traps::tBodyArray($mname) 0 ]
		set Spc [lindex $::Traps::tBodyArray($mname) 1 ]
		#$t.tx insert 0.0 $::Traps::tBodyArray($mname)
		$t.eent insert 0 $Ent
		$t.espc insert 0 $Spc
	    }	
	    $t.eent configure -state normal
	    $t.espc configure -state normal
	}

    } elseif {[string equal $act "add"]} {
	wm title $w [msgcat::mc "Add New Trap"]
	$t.emname configure -state normal
    } else {
	return;
    }
#
# Start entry validation
#
    $t.emname configure -validate focusout
#
    if { [string equal [wm state $w] withdrawn ] } {
        wm deiconify $w
    } 
    raise $w
        
    set ::Traps::editTrap_exitFlag 0
    update idletasks
    set oldFocus [focus]
    set oldGrab [grab current $w]
    if { [string compare $oldGrab ""] } {
	set grabStatus [grab status $oldGrab]
    }
    grab $w 
    focus $b.ok
    
    tkwait variable ::Traps::editTrap_exitFlag

    if {[winfo exists $w]} {
	wm withdraw $w
	catch {focus $oldFocus}
	grab release $w
	if {$::Traps::editTrap_exitFlag==1} {
	    # Ok button pressed
	    if {[string equal $act "edit"]} {
		if { [lsearch $::trapMethodsInternal $mname] <0 } {
		    #set ::Traps::tBodyArray($mname) [ $t.tx get 0.0 "end-1 chars" ]
		    set ::Traps::tBodyArray($mname) [list [ $t.eent get ] [ $t.espc get ]]
		}
	    } elseif {[string equal $act "add"]} {
		set mname [ $t.emname get ]	    
		if { $mname != "" } {
		    lappend ::Traps::tList $mname
		    #set ::Traps::tBodyArray($mname) [ $t.tx get 0.0 "end-1 chars" ]
		    set ::Traps::tBodyArray($mname) [list [ $t.eent get ] [ $t.espc get ]]
		}
	    }	    
	}
    #
    # Stop entry validation
    #
	$t.emname configure -validate none
    # cleanup entries
	$t.emname configure -state normal
	$t.emname delete  0 end
	$t.eent configure -state normal
	$t.eent delete 0 end
	$t.espc configure -state normal
	$t.espc delete 0 end
    }
    if {[string compare $oldGrab ""] && [winfo exists $oldGrab] } {
	if {![string compare $grabStatus "global"]} {
	    grab -global $oldGrab
	} else {
	    grab $oldGrab
	}
    }
    return
}
#
#---------------------------------------------------------------------------
# ViewTraps 
#  
#
#  Usage:
#
#
#
#---------------------------------------------------------------------------
proc ViewTraps_GUIconstructor { } {
    set w .vtraps
    set f $w.mFrame
    set l $f.listFrame	
    set c $f.btFrame	
    set b $w.buttonFrame
    toplevel $w -class Dialog
    wm transient $w [winfo toplevel [winfo parent $w]]	
    wm withdraw $w
#	
    frame $f
    pack $f -side top -padx 5 -pady 5 -expand off -anchor n
#
    frame $l
    pack $l -side top -padx 5 -pady 5 -expand on -fill both
	
    listbox $l.elist -width 48 -height 16 -yscrollcommand "$l.sy set" \
	-selectbackground navy -selectforeground white -listvar ::Traps::tList
    grid $l.elist -in $l \
	-row 2 -column 0 -columnspan 1 -rowspan 1 -sticky news
    scrollbar $l.sy -command "$l.elist yview"
    grid $l.sy -in $l \
	-row 2 -column 1 -columnspan 1 -rowspan 1 -sticky news
    bind $l.elist <Double-1> "EditTrap  edit $l.elist"
#
    frame $c
    pack $c -side bottom -padx 5 -pady 5 -expand off -anchor s 
    button $c.new    -text [msgcat::mc "Add"]        -command "EditTrap  add     $l.elist"
    button $c.edit   -text [msgcat::mc "Edit"]   -command "EditTrap edit    $l.elist"
    button $c.delete -text [msgcat::mc "Delete"] -command "EditTrap delete  $l.elist" 
    pack $c.new $c.edit $c.delete -side left -padx 5 -pady 5	
#    
    frame $b
    pack $b -side bottom -padx 5 -pady 5 -expand off -anchor s
    button $b.ok -text [msgcat::mc "Ok"] -command "set ::Traps::viewTraps_exitFlag 1"
    button $b.cancel -text [msgcat::mc "Cancel"] -command "set ::Traps::viewTraps_exitFlag 2"
    pack $b.ok $b.cancel -side left -padx 5 -pady 5
#
    update idletasks
    set x [expr {[winfo screenwidth $w]/2 - [winfo reqwidth $w]/2 \
	- [winfo vrootx [winfo parent $w]]}]
    set y [expr {[winfo screenheight $w]/2 - [winfo reqheight $w]/2 \
	- [winfo vrooty [winfo parent $w]]}]
    wm geom $w +$x+$y
#
#    wm deiconify $w
#    raise $w
    wm resizable $w 0 0
    bind $w <Destroy> "set ::Traps::viewTraps_exitFlag 2"
    bind $w <Escape> "set ::Traps::viewTraps_exitFlag 2"
}
#
proc ViewTraps { hd } {
    #
    set w .vtraps
    set f $w.mFrame
    set l $f.listFrame	
    set c $f.btFrame	
    set b $w.buttonFrame
    #
    if { ![winfo exists $w] } {
	ViewTraps_GUIconstructor
    }
#    
#  Load actual data
#
    wm title $w $hd
    set ::Traps::tList       $::trapMethods
    array set 	::Traps::tBodyArray [array get ::trapMBodyArray ]
#
    set ::Traps::viewTraps_exitFlag 0
    update idletasks
    wm deiconify $w
    raise $w
    set oldFocus [focus]
    set oldGrab [grab current $w]
    if { [string compare $oldGrab ""] } {
	set grabStatus [grab status $oldGrab]
    }
    grab $w 
    focus $b.ok
    
    tkwait variable ::Traps::viewTraps_exitFlag

    if {[winfo exists $w]} {
	wm withdraw $w
	wm withdraw $w
	catch {focus $oldFocus}
	grab release $w
	if {$::Traps::viewTraps_exitFlag==1} {
	    # Ok button pressed
	    set ::trapMethods  $::Traps::tList
	    array set ::trapMBodyArray [array get ::Traps::tBodyArray]
	}
    }
    # cleanup entries
    set ::Traps::tList [ list ]
    array unset ::Traps::tBodyArray
    if {[string compare $oldGrab ""]} {
	if {![string compare $grabStatus "global"]} {
	    grab -global $oldGrab
	} else {
	    grab $oldGrab
	}
    }
    return
}
#
# 
proc LinkTrap_GUIconstructor { } { 
    set w .nodeProperties.linkTrap
    set l $w.listFrame
    set b $w.buttonFrame
#
    toplevel $w -class Dialog
    wm transient $w [winfo toplevel [winfo parent $w]]	
    wm withdraw $w
#
    frame $l
    pack $l -side top -padx 5 -pady 5 -expand on -fill both    
#
    listbox $l.elist -width 48 -height 16 -yscrollcommand "$l.sy set" \
	-selectmode extended \
	-selectbackground navy -selectforeground white -listvar ::Traps::tList
    grid $l.elist -in $l \
	-row 2 -column 0 -columnspan 1 -rowspan 1 -sticky news
    scrollbar $l.sy -command "$l.elist yview"
    grid $l.sy -in $l \
	-row 2 -column 1 -columnspan 1 -rowspan 1 -sticky news
#
    frame $b
    pack $b -side bottom -padx 5 -pady 5 -expand off -anchor s
    button $b.ok -text [msgcat::mc "Ok"] -command "set ::Traps::linkTrap_exitFlag 1"
    button $b.cancel -text [msgcat::mc "Cancel"] -command "set ::Traps::linkTrap_exitFlag 2"
    pack $b.ok $b.cancel -side left -padx 5 -pady 5

    update idletasks
    set x [expr {[winfo screenwidth $w]/2 - [winfo reqwidth $w]/2 \
    	- [winfo vrootx [winfo parent $w]]}]
    set y [expr {[winfo screenheight $w]/2 - [winfo reqheight $w]/2 \
    	- [winfo vrooty [winfo parent $w]]}]
    wm geom $w +$x+$y
    #	
    bind $w <Destroy> "set ::Traps::linkTrap_exitFlag 2"
    bind $w <Escape>  "set ::Traps::linkTrap_exitFlag 2"    
#    
}
#
proc LinkTrap { lbx mode } {
    set w .nodeProperties.linkTrap
    set l $w.listFrame
    set b $w.buttonFrame
    
    if { [string equal $mode "delete"] } {
	foreach i [$lbx curselection] {
	    $lbx delete $i
	} 
	return
    }    
    
    if { ![winfo exists $w] } {
	LinkTrap_GUIconstructor
    }
#    
#  Load actual data
#
    set wassel [ $lbx get 0 end ]
    foreach i $::trapMethods {
	if { [lsearch $wassel $i ] <0 } {
	    lappend ::Traps::tList $i
	}
    }        
#
    set ::Traps::linkTrap_exitFlag 0
    update idletasks
    wm deiconify $w
    raise $w
    set oldFocus [focus]
    set oldGrab [grab current $w]
    if { [string compare $oldGrab ""] } {
	set grabStatus [grab status $oldGrab]
    }
    grab $w 
    focus $b.ok
    
    tkwait variable ::Traps::linkTrap_exitFlag

    if {[winfo exists $w]} {
	wm withdraw $w
	catch {focus $oldFocus}
	grab release $w
	if {$::Traps::linkTrap_exitFlag==1} {
	    # Ok button pressed
	    foreach i [ $l.elist curselection ]   {
		$lbx insert end [$l.elist get $i ]
	    }
	}
    }
    # cleanup entries
    set ::Traps::tList [ list ]
    if {[string compare $oldGrab ""]} {
	if {![string compare $grabStatus "global"]} {
	    grab -global $oldGrab
	} else {
	    grab $oldGrab
	}
    }
    return
}
#
lappend ::GUI_pregenerate_list [list {ViewTraps_GUIconstructor} {Loading Trap dialog...}]
lappend ::GUI_pregenerate_list [list {EditTrap_GUIconstructor} {Loading Trap dialog...}]
lappend ::GUI_pregenerate_list [list {LinkTrap_GUIconstructor} {Loading Trap dialog...}]
#