<commit_before>Imports System
Imports System.Drawing
Imports System.ComponentModel
Imports System.Collections.Generic
Imports System.Windows.Forms
Imports System.Reflection

Namespace Windows

  ''' <summary>
  ''' Windows Forms extender control that automatically
  ''' enables and disables detail form controls based
  ''' on the authorization settings from a CSLA .NET 
  ''' business object.
  ''' </summary>
  <DesignerCategory("")> _
  <ProvideProperty("ApplyAuthorization", GetType(Control))> _
  <ToolboxItem(True), ToolboxBitmap(GetType(ReadWriteAuthorization), "Csla.Windows.ReadWriteAuthorization")> _
  Public Class ReadWriteAuthorization
    Inherits System.ComponentModel.Component

    Implements IExtenderProvider

    Private _sources As New Dictionary(Of Control, Boolean)

    ''' <summary>
    ''' Creates an instance of the object.
    ''' </summary>
    ''' <param name="container">Container for the control.</param>
    Public Sub New(ByVal container As System.ComponentModel.IContainer)
      container.Add(Me)
    End Sub

    ''' <summary>
    ''' Gets a value indicating whether the extender control
    ''' can extend the specified control.
    ''' </summary>
    ''' <param name="extendee">The control to be extended.</param>
    ''' <remarks>
    ''' Any control implementing either a ReadOnly property or
    ''' Enabled property can be extended.
    ''' </remarks>
    Public Function CanExtend( _
      ByVal extendee As Object) As Boolean _
      Implements IExtenderProvider.CanExtend

      If IsPropertyImplemented(extendee, "ReadOnly") OrElse _
          IsPropertyImplemented(extendee, "Enabled") Then
        Return True

      Else
        Return False
      End If

    End Function

    ''' <summary>
    ''' Gets the custom ApplyAuthorization extender
    ''' property added to extended controls.
    ''' </summary>
    ''' <param name="source">Control being extended.</param>
    <Category("Csla")> _
    Public Function GetApplyAuthorization( _
      ByVal source As Control) As Boolean

      Dim result As Boolean
      If _sources.TryGetValue(source, result) Then
        Return result

      Else
        Return False
      End If

    End Function

    ''' <summary>
    ''' Sets the custom ApplyAuthorization extender
    ''' property added to extended controls.
    ''' </summary>
    ''' <param name="source">Control being extended.</param>
    ''' <param name="value">New value of property.</param>
    <Category("Csla")> _
    Public Sub SetApplyAuthorization( _
      ByVal source As Control, ByVal value As Boolean)
      If _sources.ContainsKey(source) Then
        _sources.Item(source) = value
      Else
        _sources.Add(source, value)
      End If
    End Sub

    ''' <summary>
    ''' Causes the ReadWriteAuthorization control
    ''' to apply authorization rules from the business
    ''' object to all extended controls on the form.
    ''' </summary>
    ''' <remarks>
    ''' Call this method to refresh the display of detail
    ''' controls on the form any time the authorization
    ''' rules may have changed. Examples include: after
    ''' a user logs in or out, and after an object has
    ''' been updated, inserted, deleted or retrieved
    ''' from the database.
    ''' </remarks>
    Public Sub ResetControlAuthorization()

      For Each item As KeyValuePair(Of Control, Boolean) In _sources
        If item.Value Then
          ' apply authorization rules
          ApplyAuthorizationRules(item.Key)
        End If
      Next

    End Sub

    Private Sub ApplyAuthorizationRules(ByVal control As Control)

      For Each binding As Binding In control.DataBindings
        ' get the BindingSource if appropriate
        If TypeOf binding.DataSource Is BindingSource Then
          Dim bs As BindingSource = CType(binding.DataSource, BindingSource)
          ' get the BusinessObject if appropriate
          Dim ds As Csla.Security.IAuthorizeReadWrite = _
            TryCast(bs.Current, Csla.Security.IAuthorizeReadWrite)
          If ds IsNot Nothing Then
            ' get the object property name
            Dim propertyName As String = _
              binding.BindingMemberInfo.BindingField

            ApplyReadRules(control, binding, _
              ds.CanReadProperty(propertyName))
            ApplyWriteRules(control, binding, _
              ds.CanWriteProperty(propertyName))
          End If
        End If
      Next

    End Sub

    Private Sub ApplyReadRules( _
      ByVal ctl As Control, ByVal binding As Binding, _
      ByVal canRead As Boolean)

      ' enable/disable reading of the value
      If canRead Then
        Dim couldRead As Boolean = ctl.Enabled
        ctl.Enabled = True
        RemoveHandler binding.Format, AddressOf ReturnEmpty
        If Not couldRead Then binding.ReadValue()<commit_msg>Fix line 124 bugid: 300<commit_after>Imports System
Imports System.Drawing
Imports System.ComponentModel
Imports System.Collections.Generic
Imports System.Windows.Forms
Imports System.Reflection
Imports Csla.Security

Namespace Windows

  ''' <summary>
  ''' Windows Forms extender control that automatically
  ''' enables and disables detail form controls based
  ''' on the authorization settings from a CSLA .NET 
  ''' business object.
  ''' </summary>
  <DesignerCategory("")> _
  <ProvideProperty("ApplyAuthorization", GetType(Control))> _
  <ToolboxItem(True), ToolboxBitmap(GetType(ReadWriteAuthorization), "Csla.Windows.ReadWriteAuthorization")> _
  Public Class ReadWriteAuthorization
    Inherits System.ComponentModel.Component

    Implements IExtenderProvider

    Private _sources As New Dictionary(Of Control, Boolean)

    ''' <summary>
    ''' Creates an instance of the object.
    ''' </summary>
    ''' <param name="container">Container for the control.</param>
    Public Sub New(ByVal container As System.ComponentModel.IContainer)
      container.Add(Me)
    End Sub

    ''' <summary>
    ''' Gets a value indicating whether the extender control
    ''' can extend the specified control.
    ''' </summary>
    ''' <param name="extendee">The control to be extended.</param>
    ''' <remarks>
    ''' Any control implementing either a ReadOnly property or
    ''' Enabled property can be extended.
    ''' </remarks>
    Public Function CanExtend( _
      ByVal extendee As Object) As Boolean _
      Implements IExtenderProvider.CanExtend

      If IsPropertyImplemented(extendee, "ReadOnly") OrElse _
          IsPropertyImplemented(extendee, "Enabled") Then
        Return True

      Else
        Return False
      End If

    End Function

    ''' <summary>
    ''' Gets the custom ApplyAuthorization extender
    ''' property added to extended controls.
    ''' </summary>
    ''' <param name="source">Control being extended.</param>
    <Category("Csla")> _
    Public Function GetApplyAuthorization( _
      ByVal source As Control) As Boolean

      Dim result As Boolean
      If _sources.TryGetValue(source, result) Then
        Return result

      Else
        Return False
      End If

    End Function

    ''' <summary>
    ''' Sets the custom ApplyAuthorization extender
    ''' property added to extended controls.
    ''' </summary>
    ''' <param name="source">Control being extended.</param>
    ''' <param name="value">New value of property.</param>
    <Category("Csla")> _
    Public Sub SetApplyAuthorization( _
      ByVal source As Control, ByVal value As Boolean)
      If _sources.ContainsKey(source) Then
        _sources.Item(source) = value
      Else
        _sources.Add(source, value)
      End If
    End Sub

    ''' <summary>
    ''' Causes the ReadWriteAuthorization control
    ''' to apply authorization rules from the business
    ''' object to all extended controls on the form.
    ''' </summary>
    ''' <remarks>
    ''' Call this method to refresh the display of detail
    ''' controls on the form any time the authorization
    ''' rules may have changed. Examples include: after
    ''' a user logs in or out, and after an object has
    ''' been updated, inserted, deleted or retrieved
    ''' from the database.
    ''' </remarks>
    Public Sub ResetControlAuthorization()

      For Each item As KeyValuePair(Of Control, Boolean) In _sources
        If item.Value Then
          ' apply authorization rules
          ApplyAuthorizationRules(item.Key)
        End If
      Next

    End Sub

    Private Sub ApplyAuthorizationRules(ByVal control As Control)

      For Each binding As Binding In control.DataBindings
        ' get the BindingSource if appropriate
        If TypeOf binding.DataSource Is BindingSource Then
          Dim bs As BindingSource = CType(binding.DataSource, BindingSource)
          ' get the BusinessObject if appropriate
          Dim ds As IAuthorizeReadWrite = _
            TryCast(bs.Current, IAuthorizeReadWrite)
          If ds IsNot Nothing Then
            ' get the object property name
            Dim propertyName As String = _
              binding.BindingMemberInfo.BindingField

            ApplyReadRules(control, binding, _
              ds.CanReadProperty(propertyName))
            ApplyWriteRules(control, binding, _
              ds.CanWriteProperty(propertyName))
          End If
        End If
      Next

    End Sub

    Private Sub ApplyReadRules( _
      ByVal ctl As Control, ByVal binding As Binding, _
      ByVal canRead As Boolean)

      ' enable/disable reading of the value
      If canRead Then
        Dim couldRead As Boolean = ctl.Enabled
        ctl.Enabled = True
        RemoveHandler binding.Format, AddressOf ReturnEmpty
        If Not couldRead Then binding.ReadValue()
{'fraction_non_alphanumeric': 0.08008298755186723, 'fraction_numerical': 0.0006224066390041493, 'mean_word_length': 2.789701257861635, 'pattern_counts': {'":': 0, '<': 65, '<?xml version=': 0, '>': 65, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4769608', 'n_tokens_mistral': 2447, 'n_tokens_neox': 2400, 'n_words': 867}