# $Revision$ on $Date$
# The above revision line needs to be the first line for all techtk files
# Copyright (c) 2006 Ansys Inc


#----------------------------------------------------------------------------
#
# PADT---------------------------------------------------- Phoenix Analysis &
#                                                         Design Technologies
#
#----------------------------------------------------------------------------
#
#       Package: AI-Environment GUI Management
#
#
#          File: parser.tcl
#
#
#     Developer: Matt Sutton
#
#
#   Description: This file contains the procedures used to parse the resource
#                file and build up a tree representation of the GUI.  The 
#                primary data structure is the GUITree.
#   
#
#      03/25/02: Demo release 1. Limited/partial widget support. 
#                Limited error checking.
#
#           
#
#-----------------------------------------------------------------------------
# For support: 1-480-813-4884 ext. 125 -or- matt.sutton@padtinc.com
#-----------------------------------------------------------------------------

namespace eval ::AIEnv::GUIManagement::Widgets {
    namespace import -force DropdownButton::*
    namespace export *
}

namespace eval ::AIEnv::GUIManagement {
    namespace export ParseResourceFile
}
    


proc ::AIEnv::GUIManagement::ParseError {Reason} {
    variable CurrLine
    global AppName
    set LineNumber [ResourceFileLineNumberStack peek]
    set FileName [ResourceFileNameStack peek]
    set ErrorMessage "Error on line: $LineNumber\nIn file: \"$FileName\"\n\nReason: \"$Reason\"\nLine: $CurrLine \n\nAI*Environment cannot continue."
    
    error $ErrorMessage
}

proc ::AIEnv::GUIManagement::ParseResourceFile {ResFileName} {
    global BuildProgressValue
    global BuildProgressIndicator
    global BuildProgressStage
    global AppName Version
    global env
    
    variable ForceEOF
    variable FastBuild
    
    set ForceEOF 0
    set BuildProgressValue 0
    if { [TryLoadFromCache] == 1} {
	set FastBuild 1
	return
    } else {
	set FastBuild 0
	if { [info exists BuildProgressStage] } {
	    $BuildProgressStage configure -text "Initializing $AppName $Version: Reading Description Files..."
	    update
	}
	
	AddIncludeFile $ResFileName
	ReadFile $ResFileName
	Parse
	
         if { ![info exists env(AI_ENV_NOCACHE)]} {

	   if { [info exists BuildProgressStage] } {
	       $BuildProgressStage configure -text "Initializing $AppName $Version: Writing Cache File..."
	       update
	   }	
	
	   WriteCache

          }		
    }
}


proc ::AIEnv::GUIManagement::ParseComment {} {
    while {[GetToken] != "_EOLToken"} {}
}

proc ::AIEnv::GUIManagement::ParseInclude {} {
    variable CATRootDir
   
    RegisterEOLErrorMessage "include"
    set FileName [CheckEOL [GetToken] "filename"]
    if {[file extension $FileName] == ""} { set FileName $FileName.ttkrf }
   
    variable CATRootDir
    set file [file join $CATRootDir resource $FileName]

    AddIncludeFile $file
    ReadFile $file
}

proc ::AIEnv::GUIManagement::ParseIncludePattern {} {
    variable CATRootDir
    
    RegisterEOLErrorMessage "include"
    set FileName [CheckEOL [GetToken] "filename"]
    if {[file extension $FileName] == ""} { set FileName $FileName.ttkrf }
    
    set dir [GetToken]
    if { $dir != "_EOLToken" } {
       if {![file isdirectory $dir] && [info exist $dir]} { set dir [set $dir] }
       if ![file isdirectory $dir] {
         # ParseError "dir field $dir is invalid in include line"
         return ""
       }
    } else {
       variable CATRootDir
       set dir [file join $CATRootDir resource]
    }

    set ff ""
    global is_win
    if $is_win {
       set ff [glob -nocomplain [file join $dir $FileName]]
    } else {
       if ![catch {exec /bin/ls -a $dir} files] {
          foreach f $files {
             if [string match $FileName $f] { lappend ff $dir/$f }
          }
       } else {
          set ff [glob -nocomplain [file join $dir $FileName]]
       }
    }

    set rr ""
    foreach f $ff { lappend rr [file tail $f] }

    foreach s [lsort -decreasing $rr] {
       set f [file join $dir $s]
       AddIncludeFile $f
     # ReadFile $f
       set ResFileID [open_with_encoding $f r]
       ResourceFileIDStack push $ResFileID
       ResourceFileNameStack push $f
       ResourceFileLineNumberStack push 0
    }
}




#-----------------------------------------------------------------------------
#
# Common procedures used during parsing
#
proc ::AIEnv::GUIManagement::EatWhite {} {
    # Eat any end of line tokens
    while {[PeekToken] == "_EOLToken"} {
        GetToken
    }
}

proc ::AIEnv::GUIManagement::RecordName {Name NodeID} {
    variable ItemNameMap
    if {![info exists ItemNameMap($Name)]} {
        set ItemNameMap($Name) $NodeID
    } else {
        ParseError "The name \"$Name\" is already in use."
    }
}

proc ::AIEnv::GUIManagement::RegisterEOLErrorMessage {Msg} {
    variable EOLErrorMessage
    set EOLErrorMessage "Unexpected end of line encountered while parsing the \"$Msg\" command."
}

proc ::AIEnv::GUIManagement::CheckEOL {Token Quantity} {
    variable EOLErrorMessage
    if { $Token == "_EOLToken" } {
        ParseError "$EOLErrorMessage Expected a $Quantity"
    } else {
        return $Token
    }
}

proc ::AIEnv::GUIManagement::CheckDefaultName {Token Widget} {
	variable WidgetCounters
	if { $Token == "" } {
		if { [info exists WidgetCounters($Widget)] == 1 } {
			set Counter $WidgetCounters($Widget)
		} else {
			set Counter -1
		}
		incr Counter
		set WidgetCounters($Widget) $Counter
		return ${Widget}${Counter}
	} else {
		return $Token
	}
}

proc ::AIEnv::GUIManagement::CheckNumber {Token Quantity} {
    variable EOLErrorMessage
    #Try and match a character in token to something other than a number
    switch -regexp $Token {
        [0-9]* { return $Token }
        default {
            ParseError "Expecting a number while parsing the \"$Quantity\" option of the $EOLErrorMessage command."
        }
    }
}

#-----------------------------------------------------------------------------
#
# Driver that dispatches the parsing work to procedures based on the
# command token
#

proc ::AIEnv::GUIManagement::Parse {} {
    set Token [GetToken]

    while {$Token != "EOF"} {
        switch -exact $Token {
            "winMenu"            { ParseWinMenu }
            "topMenu"            { ParseTopMenu }
            "menu"               { ParseMenu }
            "menuItem"           { ParseMenuItem }
            "separator"          { ParseSeparator }
            "menuCheck"          { ParseMenuCheck }
            "menuRadio"          { ParseMenuRadio }
            "tabbedIconBar"      { ParseTabbedIconBar }
            "iconBar"            { ParseIconBar }
            "iconButton"         { ParseIconButton }
            "iconToggle"         { ParseIconToggle }
            "iconDrop"           { ParseIconDrop }
            "dataEntryZone"      { ParseDataEntryZoneVariable }
            "iconZone"           { ParseIconZoneVariable }
            "menuZone"           { ParseMenuZoneVariable }
            "tabbedIconZone"     { ParseTabbedIconZoneVariable }
            "textEntry"          { ParseTextEntry }
            "textEntry2"         { ParseTextEntry2 }
            "textEntryEdit"      { ParseTextEntryEdit }
            "textTableEntry"     { ParseTextTableEntry }
            "TableEntry"         { ParseTableEntry }
            "entityChooser"      { ParseEntityChooser }
            "entityAddRemover"   { ParseEntityAddRemover  }
            "coordsEntry"        { ParseCoordsEntry }
            "frame"              { ParseFrame }
            "checkFrame"         { ParseCheckFrame }
            "radioFrame"         { ParseRadioFrame }
            "radioGroup"         { ParseRadioGroup }
            "radioItem"          { ParseRadioItem }
            "dropDownEntry"      { ParseDropDownEntry }
            "integerEntry"       { ParseIntegerEntry }
            "integerEntryIcon"   { ParseIntegerEntryIcon }
            "textButton"         { ParseTextButton }
            "iconButtonGroup"    { ParseIconButtonGroup }
            "table"              { ParseTable }
            "checkItem"          { ParseCheckItem }
            "checkItem2"          { ParseCheckItem2 }
            "dialogSpace"        { ParseDialogSpace }
            "dataEntryDialog"    { ParseDataEntryDialog }
            "dialogWindow"       { ParseDialogWindow }
            "pickScreenCoord"    { ParsePickScreenCoord }
            "#"                  { ParseComment }
            "include"            { ParseInclude }
            "includePattern"     { ParseIncludePattern }
            "group"              { ParseGroup }
            "dataTable"          { ParseDataTable }
            "_EOLToken"          { EatWhite }
            "EOF"                { ParseEOF }
            "greyGroup"          { ParseGreyGroup }
            "greyGroupItem"      { ParseGreyGroupItem }
            "wizard"             { ParseWizard }
            "wizardPage"         { ParseWizardPage }
            "wizardColumn"       { ParseWizardColumn }
            "hotKey"             { ParseHotKey }
            "popupMenu"          { ParsePopupMenu }
            "listBox"            { ParseListBox }
            "scaleBar"           { ParseScaleBar }
            "animator"           { ParseAnimator }
            "ampEntry"           { ParseAmpEntry } 
            "entryButton"        { ParseEntryButton }
            "entityButton"       { ParseEntityButton }
            "bmpOptMenu"         { ParseBmpOptMenu }
            "textOptMenu"        { ParseTextOptMenu }
            "colorOptMenu"       { ParseColorOptMenu }
            "textLabel"          { ParseTextLabel }
            "entityMover"        { ParseEntityMover }
            "multiSelect"        { ParseMultiSelect }
            "multiSelectRows"    { ParseMultiSelectRows }
            "multiSelectColumns" { ParseMultiSelectColumns }
            "exclusiveGroup"     { ParseExclusiveGroup }
            "exclusiveGroupItem" { ParseExclusiveGroupItem }
            "ifdef"              { ParseIfDef }
            "ifndef"             { ParseIfNDef }
            "endif"              { ParseEndIf }
            "columnMultiSelect"  { ParseColumnMultiSelect }
            "cmsColumn"          { ParseCMSColumn }
            "cmsItem"            { ParseCMSItem }
            "fileChooser"        { ParseFileChooser }
            "rFChooser"          { ParseRFChooser }
            default              { ParseUnknown $Token }
        }
        EatWhite
        set Token [GetToken]
    }
    
}

proc ::AIEnv::GUIManagement::ParseUnknown {Token} {
	variable ItemNameMap
	set GUITree [TreeStack peek_one]
	if { [ParentStack size] == 0 } {
		ParseError "Unknown token: \"$Token\"" 
	} else {
		set ParentType [$GUITree get [ParentStack peek_one] -key "Type"]
		switch -exact -- $ParentType {
			WizardPage {
				set NodeID [$GUITree insert [ParentStack peek_one] end]
				$GUITree set $NodeID -key "Type" "WizardColumnName"
				$GUITree set $NodeID -key "Name" $Token
				#set ItemNameMap($Token) $NodeID
			}
			MultiSelectRows {
				set ParentNodeID [$GUITree parent [ParentStack peek_one]]
				set RowList [$GUITree get $ParentNodeID -key "RowLabelList"]
				lappend RowList "$Token"
				$GUITree set $ParentNodeID -key "RowLabelList" $RowList
			}
			MultiSelectColumns {
				set ParentNodeID [$GUITree parent [ParentStack peek_one]]
				set ColumnList [$GUITree get $ParentNodeID -key "ColumnLabelList"]
				lappend ColumnList "$Token"
				$GUITree set $ParentNodeID -key "ColumnLabelList" $ColumnList				
			}
			default {
				ParseError "Unknown token: $Token"
			}
		}
	}
}

