
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 WDAttribs InternalWindowing DialogFrame DefPadX DefPadY \
        ActiveDialog currentNode
    
    if {$ActiveDialog=="Arc Attributes"} {DestroyActiveDialog}
    if {$ActiveDialog=="Node Attributes"} {DestroyActiveDialog}

    if {$InternalWindowing} {
        set WDAttribs $DialogFrame.labels
        if {![winfo exists $WDAttribs]} {
            frame $WDAttribs
        }
        
        RegisterDialog "Node Attributes" $WDAttribs SetNodeAttributes {}
    } else {
        set WDAttribs .labels
        if {![winfo exists $WDAttribs]} {
            toplevel $WDAttribs
            wm title $WDAttribs "Node Attributes"
            wm resizable $WDAttribs 0 0
        }
    }
    

    UploadLabels
    if {$currentNode==""} {set currentNode 0}
    DownloadNodeLabels $currentNode

    set LFrame $WDAttribs.lframe
    frame $LFrame
     
    label $LFrame.indexText -text "Node Index" -anchor w
    grid $LFrame.indexText -in $LFrame \
        -row 0 -column 0 -rowspan 1 -columnspan 1 -sticky news -padx 1m -pady 2m
    label $LFrame.index -textvariable ThisNode -anchor w
    grid $LFrame.index -in $LFrame \
        -row 0 -column 1 -rowspan 1 -columnspan 1 -sticky news -padx 1m -pady 2m

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

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

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

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

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

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

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

    frame $WDAttribs.buttons
    button $WDAttribs.buttons.prev -image move_minus -height 20 -width 20 -command {
        if {[QualifiedNodeAttrib]} {
            UploadLabels
            global ThisNode ThisGraph
            incr ThisNode -1
            if {$ThisNode==-1} {
                set ThisNode [expr {[$ThisGraph #nodes]-1}]
            }
            DownloadNodeLabels $ThisNode
        }
    }
    
    button $WDAttribs.buttons.next -image move_plus -height 20 -width 20 -command {
        if {[QualifiedNodeAttrib]} {
            UploadLabels
            global ThisNode ThisGraph
            incr ThisNode
            if {$ThisNode==[$ThisGraph #nodes]} {
                set ThisNode 0
            }
            DownloadNodeLabels $ThisNode
        }
    }
    
    button $WDAttribs.buttons.done -text "Done" -command {
        if {[QualifiedNodeAttrib]} {
            UploadLabels
            if {$InternalWindowing} {
                DestroyActiveDialog
            } else {
                destroy $WDAttribs
            }
            PropagateModifications
        }
    }
    
    pack $WDAttribs.buttons.prev $WDAttribs.buttons.next $WDAttribs.buttons.done \
        -side left -fill x -padx 3m
     
    pack $WDAttribs.buttons -padx 3m -pady 3m -side bottom

    if {$InternalWindowing} {
        pack $WDAttribs -anchor nw
        AlignActiveDialog
    }
}

SetNodeAttributes
