
#-------------------------------------------------------------------------------
# Module:     fileOpen.tk
# Activation: Dynamically loaded on user request
# Scope:      Dialog for file open operations
#-------------------------------------------------------------------------------


source [file join $GoblinTclPath fileImport.tk]

proc MakeImportForm {} {
    global WDImport FileName ThisFile goblinExtension ThisFormat \
        ZipEnabled ZipExtension ThisGraph MergeMode SupportedFormats ImportFilter

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

    if {![file exists [file dirname $ImportFilter]]} {
        set ImportFilter [file join [pwd] [file tail $ImportFilter]]
    }

    set WDImport .open
    destroy $WDImport
    toplevel $WDImport
    wm withdraw $WDImport
    wm title $WDImport "Object Import"

    frame $WDImport.f1
    label $WDImport.f1.label -text "File:" -anchor w
    pack $WDImport.f1.label -side top -fill x

    entry $WDImport.f1.entry -textvariable ThisFile -bg white -width 40
    pack $WDImport.f1.entry -side left -fill x

    MakePulldownMenuButton $WDImport.f1.mergeSelect "-width 21"
    pack $WDImport.f1.mergeSelect -side right

    set MergeMode 0

    if {![$ThisGraph is sparse] } {
        set listOfImportModes { \
            "Replace Current Object"        0 }
    } else {
        set listOfImportModes { \
            "Replace Current Object"        0 \
            "Merge, Align Horizontally"     1 \
            "Merge, Align Vertically"       2 \
            "Merge, Overlay Graphs"         3 }
    }

    AttachSelectionForm $WDImport.f1.mergeSelect 5 \
        [lindex $listOfImportModes [expr 2*$MergeMode]] \
        $listOfImportModes ApplyImportModeSelection

    bind $WDImport.f1.entry <Return> OpenNow

    frame $WDImport.f2
    label $WDImport.f2.label -text "Filter:" -anchor w
    entry $WDImport.f2.entry -textvariable ImportFilter -bg white -width 66
    pack $WDImport.f2.label $WDImport.f2.entry -side top -fill x

    bind $WDImport.f2.entry <Return> DisplayImportSelection

    frame $WDImport.f3
    label $WDImport.f3.label -text "Selection:" -anchor w
    scrollbar $WDImport.f3.scroll -command "$WDImport.f3.list yview"
    pack $WDImport.f3.label -side top -fill x
    pack $WDImport.f3.scroll -side left -fill y

    DisplayImportSelection

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

    scrollbar $WDImport.f4.scroll -command "$WDImport.f4.list yview"
    listbox $WDImport.f4.list -yscrollcommand "$WDImport.f4.scroll set" -width 30

    global Ext

    foreach {format extension dummy1 dummy2 importFilter dummy3} $SupportedFormats {
        if {!$importFilter} {continue}

        $WDImport.f4.list insert end $format

        if {$extension==$ThisFormat} {
            $WDImport.f4.list activate end
            $WDImport.f4.list selection set end end
            $WDImport.f4.list see active
        }
    }

    pack $WDImport.f4.list $WDImport.f4.scroll -side left -fill y

    bind $WDImport.f4.list <Double-Button-1> {
        global Ext ImportFilter SourceFormat
        set SourceFormat [selection get]
        set ThisFormat [GetFormatProperty $SourceFormat 2]

        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]
        DisplayImportSelection
    }

    frame $WDImport.buttons
    button $WDImport.buttons$WDImport -text "Open" -command OpenNow
    button $WDImport.buttons.cancel -text "Cancel" -command {destroy $WDImport}

    pack $WDImport.buttons$WDImport $WDImport.buttons.cancel \
        -side left -fill x -padx 3m

    pack $WDImport.buttons -padx 3m -pady 3m -side bottom
    pack $WDImport.f1 $WDImport.f2 -padx 5m -pady 3m -side top -anchor w -expand 1 -fill x
    pack $WDImport.f4 $WDImport.f3 -padx 5m -pady 3m -side left -anchor w

    bind $WDImport <FocusOut> {
        if {[winfo exists $WDImport]} {
            raise $WDImport .
        }
    }

    bind . <FocusIn> {
        if {[winfo exists $WDImport]} {
            raise $WDImport .
        }
    }

    after 500 {AdjustWithRootWindow $WDImport}
    after 500 {wm resizable $WDImport 0 0}
}