proc ::AIEnv::GUIManagement::ParseEOF {} {
	variable ForceEOF
	set ForceEOF 1
}

proc ::AIEnv::GUIManagement::ParseDataEntryZoneVariable {} {
    variable GUIZones
    set Token [GetToken]
    if {$Token != "_EOLToken"} {
        set GUIZones("DataEntryZone") $Token
    } else {
        ParseError "Unexpected end of line token while parsing the dataEntryZone command. Expected a Tcl window path."
    }
}

proc ::AIEnv::GUIManagement::ParseIconZoneVariable {} {
    variable GUIZones
    set Token [GetToken]
    if {$Token != "_EOLToken"} {
        set GUIZones("IconZone") $Token
    } else {
        ParseError "Unexpected end of line token while parsing the dataEntryZone command. Expected a Tcl window path."
    }
}

proc ::AIEnv::GUIManagement::ParseMenuZoneVariable {} {
    variable GUIZones
    set Token [GetToken]
    if {$Token != "_EOLToken"} {
        set GUIZones("MenuZone") $Token
    } else {
        ParseError "Unexpected end of line token while parsing the dataEntryZone command. Expected a Tcl window path."
    }
}

proc ::AIEnv::GUIManagement::ParseTabbedIconZoneVariable {} {
    variable GUIZones
    set Token [GetToken]
    if {$Token != "_EOLToken"} {
        set GUIZones("TabbedIconZone") $Token
    } else {
        ParseError "Unexpected end of line token while parsing the dataEntryZone command. Expected a Tcl window path."
    }
}

proc ::AIEnv::GUIManagement::CheckMatchingEnd {Type} {
    variable Context
    set Token [GetToken]
    if { $Token == "END" } {
        set MatchingEND 0
        if { [WidgetTypeStack size] != 0 } {
			set CurType [WidgetTypeStack peek]
			if { $CurType == $Type } {
				set MatchingEND 1
			}
		}
		
		if { $MatchingEND == 1 } {
			# If so, pop the parent stack
			ParentStack pop
			# Since we are specifying the end of the WinMenu command, set the
			# current context back to NULL
			WidgetTypeStack pop
			set Context "NULL"
			return $Token
		} else {
			ParseError "An unmatched \"END\" statement was detected.  Expected a \"$Type END\".  Possible missing \"END\" for: $CurType"
		}
	}
	WidgetTypeStack push $Type	
	
	return $Token
}

proc ::AIEnv::GUIManagement::ParseIfDef {} {
	global env envCache
	set EnvName [CheckEOL [GetToken] "environment variable"]

	if { [info exists env($EnvName)] } {
	    if { $env($EnvName) == 0 } {
		FlushUntilMatchingEndif
	    }
	    AddEnvironmentVariable $EnvName $env($EnvName)
	    set envCache($EnvName) 1
	} else {
	    AddEnvironmentVariable $EnvName -1
	    set envCache($EnvName) 0
	    FlushUntilMatchingEndif
	}
}

proc ::AIEnv::GUIManagement::ParseIfNDef {} {
	global env envCache
	set EnvName [CheckEOL [GetToken] "environment variable"]

	if { [info exists env($EnvName)] } {
	    if { $env($EnvName) == 1 } {
		FlushUntilMatchingEndif
	    }
	    AddEnvironmentVariable $EnvName $env($EnvName)
	    set envCache($EnvName) 1
	} else {
	    AddEnvironmentVariable $EnvName -1
	    set envCache($EnvName) 0
	}
}

proc ::AIEnv::GUIManagement::ParseEndIf {} {
	
}

proc ::AIEnv::GUIManagement::FlushUntilMatchingEndif {} {
    set Valid 1
    while { $Valid > 0 } {
	set Token [GetToken]
	switch -- $Token {
	    EOF { set Valid 0 }
	    endif { incr Valid -1 }
	    ifdef - ifndef { incr Valid 1 }
	}
    }
}
proc ::AIEnv::GUIManagement::ShowName {dez item show} {
    variable ItemNameMap
    variable GUIDisplayData
	set node ItemNameMap($item)
	if ![info exists $node] return
	set node [set $node]
	if {$dez == ""} {
		set dez [TreeStack peek]
	}
	set wins [$dez get $node -key "WindowPath"]
	if {[llength $wins] != 2} return
	foreach "Label Text" $wins ""
	if !$show {
		grid forget $Label
		grid forget $Text
	} else {
	    grid configure $Label -row 1 -column 0 -sticky e 
		grid configure $Text -row 1 -column 1 -columnspan 2 -sticky ew -padx $GUIDisplayData("DEZPadx")
	}	
}
proc ::AIEnv::GUIManagement::ResetPicker {dez picker args} {
	# reset picker image/label/helptext. 
	#  
    variable ItemNameMap    
	set node ItemNameMap($picker)
	if ![info exists $node] return
	set node [set $node]
	if {$dez == ""} {
		set dez [TreeStack peek]
	}
	set type [$dez get $node -key "Type"]
	foreach {u v} $args {
	    set $u $v
	}
    set wins [$dez get $node -key "WindowPath"]
	foreach w $wins {
		set ew [lindex [split $w -] end]
		if {$ew == "label"} {
		    set wlabel $w
		    if [info exists ResetToggleGrey] {
		        if {[winfo exists $w] && ([$w cget -state] == "disabled")} {
                    $w configure -state normal -fg black
                }
            }
		}
		if {$ew == "text"} {
		    if [info exists ResetToggleGrey] {
		        if {[winfo exists $w] && ([$w cget -state] == "disabled")} {
                    $w configure -state normal -bg white
                }
            }
		}
		if {$ew == "buttonbox"} {
		    set wpick ${w}.b0
		    if [info exists ResetToggleGrey] {
		        foreach ww [winfo children $w] {
                    if {[winfo exists $ww] && ([$ww cget -state] == "disabled")} {
                        $ww configure -state normal
                    }
                }
            }
	    }
	}
	if {[info exists wlabel] && [winfo exists $wlabel] && [info exists label]} {
		$wlabel configure -text $label
	}
	if {![info exists wpick] || ![winfo exists $wpick]} return
	if [info exists image] {
		set im [vam_image $image]
		if {$im != ""} {
			$wpick configure -image $im
		}
	}
	if [info exists helptext] {
		$wpick configure -helptext $helptext
	}
	if [info exists command] {
		$wpick configure -command $command
	}
}

#-----------------------------------------------------------------------------
#
# Procedure to parse the WinMenu Command
# 
proc ::AIEnv::GUIManagement::ParseWinMenu {} {
    variable ItemNameMap
    variable Context
	
	set GUITree [TreeStack peek_one]
	
	set Token [CheckMatchingEnd "WinMenu"]
   	if { $Token == "END" } {
		return
	}
    set NodeID [$GUITree insert $ItemNameMap("Menu") end] 
    $GUITree set $NodeID -key "Type" "MenuBar"
    $GUITree set $NodeID -key "Name" windowMenu
    RecordName windowMenu $NodeID
    ParentStack push $NodeID
    
    variable GUIItemNameMap
    
}

proc ::AIEnv::GUIManagement::ParsePopupMenu {} {
	variable ItemNameMap
	set GUITree [TreeStack peek_one]
	
	set Token [CheckMatchingEnd "PopupMenu"]
	if { $Token == "END" } {
		return
	}
	
	set NodeID [$GUITree insert $ItemNameMap("Popup") end]
	$GUITree set $NodeID -key "Type" "PopupMenu"
	set Name [CheckEOL $Token "popupMenu"]
	$GUITree set $NodeID -key "Name" $Name
	
	RecordName $Name $NodeID
	ParentStack push $NodeID
}



#-----------------------------------------------------------------------------
#
proc ::AIEnv::GUIManagement::ParseHotKey {} {
    variable ItemNameMap
    set GUITree [TreeStack peek_one]
    
    set NodeID [$GUITree insert $ItemNameMap("HotKey") end]
    $GUITree set $NodeID -key "Type" "HotKey"
    
    set Key [CheckEOL [GetToken] "Key"]
    $GUITree set $NodeID -key "Key" $Key
    
    set Command [CheckEOL [GetToken] "Command"]
    $GUITree set $NodeID -key "Command" $Command
    
    set Help [CheckEOL [GetToken] "Help"]
    $GUITree set $NodeID -key "Help" $Help
    
    set Tab [CheckEOL [GetToken] "Tab"]
    $GUITree set $NodeID -key "Tab" $Tab
}

#-----------------------------------------------------------------------------

# Procedure to parse the menuItem Command
# 
proc ::AIEnv::GUIManagement::ParseMenuItem {} {
    
    set GUITree [TreeStack peek_one]
    # Create a node
	set NodeID [$GUITree insert [ParentStack peek_one] end]
	# Add all the data
	$GUITree set $NodeID -key "Type" "MenuItem"
	set Name [CheckEOL [GetToken] "name"]
	$GUITree set $NodeID -key "Name" $Name
	$GUITree set $NodeID -key "Proc" [CheckEOL [GetToken] "proc"]
	$GUITree set $NodeID -key "GreyFlag" 0

	# Add the name to the name map
	RecordName $Name $NodeID

}


#-----------------------------------------------------------------------------
#
# Procedure to parse the topMenu Command
#
proc ::AIEnv::GUIManagement::ParseTopMenu {} {
    variable ItemNameMap
 
    set GUITree [TreeStack peek_one]
    set Token [CheckMatchingEnd "TopMenu"]
    if { $Token == "END" } { return "" }

    CheckEOL $Token winMenu
    set Name $Token

    set NodeID [$GUITree insert [ParentStack peek_one] end]
 
    # Add the data associated with this node to the tree
    $GUITree set $NodeID -key "Type" "TopMenu"
    $GUITree set $NodeID -key "Name" $Name
    $GUITree set $NodeID -key "GreyFlag" 0
 
    set Token [GetToken]
    if { $Token != "_EOLToken" } {
       $GUITree set $NodeID -key "Proc" $Token
    } else {
       $GUITree set $NodeID -key "Proc" "NULL"
    }

        # This is a container type, so push it onto the parent stack
    ParentStack push $NodeID
    RecordName $Name $NodeID
}

#-----------------------------------------------------------------------------
#
# Procedure to parse the menu Command
# 
proc ::AIEnv::GUIManagement::ParseMenu {} {
    variable ItemNameMap
	
	set GUITree [TreeStack peek_one]
	set Token [CheckMatchingEnd "Menu"]
    if { $Token == "END" } {
		return
	}
    
    CheckEOL $Token winMenu
    # The next token in the stream should be the Menu name
	set Name $Token

	# Since this is menu, by default it is inserted as a child
	# of the winMenu node. This "should" be the top of the 
	# parent stack if all is going well.
#
# TO DO: Add code to assert that the ParentStack top does 
# indeed contain a winMenu item.
#
	set NodeID [$GUITree insert [ParentStack peek_one] end]

	# Add the data associated with this node to the tree
	$GUITree set $NodeID -key "Type" "Menu"
	$GUITree set $NodeID -key "Name" $Name
	$GUITree set $NodeID -key "GreyFlag" 0

	# This is a container type, so push it onto the parent stack
	ParentStack push $NodeID

	RecordName $Name $NodeID
}





