Imports Microsoft.VisualBasic
Imports System
Imports System.Linq
Imports System.Text
Imports DevExpress.ExpressApp
Imports DevExpress.Data.Filtering
Imports System.Collections.Generic
Imports DevExpress.Persistent.Base
Imports DevExpress.ExpressApp.Utils
Imports DevExpress.ExpressApp.Layout
Imports DevExpress.ExpressApp.Actions
Imports DevExpress.ExpressApp.Editors
Imports DevExpress.ExpressApp.Templates
Imports DevExpress.Persistent.Validation
Imports DevExpress.ExpressApp.SystemModule
Imports DevExpress.ExpressApp.Model.NodeGenerators
Imports DevExpress.XtraSpreadsheet
Imports DevExpress.Persistent.BaseImpl

' For more typical usage scenarios, be sure to check out http://documentation.devexpress.com/#Xaf/clsDevExpressExpressAppViewControllertopic.
Partial Public Class SpreadImportVC
    Inherits ViewController
    Private _SpreadsheetControl As SpreadsheetControl

    Public Sub New()
        InitializeComponent()
        RegisterActions(components)
        ' Target required Views (via the TargetXXX properties) and create their Actions.
        
    End Sub
    Protected Overrides Sub OnActivated()
        MyBase.OnActivated()
        ' Perform various tasks depending on the target View.
        If View.Id = "SpreadImport_DetailView" Then
            Dim _DetailView As DetailView = TryCast(View, DetailView)
            Dim _SpreadXML_ViewItem As ViewItem = _DetailView.FindItem("SpreadXML")
            If _SpreadXML_ViewItem IsNot Nothing Then
                AddHandler _SpreadXML_ViewItem.ControlCreated, AddressOf _SpreadXML_ViewItem_ControlCreated
            End If
            AddHandler Frame.GetController(Of ModificationsController).SaveAction.Executing, AddressOf SpreadImport_DetailView_SaveAction_Executing
            AddHandler Frame.GetController(Of ModificationsController).SaveAndCloseAction.Executing, AddressOf SpreadImport_DetailView_SaveAction_Executing

            Frame.GetController(Of NewObjectViewController).Active.SetItemValue("", False)
            Frame.GetController(Of ModificationsController).SaveAndNewAction.Active.SetItemValue("", False)

        End If
    End Sub
    Protected Overrides Sub OnViewControlsCreated()
        MyBase.OnViewControlsCreated()
        ' Access and customize the target View control.
    End Sub
    Protected Overrides Sub OnDeactivated()
        ' Unsubscribe from previously subscribed events and release other references and resources.
        MyBase.OnDeactivated()
    End Sub

    Private Sub _SpreadXML_ViewItem_ControlCreated(sender As Object, e As EventArgs)
        Dim _SpreadImport As SpreadImport = TryCast(View.SelectedObjects(0), SpreadImport)
        Dim _ItemControl As Object = TryCast(sender, ViewItem).Control
        Dim _SpreadSimpleUserControl As SpreadSimpleUserControl = TryCast(_ItemControl, SpreadSimpleUserControl)

        If _SpreadSimpleUserControl IsNot Nothing Then
            _SpreadsheetControl = TryCast(_ItemControl, SpreadSimpleUserControl).SpreadsheetControl1

            If _SpreadImport IsNot Nothing Then
                If _SpreadImport.SpreadFile IsNot Nothing Then
                    Dim _FilePath As String = System.IO.Path.Combine(System.IO.Path.GetTempPath(), _SpreadImport.SpreadFile.FileName)
                    Dim _Stream As System.IO.Stream = New System.IO.FileStream(_FilePath, System.IO.FileMode.Create)
                    _SpreadImport.SpreadFile.SaveToStream(_Stream)
                    _Stream.Close()


                    Dim _StreamFile = New System.IO.FileStream(_FilePath, System.IO.FileMode.Open, System.IO.FileAccess.Read)
                    Dim _StreamReader As New System.IO.StreamReader(_StreamFile)
                    Dim workbook As DevExpress.Spreadsheet.IWorkbook = _SpreadsheetControl.Document
                    _StreamFile.Seek(0, System.IO.SeekOrigin.Begin)
                    workbook.LoadDocument(_StreamFile, DevExpress.Spreadsheet.DocumentFormat.OpenXml)
                    _StreamFile.Close()
                End If
            End If
        End If
    End Sub

    Private Sub SpreadImport_DetailView_SaveAction_Executing(sender As Object, e As ComponentModel.CancelEventArgs)
        Dim _SpreadImport As SpreadImport = TryCast(View.SelectedObjects(0), SpreadImport)
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _DetailView As DetailView = TryCast(View, DetailView)
        Dim _SpreadXML_ViewItem As ViewItem = _DetailView.FindItem("SpreadXML")
        Dim _ItemControl As Object
        Dim _SpreadSimpleUserControl As SpreadSimpleUserControl

        If _SpreadXML_ViewItem IsNot Nothing Then
            _ItemControl = _SpreadXML_ViewItem.Control
            _SpreadSimpleUserControl = TryCast(_ItemControl, SpreadSimpleUserControl)
            _SpreadsheetControl = TryCast(_ItemControl, SpreadSimpleUserControl).SpreadsheetControl1
        End If

        If _SpreadsheetControl IsNot Nothing Then
            Dim _FilePath As String = System.IO.Path.Combine(System.IO.Path.GetTempPath(), "IMPORT.XML")
            Dim _Stream As System.IO.Stream = New System.IO.FileStream(_FilePath, System.IO.FileMode.Create)
            _SpreadsheetControl.Document.SaveDocument(_Stream, DevExpress.Spreadsheet.DocumentFormat.OpenXml)
            _Stream.Close()

            Dim _StreamFile = New System.IO.FileStream(_FilePath, System.IO.FileMode.Open, System.IO.FileAccess.Read)
            Dim _StreamReader As New System.IO.StreamReader(_StreamFile)
            _StreamFile.Seek(0, System.IO.SeekOrigin.Begin)

            Dim _FileData As FileData
            _FileData = _ocur.CreateObject(Of FileData)()

            _FileData.LoadFromStream("IMPORT.XML", _StreamFile)

            _SpreadImport.SpreadFile = _FileData
        End If


    End Sub



    
    Private Sub aSpreadImport_LoadDocument_Execute(sender As Object, e As SimpleActionExecuteEventArgs) Handles aSpreadImport_LoadDocument.Execute
        Dim openFileDialog1 As New Windows.Forms.OpenFileDialog()

        openFileDialog1.InitialDirectory = My.Computer.FileSystem.SpecialDirectories.MyDocuments
        openFileDialog1.Filter = "XML files (*.xml)|*.xml|All files (*.*)|*.*"
        openFileDialog1.FilterIndex = 1
        openFileDialog1.RestoreDirectory = True

        If openFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
            _SpreadsheetControl.LoadDocument(openFileDialog1.FileName, DevExpress.Spreadsheet.DocumentFormat.OpenXml)
        End If
    End Sub

    Private Sub aSpreadImport_SaveDocument_Execute(sender As Object, e As SimpleActionExecuteEventArgs) Handles aSpreadImport_SaveDocument.Execute
        Dim openFileDialog1 As New Windows.Forms.SaveFileDialog()

        openFileDialog1.InitialDirectory = My.Computer.FileSystem.SpecialDirectories.MyDocuments
        openFileDialog1.Filter = "XML files (*.xml)|*.xml|All files (*.*)|*.*"
        openFileDialog1.FilterIndex = 1
        openFileDialog1.RestoreDirectory = True

        If openFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
            _SpreadsheetControl.SaveDocument(openFileDialog1.FileName, DevExpress.Spreadsheet.DocumentFormat.OpenXml)
        End If
    End Sub
End Class
