Imports System
Imports System.ComponentModel
Imports System.Collections.Generic
Imports System.Diagnostics
Imports System.Text

Imports DevExpress.ExpressApp
Imports DevExpress.ExpressApp.Actions
Imports DevExpress.Persistent.Base
Imports DevExpress.ExpressApp.Win.Editors
Imports DevExpress.ExpressApp.ConditionalAppearance
Imports DevExpress.ExpressApp.Editors
Imports DevExpress.ExpressApp.Model
Imports DevExpress.Data.Filtering
Imports DevExpress.ExpressApp.Utils
Imports DevExpress.ExpressApp.TreeListEditors.Win
Imports DevExpress.ExpressApp.Win.Core
Imports DevExpress.Xpo
Imports DevExpress.XtraEditors.Repository
Imports DevExpress.ExpressApp.PivotGrid.Win
Imports DevExpress.ExpressApp.PivotGrid
Imports DevExpress.ExpressApp.SystemModule
Imports DevExpress.ExpressApp.Win.SystemModule

Public Class CentralListViewVC
    Inherits DevExpress.ExpressApp.ViewController(Of ListView)
    Dim _WaitFormClass As New WaitFormClass
    Public Sub New()
        MyBase.New()

        'This call is required by the Component Designer.
        InitializeComponent()
        RegisterActions(components)

    End Sub
    Protected Overrides Sub OnActivated()
        MyBase.OnActivated()
      

        Frame.GetController(Of CentralListViewVC).aSaveCentralListViewSettings.Active.SetItemValue("", False)
        Frame.GetController(Of CentralListViewVC).aResetCentralListViewSettings.Active.SetItemValue("", False)

        Dim _IModelListViewExtender As IModelListViewExtender = TryCast(View.Model, IModelListViewExtender)
        If _IModelListViewExtender.IsCentralListView Then

            Frame.GetController(Of CentralListViewVC).aSaveCentralListViewSettings.Active.SetItemValue("", True)
            Frame.GetController(Of CentralListViewVC).aResetCentralListViewSettings.Active.SetItemValue("", True)

            AddHandler View.ControlsCreating, AddressOf View_ControlCreating

        End If
    End Sub
    Protected Overrides Sub OnDeactivated()
        MyBase.OnDeactivated()
        RemoveHandler View.ControlsCreating, AddressOf View_ControlCreating
    End Sub

    Private Sub View_ControlCreating(sender As Object, e As EventArgs)
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _ModelListView As ModelListView
        Dim _IModelListView As IModelListView = TryCast(View.Model, IModelListView)
        Dim _PropertyCollectionSource As PropertyCollectionSource

        _PropertyCollectionSource = TryCast(TryCast(View, ListView).CollectionSource, PropertyCollectionSource)

        If _PropertyCollectionSource IsNot Nothing Then
            If TryCast(TryCast(View, ListView).CollectionSource, PropertyCollectionSource).MasterObject IsNot Nothing Then
                Dim _MasterObject As Guid = TryCast(TryCast(View, ListView).CollectionSource, PropertyCollectionSource).MasterObject.oid
                _ModelListView = _onew.FindObject(Of ModelListView)(CriteriaOperator.Parse("Id=? and MasterObject=?", View.Id, _MasterObject))
            Else
                _ModelListView = _onew.FindObject(Of ModelListView)(CriteriaOperator.Parse("Id=?", View.Id))
            End If
        Else
            _ModelListView = _onew.FindObject(Of ModelListView)(CriteriaOperator.Parse("Id=?", View.Id))
        End If

        If _ModelListView IsNot Nothing Then
            If _IModelListView.EditorType = GetType(PivotGridListEditor) Then
                TryCast(View.Model, IModelPivotListView).PivotSettings.Settings = _ModelListView.PivotSettings
                View.SaveModel()
                'View.LoadModel()
            End If
            If _IModelListView.EditorType = GetType(GridListEditor) Then
                TryCast(View.Model, IModelListView).IsFooterVisible = _ModelListView.IsFooterVisible
                TryCast(View.Model, IModelListView).IsGroupPanelVisible = _ModelListView.IsGroupPanelVisible
                TryCast(View.Model, IModelListViewShowAutoFilterRow).ShowAutoFilterRow = _ModelListView.ShowAutoFilterRow
                TryCast(View.Model, IModelListViewShowFindPanel).ShowFindPanel = _ModelListView.ShowFindPanel

                For Each _Column In TryCast(View.Model, IModelListView).Columns
                    _Column.Index = -1
                    _Column.GroupIndex = -1
                Next

                For Each _ModelListViewColumn As ModelListViewColumn In _ModelListView.ModelListViewColumn
                    Dim _Found As Boolean = False

                    For Each _Column In TryCast(View.Model, IModelListView).Columns
                        If _Column.Id = _ModelListViewColumn.Id Then
                            _Column.Index = _ModelListViewColumn.Index
                            _Column.GroupIndex = _ModelListViewColumn.GroupIndex
                            _Column.Width = _ModelListViewColumn.Width
                            _Column.GroupInterval = _ModelListViewColumn.GroupInterval
                            _Found = True
                            Exit For
                        End If
                    Next
                    If _Found = False Then
                        Dim _Column As IModelColumn = TryCast(View.Model, IModelListView).Columns.AddNode(Of IModelColumn)(_ModelListViewColumn.Id)
                        TryCast(_Column, IModelColumnExtender).HideValueIfZero = _ModelListViewColumn.HideValueIfZero
                        TryCast(_Column, IModelColumnExtender).InLineCriteria = _ModelListViewColumn.InLineCriteria
                        TryCast(_Column, IModelColumnExtender).InLineEdit = _ModelListViewColumn.InLineEdit
                        _Column.AllowEdit = _ModelListViewColumn.AllowEdit
                        'TryCast(_Column, IModelColumnAllowSort).AllowSort = _ModelListViewColumn.AllowSort
                        _Column.GroupInterval = _ModelListViewColumn.GroupInterval
                        _Column.ImmediatePostData = _ModelListViewColumn.ImmediatePostData
                        _Column.IsPassword = _ModelListViewColumn.IsPassword
                        _Column.PropertyName = _ModelListViewColumn.PropertyName
                        _Column.GroupIndex = _ModelListViewColumn.GroupIndex
                        _Column.MaxLength = _ModelListViewColumn.MaxLength
                        _Column.SortIndex = _ModelListViewColumn.SortIndex
                        _Column.Width = _ModelListViewColumn.Width
                        _Column.Caption = _ModelListViewColumn.Caption
                        _Column.Index = _ModelListViewColumn.Index
                    End If
                Next
                View.SaveModel()
                'View.LoadModel()
            End If
        End If
    End Sub

    Private Sub aSaveCentralListViewSettings_Execute(sender As Object, e As SimpleActionExecuteEventArgs) Handles aSaveCentralListViewSettings.Execute
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _ModelListView As ModelListView
        Dim _ModelListViewColumn As ModelListViewColumn
        Dim _IModelListView As IModelListView = TryCast(View.Model, IModelListView)
        Try
            GLOBAL_HAS_Audit = False
            View.SaveModel()
            If TryCast(TryCast(View, ListView).CollectionSource, PropertyCollectionSource).MasterObject IsNot Nothing Then
                Dim _MasterObject As Guid = TryCast(TryCast(View, ListView).CollectionSource, PropertyCollectionSource).MasterObject.oid
                _ModelListView = _onew.FindObject(Of ModelListView)(CriteriaOperator.Parse("Id=? and MasterObject=?", View.Id, _MasterObject))

                If _ModelListView Is Nothing Then
                    _ModelListView = _onew.CreateObject(Of ModelListView)()
                    _ModelListView.MasterObject = _MasterObject
                    _ModelListView.Id = View.Id
                End If
            Else
                _ModelListView = _onew.FindObject(Of ModelListView)(CriteriaOperator.Parse("Id=?", View.Id))
                If _ModelListView Is Nothing Then
                    _ModelListView = _onew.CreateObject(Of ModelListView)()
                    _ModelListView.Id = View.Id
                End If
            End If
            If _ModelListView IsNot Nothing Then
                If _IModelListView.EditorType = GetType(PivotGridListEditor) Then
                    View.SaveModel()
                    _ModelListView.PivotSettings = TryCast(View.Model, IModelPivotListView).PivotSettings.Settings
                End If
                If _IModelListView.EditorType = GetType(GridListEditor) Then
                    View.SaveModel()
                    _ModelListView.IsFooterVisible = TryCast(View.Model, IModelListView).IsFooterVisible
                    _ModelListView.IsGroupPanelVisible = TryCast(View.Model, IModelListView).IsGroupPanelVisible
                    _ModelListView.ShowAutoFilterRow = TryCast(View.Model, IModelListViewShowAutoFilterRow).ShowAutoFilterRow
                    _ModelListView.ShowFindPanel = TryCast(View.Model, IModelListViewShowFindPanel).ShowFindPanel

                    _onew.Delete(_ModelListView.ModelListViewColumn)


                    _WaitFormClass.Initwork(1, 1, TryCast(View.Model, IModelListView).Columns.Count)
                    For Each _Column In TryCast(View.Model, IModelListView).Columns
                        _WaitFormClass.DoWork()
                        _ModelListViewColumn = _onew.CreateObject(Of ModelListViewColumn)()
                        _ModelListViewColumn.ModelListView = _ModelListView
                        _ModelListViewColumn.Index = _Column.Index
                        _ModelListViewColumn.GroupIndex = _Column.GroupIndex
                        _ModelListViewColumn.HideValueIfZero = TryCast(_Column, IModelColumnExtender).HideValueIfZero
                        _ModelListViewColumn.InLineCriteria = TryCast(_Column, IModelColumnExtender).InLineCriteria
                        _ModelListViewColumn.InLineEdit = TryCast(_Column, IModelColumnExtender).InLineEdit
                        _ModelListViewColumn.AllowEdit = _Column.AllowEdit
                        _ModelListViewColumn.AllowSort = TryCast(_Column, IModelColumnAllowSort).AllowSort
                        _ModelListViewColumn.GroupInterval = _Column.GroupInterval
                        _ModelListViewColumn.ImmediatePostData = _Column.ImmediatePostData
                        _ModelListViewColumn.IsPassword = _Column.IsPassword
                        _ModelListViewColumn.PropertyName = _Column.PropertyName
                        _ModelListViewColumn.GroupIndex = _Column.GroupIndex
                        _ModelListViewColumn.MaxLength = _Column.MaxLength
                        _ModelListViewColumn.SortIndex = _Column.SortIndex
                        If _Column.Index >= 0 Then
                            _ModelListViewColumn.Width = TryCast(TryCast(View, ListView).Editor, GridListEditor).Columns(_Column.Index).Width
                        End If
                        _ModelListViewColumn.Caption = _Column.Caption
                        _ModelListViewColumn.Index = _Column.Index
                        _ModelListViewColumn.Id = _Column.Id
                    Next
                End If
                _onew.CommitChanges()
            End If
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, "HIBA !")
        Finally
            GLOBAL_HAS_Audit = True
            _Waitformclass.FinalWork
        End Try
    End Sub
    Private Sub aResetCentralListViewSettings_Execute(sender As Object, e As SimpleActionExecuteEventArgs) Handles aResetCentralListViewSettings.Execute
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _ModelListView As ModelListView
        Dim _IModelListView As IModelListView = TryCast(View.Model, IModelListView)

        If TryCast(TryCast(View, ListView).CollectionSource, PropertyCollectionSource).MasterObject IsNot Nothing Then
            Dim _MasterObject As Guid = TryCast(TryCast(View, ListView).CollectionSource, PropertyCollectionSource).MasterObject.oid
            _ModelListView = _onew.FindObject(Of ModelListView)(CriteriaOperator.Parse("Id=? and MasterObject=?", View.Id, _MasterObject))
        Else
            _ModelListView = _onew.FindObject(Of ModelListView)(CriteriaOperator.Parse("Id=?", View.Id))
        End If

        If _ModelListView IsNot Nothing Then
            Dim _ModelListViewColumn_Collection As New XPCollection(Of ModelListViewColumn)(_onew.Session, CriteriaOperator.Parse("ModelListView=?", _ModelListView))

            _onew.Delete(_ModelListViewColumn_Collection)
            _onew.Delete(_ModelListView)

            _onew.CommitChanges()
        End If

    End Sub


    '//CSÍK


End Class