#-----------------------------------------------------------------------------
#
# Procedure to parse the menuCheck Command
# 
proc ::AIEnv::GUIManagement::ParseMenuCheck {} {
    RegisterEOLErrorMessage "menuCheck"
	set GUITree [TreeStack peek_one]
	set NodeID [$GUITree insert [ParentStack peek_one] end]
	$GUITree set $NodeID -key "Type" "MenuCheck"
	$GUITree set $NodeID -key "Name" [CheckEOL [GetToken] "name"]
	$GUITree set $NodeID -key "Proc" [CheckEOL [GetToken] "proc"]
	$GUITree set $NodeID -key "CheckFlag" [CheckEOL [GetToken] "checkFlag"]
	$GUITree set $NodeID -key "GreyFlag" 0
    RecordName [$GUITree get $NodeID -key "Name"] $NodeID
 
}


#-----------------------------------------------------------------------------
#
# Procedure to parse the menuRadio Command
# 
proc ::AIEnv::GUIManagement::ParseMenuRadio {} {
    RegisterEOLErrorMessage "menuRadio"
	set GUITree [TreeStack peek_one]
	set NodeID [$GUITree insert [ParentStack peek_one] end]
	$GUITree set $NodeID -key "Type" "MenuRadio"
	$GUITree set $NodeID -key "Name" [CheckEOL [GetToken] "name"]
	$GUITree set $NodeID -key "Proc" [CheckEOL [GetToken] "proc"]
	$GUITree set $NodeID -key "VarName" [CheckEOL [GetToken] "variable"]
	$GUITree set $NodeID -key "Value" [CheckEOL [GetToken] "value"]
	$GUITree set $NodeID -key "GreyFlag" 0
    RecordName [$GUITree get $NodeID -key "Name"] $NodeID
 
}


#-----------------------------------------------------------------------------
#
# Procedure to parse the separator Command
# 
proc ::AIEnv::GUIManagement::ParseSeparator {} {
    # Just push a seperator type onto the tree
    set GUITree [TreeStack peek_one]
    set NodeID [$GUITree insert [ParentStack peek_one] end]
    $GUITree set $NodeID -key "Type" "Separator"
}


#-----------------------------------------------------------------------------
#
# Procedure to parse the tabbed Icon Bar command
# 
proc ::AIEnv::GUIManagement::ParseTabbedIconBar {} {
    variable Context
    variable ItemNameMap
    
    RegisterEOLErrorMessage "tabbedIconBar"
    
    set Token [CheckMatchingEnd "TabbedIcon"]
    if { $Token == "END" } {
    	return
	}
	
	set GUITree [TreeStack peek_one]
        
    set NodeID [$GUITree insert $ItemNameMap("TabbedIcon")  end]
    
    $GUITree set $NodeID -key "Type" "TabbedIcon"
    $GUITree set $NodeID -key "Name" $Token
    $GUITree set $NodeID -key "Label" [CheckEOL [GetToken] "label"]
    $GUITree set $NodeID -key "ImageName" [CheckEOL [GetToken] "iconImage"]
    $GUITree set $NodeID -key "ToolTip" [CheckEOL [GetToken] "toolTip"]
    $GUITree set $NodeID -key "GreyFlag" 0
    
    # Add the name to the name map
    RecordName [$GUITree get $NodeID -key "Name"] $NodeID
    
    set Context "TabbedIcon"
    
    # Set up the parent stack
    ParentStack push $NodeID
   
}

#-----------------------------------------------------------------------------
#
# Procedure to parse the Icon Bar command
#
proc ::AIEnv::GUIManagement::ParseIconBar {} {
    variable ItemNameMap
    RegisterEOLErrorMessage "iconBar"
    
    set Token [CheckMatchingEnd "IconBar"]
    if {$Token == "END"} {
		return
	}
	
	
		
    set GUITree [TreeStack peek_one]
   	   	
	set ParentNode ""
	if { [ParentStack size] == 0 } {
		set ParentNode $ItemNameMap("Icon")
	} else {
		set CurType [$GUITree get [ParentStack peek_one] -key "Type"]
		if { ($CurType == "TabbedIcon") || ($CurType == "Icon") } {
			set ParentNode [ParentStack peek_one]
		} else {
			ParseError "An icon bar cannot be used within a $CurType"
		}
	}
	
	if { $ParentNode != "" } {
		set NodeID [$GUITree insert $ParentNode end]
		$GUITree set $NodeID -key "Type" "IconBar"
		set Name [CheckEOL $Token "iconBar"]
		$GUITree set $NodeID -key "Name" $Name
		$GUITree set $NodeID -key "GreyFlag" 0
		
		ParentStack push $NodeID
		RecordName $Name $NodeID
	} else {
		ParseError "No parent node was determined while parsing an icon bar."
	}
}



#-----------------------------------------------------------------------------
#
# Procedure to parse the Icon Button Group command
#
proc ::AIEnv::GUIManagement::ParseIconButtonGroup {} {
    RegisterEOLErrorMessage "iconButtonGroup"
    variable IconButtonTextLength

    set GUITree [TreeStack peek_one]
    set NodeID [$GUITree insert [ParentStack peek_one] end]

    $GUITree set $NodeID -key "Type" "IconButtonGroup"
    set Name [CheckEOL [GetToken] "name"]
    $GUITree set $NodeID -key "Name" $Name
    $GUITree set $NodeID -key "Proc" [CheckEOL [GetToken] "proc"]
    $GUITree set $NodeID -key "Side" [CheckEOL [GetToken] "side"]
    $GUITree set $NodeID -key "Anchor" [CheckEOL [GetToken] "anchor"]

    $GUITree set $NodeID -key "GreyFlag" 0

    set Token [GetToken]
    set n 0
    while { $Token != "_EOLToken" } {
       $GUITree set $NodeID -key "Button_$n" $Token
       incr n
       set Token [GetToken]
    }
    $GUITree set $NodeID -key "NumButtons" $n

    RecordName $Name $NodeID
}


#-----------------------------------------------------------------------------
#
# Procedure to parse the Icon Button command
#
proc ::AIEnv::GUIManagement::ParseIconButton {} {
    RegisterEOLErrorMessage "iconButton"
    variable IconButtonTextLength
    
    set GUITree [TreeStack peek_one]
    set NodeID [$GUITree insert [ParentStack peek_one] end]
    
    $GUITree set $NodeID -key "Type" "IconButton"
    set Name [CheckEOL [GetToken] "name"]
    $GUITree set $NodeID -key "Name" $Name
    set Label [CheckEOL [GetToken] "label"]
    $GUITree set $NodeID -key "Label" $Label
    $GUITree set $NodeID -key "ImageName" [CheckEOL [GetToken] "image name"]
    $GUITree set $NodeID -key "Proc" [CheckEOL [GetToken] "procedure"]
    $GUITree set $NodeID -key "ToolTip" [CheckEOL [GetToken] "tooltip"]
    $GUITree set $NodeID -key "GreyFlag" 0
    
    # Record the iconbutton's label length incase the user wants to use
    # the text button instead
    if {![info exists IconButtonTextLength] } {
		set IconButtonTextLength [string length $Label]
	} elseif { $IconButtonTextLength < [string length $Label] } {
		set IconButtonTextLength [string length $Label]
	}
    # Add the name to the name map
    RecordName $Name $NodeID
}

#-----------------------------------------------------------------------------
#
# Procedure to parse the Icon Toggle command
#
proc ::AIEnv::GUIManagement::ParseIconToggle {} {
    RegisterEOLErrorMessage "iconToggle"
    
    set GUITree [TreeStack peek_one]
    set NodeID [$GUITree insert [ParentStack peek_one] end]

    $GUITree set $NodeID -key "Type" "IconToggle"
    set Name [CheckEOL [GetToken] "name"]
    $GUITree set $NodeID -key "Name" $Name
    $GUITree set $NodeID -key "Label" [CheckEOL [GetToken] "label"]
    $GUITree set $NodeID -key "ImageName" [CheckEOL [GetToken] "image name"]
    $GUITree set $NodeID -key "Proc" [CheckEOL [GetToken] "procedure"]
    $GUITree set $NodeID -key "ToolTip" [CheckEOL [GetToken] "tooltip"]
    $GUITree set $NodeID -key "GreyFlag" 0
    
    RecordName $Name $NodeID
}

#-----------------------------------------------------------------------------
#
# Procedure to parse the Icon Drop command
#
proc ::AIEnv::GUIManagement::ParseIconDrop {} {
    set Token [CheckMatchingEnd "IconDrop"]
    if { $Token == "END" } {
		return
	}
	set GUITree [TreeStack peek_one]
		
	set NodeID [$GUITree insert [ParentStack peek_one] end]
	
	$GUITree set $NodeID -key "Type" "IconDrop"
	set Name [CheckEOL $Token "name"]
	$GUITree set $NodeID -key "Name" $Name
	$GUITree set $NodeID -key "GreyFlag" 0
	ParentStack push $NodeID
	RecordName $Name $NodeID
}

proc ::AIEnv::GUIManagement::ParseGroup {} {
    RegisterEOLErrorMessage "group"
     # Check for consistent types
    set Token [CheckEOL [GetToken] "name -or- END"]
    set GUITree [TreeStack peek_one]
        
    if { $Token == "END" } {
        ParentStack pop
    } else {
        set Name $Token
        set NodeID [$GUITree insert [ParentStack peek_one] end]
        $GUITree set $NodeID -key "Type" "Group"
        $GUITree set $NodeID -key "Name" $Name
        $GUITree set $NodeID -key "GreyFlag" 0
        
        ParentStack push $NodeID
        # Add the name to the name map
        RecordName $Name $NodeID
    }
}

#-----------------------------------------------------------------------------
#
# Procedure to parse the Text Entry command
#
proc ::AIEnv::GUIManagement::ParseTextEntry {} {
    RegisterEOLErrorMessage "textEntry"
    set ValidTypes [list "NUM" "INT" "TEXT" "READONLY" "INVISIBLE"]

	set GUITree [TreeStack peek_one]
    # Insert a node into the tree
    set NodeID [$GUITree insert [ParentStack peek_one] end]
	
    $GUITree set $NodeID -key "Type" "TextEntry"
    set Name [CheckEOL [GetToken] "name"]
    $GUITree set $NodeID -key "Name" $Name
    $GUITree set $NodeID -key "Label" [CheckEOL [GetToken] "label"]
    $GUITree set $NodeID -key "Variable" [CheckEOL [GetToken] "variable"]
    # Check for consistent types
    set Token [GetToken]
    set Token [string toupper $Token]
    if { [lsearch -exact $ValidTypes $Token] != -1 } {
        $GUITree set $NodeID -key "EntryType" $Token
    } else {
        ParseError "Unknown text entry type: \"$Token\" while parsing the textEntry command"
    }
    $GUITree set $NodeID -key "GreyFlag" 0
    
    # Add the name to the name map
    RecordName $Name $NodeID
}


#-----------------------------------------------------------------------------
#
# Procedure to parse the Text Entry2 command
#
proc ::AIEnv::GUIManagement::ParseTextEntry2 {} {
    RegisterEOLErrorMessage "textEntry2"
    set ValidTypes [list "NUM" "INT" "TEXT" "READONLY"]

	set GUITree [TreeStack peek_one]
    # Insert a node into the tree
    set NodeID [$GUITree insert [ParentStack peek_one] end]
	
    $GUITree set $NodeID -key "Type" "TextEntry2"
    set Name [CheckEOL [GetToken] "name"]
    $GUITree set $NodeID -key "Name" $Name
    $GUITree set $NodeID -key "Label" [CheckEOL [GetToken] "label"]
    $GUITree set $NodeID -key "Variable" [CheckEOL [GetToken] "variable"]
    # Check for consistent types
    set Token [GetToken]
    set Token [string toupper $Token]
    if { [lsearch -exact $ValidTypes $Token] != -1 } {
        $GUITree set $NodeID -key "EntryType" $Token
    } else {
        ParseError "Unknown text entry type: \"$Token\" while parsing the textEntry command"
    }
    
    $GUITree set $NodeID -key "Variable2" [CheckEOL [GetToken] "variable2"]
    # Check for consistent types
    set Token [GetToken]
    set Token [string toupper $Token]
    if { [lsearch -exact $ValidTypes $Token] != -1 } {
        $GUITree set $NodeID -key "EntryType2" $Token
    } else {
        ParseError "Unknown text entry type: \"$Token\" while parsing the textEntry2 command"
    }
    $GUITree set $NodeID -key "GreyFlag" 0
    
    # Add the name to the name map
    RecordName $Name $NodeID
}