proc ApplyImportModeSelection {mode} {
    global MergeMode

    set MergeMode $mode
}

proc DisplayImportSelection {} {
    global WDImport ImportFilter 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 $WDImport.f3.list
    listbox $WDImport.f3.list -yscrollcommand "$WDImport.f3.scroll set" -width 25
    pack $WDImport.f3.list -side left -before $WDImport.f3.scroll

    $WDImport.f3.list insert end ".."
    foreach i [lsort [glob -nocomplain *]] {
        if {[file isdirectory $i]} {$WDImport.f3.list insert end $i}
    }
    foreach i [lsort [glob -nocomplain [file tail $ImportFilter]]] {
        if {![file isdirectory $i]} {$WDImport.f3.list insert end $i}
    }
    if {$ZipExtension!="" && [file tail $ImportFilter]!="*"} {
        foreach i [lsort [glob -nocomplain \
                [file tail $ImportFilter]$ZipExtension]] {
            if {![file isdirectory $i]} {$WDImport.f3.list insert end $i}
        }
    }

    bind $WDImport.f3.list <Double-Button-1> {
        if {[file isdirectory [selection get]]} {
            cd [selection get]
            set ImportFilter [file join [pwd] [file tail $ImportFilter]]
            set ThisFile ""
            DisplayImportSelection
        } else {
            set ThisFile [selection get]
        }
    }
}

proc OpenNow {} {
    global WDImport OrigFileName FileName ImportFilter ThisFile ThisFormat ThisGraph \
        goblinExtension TraceDir WorkingDir UnzipCommand \
        ZipExtension ZipEnabled MergeMode SystemTmpDir

    set importDir [file dirname $ImportFilter]

    if {[file exists [file join $importDir $ThisFile]] && \
        ![file isdirectory [file join $importDir $ThisFile]]} {

        destroy $WDImport

        set tmpGraph $ThisGraph
        set newFileName [file join $importDir [file rootname $ThisFile]]
        set ThisGraph [ChooseObjectName]
        set ZipEnabled \
            [expr ![string compare [file extension $ThisFile] $ZipExtension]]

        if {$ZipEnabled} {
            set tmpName [file join $SystemTmpDir "goblin.tmp"]
            file copy -force $newFileName$ZipExtension $tmpName$ZipExtension
            eval exec $UnzipCommand $tmpName$ZipExtension
            file rename -force $tmpName $newFileName
            set ThisFile [file rootname $ThisFile]
            set ZipEnabled 1
        }

        if {[catch {AsynchronousImport $ThisGraph \
                [file join $importDir $ThisFile] $ThisFormat} result]} {
            set ThisGraph $tmpGraph
            return
        }

        DeleteTempFiles

        if {$ZipEnabled} {
            file delete -force $newFileName
            set OrigFileName [file rootname $newFileName]
        } else {
            set OrigFileName $newFileName
        }

        set FileName [file join $TraceDir [file tail $OrigFileName]]

        ResumeSession

        set WorkingDir $importDir
        $ThisGraph write "$FileName.$goblinExtension"
        InitFile

        if {$MergeMode>0} {
            if {[$ThisGraph is sparse]} {
                if {$MergeMode==1} {
                    $ThisGraph merge -right [$tmpGraph handle]
                } else {
                    if {$MergeMode==2} {
                        $ThisGraph merge -below [$tmpGraph handle]
                    } else {
                        $ThisGraph merge -overlay [$tmpGraph handle]
                    }
                }

                PropagateModifications
                PostStatusInfo "merged to problem: $OrigFileName"
            } else {
                PostStatusInfo "merge failed, target must be a sparse graph object"
            }
        } else {
            DisplayObject
            PostStatusInfo "imported problem: $OrigFileName"
        }

        $tmpGraph delete
    }
}

MakeImportForm