#		
#  Module Bgp.tcl
#
#
# $Log: Bgp.tcl,v $
# Revision 1.1.1.1  2006/09/21 10:57:02  vfom
# Loading TkNetmon to CVS
#
# Revision 1.3  2003/12/30 05:18:21  vfom
# predrelease 2.0.5a
#
# Revision 1.2  2002/03/26 11:22:03  vfom
# *** empty log message ***
#
# Revision 1.1  2002/03/26 06:24:45  vfom
# Changes in Node dialog and LoadFile proc.
#
#
namespace eval ::Bgp {
variable	editBgp_exitFlag	0 \
		nodeKey		"" \
		err_msg		{} \
		bgplist		[list] 
array set 	mArray		[list]
}
#
#             
proc EditBgp_GUIconstructor {  } {
    set w .nodeProperties.editBgp
    set e $w.entryFrame
    set m $w.methodsFrame
    set mg $m.grid
    set mb $m.buttons
    set p $w.peersFrame
    set pg $p.grid
    set pb $p.buttons    
    set b $w.buttonFrame
#
    toplevel $w -class Dialog
    wm transient $w [winfo toplevel [winfo parent $w]]	
    wm withdraw $w
#
    frame $e -borderwidth 1 -relief sunken 
    pack $e -side top -padx 5 -pady 5 -expand on -fill both
    set curr_row 0
    label  $e.lasn -text [msgcat::mc "Autonomous system:"]
    entry  $e.easn -width 48 \
	-validatecommand {  return [EditBgp_validateAsn [%W get] ] } \
	-invalidcommand  "EditBgp_invalidAsn %W"
    grid $e.lasn -in $e -row $curr_row -column 0 -sticky e
    grid $e.easn -in $e -row $curr_row -column 1 -sticky w
    grid configure  $e.lasn $e.easn  -padx 5 -pady 5
    incr curr_row
#
    label  $e.ldescr -text [msgcat::mc "Description:"]
    entry  $e.edescr -width 48
    grid $e.ldescr -in $e -row $curr_row -column 0 -sticky e
    grid $e.edescr -in $e -row $curr_row -column 1 -sticky w
    grid configure  $e.ldescr $e.edescr -padx 5 -pady 5
    incr curr_row
#
    label  $e.ldir -text [msgcat::mc "Data directory:"]
    entry  $e.edir -width 48
    grid $e.ldir -in $e -row $curr_row -column 0 -sticky e
    grid $e.edir -in $e -row $curr_row -column 1 -sticky w
    grid configure  $e.ldir $e.edir -padx 5 -pady 5
    incr curr_row    
#
    frame $m -borderwidth 1 -relief sunken 
    pack $m -side top -padx 5 -pady 5 -expand on -fill both -anchor s
    label $m.lb -text [msgcat::mc "Bgp methods"] -font $::headerFont
    grid $m.lb -in $m -column 0 -row 0 -sticky w
    grid rowconfig $m 1 -weight 1 -minsize 0
    grid columnconfig $m 0 -weight 1 -minsize 0
    frame $mg
    grid $mg -in $m -column 0 -row 1 -sticky news
    grid configure $mg  -padx 5 -pady 5
    
    array set ::Bgp::mArray [list {0,0} [msgcat::mc "Type"] {0,1} [msgcat::mc "Name"] {0,2} [msgcat::mc "Parameters"] ]
    
    
    grid rowconfig $mg 0 -weight 1 -minsize 0
    grid columnconfig $mg 0 -weight 1 -minsize 0
#
	table $m.tbl -rows 1  -cols 3 \
	    -variable ::Bgp::mArray \
	    -titlerows 1 -titlecols 0 \
	    -yscrollcommand "$m.sy set "  \
	    -xscrollcommand "$m.sx set "  \
	    -roworigin 0 -colorigin 0 \
	    -width 450 -height 400 \
	    -selectmode browse \
	    -flashmode 0 \
	    -background #f700f700f700 \
	    -borderwidth 2 \
	    -selecttype row	\
	    -state disabled  \
	    -resizeborders both \
	    -anchor w
    scrollbar $m.sy -command "$m.tbl yview"
    scrollbar $m.sx -orient horiz -command "$m.tbl xview"
    grid $m.tbl -in $mg \
	-row 0 -column 0 -rowspan 1 -columnspan 1 -sticky news
    grid $m.sy -in $mg -column 1 -row 0 \
	-columnspan 1 -rowspan 1 -sticky news
    grid $m.sx -in $mg -column 0 -row 1 \
	-columnspan 1 -rowspan 1 -sticky news
    $m.tbl tag config title -bg #0000bf00ff00 -relief groove
    $m.tbl tag config sel -fg #f700f700f700 -bg #48003d008b00 
    $m.tbl width 0 12 1 18 2 42
    bind $m.tbl <Double-1> "LinkMethod $w $m.tbl {}   edit"
#
    frame $mb	
    grid $mb -in $m -column 0 -row 2 \
	-columnspan 1 -rowspan 1 -sticky n
    button	$mb.badd   -text [msgcat::mc "New Save"] -command "LinkMethod $w $m.tbl Save add"
    button	$mb.bedit  -text [msgcat::mc "Edit"]     -command "LinkMethod $w $m.tbl {}   edit"
    button	$mb.bdel   -text [msgcat::mc "Delete"]   -command "LinkMethod $w $m.tbl {}   delete"
    pack $mb.badd $mb.bedit $mb.bdel -padx 5 -pady 5 -side left 
#	
    frame $p -borderwidth 1 -relief sunken
    pack $p -side top -padx 5 -pady 5 -expand on -fill both -anchor s
    label $p.lb -text "Peers" -font $::headerFont
    grid $p.lb -in $p -column 0 -row 0 -sticky nw
    grid rowconfig $p 0 -weight 1 -minsize 0
    grid columnconfig $p 1 -weight 1 -minsize 0
    frame $pg
    grid $pg -in $p -column 1 -row 0 -sticky news
    grid configure $pg -padx 5 -pady 5

    grid rowconfig $pg 0 -weight 1 -minsize 0
    grid columnconfig $pg 0 -weight 1 -minsize 0
    listbox $p.lbx -width 24 -height 4  -yscrollcommand "$p.sy set "
    scrollbar $p.sy -command "$p.lbx yview"
    grid $p.lbx -in $pg \
	-row 0 -column 0 -rowspan 1 -columnspan 1 -sticky news
    grid $p.sy -in $pg -column 1 -row 0 \
	-columnspan 1 -rowspan 1 -sticky news
    
    bind $p.lbx <Double-1> "EditPeer $w $p.lbx edit"

    frame $pb	
    grid $pb -in $p -column 2 -row 0 \
	-columnspan 1 -rowspan 1 -sticky n
    button	$pb.badd   -text [msgcat::mc "Add"]    -command "EditPeer $w $p.lbx     add"
    button	$pb.bedit  -text [msgcat::mc "Edit"]   -command "EditPeer $w $p.lbx     edit"
    button	$pb.bdel   -text [msgcat::mc "Delete"] -command "EditPeer $w $p.lbx     delete"
    pack $pb.badd $pb.bedit $pb.bdel -padx 5 -pady 5 -side top -expand on -fill x
#
    frame $b 
    pack $b -side bottom -padx 5 -pady 5 -expand off -anchor s
    button $b.ok -text [msgcat::mc "Ok"] -command "EditBgp_OkButton $e"
    button $b.cancel -text [msgcat::mc "Cancel"] -command "set ::Bgp::editBgp_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
    set wdt [winfo reqwidth $w]
    set ht  [expr [winfo reqheight $w] +150 ]
    wm geom $w [format "%sx%s+%s+%s" $wdt $ht $x $y ]
    #
    bind $w <Destroy> "set ::Bgp::editBgp_exitFlag 2"
    bind $w <Escape>  "set ::Bgp::editBgp_exitFlag 2"
}
#
proc EditBgp_getGUIgeometry { } {
    set w .nodeProperties.editBgp
    set e $w.entryFrame
    set m $w.methodsFrame
    set mg $m.grid
    set mb $m.buttons
    set p $w.peersFrame
    set pg $p.grid
    set pb $p.buttons    
    set b $w.buttonFrame
    #
    if { [winfo exists $w] } {

    }
}