#-----------------------------------------------------------------------------
#
proc ::AIEnv::GUIManagement::ParseTextEntryEdit {} {
    RegisterEOLErrorMessage "textEntryEdit"
    set ValidTypes [list "NUM" "INT" "TEXT" "READONLY"]

        set GUITree [TreeStack peek_one]
    # Insert a node into the tree
    set NodeID [$GUITree insert [ParentStack peek_one] end]

    $GUITree set $NodeID -key "Type" "TextEntryEdit"
    set Name [CheckEOL [GetToken] "name"]
    $GUITree set $NodeID -key "Name" $Name
    $GUITree set $NodeID -key "Label" [CheckEOL [GetToken] "label"]
    $GUITree set $NodeID -key "Variable" [CheckEOL [GetToken] "variable"]

    $GUITree set $NodeID -key "GreyFlag" 0

    # Add the name to the name map
    RecordName $Name $NodeID
}


#-----------------------------------------------------------------------------
#
# Procedure to parse the Text Table Entry command
#
proc ::AIEnv::GUIManagement::ParseTextTableEntry {} {
    RegisterEOLErrorMessage "textTableEntry"
    set ValidTypes [list "NUM" "INT" "TEXT"]

	set GUITree [TreeStack peek_one]
    # Insert a node into the tree
    set NodeID [$GUITree insert [ParentStack peek_one] end]
	
    $GUITree set $NodeID -key "Type" "TextTableEntry"
    set Name [CheckEOL [GetToken] "name"]
    $GUITree set $NodeID -key "Name" $Name
    $GUITree set $NodeID -key "Label" [CheckEOL [GetToken] "label"]
    $GUITree set $NodeID -key "Variable" [CheckEOL [GetToken] "variable"]
    # Check for consistent types
    set Token [GetToken]
    set Token [string toupper $Token]
    if { [lsearch -exact $ValidTypes $Token] != -1 } {
        $GUITree set $NodeID -key "EntryType" $Token
    } else {
        ParseError "Unknown text entry type: \"$Token\" while parsing the textTableEntry command"
    }
    $GUITree set $NodeID -key "TableType" [CheckEOL [GetToken] "tabletype"]
    $GUITree set $NodeID -key "GreyFlag" 0
    
    # Add the name to the name map
    RecordName $Name $NodeID
}

#-----------------------------------------------------------------------------
#
# Procedure to parse the Table Entry command
#
proc ::AIEnv::GUIManagement::ParseTableEntry {} {
    RegisterEOLErrorMessage "TableEntry"
    set ValidTypes [list "NUM" "INT" "TEXT"]

	set GUITree [TreeStack peek_one]
    # Insert a node into the tree
    set NodeID [$GUITree insert [ParentStack peek_one] end]
	
    $GUITree set $NodeID -key "Type" "TableEntry"
    set Name [CheckEOL [GetToken] "name"]
    $GUITree set $NodeID -key "Name" $Name
    $GUITree set $NodeID -key "Label" [CheckEOL [GetToken] "label"]
    $GUITree set $NodeID -key "Variable" [CheckEOL [GetToken] "variable"]
    # Check for consistent types
    set Token [GetToken]
    set Token [string toupper $Token]
    if { [lsearch -exact $ValidTypes $Token] != -1 } {
        $GUITree set $NodeID -key "EntryType" $Token
    } else {
        ParseError "Unknown text entry type: \"$Token\" while parsing the TableEntry command"
    }
    $GUITree set $NodeID -key "TableType" [CheckEOL [GetToken] "tabletype"]
    $GUITree set $NodeID -key "GreyFlag" 0
    
    # Add the name to the name map
    RecordName $Name $NodeID
}

#--------------------------------------------------

proc ::AIEnv::GUIManagement::ParseColorOptMenu {} {
   RegisterEOLErrorMessage "colorOptMenu"

   set GUITree [TreeStack peek_one]
   set NodeID [$GUITree insert [ParentStack peek_one] end]

   $GUITree set $NodeID -key "Type" "ColorOptMenu"
   set Name [CheckEOL [GetToken] "name"]
   $GUITree set $NodeID -key "Name" $Name
   $GUITree set $NodeID -key "Label" [CheckEOL [GetToken] "label"]
   $GUITree set $NodeID -key "VarName" [CheckEOL [GetToken] "varName"]
   $GUITree set $NodeID -key "Proc" [CheckEOL [GetToken] "proc"]

   $GUITree set $NodeID -key "GreyFlag" 0

   RecordName $Name $NodeID
}


#--------------------------------------------------

proc ::AIEnv::GUIManagement::ParseTextOptMenu {} {
   RegisterEOLErrorMessage "textOptMenu"

   set GUITree [TreeStack peek_one]
   set NodeID [$GUITree insert [ParentStack peek_one] end]

   $GUITree set $NodeID -key "Type" "TextOptMenu"
   set Name [CheckEOL [GetToken] "name"]
   $GUITree set $NodeID -key "Name" $Name
   $GUITree set $NodeID -key "Label" [CheckEOL [GetToken] "label"]
   $GUITree set $NodeID -key "VarName" [CheckEOL [GetToken] "varName"]
   $GUITree set $NodeID -key "MList" [CheckEOL [GetToken] "mList"]
   $GUITree set $NodeID -key "NRows" [CheckEOL [GetToken] "nRows"]
   $GUITree set $NodeID -key "Proc" [CheckEOL [GetToken] "proc"]

   set Token [GetToken]
   if { $Token != "_EOLToken" } {
      $GUITree set $NodeID -key "LabelSide" $Token
   } else {
      $GUITree set $NodeID -key "LabelSide" LEFT
   }

   $GUITree set $NodeID -key "GreyFlag" 0

   RecordName $Name $NodeID
}

#--------------------------------------------------

proc ::AIEnv::GUIManagement::ParseBmpOptMenu {} {
   RegisterEOLErrorMessage "bmpOptMenu"

   set GUITree [TreeStack peek_one]
   set NodeID [$GUITree insert [ParentStack peek_one] end]

   $GUITree set $NodeID -key "Type" "BmpOptMenu"
   set Name [CheckEOL [GetToken] "name"]
   $GUITree set $NodeID -key "Name" $Name
   $GUITree set $NodeID -key "Label" [CheckEOL [GetToken] "label"]
   $GUITree set $NodeID -key "VarName" [CheckEOL [GetToken] "varName"]
   $GUITree set $NodeID -key "BmpPath" [CheckEOL [GetToken] "bmpPath"]
   $GUITree set $NodeID -key "BmpFiles" [CheckEOL [GetToken] "bmpFiles"]
   $GUITree set $NodeID -key "NRows" [CheckEOL [GetToken] "nRows"]
   $GUITree set $NodeID -key "Proc" [CheckEOL [GetToken] "proc"]
 # $GUITree set $NodeID -key "WVarName" [CheckEOL [GetToken] "wVarName"]
  
   $GUITree set $NodeID -key "GreyFlag" 0

   RecordName $Name $NodeID
}


#--------------------------------------------------

proc ::AIEnv::GUIManagement::ParseEntryButton  {} {
   RegisterEOLErrorMessage "entryButton"

   set GUITree [TreeStack peek_one]
   set NodeID [$GUITree insert [ParentStack peek_one] end]

   $GUITree set $NodeID -key "Type" "EntryButton"
   set Name [CheckEOL [GetToken] "name"]
   $GUITree set $NodeID -key "Name" $Name
   $GUITree set $NodeID -key "Label" [CheckEOL [GetToken] "label"]
   $GUITree set $NodeID -key "ImageName" [CheckEOL [GetToken] "image name"]
   $GUITree set $NodeID -key "VarName" [CheckEOL [GetToken] "varName"]
   $GUITree set $NodeID -key "Proc" [CheckEOL [GetToken] "proc"]
   $GUITree set $NodeID -key "ToolTip" [CheckEOL [GetToken] "tooltip"]

   $GUITree set $NodeID -key "GreyFlag" 0

   RecordName $Name $NodeID
}

#--------------------------------------------------

proc ::AIEnv::GUIManagement::ParseEntityButton {} {
   RegisterEOLErrorMessage "entityButton"

   set GUITree [TreeStack peek_one]
   set NodeID [$GUITree insert [ParentStack peek_one] end]

   $GUITree set $NodeID -key "Type" "EntityButton"
   set Name [CheckEOL [GetToken] "name"]
   $GUITree set $NodeID -key "Name" $Name
   $GUITree set $NodeID -key "Label" [CheckEOL [GetToken] "label"]
   $GUITree set $NodeID -key "ImageName" [CheckEOL [GetToken] "image name"]
   $GUITree set $NodeID -key "ListName" [CheckEOL [GetToken] "listName"]
   $GUITree set $NodeID -key "Proc" [CheckEOL [GetToken] "proc"]
   $GUITree set $NodeID -key "ToolTip" [CheckEOL [GetToken] "tooltip"]

   set Token [GetToken]
   if { $Token != "_EOLToken" } {
      $GUITree set $NodeID -key "EntryType" [string toupper $Token]
   } else {
      $GUITree set $NodeID -key "EntryType" "NULL"
   }

   $GUITree set $NodeID -key "GreyFlag" 0

   RecordName $Name $NodeID
}

#-----------------------------------------------------------------------------
#
# Procedure to parse the Entity Chooser command
#

proc ::AIEnv::GUIManagement::ParseCoordsEntry {} {
    RegisterEOLErrorMessage "coordsEntry"

    set GUITree [TreeStack peek_one]
    set NodeID [$GUITree insert [ParentStack peek_one] end]


    $GUITree set $NodeID -key "Type" "CoordsEntry"
    set Name [CheckEOL [GetToken] "name"]
    $GUITree set $NodeID -key "Name" $Name
    $GUITree set $NodeID -key "Label" [CheckEOL [GetToken] "label"]
    $GUITree set $NodeID -key "ListName" [CheckEOL [GetToken] "listName"]
    $GUITree set $NodeID -key "GreyFlag" 0

    RecordName $Name $NodeID
}

proc ::AIEnv::GUIManagement::ParseEntityAddRemover {} {
    ParseEntityChooser 1
}

