global FileName Filter ThisFile goblinExtension GoblinTclPath ThisFormat

source [file join $GoblinTclPath import.tk]

destroy .open
toplevel .open
wm title .open "GOBLET Graph Import"
wm resizable .open 0 0

set ThisFormat $goblinExtension
set ThisFile [file tail $FileName].$ThisFormat
     
frame .open.f1
    label .open.f1.label -text "File:" -anchor w
    entry .open.f1.entry -textvariable ThisFile -bg white -width 70
    pack .open.f1.label .open.f1.entry -side top -fill x

    proc OpenNow {} {
        global OrigFileName FileName Filter ThisFile ThisFormat ThisGraph
	global goblinExtension TraceDir
        if {[file exists [file join $Filter $ThisFile]] && \
                ![file isdirectory [file join $Filter $ThisFile]]} {
            $ThisGraph delete
            DeleteTempFiles
            set OrigFileName [file join $Filter [file rootname $ThisFile]]
            set FileName [file join $TraceDir [file tail $OrigFileName]]
            ResumeSession
            if {[catch {goblinImport $ThisGraph [file join $Filter $ThisFile] $ThisFormat} result]} {
                destroy .failureInfo
                toplevel .failureInfo
                wm title .failureInfo "GOBLIN Info"
                wm resizable .failureInfo 0 0
                message .failureInfo.msg -width 12c -anchor w \
                    -text "Import has failed: $result"
                button .failureInfo.ok -text "OK" -command {destroy .failureInfo}
                pack .failureInfo.msg -padx 10m -pady 5m
                pack .failureInfo.ok -padx 3m -pady 3m
                grab set .failureInfo
                goblin sparse graph $ThisGraph
            } else {destroy .open}
            $ThisGraph write $FileName.$goblinExtension
            $ThisGraph delete
            goblin read $ThisGraph $FileName.$goblinExtension
            InitFile
        }
    }
    
    bind .open.f1.entry <Return> OpenNow
     
frame .open.f2
    label .open.f2.label -text "Filter:" -anchor w
    entry .open.f2.entry -textvariable Filter -bg white -width 70
    pack .open.f2.label .open.f2.entry -side top -fill x
    
    bind .open.f2.entry <Return> DisplaySelection
     
frame .open.f3
    label .open.f3.label -text "Selection:" -anchor w
    scrollbar .open.f3.scroll -command ".open.f3.list yview"
    pack .open.f3.label -side top -fill x
    pack .open.f3.scroll -side left -fill y
    
    proc DisplaySelection {} {
        global Filter ThisFile
        if {![file exists $Filter]} {return}
        cd $Filter
        
        destroy .open.f3.list
        listbox .open.f3.list -yscrollcommand ".open.f3.scroll set" -width 30
        pack .open.f3.list -side left -before .open.f3.scroll
    
        .open.f3.list insert end ".."
        foreach i [lsort [glob -nocomplain *]] {
            if {[file isdirectory $i]} {.open.f3.list insert end $i}
        }
        foreach i [lsort [glob -nocomplain *]] {
            if {![file isdirectory $i]} {.open.f3.list insert end $i}
        }

        bind .open.f3.list <Double-Button-1> {
            if {[file isdirectory [selection get]]} {
                cd [selection get]
                set Filter [pwd]
                set ThisFile ""
                DisplaySelection
            } else {
                set ThisFile [selection get]
            }
        }
    }

    DisplaySelection
    
frame .open.f4
    label .open.f4.label -text "File Format:" -anchor w
    pack .open.f4.label -side top -fill x

    scrollbar .open.f4.scroll -command ".open.f4.list yview"
    listbox .open.f4.list -yscrollcommand ".open.f4.scroll set" -width 25
    
	global Ext
    
    foreach {format extension} { \
        "GOBLIN Graph Object"               gob  \
        "DIMACS Undirected Graph"           edge \
        "DIMACS Max Flow Instance"          max  \
        "DIMACS Min Cost Flow Problem"      min  \
        "DIMACS Assignment Problem"         asn  \
        "DIMACS Geometric Matching"         geom \
        "TSPLIB Symmetric TSP"              tsp  \
        "TSPLIB Asymmetric TSP"             atsp \
        "TSPLIB Hamiltonian Cycle Problem"  hcp  \
        } {
        .open.f4.list insert end $format
        set Ext($format) $extension
    }

    pack .open.f4.list .open.f4.scroll -side left -fill y
    
    bind .open.f4.list <Double-Button-1> {
		global Ext
        set ThisFormat $Ext([selection get])
        set ThisFile [file rootname [file tail $ThisFile]].$ThisFormat
    }
     
frame .open.buttons
    button .open.buttons.open -text "Open" -command OpenNow
    button .open.buttons.cancel -text "Cancel" -command {destroy .open}
    
    pack .open.buttons.open .open.buttons.cancel \
        -side left -fill x -padx 3m
     
pack .open.buttons -padx 3m -pady 3m -side bottom
pack .open.f1 .open.f2 -padx 10m -pady 5m -side top -anchor w
pack .open.f4 .open.f3 -padx 10m -pady 5m -side left -anchor w
