Imports System.Drawing
Imports System.Windows.Forms
Imports DevExpress.XtraGrid.Views.Grid
Imports DevExpress.XtraTreeList

Public Class GridBuilder
    Private ReadOnly EvenRowBackColor = Color.Gainsboro

    Public ReadOnly Property Views As New List(Of GridView)

    Public Sub New(GridView As GridView)
        Views.Add(GridView)
    End Sub

    Public Sub New(GridViews As List(Of GridView))
        Views.AddRange(GridViews)
    End Sub

    ''' <summary>
    ''' Applies common properties to all GridViews
    ''' </summary>
    Public Function WithDefaults() As GridBuilder
        For Each oView In Views
            WithDefaults(oView)
        Next

        Return Me
    End Function

    ''' <summary>
    ''' Applies common properties to the supplied GridView
    ''' </summary>
    Public Function WithDefaults(GridView As GridView) As GridBuilder
        GridView.OptionsView.EnableAppearanceEvenRow = True
        GridView.OptionsView.ShowAutoFilterRow = True
        GridView.Appearance.EvenRow.BackColor = EvenRowBackColor
        GridView.Appearance.EvenRow.Options.UseBackColor = True

        Return Me
    End Function

    ''' <summary>
    ''' Applies common properties to the supplied TreeList
    ''' </summary>
    Public Function WithDefaults(TreeList As TreeList) As GridBuilder
        TreeList.OptionsView.EnableAppearanceEvenRow = True
        TreeList.OptionsView.ShowAutoFilterRow = True
        TreeList.Appearance.EvenRow.BackColor = EvenRowBackColor
        TreeList.Appearance.EvenRow.Options.UseBackColor = True

        Return Me
    End Function

    ''' <summary>
    ''' Applies read-only properties to all GridViews
    ''' </summary>
    Public Function WithReadOnlyOptions() As GridBuilder
        For Each oView In Views
            WithReadOnlyOptions(oView)
        Next

        Return Me
    End Function

    ''' <summary>
    ''' Applies read-only properties to the supplied GridView
    ''' </summary>
    Public Function WithReadOnlyOptions(GridView As GridView) As GridBuilder
        GridView.OptionsBehavior.Editable = False
        GridView.OptionsBehavior.ReadOnly = True

        Return Me
    End Function

    ''' <summary>
    ''' Applies read-only properties to the supplied TreeList
    ''' </summary>
    Public Function WithReadOnlyOptions(TreeList As TreeList) As GridBuilder
        TreeList.OptionsBehavior.Editable = False
        TreeList.OptionsBehavior.ReadOnly = True

        Return Me
    End Function

    Public Function WithClipboardHandler() As GridBuilder
        For Each oGridView In Views
            WithClipboardHandler(oGridView)
        Next

        Return Me
    End Function

    Public Function WithClipboardHandler(View As GridView) As GridBuilder
        AddHandler View.KeyDown, AddressOf GridView_ClipboardHandler

        Return Me
    End Function

    Private Sub GridView_ClipboardHandler(sender As Object, e As KeyEventArgs)
        Dim view As GridView = CType(sender, GridView)
        If e.Control AndAlso e.KeyCode = Keys.C Then
            If view.GetRowCellValue(view.FocusedRowHandle, view.FocusedColumn) IsNot Nothing AndAlso view.GetRowCellValue(view.FocusedRowHandle, view.FocusedColumn).ToString() <> [String].Empty Then
                Clipboard.SetText(view.GetRowCellValue(view.FocusedRowHandle, view.FocusedColumn).ToString())
            End If
            e.Handled = True
        End If
    End Sub
End Class