proc EditBgp { elbox mode } {
#
    set w .nodeProperties.editBgp
    set e $w.entryFrame
    set m $w.methodsFrame
    set mg $m.grid
    set mb $m.buttons
    set p $w.peersFrame
    set pg $p.grid
    set pb $p.buttons    
    set b $w.buttonFrame
    #
    if { ![winfo exists $w] } {
	EditBgp_GUIconstructor
    }
    #
    set bgpind   [ $elbox curselection ]
    set bgpasn   ""
    if { $bgpind !="" } { set bgpasn  [ $elbox get  $bgpind ]  }
    set bgpkey   [ DLsecondByFirst $::Bgp::bgplist $bgpasn ]	
    switch $mode {
	delete { 
	    if { $bgpasn == "" } { return }
	    if { $bgpkey != "" } {
	    	unset ::bgp_methodslist($bgpkey)
	    	unset ::bgp_peerslist($bgpkey)
		unset ::bgp_asn($bgpkey)
		unset ::bgp_owner($bgpkey)
		unset ::bgp_descr($bgpkey)
		unset ::bgp_datadir($bgpkey)
	    }
	    set ::Bgp::bgplist [ DLdeleteByFirst $::Bgp::bgplist $bgpasn ]
	    $elbox delete $bgpind
	    return
	}
	edit { 
	    if { $bgpasn == "" } { return }
	    if { $bgpkey == "" } { 
		set bgpkey .bgp$::bgpIndex
	    	set ::bgp_methodslist($bgpkey) [list]
	    	set ::bgp_peerslist($bgpkey) [list]
		set ::bgp_asn($bgpkey)	$bgpasn
		set ::bgp_owner($bgpkey)	$::Bgp::nodeKey
		set ::bgp_descr($bgpkey)	""
		set ::bgp_datadir($bgpkey)  	""
		incr ::bgpIndex		
	    }
	    $e.easn configure -state normal
	    $e.easn  insert 0 $::bgp_asn($bgpkey)	
	    $e.easn configure -validate none
	    $e.easn configure -state disabled
	}
	add  { 
	    set bgpkey .bgp$::bgpIndex
	    set ::bgp_methodslist($bgpkey) [list]
	    set ::bgp_peerslist($bgpkey) [list]
	    set ::bgp_asn($bgpkey)	""
	    set ::bgp_owner($bgpkey)	$::Bgp::nodeKey
	    set ::bgp_descr($bgpkey)	""
	    set ::bgp_datadir($bgpkey)	""
	    incr ::bgpIndex		
	    $e.easn configure -state normal
	    $e.easn configure -validate focusout 
	}
    }
    #    
    # Load actiual data
    #
    $e.edescr insert 0 $::bgp_descr($bgpkey)
    $e.edir   insert 0 $::bgp_datadir($bgpkey)
    set idx 1
    foreach i $::bgp_methodslist($bgpkey) {
	set ::Bgp::mArray($idx,0) [lindex $i 0]
	set ::Bgp::mArray($idx,1) [lindex $i 1]
	set ::Bgp::mArray($idx,2) [lindex $i 2]
	incr idx
    }
    $m.tbl configure -rows $idx
    #
    foreach i $::bgp_peerslist($bgpkey) {
	$p.lbx insert end $i
    }
    #
    if { [string equal [wm state $w] withdrawn ] } {
        wm deiconify $w
    }
    raise $w
        
    set ::Bgp::editBgp_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 ::Bgp::editBgp_exitFlag

    if {[winfo exists $w]} {
	wm withdraw $w
	catch {focus $oldFocus}
	grab release $w
	if {$::Bgp::editBgp_exitFlag==1} {
	    if {[string equal $mode add ]} {
		set ::bgp_asn($bgpkey)	[ $e.easn get ]	
		$elbox insert end $::bgp_asn($bgpkey)
		lappend ::Bgp::bgplist [list $::bgp_asn($bgpkey) $bgpkey ]
	    }
	    set ::bgp_descr($bgpkey)	[ $e.edescr get ]	
	    set ::bgp_datadir($bgpkey)	[ $e.edir get ]	
	    #
	    set nmethods [ expr [array size ::Bgp::mArray] / 3 ]
	    set tmp_mlist [list]
	    for {set i 1} {$i <$nmethods} {incr i} {
		lappend tmp_mlist [list $::Bgp::mArray($i,0) $::Bgp::mArray($i,1) $::Bgp::mArray($i,2)]
	    }
	    set ::bgp_methodslist($bgpkey) $tmp_mlist	    
	    # 
	    set ::bgp_peerslist($bgpkey) [$p.lbx get 0 end ]
	}
	#
	# Stop entry validation
	#
	$e.easn configure -state normal
	$e.easn configure -validate none 	
	$e.easn delete 0 end
	$e.edescr delete 0 end
	$e.edir delete 0 end
	$m.tbl configure -rows 1
	$p.lbx delete 0 end
    }	
    array unset ::Bgp::mArray
    array set ::Bgp::mArray [list {0,0} [msgcat::mc "Type"] {0,1} [msgcat::mc "Name"] {0,2} [msgcat::mc "Parameters"] ]
    #
    if {[string compare $oldGrab ""] && [winfo exists $oldGrab] } {
	if {![string compare $grabStatus "global"]} {
	    grab -global $oldGrab
	} else {
	    grab $oldGrab
	}
    }
    return
}
#
#
proc EditBgp_validateAsn { asn } {
    if { $asn==""} { return true }
    regsub -all -- {[0-9]+} $asn {} rez
    if { $rez!="" } { 
	set ::Bgp::err_msg [msgcat::mc "Enter valid autonomous system number."]
	return false 
    }
    set asns [ DLfirstParameterList $::Bgp::bgplist ]
    if { [lsearch $asns $asn ] >= 0 } { 
	set ::Bgp::err_msg [msgcat::mc "Such an autonomous system exist."]
	return false
    }
    return true 
}
#
proc EditBgp_invalidAsn { e } {
    tk_dialog $e.error [msgcat::mc "Error"] \
	$::Bgp::err_msg  error 0 [msgcat::mc "Ok"] 
    focus $e
}
#
proc EditBgp_OkButton { e } {
    if { [string equal [$e.easn cget -state] normal] } {
	set tmp_asn [$e.easn get]
	selection clear
	if { $tmp_asn == "" } { 
	    tk_dialog $e.error [msgcat::mc "Error"] \
	        [msgcat::mc "Enter non-null autonomous system number."] \
		error 0 [msgcat::mc "Ok"] 
	    focus $e		
	    return
	}
	if { ![$e.easn validate ]  } {
	    return
	}
    }
    set ::Bgp::editBgp_exitFlag 1
}
#
proc EditPeer { p elb mode } {
    switch $mode {
	add {
	    set tmp_ip [ EditIpAddr $p {} ]
	    if {$tmp_ip != "" } {
		$elb insert end $tmp_ip
	    }
	}
	edit {
	    set old_ip_ind [$elb curselection]
	    if { $old_ip_ind == "" } { return }
	    set old_ip [$elb get $old_ip_ind ]
	    set tmp_ip [ EditIpAddr $p $old_ip ]
	    if {$tmp_ip != "" } {
		$elb delete $old_ip_ind
		$elb insert $old_ip_ind $tmp_ip		    
	    }	
	    $elb selection set $old_ip_ind
	}
	delete {
	    set old_ip_ind [$elb curselection]
	    if { $old_ip_ind == "" } { return }
	    $elb delete $old_ip_ind
	}
    }
}
#
lappend ::GUI_pregenerate_list [list {EditBgp_GUIconstructor} {Loading Bgp dialog...}]
#