BindingList.cs :  » 2.6.4-mono-.net-core » System.ComponentModel » System » ComponentModel » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » 2.6.4 mono .net core » System.ComponentModel 
System.ComponentModel » System » ComponentModel » BindingList.cs
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
// 
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
// Copyright (c) 2007 Novell, Inc.
//

#if NET_2_0

using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Reflection;

namespace System.ComponentModel{
  [SerializableAttribute] 
  public class BindingList<T> : Collection<T>,
    IBindingList, IList, ICollection, 
    IEnumerable, ICancelAddNew, IRaiseItemChangedEvents
  {
    bool allow_edit = true;
    bool allow_remove = true;
    bool allow_new;
    bool allow_new_set;

    bool raise_list_changed_events = true;
    
    bool type_has_default_ctor;
    bool type_raises_item_changed_events;

    bool add_pending;
    int pending_add_index;

    void CheckType ()
    {
      ConstructorInfo ci = typeof (T).GetConstructor (Type.EmptyTypes);
      type_has_default_ctor = (ci != null);
      type_raises_item_changed_events = typeof (INotifyPropertyChanged).IsAssignableFrom (typeof (T));
    }

    public BindingList (IList<T> list) : base(list)
    {
      CheckType ();
    }

    public BindingList () : base ()
    {
      CheckType ();
    }

    public bool AllowEdit {
      get { return allow_edit; }
      set {
        if (allow_edit != value) {
          allow_edit = value;

          if (raise_list_changed_events)
            OnListChanged (new ListChangedEventArgs (ListChangedType.Reset, -1 /* XXX */));
        }
      }
    }

    public bool AllowNew {
      get {
        /* if the user explicitly it, return that value */
        if (allow_new_set)
          return allow_new;

        /* if the list type has a default constructor we allow new */
        if (type_has_default_ctor)
          return true;

        /* if the user adds a delegate, we return true even if
           the type doesn't have a default ctor */
        if (AddingNew != null)
          return true;

        return false;
      }
      set {
        // this funky check (using AllowNew
        // instead of allow_new allows us to
        // keep the logic for the 3 cases in
        // one place (the getter) instead of
        // spreading them around the file (in
        // the ctor, in the AddingNew add
        // handler, etc.
        if (AllowNew != value) {
          allow_new_set = true;

          allow_new = value;

          if (raise_list_changed_events)
            OnListChanged (new ListChangedEventArgs (ListChangedType.Reset, -1 /* XXX */));
        }
      }
    }

    public bool AllowRemove {
      get { return allow_remove; }
      set {
        if (allow_remove != value) {
          allow_remove = value;

          if (raise_list_changed_events)
            OnListChanged (new ListChangedEventArgs (ListChangedType.Reset, -1 /* XXX */));
        }
      }
    }

    protected virtual bool IsSortedCore {
      get { return false; }
    }

    public bool RaiseListChangedEvents {
      get { return raise_list_changed_events; }
      set { raise_list_changed_events = value; }
    }

    protected virtual ListSortDirection SortDirectionCore {
      get { return ListSortDirection.Ascending; }
    }

    protected virtual PropertyDescriptor SortPropertyCore {
      get { return null; }
    }

    protected virtual bool SupportsChangeNotificationCore {
      get { return true; }
    }

    protected virtual bool SupportsSearchingCore {
      get { return false; }
    }

    protected virtual bool SupportsSortingCore {
      get { return false; }
    }

    public event AddingNewEventHandler AddingNew;
    public event ListChangedEventHandler ListChanged;

    public T AddNew ()
    {
      return (T)AddNewCore ();
    }

    protected virtual object AddNewCore ()
    {
      if (!AllowNew)
        throw new InvalidOperationException ();

      AddingNewEventArgs args = new AddingNewEventArgs ();

      OnAddingNew (args);

      T new_obj = (T)args.NewObject;
      if (new_obj == null) {
        if (!type_has_default_ctor)
          throw new InvalidOperationException ();

        new_obj = (T)Activator.CreateInstance (typeof (T));
      }

      Add (new_obj);
      pending_add_index = IndexOf (new_obj);
      add_pending = true;
      
      return new_obj;
    }

    protected virtual void ApplySortCore (PropertyDescriptor prop, ListSortDirection direction)
    {
      throw new NotSupportedException ();
    }

    public virtual void CancelNew (int itemIndex)
    {
      if (!add_pending)
        return;

      if (itemIndex != pending_add_index)
        return;

      add_pending = false;

      base.RemoveItem (itemIndex);

      if (raise_list_changed_events)
        OnListChanged (new ListChangedEventArgs (ListChangedType.ItemDeleted, itemIndex));
    }

    protected override void ClearItems ()
    {
      EndNew (pending_add_index);

      base.ClearItems ();

      OnListChanged (new ListChangedEventArgs (ListChangedType.Reset, -1));
    }

    public virtual void EndNew (int itemIndex)
    {
      if (!add_pending)
        return;

      if (itemIndex != pending_add_index)
        return;

      add_pending = false;
    }

    protected virtual int FindCore (PropertyDescriptor prop, object key)
    {
      throw new NotSupportedException ();
    }

    protected override void InsertItem (int index, T item)
    {
      EndNew (pending_add_index);

      base.InsertItem (index, item);

      if (raise_list_changed_events)
        OnListChanged (new ListChangedEventArgs (ListChangedType.ItemAdded, index));
    }

    protected virtual void OnAddingNew (AddingNewEventArgs e)
    {
      if (AddingNew != null)
        AddingNew (this, e);
    }

    protected virtual void OnListChanged (ListChangedEventArgs e)
    {
      if (ListChanged != null)
        ListChanged (this, e);
    }

    protected override void RemoveItem (int index)
    {
      if (!AllowRemove)
        throw new NotSupportedException ();

      EndNew (pending_add_index);

      base.RemoveItem (index);

      if (raise_list_changed_events)
        OnListChanged (new ListChangedEventArgs (ListChangedType.ItemDeleted, index));
    }

    protected virtual void RemoveSortCore ()
    {
      throw new NotSupportedException ();
    }

    public void ResetBindings ()
    {
      OnListChanged (new ListChangedEventArgs (ListChangedType.Reset, -1));
    }

    public void ResetItem (int position)
    {
      OnListChanged (new ListChangedEventArgs (ListChangedType.ItemChanged, position));
    }

    protected override void SetItem (int index, T item)
    {
      base.SetItem (index, item);

      OnListChanged (new ListChangedEventArgs (ListChangedType.ItemChanged, index));
    }

    void IBindingList.AddIndex (PropertyDescriptor index)
    {
      /* no implementation by default */
    }

    object IBindingList.AddNew ()
    {
      return AddNew ();
    }

    void IBindingList.ApplySort (PropertyDescriptor property, ListSortDirection direction)
    {
      ApplySortCore (property, direction);
    }

    int IBindingList.Find (PropertyDescriptor property, object key)
    {
      return FindCore (property, key);
    }

    void IBindingList.RemoveIndex (PropertyDescriptor property)
    {
      /* no implementation by default */
    }

    void IBindingList.RemoveSort ()
    {
      RemoveSortCore ();
    }

    bool IBindingList.IsSorted {
      get { return IsSortedCore; }
    }

    ListSortDirection IBindingList.SortDirection {
      get { return SortDirectionCore; }
    }

    PropertyDescriptor IBindingList.SortProperty {
      get { return SortPropertyCore; }
    }

    bool IBindingList.AllowEdit {
      get { return AllowEdit; }
    }

    bool IBindingList.AllowNew {
      get { return AllowNew; }
    }

    bool IBindingList.AllowRemove {
      get { return AllowRemove; }
    }

    bool IBindingList.SupportsChangeNotification {
      get { return SupportsChangeNotificationCore; }
    }

    bool IBindingList.SupportsSearching {
      get { return SupportsSearchingCore; }
    }

    bool IBindingList.SupportsSorting {
      get { return SupportsSortingCore; }
    }

    bool IRaiseItemChangedEvents.RaisesItemChangedEvents {
      get { return type_raises_item_changed_events; }
    }
  }

}

#endif
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.