#-----------------------------------------------------------------------------
#
# Procedure to parse the Entity Chooser command
#
proc ::AIEnv::GUIManagement::ParseEntityChooser {{add_rem 0}} {
    set widget_type "entityChooser"
    if $add_rem {
        set widget_type "entityAddRemover"
    }
    RegisterEOLErrorMessage $widget_type
    set GUITree [TreeStack peek_one]
    set NodeID [$GUITree insert [ParentStack peek_one] end]
    set ValidTypes [list "GEOMETRY" "POINTORXYZ" "VID_PARAM" "POINT" "CURVE" "CURVE_OR_COMPCURVE" "SURF" "BODY" "DENSITY" "NODE" "ELEM" "ENDPOINT" "EDGE" "POINT_0D_EL" "CURVE_1D_EL" "SURF_2D_EL" "BODY_3D_EL" "ENTITY" "ANY_ENTITY" "SUBSET" "PART" "PART_0D" "PART_1D" "PART_0D_1D" "PART_0D_1D_2D" "PART_2D" "PART_3D" "PART_PRISM" "PART_VOLUME" "SUBPART" "HEX_COMPCURVE" "HEX_VERTEX" "HEX_EDGE" "HEX_FACE" "HEX_BLOCK" "STR_DOMAIN" "STR_SUBFACE" "STR_EDGE" "STR_VERTEX" "STR_NODE" "1D_EL" "2D_EL" "3D_EL"]
	
    set widget_type "EntityChooser"
    if $add_rem {
        set widget_type "EntityAddRemover"
    }
    $GUITree set $NodeID -key "Type" $widget_type
    set Name [CheckEOL [GetToken] "name"]
    $GUITree set $NodeID -key "Name" $Name
    $GUITree set $NodeID -key "Label" [CheckEOL [GetToken] "label"]
    $GUITree set $NodeID -key "ListName" [CheckEOL [GetToken] "listName"]
    $GUITree set $NodeID -key "GreyFlag" 0
    
    # TO DO... Add selection type logic
    set Token [CheckEOL [GetToken] "type"]
    # Keep a copy of what the user actually typed in for error reporting
    set UserSuppliedToken $Token
    # Make it uppercase
    set Token [string toupper $Token]
    if { [lsearch -exact $ValidTypes $Token] != -1 } {
    	$GUITree set $NodeID -key "ChoiceType" $Token
	} else {
		ParseError "Unknown selection type: \"$UserSuppliedToken\" while parsing the entityChooser command. Item name: \"$Name\""
	}

    set Token [CheckEOL [GetToken] "multiplePick"]

    # Three possiblities exist here.
    # 1. the user can specify a number with a preceeding < sign. Iterpreted as the greatest number of
    #	 items the user can pick.
    # 2. The user can specify a single number. Interpreted as the exact number of items to pick
    # 3. The user can specify the keyword ANY.  Interpreted as there being no limit to the number of items
    #    to pick

    # Check option 1
    if { [regexp < $Token] } {
		regexp {[0-9]*[0-9]} $Token Num
		$GUITree set $NodeI1D -key "ExactNumber" 0
		$GUITree set $NodeID -key "NumberOfEntities" $Num
	} elseif {[regexp {[0-9]*[0-9]} $Token] } {
		$GUITree set $NodeID -key "ExactNumber" 1
		$GUITree set $NodeID -key "NumberOfEntities" $Token
	} elseif { [regexp -nocase ANY $Token] } {
		$GUITree set $NodeID -key "ExactNumber" 0
		$GUITree set $NodeID -key "NumberOfEntities" "ANY"
	} else {
		Error "Unknown entity selection specification: \"$Token\" while parsing entityChooser."
	}

    	$GUITree set $NodeID -key "SelectOptions" ""
        $GUITree set $NodeID -key "SelectMessage" ""
        $GUITree set $NodeID -key "EntryType" ""
        set Token [GetToken]
        if { $Token != "_EOLToken" } {
	    $GUITree set $NodeID -key "SelectOptions" $Token
	    set Token [GetToken]
	    if { $Token != "_EOLToken" } {
		$GUITree set $NodeID -key "SelectMessage" $Token
		set Token [GetToken]
		if { $Token == "READONLY" } {
		    $GUITree set $NodeID -key "EntryType" $Token
		}
	    }
	}

        # Removed autoapply as this is not really an option - Ganesan
        $GUITree set $NodeID -key "AutoApply" 0

        # Add the name to the name map
        RecordName $Name $NodeID
}

#-----------------------------------------------------------------------------
#
# Procedure to parse the Frame command
#
proc ::AIEnv::GUIManagement::ParseFrame {} {
    # The first token might be an end
   	set Token [CheckMatchingEnd "Frame"]
    if { $Token == "END" } {
   		return
	}
   	set GUITree [TreeStack peek_one]

    RegisterEOLErrorMessage "frame"
    set NodeID [$GUITree insert [ParentStack peek_one] end]
    $GUITree set $NodeID -key "Type" "Frame"
    set Name [CheckEOL $Token "name"]
    $GUITree set $NodeID -key "Name" $Name
    $GUITree set $NodeID -key "Label" [CheckEOL [GetToken] "label"]
    $GUITree set $NodeID -key "GreyFlag" 0

    # This is a container so push it on the stack
    ParentStack push $NodeID

    # Add the name to the name map
    RecordName $Name $NodeID
}

#-----------------------------------------------------------------------------
#
# Procedure to parse the RadioFrame command
#
proc ::AIEnv::GUIManagement::ParseRadioFrame {} {
    # The first token might be an end
    set Token [CheckMatchingEnd "RadioFrame"]
    if { $Token == "END" } { return }
    set GUITree [TreeStack peek_one]

    RegisterEOLErrorMessage "radioFrame"
    set NodeID [$GUITree insert [ParentStack peek_one] end]
    $GUITree set $NodeID -key "Type" "RadioFrame"
    set Name [CheckEOL $Token "name"]
    $GUITree set $NodeID -key "Name" $Name
    $GUITree set $NodeID -key "Label" [CheckEOL [GetToken] "label"]
    $GUITree set $NodeID -key "Variable" [CheckEOL [GetToken] "variable"]
    $GUITree set $NodeID -key "Value" [CheckEOL [GetToken] "value"]
    $GUITree set $NodeID -key "Proc" [CheckEOL [GetToken] "proc"]
    $GUITree set $NodeID -key "GreyFlag" 0

    # This is a container so push it on the stack
    ParentStack push $NodeID

    # Add the name to the name map
    RecordName $Name $NodeID
}


#-----------------------------------------------------------------------------
#
# Procedure to parse the CheckFrame command
#
proc ::AIEnv::GUIManagement::ParseCheckFrame {} {
    # The first token might be an end
    set Token [CheckMatchingEnd "CheckFrame"]
    if { $Token == "END" } { return }
    set GUITree [TreeStack peek_one]

    RegisterEOLErrorMessage "checkFrame"
    set NodeID [$GUITree insert [ParentStack peek_one] end]
    $GUITree set $NodeID -key "Type" "CheckFrame"
    set Name [CheckEOL $Token "name"]
    $GUITree set $NodeID -key "Name" $Name
    $GUITree set $NodeID -key "Label" [CheckEOL [GetToken] "label"]
    $GUITree set $NodeID -key "Variable" [CheckEOL [GetToken] "variable"]
    $GUITree set $NodeID -key "Proc" [CheckEOL [GetToken] "proc"]
    $GUITree set $NodeID -key "GreyFlag" 0
   
    # This is a container so push it on the stack
    ParentStack push $NodeID

    # Add the name to the name map
    RecordName $Name $NodeID
}

#-----------------------------------------------------------------------------
#
# Procedure to parse the Radio Group command
#
proc ::AIEnv::GUIManagement::ParseRadioGroup {} {
    # The first token might be an end   
   	set Token [CheckMatchingEnd "RadioGroup"]
    if { $Token == "END" } {
   		return
	}
    RegisterEOLErrorMessage "radioGroup"
    set GUITree [TreeStack peek_one]
    set NodeID [$GUITree insert [ParentStack peek_one] end]
    $GUITree set $NodeID -key "Type" "RadioGroup"
    set Name [CheckEOL $Token "name"]
    $GUITree set $NodeID -key "Name" $Name
    $GUITree set $NodeID -key "Label" [CheckEOL [GetToken] "label"]
    $GUITree set $NodeID -key "NumCol" [CheckNumber [CheckEOL [GetToken] "numCol"] "numCol"]
    $GUITree set $NodeID -key "Variable" [CheckEOL [GetToken] "variable"]
    $GUITree set $NodeID -key "Default" [CheckNumber [CheckEOL [GetToken] "default"] "numCol"]
    $GUITree set $NodeID -key "Proc" [CheckEOL [GetToken] "proc"]
    
    set Token [GetToken]
    if { $Token != "_EOLToken" } {
       $GUITree set $NodeID -key "DrawBox" $Token
    } else {
       $GUITree set $NodeID -key "DrawBox" 1
    }
	
    $GUITree set $NodeID -key "GreyFlag" 0

    # This is a container so push it on the stack
    ParentStack push $NodeID

    # Add the name to the name map
    RecordName $Name $NodeID
}

#-----------------------------------------------------------------------------
#
# Procedure to parse the Radio Item command
#
proc ::AIEnv::GUIManagement::ParseRadioItem {} {

    RegisterEOLErrorMessage "radioItem"
	set GUITree [TreeStack peek_one]
	
    set NodeID [$GUITree insert [ParentStack peek_one] end]
    $GUITree set $NodeID -key "Type" "RadioItem"
    set Name [CheckEOL [GetToken] "name"]
    $GUITree set $NodeID -key "Name" $Name
    $GUITree set $NodeID -key "Label" [CheckEOL [GetToken] "label"]
    $GUITree set $NodeID -key "Value" [CheckEOL [GetToken] "value"]
    $GUITree set $NodeID -key "GreyFlag" 0
    
    # Add the name to the name map
    RecordName $Name $NodeID
}

#-----------------------------------------------------------------------------
#
# Procedure to parse the Drop Down Entry command
#
proc ::AIEnv::GUIManagement::ParseDropDownEntry {} {

    RegisterEOLErrorMessage "dropDownEntry"
	
	set GUITree [TreeStack peek_one]
    set NodeID [$GUITree insert [ParentStack peek_one] end]
    $GUITree set $NodeID -key "Type" "DropDownEntry"
    set Name [CheckEOL [GetToken] "name"]
    $GUITree set $NodeID -key "Name" $Name
    $GUITree set $NodeID -key "Label" [CheckEOL [GetToken] "label"]
    $GUITree set $NodeID -key "List" [CheckEOL [GetToken] "list"]
    $GUITree set $NodeID -key "Variable" [CheckEOL [GetToken] "variable"]
    $GUITree set $NodeID -key "MultiFlag" [CheckEOL [GetToken] "multiFlag"]

    set Token [GetToken]
    if { $Token != "_EOLToken" } {
       $GUITree set $NodeID -key "ModifyMode" $Token
    } else {
       $GUITree set $NodeID -key "ModifyMode" "ANY"
    }

    $GUITree set $NodeID -key "GreyFlag" 0

    # Add the name to the name map
    RecordName $Name $NodeID
}



#-----------------------------------------------------------------------------
#
# Procedure to parse the Integer Entry command
#
proc ::AIEnv::GUIManagement::ParseIntegerEntry {} {

    RegisterEOLErrorMessage "integerEntry"
    set GUITree [TreeStack peek_one]
    
    set NodeID [$GUITree insert [ParentStack peek_one] end]
    $GUITree set $NodeID -key "Type" "IntegerEntry"
    set Name [CheckEOL [GetToken] "name"]
    $GUITree set $NodeID -key "Name" $Name
    $GUITree set $NodeID -key "Label" [CheckEOL [GetToken] "label"]
    $GUITree set $NodeID -key "Variable" [CheckEOL [GetToken] "variable"]
    $GUITree set $NodeID -key "Min" [CheckEOL [GetToken] "minimum"]
    $GUITree set $NodeID -key "Max" [CheckEOL [GetToken] "maximum"]
    $GUITree set $NodeID -key "Increment" [CheckEOL [GetToken] "increment"]
    $GUITree set $NodeID -key "GreyFlag" 0
	set Token [GetToken]
	if { $Token != "_EOLToken" } {
		$GUITree set $NodeID -key "Default" $Token
	} else {
		$GUITree set $NodeID -key "Default" ""
	}
    
    # Add the name to the name map
    RecordName $Name $NodeID

}

