
#-------------------------------------------------------------------------------
# Module:     open.tk
# Activation: Dynamically loaded on user request
# Scope:      Dialog for file open operations
#
# Updated: 2003/01/01 File decompression
#-------------------------------------------------------------------------------



global FileName WorkingDir ThisFile goblinExtension GoblinTclPath ThisFormat \
    ZipEnabled ZipExtension

source [file join $GoblinTclPath import.tk]

destroy .open
toplevel .open
wm title .open "Object Import"
wm resizable .open 0 0

set ThisFormat $goblinExtension
set ThisFile [file tail $FileName].$ThisFormat
if {$ZipEnabled} {set ThisFile "$ThisFile$ZipExtension"}

global ImportFilter
set ImportFilter [file join $WorkingDir *]
     
frame .open.f1
    label .open.f1.label -text "File:" -anchor w
    entry .open.f1.entry -textvariable ThisFile -bg white -width 66
    pack .open.f1.label .open.f1.entry -side top -fill x

proc OpenNow {} {
    global OrigFileName FileName ImportFilter ThisFile ThisFormat ThisGraph \
        goblinExtension TraceDir WorkingDir ZipCommand UnzipCommand \
        ZipExtension ZipEnabled
    set importDir [file dirname $ImportFilter]
    if {[file exists [file join $importDir $ThisFile]] && \
        ![file isdirectory [file join $importDir $ThisFile]]} {
        $ThisGraph delete
        DeleteTempFiles
        
        set OrigFileName [file join $importDir [file rootname $ThisFile]]
        if {[file extension $ThisFile]!=$ZipExtension} {
            set FileName [file join $TraceDir [file tail $OrigFileName]]
            set ZipEnabled 0
        } else {
            eval exec $UnzipCommand $OrigFileName$ZipExtension
            set OrigFileName [file rootname $OrigFileName]
            set FileName [file join $TraceDir [file tail $OrigFileName]]
            set ThisFile [file rootname $ThisFile]
            set ZipEnabled 1
        }
        
        ResumeSession
        if {[catch {goblinImport $ThisGraph \
                [file join $importDir $ThisFile] $ThisFormat} result]} {
            destroy .failureInfo
            toplevel .failureInfo
            wm title .failureInfo "GOBLIN Info"
            wm resizable .failureInfo 0 0
            message .failureInfo.msg -width 10c -anchor w \
                -text "Import has failed: \n$result"
            button .failureInfo.ok -text "OK" -command {destroy .failureInfo}
            pack .failureInfo.msg -padx 5m -pady 5m
            pack .failureInfo.ok -padx 5m -pady 2m
            grab set .failureInfo
            goblin sparse graph $ThisGraph
        } else {
            destroy .open
            set WorkingDir $importDir
            $ThisGraph write "$FileName.$goblinExtension"
            $ThisGraph delete
            goblin read $ThisGraph "$FileName.$goblinExtension"
            InitFile
        }
        
        if {$ZipEnabled} {
            eval exec $ZipCommand [file join $importDir $ThisFile]
        }
    }
}
    
    bind .open.f1.entry <Return> OpenNow
     
frame .open.f2
    label .open.f2.label -text "Filter:" -anchor w
    entry .open.f2.entry -textvariable ImportFilter -bg white -width 66
    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 ImportFilter ThisFile ZipExtension
        if {![file isdirectory [file dirname $ImportFilter]]} {return}
        if {[file isdirectory $ImportFilter]} {
            cd $ImportFilter
            set ImportFilter [file join $ImportFilter *]
        } else {
            cd [file dirname $ImportFilter]
        }
        
        destroy .open.f3.list
        listbox .open.f3.list -yscrollcommand ".open.f3.scroll set" -width 25
        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 [file tail $ImportFilter]]] {
            if {![file isdirectory $i]} {.open.f3.list insert end $i}
        }
        if {$ZipExtension!=""} {
            foreach i [lsort [glob -nocomplain \
                    [file tail $ImportFilter]$ZipExtension]] {
                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 ImportFilter [file join [pwd] [file tail $ImportFilter]]
                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 30
    
	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  \
        "CPLEX MPS Linear Program"          mps  \
        } {
        .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 ImportFilter
        set ThisFormat $Ext([selection get])
        if {[file extension $ThisFile]!=$ZipExtension} {
            set ThisFile [file rootname [file tail $ThisFile]].$ThisFormat
        } else {
            set ThisFile [file rootname [file rootname \
                [file tail $ThisFile]]].$ThisFormat$ZipExtension
        }
        set ImportFilter [file join [file dirname $ImportFilter] *.$ThisFormat]
        DisplaySelection
    }
     
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 5m -pady 3m -side top -anchor w
pack .open.f4 .open.f3 -padx 5m -pady 3m -side left -anchor w
