
#-------------------------------------------------------------------------------
# Module:     editArcLabels.tk
# Activation: Dynamically loaded on user request
# Scope:      Dialog for editing node labels
#-------------------------------------------------------------------------------



proc QualifiedNodeAttrib {} {
    global ThisDemand ThisDistance ThisColour ThisPotential ThisPredecessor \
        ThisNode ThisGraph
    set ret 1
    if {[catch {expr int($ThisDemand)}]} {set ret 0}
    if {$ThisDistance!="*" && [catch {expr int($ThisDistance)}]} {set ret 0}
    if {$ThisColour!="*" && ![QualifiedNode $ThisColour]} {set ret 0}
    if {[catch {expr int($ThisPotential)}]} {set ret 0}
    if {$ThisPredecessor!="*" && (![QualifiedArc $ThisPredecessor] || \
        $ThisNode!=[$ThisGraph arc $ThisPredecessor head])} {set ret 0}
    
    if {$ret==0} {
        global StatusInfo
        set StatusInfo "invalid node attributes"
    } else {
        set StatusInfo ""
    }
    
    return $ret
}


proc SetNodeAttributes {} {
    global WDNodeAttribs DefPadX currentNode ThisNode LNodeFrame RegisteredPath

    set title "Node Attributes"

    if {[RegisterFormOrWindow $title nodeLabels WDNodeAttribs SetNodeAttributes \
            "Done"          {
                if {[QualifiedNodeAttrib]} {
                    UploadNodeLabels
                    InitFile
                } else {
                    return
                }
            } \
            "Apply"         {
                if {[QualifiedNodeAttrib]} {
                    UploadNodeLabels
                    InitFile
                }
            } \
            "Arcs"          {
                if {[QualifiedNodeAttrib]} {
                    source [file join $GoblinTclPath editArcLabels.tk]
                }
            } \
            ""              {} \
        ]
    } {
        UploadNodeLabels
        DownloadNodeLabels $ThisNode
        focus $LNodeFrame.index
        return
    }
    

    set LNodeFrame $WDNodeAttribs.lframe
    frame $LNodeFrame
     
    label $LNodeFrame.indexText -text "Node Index:" -anchor w
    grid $LNodeFrame.indexText -in $LNodeFrame \
        -row 0 -column 0 -rowspan 1 -columnspan 1 -sticky news -padx 1m -pady 2m
    entry $LNodeFrame.index -textvariable ThisNode -width 5 -bg white
    grid $LNodeFrame.index -in $LNodeFrame \
        -row 0 -column 1 -rowspan 1 -columnspan 1 -sticky news -padx 1m -pady 2m

    label $LNodeFrame.demandText -text "Demand:" -anchor w
    grid $LNodeFrame.demandText -in $LNodeFrame \
        -row 1 -column 0 -rowspan 1 -columnspan 1 -sticky news -padx 1m
    entry $LNodeFrame.demand -textvariable ThisDemand -bg white -width 10
    grid $LNodeFrame.demand -in $LNodeFrame \
        -row 1 -column 1 -rowspan 1 -columnspan 1 -sticky news -padx 1m

    label $LNodeFrame.distanceText -text "Distance Label:" -anchor w
    grid $LNodeFrame.distanceText -in $LNodeFrame \
        -row 2 -column 0 -rowspan 1 -columnspan 1 -sticky news -padx 1m
    entry $LNodeFrame.distance -textvariable ThisDistance -bg white -width 10
    grid $LNodeFrame.distance -in $LNodeFrame \
        -row 2 -column 1 -rowspan 1 -columnspan 1 -sticky news -padx 1m

    label $LNodeFrame.colourText -text "Colour:" -anchor w
    grid $LNodeFrame.colourText -in $LNodeFrame \
        -row 3 -column 0 -rowspan 1 -columnspan 1 -sticky news -padx 1m
    entry $LNodeFrame.colour -textvariable ThisColour -bg white -width 10
    grid $LNodeFrame.colour -in $LNodeFrame \
        -row 3 -column 1 -rowspan 1 -columnspan 1 -sticky news -padx 1m

    label $LNodeFrame.potentialText -text "Potential:" -anchor w
    grid $LNodeFrame.potentialText -in $LNodeFrame \
        -row 4 -column 0 -rowspan 1 -columnspan 1 -sticky news -padx 1m
    entry $LNodeFrame.potential -textvariable ThisPotential -bg white -width 10
    grid $LNodeFrame.potential -in $LNodeFrame \
        -row 4 -column 1 -rowspan 1 -columnspan 1 -sticky news -padx 1m

    label $LNodeFrame.predecessorText -text "Predecessor:" -anchor w
    grid $LNodeFrame.predecessorText -in $LNodeFrame \
        -row 5 -column 0 -rowspan 1 -columnspan 1 -sticky news -padx 1m
    entry $LNodeFrame.predecessor -textvariable ThisPredecessor -bg white -width 10
    grid $LNodeFrame.predecessor -in $LNodeFrame \
        -row 5 -column 1 -rowspan 1 -columnspan 1 -sticky news -padx 1m

    grid rowconfig    $LNodeFrame 0 -weight 1 -minsize 0
    grid columnconfig $LNodeFrame 0 -weight 1 -minsize 0

    pack $LNodeFrame -side top -padx $DefPadX -pady 5m
    
    bind $LNodeFrame.index       <Return> {focus $LNodeFrame.demand}
    bind $LNodeFrame.demand      <Return> {focus $LNodeFrame.distance}
    bind $LNodeFrame.distance    <Return> {focus $LNodeFrame.colour}
    bind $LNodeFrame.colour      <Return> {focus $LNodeFrame.potential}
    bind $LNodeFrame.potential   <Return> {focus $LNodeFrame.predecessor}
    bind $LNodeFrame.predecessor <Return> {
        if {[QualifiedNodeAttrib]} {
            UploadNodeLabels
            incr ThisNode
            if {$ThisNode==[$ThisGraph #nodes]} {
                set ThisNode 0
            }
            DownloadNodeLabels $ThisNode
        }
        focus $LNodeFrame.index
    }

    bind $LNodeFrame.index <FocusOut> {
        if {![QualifiedNode $ThisNode]} {
            focus $LNodeFrame.index
        } else {
            DownloadNodeLabels $ThisNode
        }
    }

    bind $LNodeFrame.index <Up> {
        if {[QualifiedNodeAttrib]} {
            UploadNodeLabels
            incr ThisNode -1
            if {$ThisNode==-1} {
                set ThisNode [expr {[$ThisGraph #nodes]-1}]
            }
            DownloadNodeLabels $ThisNode
        }
    }
    
    bind $LNodeFrame.index <Down> {
        if {[QualifiedNodeAttrib]} {
            UploadNodeLabels
            incr ThisNode
            if {$ThisNode==[$ThisGraph #nodes]} {
                set ThisNode 0
            }
            DownloadNodeLabels $ThisNode
        }
    }
    
    focus $LNodeFrame.index


    DisplayForm $title
}

SetNodeAttributes