#-----------------------------------------------------------------------------
#
# Procedure to parse the Integer Entry Icon command
#
proc ::AIEnv::GUIManagement::ParseIntegerEntryIcon {} {

    RegisterEOLErrorMessage "integerEntryIcon"
    set GUITree [TreeStack peek_one]
    
    set NodeID [$GUITree insert [ParentStack peek_one] end]
    $GUITree set $NodeID -key "Type" "IntegerEntryIcon"
    set Name [CheckEOL [GetToken] "name"]
    $GUITree set $NodeID -key "Name" $Name
    $GUITree set $NodeID -key "Label" [CheckEOL [GetToken] "label"]
    $GUITree set $NodeID -key "Variable" [CheckEOL [GetToken] "variable"]
    $GUITree set $NodeID -key "Min" [CheckEOL [GetToken] "minimum"]
    $GUITree set $NodeID -key "Max" [CheckEOL [GetToken] "maximum"]
    $GUITree set $NodeID -key "Increment" [CheckEOL [GetToken] "increment"]
    $GUITree set $NodeID -key "Proc" [CheckEOL [GetToken] "proc"]
    $GUITree set $NodeID -key "GreyFlag" 0
	set Token [GetToken]
	if { $Token != "_EOLToken" } {
		$GUITree set $NodeID -key "Default" $Token
	} else {
		$GUITree set $NodeID -key "Default" ""
	}
    
    # Add the name to the name map
    RecordName $Name $NodeID

}


#-----------------------------------------------------------------------------
#
# Procedure to parse the Text button command
#
proc ::AIEnv::GUIManagement::ParseTextButton {} {

    RegisterEOLErrorMessage "textButton"
	set GUITree [TreeStack peek_one]
	
    set NodeID [$GUITree insert [ParentStack peek_one] end]
    $GUITree set $NodeID -key "Type" "TextButton"
    set Name [CheckEOL [GetToken] "name"]
    $GUITree set $NodeID -key "Name" $Name
    $GUITree set $NodeID -key "Label" [CheckEOL [GetToken] "label"]
    $GUITree set $NodeID -key "Proc" [CheckEOL [GetToken] "proc"]
    $GUITree set $NodeID -key "GreyFlag" 0
    
    # Add the name to the name map
    RecordName $Name $NodeID

}

#-----------------------------------------------------------------------------
#
# Procedure to parse the Table command
#
proc ::AIEnv::GUIManagement::ParseTable {} {

    RegisterEOLErrorMessage "table"
	
	set GUITree [TreeStack peek_one]
	
    set NodeID [$GUITree insert [ParentStack peek_one] end]
    $GUITree set $NodeID -key "Type" "Table"
    set Name [CheckEOL [GetToken] "name"]
    $GUITree set $NodeID -key "Name" $Name
    $GUITree set $NodeID -key "Label" [CheckEOL [GetToken] "label"]
    $GUITree set $NodeID -key "TableID" [CheckEOL [GetToken] "tableID"]
    $GUITree set $NodeID -key "NumCol" [CheckEOL [GetToken] "ncol"]
    $GUITree set $NodeID -key "GreyFlag" 0
    
    # Add the name to the name map
    RecordName $Name $NodeID

}

#-----------------------------------------------------------------------------
#
# Procedure to parse the Check Item command
#
proc ::AIEnv::GUIManagement::ParseCheckItem {} {

    RegisterEOLErrorMessage "checkItem"
	set GUITree [TreeStack peek_one]
	
    set NodeID [$GUITree insert [ParentStack peek_one] end]

    $GUITree set $NodeID -key "Type" "CheckItem"
    set Name [CheckEOL [GetToken] "name"]
    $GUITree set $NodeID -key "Name" $Name
    $GUITree set $NodeID -key "Label" [CheckEOL [GetToken] "label"]
    $GUITree set $NodeID -key "Variable" [CheckEOL [GetToken] "variable"]
    $GUITree set $NodeID -key "Proc" [CheckEOL [GetToken] "proc"]
    $GUITree set $NodeID -key "GreyFlag" 0
    
    # Add the name to the name map
    RecordName $Name $NodeID

}

#-----------------------------------------------------------------------------
#
# Procedure to parse the Check Item + 2 buttons command
#
proc ::AIEnv::GUIManagement::ParseCheckItem2 {} {

    RegisterEOLErrorMessage "checkItem2"
	set GUITree [TreeStack peek_one]
	
    set NodeID [$GUITree insert [ParentStack peek_one] end]

    $GUITree set $NodeID -key "Type" "CheckItem2"
    set Name [CheckEOL [GetToken] "name"]
    $GUITree set $NodeID -key "Name" $Name
    $GUITree set $NodeID -key "Label" [CheckEOL [GetToken] "label"]
    $GUITree set $NodeID -key "Label1" [CheckEOL [GetToken] "label1"]
    $GUITree set $NodeID -key "Label2" [CheckEOL [GetToken] "label2"]
    $GUITree set $NodeID -key "Variable" [CheckEOL [GetToken] "variable"]
    $GUITree set $NodeID -key "Proc" [CheckEOL [GetToken] "proc"]
    $GUITree set $NodeID -key "Proc1" [CheckEOL [GetToken] "proc1"]
    $GUITree set $NodeID -key "Proc2" [CheckEOL [GetToken] "proc2"]
    $GUITree set $NodeID -key "GreyFlag" 0
    
    # Add the name to the name map
    RecordName $Name $NodeID

}
#-----------------------------------------------------------------------------
#
# Procedure to parse the dialog space command
#
proc ::AIEnv::GUIManagement::ParseDialogSpace {} {

    RegisterEOLErrorMessage "dialogSpace"
    set GUITree [TreeStack peek_one]

    set NodeID [$GUITree insert [ParentStack peek_one] end]

    $GUITree set $NodeID -key "Type" "DialogSpace"
    set Token [GetToken]
    if {$Token != "_EOLToken"} {
        $GUITree set $NodeID -key "Quantity" [CheckNumber $Token "quantity"]
    } else {
        $GUITree set $NodeID -key "Quantity" 1
    }   
}

#-----------------------------------------------------------------------------
#
# Procedure to parse the Data Entry command
#
proc ::AIEnv::GUIManagement::ParseDataEntryDialog {} {
           
    set Token [CheckMatchingEnd "DataEntryDialog"]
	if { $Token == "END" } {
		TreeStack pop
		return
	}

    RegisterEOLErrorMessage "dataEntryDialog"
    set Name [CheckEOL $Token "name"]
    # Create the DEZ
	    BeginTypeDefinition DEZ
	    	BeginObjectDefinition $Name
	    		$Name init
	    		set DEZTree [$Name gettree]
	    		# Create the AllowActiveReload variable
	    		set Var [CreateVariable AllowActiveReload]
	    		$Name set $Var 0
	       	EndObjectDefinition
    EndTypeDefinition
    
    $DEZTree set "root" -key "Type" "Root"
    set NodeID [$DEZTree insert "root" end]
    $DEZTree set $NodeID -key "Type" "DataEntryDialog"
    
    $DEZTree set $NodeID -key "Name" $Name
    $DEZTree set $NodeID -key "Title" [CheckEOL [GetToken] "title"]
    $DEZTree set $NodeID -key "WindowPath" "NULL"
    $DEZTree set $NodeID -key "GreyFlag" 0
    $DEZTree set $NodeID -key "saveFlag" 1

    set Token [GetToken]
    if {$Token != "_EOLToken"} {
	if [string match $Token nosave] {
	    $DEZTree set $NodeID -key "saveFlag" 0
	}
    }
    
    TreeStack push $DEZTree
    # This is a container
    ParentStack push $NodeID

    RecordName $Name $NodeID
}

#-----------------------------------------------------------------------------
#
# Procedure to parse the Dialog Window command
#
proc ::AIEnv::GUIManagement::ParseDialogWindow {} {

    # Grab the first token, it will either be an END or a name
    set Token [CheckMatchingEnd "DialogWindow"]
	if { $Token == "END" } {
		return
	}
	
	set GUITree [TreeStack peek_one]
	
    RegisterEOLErrorMessage "dialogWindow"
    set ValidSizes [list "SMALL" "MEDIUM" "LARGE"]
    
    set NodeID [$GUITree insert $ItemNameMap("DialogWindowRoot") end]
    
    $GUITree set $NodeID -key "Type" "DialogWindow"
    set Name [CheckEOL $Token "name"]
    $GUITree set $NodeID -key "Name" $Name
    $GUITree set $NodeID -key "Title" [CheckEOL [GetToken] "title"]
    # Check for valid sizes
    set Token [GetToken]
    CheckEOL $Token "size"
    if {[lsearch -exact $ValidSizes $Token] != -1} {
        set Size $Token
    } else {
        ::ParseError "Error: Unknown size: \"$Token\" while parsing the dialogWindow command"
    }
    $GUITree set $NodeID -key "Size" $Token
    $GUITree set $NodeID -key "AcceptProc" [CheckEOL [GetToken] "acceptProc"]
    $GUITree set $NodeID -key "ApplyProc" [CheckEOL [GetToken] "applyProc"]
    $GUITree set $NodeID -key "CancelProc" [CheckEOL [GetToken] "cancelProc"]
    $GUITree set $NodeID -key "HelpName" [CheckEOL [GetToken] "helpName"]
    $GUITree set $NodeID -key "GreyFlag" 0
    # This is a container
    ParentStack push $NodeID

    RecordName $Name $NodeID
}


#-----------------------------------------------------------------------------
#
# Procedure to parse the Pick Screen Coordinate command
#
proc ::AIEnv::GUIManagement::ParsePickScreenCoord {} {
    
    RegisterEOLErrorMessage "pickScreenCoord"
    
    set ValidTypes [list "AUTO" "POINT" "ENTITY" "PLANE"]

    set GUITree [TreeStack peek_one]
    set NodeID [$GUITree insert [ParentStack peek_one] end]

    $GUITree set $NodeID -key "Type" "PickScreenCoord"
    set Name [CheckEOL [GetToken] "name"]
    $GUITree set $NodeID -key "Name" $Name
    $GUITree set $NodeID -key "Label" [CheckEOL [GetToken] "label"]
    $GUITree set $NodeID -key "ListName" [CheckEOL [GetToken] "listName"]
    $GUITree set $NodeID -key "MaxNumber" ANY
    set Token [GetToken]
    set AutoApply 0
    if {$Token != "_EOLToken"} {
	if {[lsearch $ValidTypes $Token] != ""} {
	    $GUITree set $NodeID -key "ChoiceType" $Token
	} else {
	    ParseError "Unknown selection type: \"$Token\"."
	}
	set Token [GetToken]
	if { $Token != "_EOLToken" } {
	    set Token [string tolower $Token]
	    switch -exact $Token {
		0 {
		    set AutoApply 0
		}
		1 {
		    set AutoApply 1
		}
		false {
		    set AutoApply 0
		} 
		true {
		    set AutoApply 1
		}
		default {
		    ParseError "Unknown auto apply flag: \"$Token\"."
		}
	    }
	    set Token [GetToken]
	    if { $Token != "_EOLToken" } {
		$GUITree set $NodeID -key "MaxNumber" $Token
	    }
	} else {
	    set AutoApply 0
	}
    } else {
	$GUITree set $NodeID -key "ChoiceType" "AUTO"
    }
    
    $GUITree set $NodeID -key "GreyFlag" 0
        
    $GUITree set $NodeID -key "AutoApply" $AutoApply
    RecordName $Name $NodeID
}

#-----------------------------------------------------------------------------
#
# Procedure to parse the Data table command
#

proc ::AIEnv::GUIManagement::ParseDataTable {} {
	
	RegisterEOLErrorMessage "dataTable"
	
	set GUITree [TreeStack peek_one]
	set NodeID [$GUITree insert [ParentStack peek_one] end]
	$GUITree set $NodeID -key "Type" "DataTable"
	set Name [CheckEOL [GetToken] "name"]
	$GUITree set $NodeID -key "Name" $Name
	$GUITree set $NodeID -key "Label" [CheckEOL [GetToken] "label"]
	$GUITree set $NodeID -key "TableVariable" [CheckEOL [GetToken] "tableVar"]
	$GUITree set $NodeID -key "NumCols" [CheckEOL [GetToken] "numCols"]
	$GUITree set $NodeID -key "ColHeadings" [CheckEOL [GetToken] "colHeadingList"]
	$GUITree set $NodeID -key "GreyFlag" 0
	
	RecordName $Name $NodeID
}

