⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 typedobjectlistview.cs

📁 Linux 恢复盘制作工具 process调用busybox dd实现写*.img镜像
💻 CS
📖 第 1 页 / 共 2 页
字号:
/*
 * TypedObjectListView - A wrapper around an ObjectListView that provides type-safe delegates.
 *
 * Author: Phillip Piper
 * Date: 27/09/2008 9:15 AM
 *
 * Change log:
 * 2008-11-26   JPP  - Added tool tip getting methods
 * 2008-11-05   JPP  - Added CheckState handling methods
 * 2008-10-24   JPP  - Generate dynamic methods MkII. This one handles value types
 * 2008-10-21   JPP  - Generate dynamic methods
 * 2008-09-27   JPP  - Separated from ObjectListView.cs
 * 
 * Copyright (C) 2006-2008 Phillip Piper
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * If you wish to use this code in a closed source application, please contact phillip_piper@bigfoot.com.
 */

using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
using System.Reflection.Emit;

namespace BrightIdeasSoftware
{
    /// <summary>
    /// A TypedObjectListView is a type-safe wrapper around an ObjectListView.
    /// </summary>
    /// <remarks>
    /// <para>VCS does not support generics on controls. It can be faked to some degree, but it
    /// cannot be completely overcome. In our case in particular, there is no way to create
    /// the custom OLVColumn's that we need to truly be generic. So this wrapper is an 
    /// experiment in providing some type-safe access in a way that is useful and available today.</para>
    /// <para>A TypedObjectListView is not more efficient than a normal ObjectListView.
    /// Underneath, the same name of casts are performed. But it is easier to use since you
    /// do not have to write the casts yourself.
    /// </para>
    /// </remarks>
    /// <typeparam name="T">The class of model object that the list will manage</typeparam>
    /// <example>
    /// To use a TypedObjectListView, you write code like this:
    /// <code>
    /// TypedObjectListView<Person> tlist = new TypedObjectListView<Person>(this.listView1);
    /// tlist.CheckStateGetter = delegate(Person x) { return x.IsActive; };
    /// tlist.GetColumn(0).AspectGetter = delegate(Person x) { return x.Name; };
    /// ...
    /// </code>
    /// To iterate over the selected objects, you can write something elegant like this:
    /// <code>
    /// foreach (Person x in tlist.SelectedObjects {
    ///     x.GrantSalaryIncrease();
    /// }
    /// </code>
    /// </example>
    public class TypedObjectListView<T> where T : class
    {
        /// <summary>
        /// Create a typed wrapper around the given list.
        /// </summary>
        /// <param name="olv">The listview to be wrapped</param>
        public TypedObjectListView(ObjectListView olv)
        {
            this.olv = olv;
        }

        //--------------------------------------------------------------------------------------
        // Properties

        /// <summary>
        /// Return the model object that is checked, if only one row is checked.
        /// If zero rows are checked, or more than one row, null is returned.
        /// </summary>
        public virtual T CheckedObject
        {
            get { return (T)this.olv.CheckedObject; }
        }

        /// <summary>
        /// Return the list of all the checked model objects
        /// </summary>
        public virtual IList<T> CheckedObjects
        {
            get {
                IList checkedObjects = this.olv.CheckedObjects;
                List<T> objects = new List<T>(checkedObjects.Count);
                foreach (object x in checkedObjects)
                    objects.Add((T)x);

                return objects;
            }
            set { this.olv.CheckedObjects = (IList)value; }
        }

        /// <summary>
        /// The ObjectListView that is being wrapped
        /// </summary>
        public virtual ObjectListView ListView
        {
            get { return olv; }
            set { olv = value; }
        }
        private ObjectListView olv;

        /// <summary>
        /// Return the model object that is selected, if only one row is selected.
        /// If zero rows are selected, or more than one row, null is returned.
        /// </summary>
        public virtual T SelectedObject
        {
            get { return (T)this.olv.GetSelectedObject(); }
            set { this.olv.SelectObject(value, true); }
        }

        /// <summary>
        /// The list of model objects that are selected.
        /// </summary>
        public virtual IList<T> SelectedObjects
        {
            get {
                List<T> objects = new List<T>(this.olv.SelectedIndices.Count);
                foreach (int index in this.olv.SelectedIndices)
                    objects.Add((T)this.olv.GetModelObject(index));

                return objects;
            }
            set { this.olv.SelectObjects((IList)value); }
        }

        //--------------------------------------------------------------------------------------
        // Accessors

        /// <summary>
        /// Return a typed wrapper around the column at the given index
        /// </summary>
        /// <param name="i">The index of the column</param>
        /// <returns>A typed column or null</returns>
        public virtual TypedColumn<T> GetColumn(int i)
        {
            return new TypedColumn<T>(this.olv.GetColumn(i));
        }

        /// <summary>
        /// Return a typed wrapper around the column with the given name
        /// </summary>
        /// <param name="i">The name of the column</param>
        /// <returns>A typed column or null</returns>
        public virtual TypedColumn<T> GetColumn(string name)
        {
            return new TypedColumn<T>(this.olv.GetColumn(name));
        }

        /// <summary>
        /// Return the model object at the given index
        /// </summary>
        /// <param name="index">The index of the model object</param>
        /// <returns>The model object or null</returns>
        public virtual T GetModelObject(int index)
        {
            return (T)this.olv.GetModelObject(index);
        }

        //--------------------------------------------------------------------------------------
        // Delegates

        public delegate CheckState TypedCheckStateGetterDelegate(T rowObject);

        public virtual TypedCheckStateGetterDelegate CheckStateGetter
        {
            get { return checkStateGetter; }
            set {
                this.checkStateGetter = value;
                if (value == null)
                    this.olv.CheckStateGetter = null;
                else
                    this.olv.CheckStateGetter = delegate(object x) {
                        return this.checkStateGetter((T)x);
                    };
            }
        }
        private TypedCheckStateGetterDelegate checkStateGetter;

        public delegate bool TypedBooleanCheckStateGetterDelegate(T rowObject);

        public virtual TypedBooleanCheckStateGetterDelegate BooleanCheckStateGetter
        {
            set {
                if (value == null)
                    this.olv.BooleanCheckStateGetter = null;
                else
                    this.olv.BooleanCheckStateGetter = delegate(object x) {
                        return value((T)x);
                    };
            }
        }

        public delegate CheckState TypedCheckStatePutterDelegate(T rowObject, CheckState newValue);

        public virtual TypedCheckStatePutterDelegate CheckStatePutter
        {
            get { return checkStatePutter; }
            set {
                this.checkStatePutter = value;
                if (value == null)
                    this.olv.CheckStatePutter = null;
                else
                    this.olv.CheckStatePutter = delegate(object x, CheckState newValue) {
                        return this.checkStatePutter((T)x, newValue);
                    };
            }
        }
        private TypedCheckStatePutterDelegate checkStatePutter;

        public delegate bool TypedBooleanCheckStatePutterDelegate(T rowObject, bool newValue);

        public virtual TypedBooleanCheckStatePutterDelegate BooleanCheckStatePutter
        {
            set {
                if (value == null)
                    this.olv.BooleanCheckStatePutter = null;
                else
                    this.olv.BooleanCheckStatePutter = delegate(object x, bool newValue) {
                        return value((T)x, newValue);
                    };
            }
        }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -