Imports Telerik.WinControls
Imports Telerik.WinControls.UI
Imports TelerikWinFormsApp2.Balances.Operator.ConfigActivityType.Entities
Imports TelerikWinFormsApp2.Balances.Operator.ConfigBalanceActivity.Entities

Public Class RadForm1
    Private oBalanceActivityType As ConfigActivityTypeManager
    Private oBalanceActivityTypeCollection As New BalanceActivityTypeColletionManager
    Private oBalanceActivity As ConfigBalanceActivityManager

    Private m_BalanceActivityID As Integer = -1I

    Private Sub Loadform()
        oBalanceActivity = New ConfigBalanceActivityManager
        RadGridView1.DataSource = oBalanceActivity.BalanceActivityList
        InitializeDataGridView()

    End Sub

    Private Sub InitializeDataGridView()
        Try
            RadGridView1.Columns("BalanceActivityID").HeaderText = "ID"
            RadGridView1.Columns("BalanceActivityID").IsVisible = False
            RadGridView1.Columns("BalanceActivityName").HeaderText = "Aktivitetsnamn"
            RadGridView1.Columns("BalanceActivityDescription").HeaderText = "Beskrivning"
            RadGridView1.Columns("ProcessType").HeaderText = "Process"
            RadGridView1.Columns("StandardTime").HeaderText = "Standardtid"
            'RadGridView1.Columns("BalanceRoleID").IsVisible = False
            'RadGridView1.Columns("BalanceRoleName").HeaderText = "Rollnamn"
            'RadGridView1.Columns("BalanceFrequenzeTypeID").IsVisible = False
            'RadGridView1.Columns("BalanceFrequenzeTypeName").HeaderText = "Frekvensnamn"
            'RadGridView1.Columns("BalanceActivityTypeCollection").IsVisible = False
            'DgwExistingActivity.Columns("BalanceFrequenzeTypeName").HeaderText = "Frekvensnamn"

            RadGridView1.BestFitColumns(BestFitColumnMode.AllCells)
        Catch ex As Exception

        End Try
    End Sub

    Private Sub FillComboBox()
        ' get activity types from database and create a list of object
        oBalanceActivityType = New ConfigActivityTypeManager()

        ' set activity list object to data source
        RadMultiColumnComboBox1.DataSource = oBalanceActivityType.BalanceActivityTypeList
        RadMultiColumnComboBox1.DisplayMember = "ActivityTypeName"
        RadMultiColumnComboBox1.ValueMember = "ActivityTypeID"

        RadMultiColumnComboBox1.Columns("ActivityTypeID").IsVisible = False
        RadMultiColumnComboBox1.Columns("ActivityTypeName").HeaderText = "Aktivitetstypnamn"
        RadMultiColumnComboBox1.Columns("ActivityTypeDescription").HeaderText = "Beskrivning"
        RadMultiColumnComboBox1.Columns("Comment").HeaderText = "Kommentar"
        RadMultiColumnComboBox1.Columns("ProcessType").IsVisible = False

        RadMultiColumnComboBox1.AutoSizeDropDownToBestFit = True
        RadMultiColumnComboBox1.DropDownMinSize = New Size(420, 300)
        RadMultiColumnComboBox1.BestFitColumns()

        Dim extender As New RadMultiColumnComboBoxSelectionExtender()
        extender.AssociatedRadMultiColumnComboBox = RadMultiColumnComboBox1
    End Sub

    'Private Sub FrmTestTelerik_Load(sender As Object, e As EventArgs) Handles Me.Load
    '    SetCheckStateToActvityTypes()
    'End Sub

    Private Sub SetCheckStateToActvityTypes()
        For i As Integer = 0 To RadMultiColumnComboBox1.MultiColumnComboBoxElement.Rows.Count - 1
            Dim item As Object = RadMultiColumnComboBox1.MultiColumnComboBoxElement.Rows(i)
            Dim id As Integer = item.Cells(0).value
            'If id = GetSelectetActivityTypeIDFromDB(id) Then
            RadMultiColumnComboBox1.MultiColumnComboBoxElement.Rows(i).Tag = Boolean.TrueString
            'End If
        Next
    End Sub

    Private Sub SetSelectedActivityTypeToDataBase(ByVal currentRow As GridViewRowInfo)
        Try
            Dim activityTypeItem As Object = oBalanceActivityTypeCollection.BalanceActivityTypeCollection(currentRow.Cells(0).Value)
            For i As Integer = 0 To activityTypeItem.Count - 1
                If currentRow.Cells(0).Value = activityTypeItem(i).BalanceActivityID Then
                    For j As Integer = 0 To RadMultiColumnComboBox1.MultiColumnComboBoxElement.Rows.Count - 1
                        Dim item As Object = RadMultiColumnComboBox1.MultiColumnComboBoxElement.Rows(j)
                        If item.Cells(0).value = activityTypeItem(i).BalanceActivityTypeID Then
                            RadMultiColumnComboBox1.MultiColumnComboBoxElement.Rows(i).Tag = Boolean.TrueString
                        End If
                    Next
                End If
            Next
        Catch ex As Exception

        End Try
    End Sub

    Private Sub RadButton1_Click(sender As Object, e As EventArgs) Handles RadButton1.Click
        FillComboBox()
    End Sub

    Private Sub RadButton2_Click(sender As Object, e As EventArgs) Handles RadButton2.Click
        SetSelectedActivityTypeToDataBase(RadGridView1.CurrentRow)
        'SetCheckStateToActvityTypes()
    End Sub

    Private Sub RadForm1_Load(sender As Object, e As EventArgs) Handles Me.Load
        Loadform()
    End Sub
End Class


<ComponentModel.ToolboxItem(False)>
Public Class RadMultiColumnComboBoxSelectionExtender
    Inherits System.ComponentModel.Component
    Private m_autoCompleteBoxElement As RadAutoCompleteBoxElement
    Private rows As New System.Collections.Generic.Dictionary(Of String, GridViewRowInfo)()
    Private m_associatedRadMultiColumnComboBox As RadMultiColumnComboBox
    Private originalSize As System.Drawing.Size = System.Drawing.Size.Empty

    Public ReadOnly Property AutoCompleteBoxElement() As RadAutoCompleteBoxElement
        Get
            Return Me.m_autoCompleteBoxElement
        End Get
    End Property

    ''' <summary>
    ''' Gets the tokenized items.
    ''' </summary>
    Public ReadOnly Property Items() As RadTokenizedTextItemCollection
        Get
            Return Me.m_autoCompleteBoxElement.Items
        End Get
    End Property
    Public Property AssociatedRadMultiColumnComboBox() As RadMultiColumnComboBox
        Get
            Return Me.m_associatedRadMultiColumnComboBox
        End Get
        Set(value As RadMultiColumnComboBox)
            Me.SetAssociatedRadMultiColumnComboBox(value)
        End Set
    End Property

    Private Sub SetAssociatedRadMultiColumnComboBox(radMultiColumnComboBox As RadMultiColumnComboBox)
        If radMultiColumnComboBox Is Nothing AndAlso Me.m_associatedRadMultiColumnComboBox IsNot Nothing Then
            Me.SetAssociatedRadMultiColumnComboBoxCoreToNull()
        End If
        Me.SetAssociatedRadMultiColumnComboBoxCore(radMultiColumnComboBox)
    End Sub

    Private Sub SetAssociatedRadMultiColumnComboBoxCoreToNull()
        RemoveHandler Me.m_associatedRadMultiColumnComboBox.HandleCreated, AddressOf associatedRadMultiColumnComboBox_HandleCreated
        RemoveHandler Me.m_associatedRadMultiColumnComboBox.DataBindingComplete, AddressOf associatedRadMultiColumnComboBox_DataBindingComplete
        RemoveHandler Me.m_associatedRadMultiColumnComboBox.EditorControl.ViewCellFormatting, AddressOf EditorControl_ViewCellFormatting
        RemoveHandler Me.m_associatedRadMultiColumnComboBox.DropDownClosing, AddressOf radMultiColumnCombobox1_DropDownClosing
        Me.m_associatedRadMultiColumnComboBox.MultiColumnComboBoxElement.TextBoxElement.Visibility = ElementVisibility.Visible
        Me.m_associatedRadMultiColumnComboBox.MultiColumnComboBoxElement.Children(2).Children.Remove(m_autoCompleteBoxElement)
        Me.m_autoCompleteBoxElement.MaxSize = New System.Drawing.Size(0, 0)
        Me.m_autoCompleteBoxElement.AutoCompleteDataSource = Nothing
        Me.m_associatedRadMultiColumnComboBox.AutoSize = False
        Me.m_associatedRadMultiColumnComboBox.Size = Me.originalSize
        For Each item As GridViewRowInfo In Me.m_associatedRadMultiColumnComboBox.EditorControl.Rows
            item.Tag = Nothing
        Next
        RemoveHandler Me.m_autoCompleteBoxElement.Items.CollectionChanged, AddressOf Items_CollectionChanged
        RemoveHandler Me.m_autoCompleteBoxElement.CreateTextBlock, AddressOf autoCompleteBoxElement_CreateTextBlock
        RemoveHandler Me.m_autoCompleteBoxElement.TokenValidating, AddressOf autoCompleteBoxElement_TokenValidating
        Me.m_autoCompleteBoxElement.Text = Me.m_associatedRadMultiColumnComboBox.Text
        RemoveHandler Me.m_autoCompleteBoxElement.KeyDown, AddressOf autoCompleteBoxElement_KeyDown
        Me.m_associatedRadMultiColumnComboBox = Nothing
        Me.rows.Clear()
    End Sub

    Private Sub SetAssociatedRadMultiColumnComboBoxCore(radMultiColumnComboBox As RadMultiColumnComboBox)
        If radMultiColumnComboBox Is Nothing Then
            Return
        End If
        Me.originalSize = radMultiColumnComboBox.Size
        Me.m_associatedRadMultiColumnComboBox = radMultiColumnComboBox
        Me.m_associatedRadMultiColumnComboBox.AutoSize = True
        AddHandler Me.m_associatedRadMultiColumnComboBox.ThemeNameChanged, AddressOf associatedRadMultiColumnComboBox_ThemeNameChanged
        AddHandler Me.m_associatedRadMultiColumnComboBox.HandleCreated, AddressOf associatedRadMultiColumnComboBox_HandleCreated
        AddHandler Me.m_associatedRadMultiColumnComboBox.DataBindingComplete, AddressOf associatedRadMultiColumnComboBox_DataBindingComplete
        Me.m_associatedRadMultiColumnComboBox.DropDownStyle = RadDropDownStyle.DropDownList
        AddHandler Me.m_associatedRadMultiColumnComboBox.EditorControl.ViewCellFormatting, AddressOf EditorControl_ViewCellFormatting
        AddHandler Me.m_associatedRadMultiColumnComboBox.DropDownClosing, AddressOf radMultiColumnCombobox1_DropDownClosing
        Me.m_autoCompleteBoxElement = New RadAutoCompleteBoxElement()
        Me.m_autoCompleteBoxElement.MinSize = New System.Drawing.Size(150, 24)
        Me.m_autoCompleteBoxElement.Multiline = True
        Me.m_autoCompleteBoxElement.DrawBorder = False
        AddHandler Me.m_autoCompleteBoxElement.KeyDown, AddressOf autoCompleteBoxElement_KeyDown
        Me.m_associatedRadMultiColumnComboBox.MultiColumnComboBoxElement.ArrowButton.ZIndex = 1
        Me.m_associatedRadMultiColumnComboBox.MultiColumnComboBoxElement.TextBoxElement.Visibility = ElementVisibility.Hidden
        Me.m_associatedRadMultiColumnComboBox.MultiColumnComboBoxElement.Children(2).Children.Add(Me.m_autoCompleteBoxElement)
        Me.m_autoCompleteBoxElement.MaxSize = New System.Drawing.Size(Me.m_associatedRadMultiColumnComboBox.Size.Width - Me.m_associatedRadMultiColumnComboBox.MultiColumnComboBoxElement.ArrowButton.Size.Width, 0)
        Me.m_autoCompleteBoxElement.AutoCompleteDataSource = New System.Collections.Generic.List(Of String)(GetAutoCompleteItems())
        Me.m_autoCompleteBoxElement.Margin = New System.Windows.Forms.Padding(0)
        AddHandler Me.m_autoCompleteBoxElement.Items.CollectionChanged, AddressOf Items_CollectionChanged
        AddHandler Me.m_autoCompleteBoxElement.CreateTextBlock, AddressOf autoCompleteBoxElement_CreateTextBlock
        AddHandler Me.m_autoCompleteBoxElement.TokenValidating, AddressOf autoCompleteBoxElement_TokenValidating
    End Sub

    Private Sub associatedRadMultiColumnComboBox_ThemeNameChanged(source As Object, args As ThemeNameChangedEventArgs)
        Me.m_autoCompleteBoxElement.MaxSize = New System.Drawing.Size(Me.m_associatedRadMultiColumnComboBox.Size.Width - Me.m_associatedRadMultiColumnComboBox.MultiColumnComboBoxElement.ArrowButton.Size.Width, 0)
    End Sub

    Private Sub autoCompleteBoxElement_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs)
        If e.KeyCode = System.Windows.Forms.Keys.F4 Then
            m_associatedRadMultiColumnComboBox.MultiColumnComboBoxElement.ShowPopup()
        End If
        If e.KeyCode = System.Windows.Forms.Keys.Escape Then
            m_associatedRadMultiColumnComboBox.MultiColumnComboBoxElement.ClosePopup(RadPopupCloseReason.CloseCalled)
        End If
    End Sub

    Private Sub associatedRadMultiColumnComboBox_DataBindingComplete(sender As Object, e As GridViewBindingCompleteEventArgs)
        m_autoCompleteBoxElement.AutoCompleteDataSource = New System.Collections.Generic.List(Of String)(GetAutoCompleteItems())
    End Sub

    Private Sub associatedRadMultiColumnComboBox_HandleCreated(sender As Object, e As EventArgs)
        m_autoCompleteBoxElement.AutoCompleteDataSource = New System.Collections.Generic.List(Of String)(GetAutoCompleteItems())
    End Sub

    Private Function GetAutoCompleteItems() As System.Collections.Generic.IEnumerable(Of String)
        Dim items As New List(Of String)
        For Each row As GridViewRowInfo In Me.m_associatedRadMultiColumnComboBox.EditorControl.Rows
            Dim value As String = row.Cells(Me.m_associatedRadMultiColumnComboBox.DisplayMember).Value.ToString().Trim()
            If Not rows.ContainsKey(value) Then
                Me.rows.Add(value, row)
            End If
            items.Add(value)
        Next
        Return items
    End Function

    Private Sub radMultiColumnCombobox1_DropDownClosing(sender As Object, args As RadPopupClosingEventArgs)
        args.Cancel = (args.CloseReason = RadPopupCloseReason.Mouse AndAlso Me.m_associatedRadMultiColumnComboBox.EditorControl.ElementTree.GetElementAtPoint(Of RadCheckBoxElement)(Me.m_associatedRadMultiColumnComboBox.EditorControl.PointToClient(System.Windows.Forms.Cursor.Position)) IsNot Nothing)
        Me.SyncCollection()
    End Sub

    Private Sub EditorControl_ViewCellFormatting(sender As Object, e As Telerik.WinControls.UI.CellFormattingEventArgs)
        If e.ColumnIndex <> -1 OrElse e.CellElement.RowIndex = -1 Then
            Return
        End If
        If e.CellElement.Children.Count = 1 Then
            Dim checkBoxElement As New RadCheckBoxElement()
            checkBoxElement.Padding = New System.Windows.Forms.Padding(0, 3, 1, 0)
            checkBoxElement.Alignment = ContentAlignment.MiddleCenter
            checkBoxElement.NotifyParentOnMouseInput = False
            e.CellElement.Children.Add(checkBoxElement)
        End If
        Dim checkBox As RadCheckBoxElement = e.CellElement.FindDescendant(Of RadCheckBoxElement)()
        RemoveHandler checkBox.CheckStateChanged, AddressOf checkBox_CheckStateChanged
        checkBox.IsChecked = e.Row.Tag IsNot Nothing AndAlso e.Row.Tag.ToString() = [Boolean].TrueString
        AddHandler checkBox.CheckStateChanged, AddressOf checkBox_CheckStateChanged
    End Sub

    Private Sub SyncCollection()
        Me.m_associatedRadMultiColumnComboBox.MultiColumnComboBoxElement.TextBoxElement.SuspendPropertyNotifications()
        Me.m_associatedRadMultiColumnComboBox.Text = String.Empty
        For Each item As GridViewRowInfo In Me.m_associatedRadMultiColumnComboBox.EditorControl.Rows
            If item.Tag IsNot Nothing AndAlso item.Tag.ToString() = [Boolean].TrueString Then
                Me.m_associatedRadMultiColumnComboBox.Text += item.Cells(Me.m_associatedRadMultiColumnComboBox.DisplayMember).Value + "; "
            End If
        Next
        Me.m_associatedRadMultiColumnComboBox.MultiColumnComboBoxElement.TextBoxElement.ResumePropertyNotifications()
        RemoveHandler m_autoCompleteBoxElement.Items.CollectionChanged, AddressOf Items_CollectionChanged
        RemoveHandler m_autoCompleteBoxElement.CreateTextBlock, AddressOf autoCompleteBoxElement_CreateTextBlock
        RemoveHandler m_autoCompleteBoxElement.TokenValidating, AddressOf autoCompleteBoxElement_TokenValidating
        m_autoCompleteBoxElement.Text = Me.m_associatedRadMultiColumnComboBox.Text
        AddHandler m_autoCompleteBoxElement.TokenValidating, AddressOf autoCompleteBoxElement_TokenValidating
        AddHandler m_autoCompleteBoxElement.CreateTextBlock, AddressOf autoCompleteBoxElement_CreateTextBlock
        AddHandler m_autoCompleteBoxElement.Items.CollectionChanged, AddressOf Items_CollectionChanged
    End Sub

    Private Sub autoCompleteBoxElement_TokenValidating(sender As Object, e As TokenValidatingEventArgs)
        If Me.rows.ContainsKey(e.Text) Then
            If Me.rows(e.Text).Tag + "" = [Boolean].TrueString Then
                e.IsValidToken = False
            End If
        Else
            e.IsValidToken = False
        End If
    End Sub

    Private Sub autoCompleteBoxElement_CreateTextBlock(sender As Object, e As CreateTextBlockEventArgs)
        If Me.rows.ContainsKey(e.Text) AndAlso TypeOf e.TextBlock Is TokenizedTextBlockElement Then
            Me.rows(e.Text).Tag = [Boolean].TrueString
            Me.rows(e.Text).InvalidateRow()
        End If
    End Sub

    Private Sub Items_CollectionChanged(sender As Object, e As Telerik.WinControls.Data.NotifyCollectionChangedEventArgs)
        If e.Action = Telerik.WinControls.Data.NotifyCollectionChangedAction.Remove Then
            For Each item As RadTokenizedTextItem In e.NewItems
                If Me.rows.ContainsKey(item.Text.Trim()) Then
                    Dim itemText As String = item.Text.Trim()
                    Me.rows(itemText).Tag = [Boolean].FalseString
                    Me.rows(itemText).InvalidateRow()
                End If
            Next
        End If
    End Sub

    Public Sub checkBox_CheckStateChanged(sender As Object, e As EventArgs)
        Dim checkBox As RadCheckBoxElement = TryCast(sender, RadCheckBoxElement)
        Dim row As GridRowElement = checkBox.FindAncestor(Of GridRowElement)()
        row.RowInfo.Tag = checkBox.IsChecked.ToString()
    End Sub
End Class
