#
# Module Methods.tcl
#
# This is some methods dialogs.
#
#  $Log: Methods.tcl,v $
#  Revision 1.2  2006/10/26 09:39:17  vfom
#  Added code for netstate server query
#
#  Revision 1.1.1.1  2006/09/21 10:57:02  vfom
#  Loading TkNetmon to CVS
#
#
# Global variables initialisation:
# 
# set pollMethodsInternal  [ list ping snmp router dns radius ]
# set pollMethods  $pollMethodsInternal
# array set pollMBodyArray [ list ]
#
# set saveMethodsInternal  [ list router interface bgp]
# set saveMethods $saveMethodsInternal
# array set saveMBodyArray [ list ]
#
#
namespace eval ::Methods {
variable	viewMethods_exitFlag	0 \
		editMethod_exitFlag	0 \
		linkMethod_exitFlag	0 \
		err_msg			{}\
		mList		[list]	  \
		mListInt	[list]	  
array set	mBodyArray	[list]	  
}
#---------------------------------------------------------------------------
# EditMethod 
#  
#
#---------------------------------------------------------------------------
proc EditMethod_GUIconstructor { p } {
    set w $p.editMethod
    set f $w.nameFrame
    set t $w.textFrame
    set b $w.mFrame
#
    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 on -fill both
    label $f.lb -text [msgcat::mc "Method name: "]
    entry $f.emname -width 48 \
	-validatecommand { 
	    if { [string equal [%W cget -state] "normal"] && 
		[lsearch $Methods::mList [%W get]]>=0 } { return false }
	    return true
	    } \
	-invalidcommand  {
	    if { [string equal [%W cget -state] "normal"] } {
		set tmp_mname [%W get]
		selection clear
		tk_dialog .methods.editMethod.error [msgcat::mc "Error"] \
		    [msgcat::mc "Such a method exist."]\
		    error 0 [msgcat::mc "Ok"]		
	    }	
	    focus %W	
	}
    pack $f.lb $f.emname -side left -padx 5 -pady 5 
#
    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 
    text $t.tx -width 60 -height 28  \
	-xscrollcommand "$t.sx set" -yscrollcommand "$t.sy set"
    grid $t.tx -in $t \
	-row 0 -column 0 -columnspan 1 -rowspan 1 -sticky news
    scrollbar $t.sy -command "$t.tx yview"
    grid $t.sy -in $t \
	-row 0 -column 1 -columnspan 1 -rowspan 1 -sticky news
    scrollbar $t.sx -command "$t.tx xview" -orient horiz
    grid $t.sx -in $t \
	-row 1 -column 0 -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 {
	if { [ .methods.editMethod.nameFrame.emname get ] == "" } {
	    tk_dialog .methods.editMethod.error [msgcat::mc "Error"] \
		[msgcat::mc "Enter non-empty method name." ]\
		error 0 [msgcat::mc "Ok"]
	    focus .methods.editMethod.nameFrame.emname	
	    return 
	}
	if { ![ .methods.editMethod.nameFrame.emname validate] } { 
	    return 
	}
	if { ![ IsBracesBalanced [.methods.editMethod.textFrame.tx get 0.0 end ] ]} {
	    tk_dialog .methods.editMethod.error [msgcat::mc "Method Body Syntax Error"] \
		[msgcat::mc "Unbalanced curve braces, OR\nclose brace before open brace." ]\
		error 0 [msgcat::mc "Ok"]
	    return
	}
	set Methods::editMethod_exitFlag 1
    }
    button $b.cancel -text [msgcat::mc "Cancel"] -command "set Methods::editMethod_exitFlag 2"
    pack $b.ok $b.cancel -side left -padx 5 -pady 5

    update idletasks
    set x [expr {[winfo screenwidth $w] - [winfo reqwidth $w] \
    	- [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 ::Methods::editMethod_exitFlag 2"
    bind $w <Escape>  "set ::Methods::editMethod_exitFlag 2"
}

proc EditMethod { p act el } {
    if { [string equal $act "add"] } {
	set mname ""
    } else {
	set pos  [$el curselection] 
	if { $pos != "" } {
	    set mname [lindex $::Methods::mList $pos ]    
	    if { [string equal $act "delete" ] } {
		if { [lsearch $::Methods::mListInt $mname] >=0 } { return }
		if {[info exists ::Methods::mBodyArray($mname)] } {
		    unset ::Methods::mBodyArray($mname)
		}
		$el delete $pos
		return
	    }
	} else { return }
    }
    #    
    set w $p.editMethod
    set f $w.nameFrame
    set t $w.textFrame
    set b $w.mFrame
    if { ![winfo exists $w] } {
    	EditMethod_GUIconstructor $p
    }
#    
#  Load actual data
#    
    if {[string equal $act "edit"]} {
	wm title $w [msgcat::mc "Edit Method"]
	$f.emname insert 0 $mname
	$f.emname configure -state disabled
	if { [lsearch $::Methods::mListInt $mname] >=0 } {
	    $t.tx insert 0.0 [msgcat::mc "<<internal method: not editable>>"]
	    $t.tx configure -state disabled	    
	} else {
	    if {[info exists ::Methods::mBodyArray($mname)]} {
		$t.tx insert 0.0 $::Methods::mBodyArray($mname)
	    }	
	    $t.tx configure -state normal
	}

    } elseif {[string equal $act "add"]} {
	wm title $w [msgcat::mc "Add New Method"]
	$f.emname configure -state normal
    } else {
	return;
    }
#
# Start entry validation
#
    $f.emname configure -validate focusout
#
    if { [string equal [wm state $w] withdrawn ] } {
        wm deiconify $w
    } 
    raise $w
        
    set ::Methods::editMethod_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 ::Methods::editMethod_exitFlag

    if {[winfo exists $w]} {
	wm withdraw $w
	catch {focus $oldFocus}
	grab release $w
	if {$::Methods::editMethod_exitFlag==1} {
	    # Ok button pressed
	    if {[string equal $act "edit"]} {
		if { [lsearch $::Methods::mListInt $mname] <0 } {
		    set ::Methods::mBodyArray($mname) [ $t.tx get 0.0 "end-1 chars" ]
		}
	    } elseif {[string equal $act "add"]} {
		set mname [ $f.emname get ]	    
		if { $mname != "" } {
		    lappend ::Methods::mList $mname
		    set ::Methods::mBodyArray($mname) [ $t.tx get 0.0 "end-1 chars" ]
		}
	    }
	    
	}
    #
    # Stop entry validation
    #
	$f.emname configure -validate none
    # cleanup entries
	$f.emname configure -state normal
	$f.emname delete  0 end
	$t.tx configure -state normal
	$t.tx delete 0.0 end
    }
    if {[string compare $oldGrab ""] && [winfo exists $oldGrab] } {
	if {![string compare $grabStatus "global"]} {
	    grab -global $oldGrab
	} else {
	    grab $oldGrab
	}
    }

    return
}
#---------------------------------------------------------------------------
# ViewMethods 
#  
#
#  Usage:
#
# ViewMethods pollMethods pollMethodsInternal pollMBodyArray "Configure Poll Methods"
#
# ViewMethods saveMethods saveMethodsInternal saveMBodyArray "Configure Save Methods"
#
#---------------------------------------------------------------------------
proc ViewMethods_GUIconstructor { } {
    set w .methods
    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 ::Methods::mList
    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> "EditMethod $w 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 "EditMethod $w add     $l.elist"
    button $c.edit   -text [msgcat::mc "Edit"]   -command "EditMethod $w edit    $l.elist"
    button $c.delete -text [msgcat::mc "Delete"] -command "EditMethod $w 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 ::Methods::viewMethods_exitFlag 1"
    button $b.cancel -text [msgcat::mc "Cancel"] -command "set ::Methods::viewMethods_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 ::Methods::viewMethods_exitFlag 2"
    bind $w <Escape> "set ::Methods::viewMethods_exitFlag 2"
}
proc ViewMethods { lst lstint arr hd } {
    #
    set w .methods
    set f $w.mFrame
    set l $f.listFrame	
    set c $f.btFrame	
    set b $w.buttonFrame
    #
    if { [winfo exists $w] } {
	if { [string equal [wm state $w] withdrawn ] } {
	    wm deiconify $w
	}
	raise $w    
    } else {
	ViewMethods_GUIconstructor
    }
#    
#  Load actual data
#
    wm title $w $hd

    upvar $lst xlst
    upvar $lstint xlstint
    upvar $arr xarr
    set Methods::mList       $xlst
    set Methods::mListInt    $xlstint
    array set Methods::mBodyArray [array get xarr]
#
# 
#
    set Methods::viewMethods_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 Methods::viewMethods_exitFlag

    if {[winfo exists $w]} {
	wm withdraw $w
	catch {focus $oldFocus}
	grab release $w
	if {$Methods::viewMethods_exitFlag==1} {
	    # Ok button pressed
	    set xlst  $Methods::mList
	    array set xarr [array get ::Methods::mBodyArray]
	}
    }
    # cleanup entries
    set Methods::mList [ list ]
    set Methods::mListInt [ list ]
    array unset Methods::mBodyArray
    if {[string compare $oldGrab ""]} {
	if {![string compare $grabStatus "global"]} {
	    grab -global $oldGrab
	} else {
	    grab $oldGrab
	}
    }
    return
}
#---------------------------------------------------------------------------
# LinkMethod 
#  
#
#---------------------------------------------------------------------------
proc LinkMethod_GUIconstructor { p } {
    if { $p=="." } { set p {} }
    set w $p.linkMethod
    set e $w.entryFrame
    set b $w.buttonFrame
#
    toplevel $w -class Dialog
    wm transient $w [winfo toplevel [winfo parent $w]]	
    wm withdraw $w
#
    frame $e
    pack $e -side top -padx 5 -pady 5 -expand on -fill both
    set curr_row 0
    label  $e.lname -text [msgcat::mc "Save Method name:"]
    entry  $e.ename -width 54 	
    button $e.bname -text "..." 
    # -command "tk_chooseOption $w $e.ename ::saveMethods"
    grid $e.lname -in $e -row $curr_row -column 0 -sticky e
    grid $e.ename -in $e -row $curr_row -column 1 -sticky w
    grid $e.bname -in $e -row $curr_row -column 2 -sticky w
    incr curr_row
#
    label  $e.lprm -text [msgcat::mc "Parameters:"]
    entry  $e.eprm -width 54 \
	-validatecommand {  return [IsQuotesBalanced [%W get] ] } \
	-invalidcommand  "LinkMethod_invalidPrm %W"
    grid $e.lprm -in $e -row $curr_row -column 0 -sticky e
    grid $e.eprm -in $e -row $curr_row -column 1 -sticky w
    incr curr_row
#	
    frame $b
    pack $b -side bottom -padx 5 -pady 5 -expand off -anchor s
    button $b.ok -text [msgcat::mc "Ok"] -command "LinkMethod_OkButton $e"
    button $b.cancel -text [msgcat::mc "Cancel"] -command "set ::Methods::linkMethod_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 ::Methods::linkMethod_exitFlag 2"
    bind $w <Escape>  "set ::Methods::linkMethod_exitFlag 2"
}

proc LinkMethod { p tbl deftype mode } {
    if { $p =="." } { set p {} }
    set w $p.linkMethod
    set e $w.entryFrame
    set b $w.buttonFrame
    #
    if { ![winfo exists $w] } {
	LinkMethod_GUIconstructor $p
    }
#    
    set xtArr [ $tbl cget -variable ]
    upvar $xtArr tArr
    set idx  [ $tbl tag row sel ]
#    
    switch $mode {
	add {
	    wm title $w [msgcat::mc "Link new method" ]
	    $e.bname configure -state normal
    	    set type $deftype
	}
	edit {
	    if { $idx == "" } { 
		return 
	    }  else {
		wm title $w [msgcat::mc "Edit method parameters" ]
		set type $tArr($idx,0)	     
		$e.ename configure -state normal
		$e.ename insert 0 $tArr($idx,1)	    
		$e.bname configure -state disabled
		$e.eprm  insert 0 $tArr($idx,2)	    
	    }
	}
	delete  {
	    if { $idx == "" } { return }  else {
		set ncols [$tbl cget -cols ]
		set nrows [expr [ array size tArr  ] / $ncols]
		set prev_idx $idx
		set next_idx [expr $prev_idx+1]
		while { $next_idx < $nrows }  {
		    for { set j 0} {$j<$ncols} {incr j} { 
			set tArr($prev_idx,$j) $tArr($next_idx,$j) 
		    }
		    set prev_idx $next_idx
		    set next_idx [expr $prev_idx+1]
		}		    
		for { set j 0} {$j<$ncols} {incr j} { 
		    unset tArr($prev_idx,$j) 
		}
		$tbl configure -rows [expr $nrows -1]
		return
	    }
	}
    }
    switch $type {
	Save {
	    set lst ::saveMethods
	    $e.lname configure -text [msgcat::mc "Save Method name:"]
	    $e.bname configure -command "tk_chooseOption $w $e.ename ::saveMethods"
	} 
	Method {
	    set lst ::pollMethods
	    $e.lname configure -text [msgcat::mc "Poll Method name:"]
	    $e.bname configure -command "tk_chooseOption $w $e.ename ::pollMethods"
	    } 
	default { return } 
    }
    $e.ename configure -state disabled
    #
    $e.eprm configure -validate focusout        
    #
    if { [string equal [wm state $w] withdrawn ] } {
	wm deiconify $w
    }
    raise $w
        
    set ::Methods::linkMethod_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 ::Methods::linkMethod_exitFlag

    if {[winfo exists $w]} {
	wm withdraw $w
	catch {focus $oldFocus}
	grab release $w
	if { $::Methods::linkMethod_exitFlag==1 } {
	    # Ok button pressed	    
	    switch $mode {
		add {
		    set idx [expr [ array size tArr  ] / [$tbl cget -cols ]]		    
		    set tArr($idx,0) $type 
		    set tArr($idx,1) [$e.ename get] 
		    set tArr($idx,2) [$e.eprm  get] 
		    $tbl configure -rows [expr $idx + 1 ]
		} 
		edit { 	
		    set tArr($idx,2) [$e.eprm  get] 
		}
	    }
	}
    #
    # Stop entry validation
    #
    $e.eprm configure -validate none    
    # cleanup entries
    $e.ename configure -state normal
    $e.ename delete  0 end
    $e.eprm delete  0 end
    }
    if {[string compare $oldGrab ""] && [winfo exists $oldGrab] } {
	if {![string compare $grabStatus "global"]} {
	    grab -global $oldGrab
	} else {
	    grab $oldGrab
	}
    }
    return 
}
proc LinkMethod_invalidPrm { p } {
    if { [string equal [$p cget -state] "normal"] } {
	set tmp_prm [$p get]
	selection clear
	tk_dialog $p.error [msgcat::mc "Error"] \
	    [msgcat::mc "Unbalanced quotes in parameters."] \
	    error 0 [msgcat::mc "Ok"]		
	}	
	focus $p
}
proc LinkMethod_OkButton { p } {
    set name [ $p.ename get ]
    if { $name == "" } {
	tk_dialog $p.error [msgcat::mc "Error"] \
	    [msgcat::mc "Select method."] \
	    error 0 [msgcat::mc "Ok"]		
	focus $p.ename
	return
    }
    if { ![$p.eprm validate] } { return }
    set ::Methods::linkMethod_exitFlag 1
}
#---------------------------------------------------------------------------