proc ::AIEnv::GUIManagement::ParseGreyGroup {} {
    set Token [CheckMatchingEnd "GreyGroup"]
	if { $Token == "END" } {
		return
	}
	
	set GUITree [TreeStack peek_one]
	set NodeID [$GUITree insert [ParentStack peek_one] end]
	$GUITree set $NodeID -key "Type" "GreyGroup"

	set Name [CheckEOL $Token "name"]
	$GUITree set $NodeID -key "Name" $Name
	$GUITree set $NodeID -key "Label" [CheckEOL [GetToken] "label"]
	set Token [GetToken]
	if { $Token != "_EOLToken" } {
		$GUITree set $NodeID -key "Variable" $Token
	} else {
		set VarName [$GUITree get $NodeID -key "Name"]Val
		$GUITree set $NodeID -key "Variable" $VarName
	}

    	set Proc NULL
        if { $Token != "_EOLToken" } {
	    set Token [GetToken]
	    if { $Token != "_EOLToken" } {
		set Proc $Token
	    }
	}
        $GUITree set $NodeID -key "Proc" $Proc

	$GUITree set $NodeID -key "GreyFlag" 0
	RecordName $Name $NodeID

	 # This is a container
    ParentStack push $NodeID
}


proc ::AIEnv::GUIManagement::ParseGreyGroupItem {} {
	
    set Token [CheckMatchingEnd "GreyGroupItem"]
	if { $Token == "END" } {
		return
	}
	
	set GUITree [TreeStack peek_one]
	RegisterEOLErrorMessage "greyGroupItem"
		
	set NodeID [$GUITree insert [ParentStack peek_one] end]
	$GUITree set $NodeID -key "Type" "GreyGroupItem"
	
	set Name [CheckEOL $Token "name"]
	$GUITree set $NodeID -key "Name" $Name
	$GUITree set $NodeID -key "Label" [CheckEOL [GetToken] "label"]
	$GUITree set $NodeID -key "GreyFlag" 0
	
	RecordName $Name $NodeID
	
	 # This is a container
    ParentStack push $NodeID
}

proc ::AIEnv::GUIManagement::ParseWizard {} {

    variable ItemNameMap
    set Token [CheckMatchingEnd "Wizard"]
	if { $Token == "END" } {
		return
	}		
    RegisterEOLErrorMessage "wizard"
    set GUITree [TreeStack peek_one]
    
    set NodeID [$GUITree insert $ItemNameMap("WizardRoot") end]
    $GUITree set $NodeID -key "Type" "Wizard"
    set Name [CheckEOL [CheckDefaultName $Token "wizard"] "wizard"] 
    $GUITree set $NodeID -key "Name" $Name
    $GUITree set $NodeID -key "Title" [CheckEOL [GetToken] "title"]
    set Token [GetToken]
    if { $Token != "_EOLToken" } {
    	$GUITree set $NodeID -key "Image" $Token
    } else {
		$GUITree set $NodeID -key "Image" ""
	}
    
    ParentStack push $NodeID
    
    RecordName $Name $NodeID
    
}

proc ::AIEnv::GUIManagement::ParseWizardPage {} {

    set Token [CheckMatchingEnd "WizardPage"]
	if { $Token == "END" } {
		return
	}
	
    set GUITree [TreeStack peek_one]
    
    RegisterEOLErrorMessage "wizardPage"
    
    set NodeID [$GUITree insert [ParentStack peek_one] end]
    $GUITree set $NodeID -key "Type" "WizardPage"
    set Name [CheckEOL [CheckDefaultName $Token "wizardPage"] "wizardPage"]  
	$GUITree set $NodeID -key "Name" $Name
    
    # Look for a title for this page
    set Token [GetToken]
    if { $Token != "_EOLToken" } {
		$GUITree set $NodeID -key "Title" $Token
	} else {
		$GUITree set $NodeID -key "Title" ""
	}
	
    ParentStack push $NodeID
    
    RecordName $Name $NodeID
}

proc ::AIEnv::GUIManagement::ParseWizardColumn {} {

    variable ItemNameMap
    
    set Token [CheckMatchingEnd "WizardColumn"]
	if { $Token == "END" } {
		return
	}
	
	set GUITree [TreeStack peek_one]
	RegisterEOLErrorMessage "wizardColumn"
	set NodeID [$GUITree insert $ItemNameMap("WizardColumnRoot") end]
	$GUITree set $NodeID -key "Type" "WizardColumn"
	set Name [CheckEOL $Token "name"]
	$GUITree set $NodeID -key "Name" $Name
	RecordName $Name $NodeID
	ParentStack push $NodeID
}

proc ::AIEnv::GUIManagement::ParseTextLabel {} {
    RegisterEOLErrorMessage "textLabel"
    set GUITree [TreeStack peek_one]
    set NodeID [$GUITree insert [ParentStack peek_one] end]
    
    $GUITree set $NodeID -key "Type" "TextLabel"
    set Name [CheckEOL [GetToken] "name"]
    $GUITree set $NodeID -key "Name" $Name
    $GUITree set $NodeID -key "Label" [CheckEOL [GetToken] "label"]
    set Token [GetToken]
    if { $Token != "_EOLToken" } {
	$GUITree set $NodeID -key "Image" $Token
    } else {
	$GUITree set $NodeID -key "Image" NULL
    }
    RecordName $Name $NodeID
}

proc ::AIEnv::GUIManagement::ParseAmpEntry {} {
   RegisterEOLErrorMessage "ampEntry"
   set GUITree [TreeStack peek_one]
   set NodeID [$GUITree insert [ParentStack peek_one] end]

   $GUITree set $NodeID -key "Type" "AmpEntry"
   set Name [CheckEOL [GetToken] "name"]
   $GUITree set $NodeID -key "Name" $Name
   $GUITree set $NodeID -key "Label" [CheckEOL [GetToken] "label"]
   $GUITree set $NodeID -key "Variable" [CheckEOL [GetToken] "variable"]
   $GUITree set $NodeID -key "BarVar" [CheckEOL [GetToken] "barvar"]
   $GUITree set $NodeID -key "Proc" [CheckEOL [GetToken] "proc"]

   $GUITree set $NodeID -key "GreyFlag" 0

  # Add the name to the name map
   RecordName $Name $NodeID
}

proc ::AIEnv::GUIManagement::ParseAnimator {} {
   RegisterEOLErrorMessage "animator"
   set GUITree [TreeStack peek_one]
   set NodeID [$GUITree insert [ParentStack peek_one] end]

   $GUITree set $NodeID -key "Type" "Animator"
   set Name [CheckEOL [GetToken] "name"]
   $GUITree set $NodeID -key "Name" $Name
   $GUITree set $NodeID -key "Variable" [CheckEOL [GetToken] "variable"]
   $GUITree set $NodeID -key "MinVal" [CheckEOL [GetToken] "minVal"]
   $GUITree set $NodeID -key "MaxVal" [CheckEOL [GetToken] "maxVal"]
   $GUITree set $NodeID -key "IncVal" [CheckEOL [GetToken] "incVal"]
   $GUITree set $NodeID -key "Proc" [CheckEOL [GetToken] "proc"]
   $GUITree set $NodeID -key "ValOn" [CheckEOL [GetToken] "valOn"]
   $GUITree set $NodeID -key "Length"  [CheckEOL [GetToken] "length"]

   $GUITree set $NodeID -key "GreyFlag" 0

  # Add the name to the name map
   RecordName $Name $NodeID
}

proc ::AIEnv::GUIManagement::ParseScaleBar {} {

   RegisterEOLErrorMessage "scaleBar"
   set GUITree [TreeStack peek_one]
   set NodeID [$GUITree insert [ParentStack peek_one] end]

   $GUITree set $NodeID -key "Type" "ScaleBar"
   set Name [CheckEOL [GetToken] "name"]
   $GUITree set $NodeID -key "Name" $Name
   $GUITree set $NodeID -key "Label" [CheckEOL [GetToken] "label"]
   $GUITree set $NodeID -key "Variable" [CheckEOL [GetToken] "variable"]
   $GUITree set $NodeID -key "MinVal" [CheckEOL [GetToken] "minVal"]
   $GUITree set $NodeID -key "MaxVal" [CheckEOL [GetToken] "maxVal"]
   $GUITree set $NodeID -key "IncVal" [CheckEOL [GetToken] "incVal"]
   $GUITree set $NodeID -key "Proc" [CheckEOL [GetToken] "proc"]
   $GUITree set $NodeID -key "ValOn" [CheckEOL [GetToken] "valOn"]
   $GUITree set $NodeID -key "Length"  [CheckEOL [GetToken] "length"]
   set irow [GetToken]
   set endToken $irow
   if ![string is integer -strict $irow] {set irow 0}
   $GUITree set $NodeID -key "OneRow" $irow
   $GUITree set $NodeID -key "AddLabel" NULL
   if { $endToken != "_EOLToken" } {
       set addLabel [GetToken]
       if { $addLabel != "_EOLToken" } {
           $GUITree set $NodeID -key "AddLabel" $addLabel
       }
   }
   $GUITree set $NodeID -key "GreyFlag" 0
  # Add the name to the name map
   RecordName $Name $NodeID
}


proc ::AIEnv::GUIManagement::ParseListBox {} {

	
	RegisterEOLErrorMessage "listBox"
	set GUITree [TreeStack peek_one]
	
	set NodeID [$GUITree insert [ParentStack peek_one] end]
	$GUITree set $NodeID -key "Type" "ListBox"
	set Name [CheckEOL [CheckDefaultName [GetToken] "listBox"] "name"]
	$GUITree set $NodeID -key "Name" $Name
	$GUITree set $NodeID -key "ListVariable" [CheckEOL [GetToken] "listVariable"]
	$GUITree set $NodeID -key "SelectionVariable" [CheckEOL [GetToken] "selectionVariable"]
        $GUITree set $NodeID -key "Proc" [CheckEOL [GetToken] "proc"]
        $GUITree set $NodeID -key "DkProc" [CheckEOL [GetToken] "dkproc"]
	$GUITree set $NodeID -key "Height" [CheckEOL [GetToken] "height"]
	$GUITree set $NodeID -key "GreyFlag" 0
	set Token [CheckEOL [GetToken] "multiSelectFlag"]
	
	if { $Token == 1 } {
		$GUITree set $NodeID -key "MultiSelectFlag" 1
	} elseif { $Token == 0 } {
		$GUITree set $NodeID -key "MultiSelectFlag" 0
	} else {
		ParseError "Unknown multiSelectFlag: \"$Token\" while parsing ListBox"
	}

        foreach {n k} {yScroll YScroll xScroll XScroll} {
           set Token [CheckEOL [GetToken] "$n"]
           if {($Token != 1) && ($Token != 0)} {set Token 1}
           $GUITree set $NodeID -key "$k" $Token
        }
    
    RecordName $Name $NodeID	
}


