Imports MicroFour.StrataFrame.UI.Windows.Forms
Imports System.ComponentModel
Imports System.ComponentModel.Design
Imports MicroFour.StrataFrame.Business

Public Class MyOwnTextBox
    Inherits System.Windows.Forms.TextBox
    Implements MicroFour.StrataFrame.UI.Windows.Forms.IBusinessBindable

#Region " IBusinessBindable Implementation "

    Private _BusinessObject As BusinessLayer
    Private _BindingField As String = ""
    Private _BindingFormat As String = ""
    Private _BindingProperty As String = "Text"
    Private _IgnoreManageUIReadOnlyState As Boolean = False
    Private _LocalizationKey As String = ""
    Private _PropertyToLocalize As String = "Text"
    Private _BindingUpdateMode As DataSourceUpdateMode = DataSourceUpdateMode.OnPropertyChanged
    Private _ControlSourceUpdateMode As ControlUpdateMode = ControlUpdateMode.OnPropertyChanged

    ''' <summary>
    ''' Gets or sets a value that determines whether the control will allow
    ''' its editable state to be managed by the bound business object.
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    <Category(EDITOR_CATEGORY), _
    Description(EDITOR_CONTROLUPDATEMODE_DESC)> _
    Public Property ControlSourceUpdateMode() As System.Windows.Forms.ControlUpdateMode Implements IBusinessBindable.ControlSourceUpdateMode
        Get
            Return Me._ControlSourceUpdateMode
        End Get
        Set(ByVal value As System.Windows.Forms.ControlUpdateMode)
            Me._ControlSourceUpdateMode = value
        End Set
    End Property

    ''' <summary>
    ''' The field within the business object that is bound to the control
    ''' </summary>
    <Category(EDITOR_CATEGORY), Description(EDITOR_BINDINGUPDATEMODE_DESC)> _
    Public Property BindingUpdateMode() As DataSourceUpdateMode Implements IBusinessBindable.DataSourceUpdateMode
        Get
            Return _BindingUpdateMode
        End Get
        Set(ByVal value As System.Windows.Forms.DataSourceUpdateMode)
            _BindingUpdateMode = value
        End Set
    End Property

    ''' <summary>
    ''' Makes the control available or unavailable for editing
    ''' </summary>
    ''' <value></value>
    ''' <remarks></remarks>
    <Browsable(False), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)> _
    Public Property BindingEditable() As Boolean Implements IBusinessBindable.BindingEditable
        Get
            Return Me.Enabled
        End Get
        Set(ByVal value As Boolean)
            Me.Enabled = value
        End Set
    End Property

    ''' <summary>
    ''' The field within the business object that is bound to the control
    ''' </summary>
    ''' <value></value>
    ''' <remarks></remarks>
    <Category(EDITOR_CATEGORY), Description(EDITOR_BINDINGFIELD_DESC), DefaultValue(""), EditorAttribute(ControlConstants.EDIT_BindingFieldEditor, GetType(System.Drawing.Design.UITypeEditor))> _
    Public Property BindingField() As String Implements IBusinessBindable.BindingField
        Get
            Return _BindingField
        End Get
        Set(ByVal value As String)
            _BindingField = value
        End Set
    End Property

    ''' <summary>
    ''' The business object that is used to bind the data to this field
    ''' </summary>
    ''' <value></value>
    ''' <remarks></remarks>
    <Category(EDITOR_CATEGORY), _
    Description(EDITOR_BUSINESSOBJECT_DESC), _
    Editor(ControlConstants.EDIT_BusinessObjectTypeEditor, GetType(System.Drawing.Design.UITypeEditor))> _
    Public Property BusinessObject() As BusinessLayerBase Implements IBusinessBindable.BusinessObject
        Get
            Return _BusinessObject
        End Get
        Set(ByVal value As BusinessLayerBase)
            If (Not Me.DesignMode) AndAlso (value IsNot Nothing) Then
                value.AddToListOfBoundControls(Me)
            End If

            _BusinessObject = CType(value, BusinessLayer)
        End Set
    End Property

    ''' <summary>
    ''' The property on this control to which the data is bound.
    ''' </summary>
    ''' <value></value>
    ''' <remarks></remarks>
    <Category(EDITOR_CATEGORY), Description(EDITOR_BINDINGPROPERTY_DESC), DefaultValue("Text"), EditorAttribute(ControlConstants.EDIT_BindingPropertyEditor, GetType(System.Drawing.Design.UITypeEditor))> _
    Public Property BindingProperty() As String Implements IBusinessBindable.BindingProperty
        Get
            Return _BindingProperty
        End Get
        Set(ByVal value As String)
            _BindingProperty = value
        End Set
    End Property

    ''' <summary>
    ''' If True, the control will ignore that auto manage editing state of the business object.  The control will not
    ''' be automatically enabled/disabled by the business object.
    ''' </summary>
    ''' <value></value>
    ''' <remarks></remarks>
    <Category(EDITOR_CATEGORY), Description(EDITOR_BINDINGPROPERTY_DESC), DefaultValue(False), EditorAttribute(ControlConstants.EDIT_BindingPropertyEditor, GetType(System.Drawing.Design.UITypeEditor))> _
    Public Property IgnoreManageReadOnlyState() As Boolean Implements IBusinessBindable.IgnoreManageUIReadOnlyState
        Get
            Return _IgnoreManageUIReadOnlyState
        End Get
        Set(ByVal value As Boolean)
            _IgnoreManageUIReadOnlyState = value
        End Set
    End Property

    ''' <summary>
    ''' The formst string used to format the data that is bound to the control
    ''' </summary>
    ''' <value></value>
    ''' <remarks></remarks>
    <Description(EDITOR_BINDINGFORMAT_DESC), Category(EDITOR_CATEGORY), DefaultValue("")> _
    Public Property BindingFormat() As String Implements IBusinessBindable.BindingFormat
        Get
            Return _BindingFormat
        End Get
        Set(ByVal value As String)
            _BindingFormat = value
        End Set
    End Property

    ''' <summary>
    ''' Determines if the binding update mode property is set to default
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Private Function ShouldSerializeBindingUpdateMode() As Boolean Implements IBusinessBindable.ShouldSerializeBindingUpdateMode
        Return _BindingUpdateMode <> DataSourceUpdateMode.OnPropertyChanged
    End Function

    ''' <summary>
    ''' Resets the binding update mode
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub ResetBindingUpdateMode() Implements IBusinessBindable.ResetBindingUpdateMode
        _BindingUpdateMode = DataSourceUpdateMode.OnPropertyChanged
    End Sub

    Private Sub ResetControlSourceUpdateMode()
        Me._ControlSourceUpdateMode = ControlUpdateMode.OnPropertyChanged
    End Sub

    Private Function ShouldSerializeControlSourceUpdateMode() As Boolean
        Return Me._ControlSourceUpdateMode <> ControlUpdateMode.OnPropertyChanged
    End Function
#End Region

End Class