proc ::AIEnv::GUIManagement::ParseEntityMover {} {
	
	RegisterEOLErrorMessage "entityMover"
	
	set GUITree [TreeStack peek_one]
	
	set NodeID [$GUITree insert [ParentStack peek_one] end]
	$GUITree set $NodeID -key "Type" "EntityMover"
	set Name [CheckEOL [CheckDefaultName [GetToken] "entityMover"] "name"]
	$GUITree set $NodeID -key "Name" $Name
	
	# For now only nodes are supported.  Later we can add support for points, etc...
	set ValidTypes {"NODE"}
	
	set MoveType [CheckEOL [GetToken] "entityMover"]
	set UserSuppliedType $MoveType
	set MoveType [string toupper $MoveType]
	
	if { [lsearch $ValidTypes $MoveType] != -1 } {
		$GUITree set $NodeID -key "MoverType" $MoveType
	} else {
		set OptList [join $ValidTypes ", "]
		set OptList [linsert $OptList "end-1" "or"]
		ParseError "Invalid entityMover type: \"$UserSuppliedType\".  Must be $OptList"
	}
	$GUITree set $NodeID -key "GreyFlag" 0
	RecordName $Name $NodeID
}


proc ::AIEnv::GUIManagement::ParseMultiSelect {} {

    set Token [CheckMatchingEnd "MultiSelect"]
	if { $Token == "END" } {
		return
	}
    
	RegisterEOLErrorMessage "multiSelect"
	
	set GUITree [TreeStack peek_one]
	
	set NodeID [$GUITree insert [ParentStack peek_one] end]
	$GUITree set $NodeID -key "Type" "MultiSelect"
	set Name [CheckEOL [CheckDefaultName $Token "mutliSelect"] "name"]
	$GUITree set $NodeID -key "Name" $Name
	$GUITree set $NodeID -key "Title" [CheckEOL [GetToken] "title"]
	set validTypes [list "EXCLUSIVE" "NONEXCLUSIVE"]
	set Exclusivity [string toupper [CheckEOL [GetToken] "exclusivity descriptor"]]
	if { [lsearch $validTypes $Exclusivity] != -1 } {
		if { $Exclusivity == "EXCLUSIVE" } {
			$GUITree set $NodeID -key "Exclusive" 1
		} else {
			$GUITree set $NodeID -key "Exclusive" 0
		}
	} else {
		ParseError "Unknown exclusivity flag: \"$Exclusivity\" while parsing multiSelect."
	}
	
	$GUITree set $NodeID -key "RowLabelList" " "
	$GUITree set $NodeID -key "ColumnLabelList" " "
	ParentStack push $NodeID	
	$GUITree set $NodeID -key "GreyFlag" 0
	RecordName $Name $NodeID
}

proc ::AIEnv::GUIManagement::ParseMultiSelectRows {} {

    set Token [CheckMatchingEnd "MultiSelectRows"]
	if { $Token == "END" } {
		return
	}
	RegisterEOLErrorMessage "multiSelectRows"
	
	set GUITree [TreeStack peek_one]
	
	set NodeID [$GUITree insert [ParentStack peek_one] end]
	$GUITree set $NodeID -key "Type" "MultiSelectRows"
	ParentStack push $NodeID
	
}

proc ::AIEnv::GUIManagement::ParseMultiSelectColumns {} {

    set Token [CheckMatchingEnd "MultiSelectColumns"]
	if { $Token == "END" } {
		return
	}
	
	RegisterEOLErrorMessage "multiSelectColumns"
	set GUITree [TreeStack peek_one]
	
	set NodeID [$GUITree insert [ParentStack peek_one] end]
	$GUITree set $NodeID -key "Type" "MultiSelectColumns"
	ParentStack push $NodeID	
}


proc ::AIEnv::GUIManagement::ParseExclusiveGroup {} {
    set Token [CheckMatchingEnd "ExclusiveGroup"]
	if { $Token == "END" } {
		return
	}
	
	set GUITree [TreeStack peek_one]
	
	RegisterEOLErrorMessage "exclusiveGroup"

	set NodeID [$GUITree insert [ParentStack peek_one] end]
	$GUITree set $NodeID -key "Type" "ExclusiveGroup"
	set Name [CheckEOL $Token "name"]
	$GUITree set $NodeID -key "Name" $Name
	$GUITree set $NodeID -key "FrameLabel" [CheckEOL [GetToken] "frameLabel"]
	# Allow DropDownLabel without any label and suppress " " labels
    set is_space_only [CheckEOL [GetToken] "DropDownLabel"]
    if {$is_space_only == " "} { set is_space_only "Method" }
	$GUITree set $NodeID -key "DropDownLabel" $is_space_only
	$GUITree set $NodeID -key "NumCols" [CheckEOL [GetToken] "numCols"]
	
	set Token [string toupper [CheckEOL [GetToken] "type"]]
	set ValidTypes [list "RADIO" "DROPDOWN" "ICON" "CHECK"]
	if { [lsearch $ValidTypes $Token] != -1 } {
		$GUITree set $NodeID -key "ExclusiveType" $Token
	} else {
		set OptList [join $ValidTypes ", "]
		set OptList [linsert $OptList "end-1" "or"]
	
		ParseError "Invalid exclusive group type: \"$Token\".  Must be $OptList"
	}
	
     
	set VarName ${Name}Val
	set ProcName NULL
	set Token [GetToken]
	if { $Token != "_EOLToken" } {
	   set VarName $Token
	   set Token [GetToken]
	   if { $Token != "_EOLToken" } { set ProcName $Token }
	}
	$GUITree set $NodeID -key "Variable" $VarName
	$GUITree set $NodeID -key "Proc" $ProcName
	$GUITree set $NodeID -key "GreyFlag" 0
	RecordName $Name $NodeID
	ParentStack push $NodeID		
}

proc ::AIEnv::GUIManagement::ParseExclusiveGroupItem {} {
    set Token [CheckMatchingEnd "ExclusiveGroupItem"]
	if { $Token == "END" } {
		return
	}
	
	set GUITree [TreeStack peek_one]
	RegisterEOLErrorMessage "exclusiveGroupItem"
	
	set NodeID [$GUITree insert [ParentStack peek_one] end]
	$GUITree set $NodeID -key "Type" "ExclusiveGroupItem"
	
	set Name [CheckEOL $Token "name"]
	$GUITree set $NodeID -key "Name" $Name
	$GUITree set $NodeID -key "Label" [CheckEOL [GetToken] "label"]
	set Token [GetToken]
	if { $Token != "_EOLToken" } {
		$GUITree set $NodeID -key "Icon" $Token
	} else {
		$GUITree set $NodeID -key "Icon" ""
	}
	$GUITree set $NodeID -key "GreyFlag" 0
		
	RecordName $Name $NodeID
	
	ParentStack push $NodeID	
}

            
proc ::AIEnv::GUIManagement::ParseColumnMultiSelect {} {
	set Token [CheckMatchingEnd "ColumnMultiSelect"]
	if { $Token == "END" } {
		return
	}
	
	set GUITree [TreeStack peek_one]
	RegisterEOLErrorMessage "columnMultiSelect"
	
	set NodeID [$GUITree insert [ParentStack peek_one] end]
	$GUITree set $NodeID -key "Type" "ColumnMultiSelect"
	set Name [CheckEOL $Token "name"]
	$GUITree set $NodeID -key "Name" $Name
	$GUITree set $NodeID -key "Title" [CheckEOL [GetToken] "label"]
	$GUITree set $NodeID -key "GreyFlag" 0
	
	RecordName $Name $NodeID
	ParentStack push $NodeID
	
}

proc ::AIEnv::GUIManagement::ParseCMSColumn {} {
	set Token [CheckMatchingEnd "CMSColumn"]
	if { $Token == "END" } {
		return 		
	}
	
	set GUITree [TreeStack peek_one]
	RegisterEOLErrorMessage "cmsColumn"
	# Check to make sure that the current parent is a 
	# ColumnMultiSelect
	if { [$GUITree get [ParentStack peek_one] -key "Type"] != "ColumnMultiSelect" } {
		ParseError "The cmsColumn must be used within the context of a columnMultiSelect widget."
	}
	set NodeID [$GUITree insert [ParentStack peek_one] end]
	$GUITree set $NodeID -key "Type" "CMSColumn"
	$GUITree set $NodeID -key "Label" [CheckEOL $Token "label"]
	
	ParentStack push $NodeID
}

proc ::AIEnv::GUIManagement::ParseCMSItem {} {
	
	set Token [GetToken]
		
	set GUITree [TreeStack peek_one]
	RegisterEOLErrorMessage "cmsItem"
	# Check to make sure that the current parent is a 
	# CMSColumn
	if { [$GUITree get [ParentStack peek_one] -key "Type"] != "CMSColumn" } {
		ParseError "The cmsItem must be used within the context of a cmsColumn block."
	}
	set NodeID [$GUITree insert [ParentStack peek_one] end]
	$GUITree set $NodeID -key "Type" "CMSItem"
	$GUITree set $NodeID -key "Label" [CheckEOL $Token "label"]
}

proc ::AIEnv::GUIManagement::ParseRFChooser {} {
	set GUITree [TreeStack peek_one]
	RegisterEOLErrorMessage "rFChooser"

	set NodeID [$GUITree insert [ParentStack peek_one] end]
	$GUITree set $NodeID -key "Type" "RFChooser"
	set Name [CheckEOL [GetToken] "name"]
	$GUITree set $NodeID -key "Name" $Name
	$GUITree set $NodeID -key "Label" [CheckEOL [GetToken] "label"]
	$GUITree set $NodeID -key "Variable" [CheckEOL [GetToken] "path variable"]
	$GUITree set $NodeID -key "Newflag" [CheckEOL [GetToken] "new flag"]
	$GUITree set $NodeID -key "Multiflag" [CheckEOL [GetToken] "multiflag"]
        $GUITree set $NodeID -key "GetDirflag" [CheckEOL [GetToken] "get_dir"]
	$GUITree set $NodeID -key "OkFunc" [CheckEOL [GetToken] "ok func"]
	$GUITree set $NodeID -key "DirVar" [CheckEOL [GetToken] "dir variable"]
	$GUITree set $NodeID -key "Proc"   [CheckEOL [GetToken] "proc"]

        set Token [GetToken]
        if { $Token != "_EOLToken" } {
           $GUITree set $NodeID -key "FileTypes" $Token
        } else {
           $GUITree set $NodeID -key "FileTypes" NULL
        }

	$GUITree set $NodeID -key "GreyFlag" 0
	RecordName $Name $NodeID
}

proc ::AIEnv::GUIManagement::ParseFileChooser {} {
	set GUITree [TreeStack peek_one]
	RegisterEOLErrorMessage "fileChooser"
	
	set NodeID [$GUITree insert [ParentStack peek_one] end]
	$GUITree set $NodeID -key "Type" "FileChooser"
	set Name [CheckEOL [GetToken] "name"]
	$GUITree set $NodeID -key "Name" $Name
	$GUITree set $NodeID -key "Label" [CheckEOL [GetToken] "label"]
	$GUITree set $NodeID -key "Variable" [CheckEOL [GetToken] "path variable"]
	$GUITree set $NodeID -key "Newflag" [CheckEOL [GetToken] "new flag"]
	set Token [GetToken]
	set FileTypes ""
	if { $Token != "_EOLToken" } {
		set ExtensionData [split $Token ","]
		set n [llength $ExtensionData]
		set i 0
		while {$i < $n} {
			set Description [string trim [lindex $ExtensionData $i]]
			incr i
			if { $i >= $n } {
				ParseError "The fileChooser named: \"$Name\" contains an unmatched file description/extension pair.  The description, \"$Description\" is not matched."
			}
			set Extension [string trim [lindex $ExtensionData $i]]
			if { $Extension == "" } {
				ParseError "The fileChooser named: \"$Name\" contains an unmatched file description/extension pair.  The description, \"$Description\" is not matched."
			}
			incr i
			lappend FileTypes [list $Description $Extension]
		}
	} else {
		set FileTypes ""
	}
	$GUITree set $NodeID -key "FileTypes" $FileTypes
	$GUITree set $NodeID -key "GreyFlag" 0
	RecordName $Name $NodeID
}



