Click here to Skip to main content
15,885,757 members
Articles / Desktop Programming / Windows Forms

Controls Library: Extended ListView with Column Mapping

Rate me:
Please Sign up or sign in to vote.
4.86/5 (29 votes)
27 Mar 2014CPOL5 min read 82.6K   7.6K   110  
Docking windows container, extended listview, extended property editor.
using dwf.tool;
using Gdk;
using Gtk;
using System;
using System.Collections;
using System.ComponentModel;

namespace dwf.gui
{
    public class ListEditor : Gtk.VBox, IDockContent
    {
        private IList _source;
        private Toolbar tools = new Toolbar ();
        private TagToolButton toolAdd = new TagToolButton ();
        private TagToolButton toolRemove = new TagToolButton ();
        private TagToolButton toolEdit = new TagToolButton ();
        private ToggleToolButton toolTree = new ToggleToolButton ();
        private ToolWindow addToolForm = new ToolWindow ();
        private ListEditorItemSelectHandler _handleItemSelect;
        private ListEditorItemSelectEventArg _cacheArg = new ListEditorItemSelectEventArg ();
        private PListGetEditorDelegate _handleGetCellEditor;
        private PListGetEditorDelegate _cacheHandleGetEditor;
        private PList list = new PList ();
        private PList fields = new PList ();
            
        public ListEditor ()
        {
            this.PackStart (tools, false, false, 0);
            this.PackStart (list, true, true, 0);
            
            //this.tools.IconSize = IconSize.SmallToolbar;
            this.tools.ToolbarStyle = ToolbarStyle.Icons;
            this.tools.Visible = true;
            this.tools.Add (this.toolAdd); 
            this.tools.Add (this.toolRemove); 
            this.tools.Add (this.toolEdit);
            this.tools.Add (new SeparatorToolItem ());
            this.tools.Add (this.toolTree);         
            this.tools.Name = "tools";
 
            this.list.GenerateColumns = true;
            this.list.Visible = true;
            this.list.EditMode = EditModes.ByF2;
            this.list.Name = "list";
            this.list.ModifyBg (StateType.Normal, new Color (255, 255, 255));
            this.list.SelectionChanged += ListOnSelectionChanged;
            this.list.CellDoubleClick += ListOnCellDoubleClick;
            
            this.toolAdd.Name = "toolAdd";
            this.toolAdd.Clicked += new System.EventHandler (this.ToolAddOnClick);

            this.toolRemove.Name = "toolRemove";
            this.toolRemove.Clicked += new System.EventHandler (this.ToolRemoveOnClick);
            
            this.toolEdit.Name = "toolEdit";
            this.toolEdit.Clicked += new System.EventHandler (this.ToolEditOnClick);
            
            this.toolTree.Name = "toolTree";
            this.toolTree.IsImportant = true;
            this.toolTree.Clicked += new System.EventHandler (this.ToolTreeOnClick);         
            
            this.Name = "ListEditor";
            this.Localizing ();

            this.ShowAll ();
         
            this.addToolForm.Target = fields;
            this.addToolForm.Mode = ToolShowMode.Dialog;
            this.addToolForm.ButtonAccept.Clicked += new EventHandler (AddToolFormOnAcceptClick);
            
            this._cacheHandleGetEditor = new PListGetEditorDelegate (ListOnGetCellEditor); 
            this.list.RetriveCellEditor += _cacheHandleGetEditor;

            this.fields.EditMode = EditModes.ByClick;
            this.fields.RetriveCellEditor += _cacheHandleGetEditor;
        }

        public object SelectedItem
        {
            get { return this.list.SelectedItem; }
            set { this.list.SelectedItem = value; }
        }
        
        public void Localizing ()
        {
            string name = "ListEditor";
            this.toolTree.Label = Localize.Get (name, "Tree");
            this.toolTree.TooltipText = Localize.Get (name, "Tree");
            this.toolTree.IconWidget = new Gtk.Image (GuiTool.GetImage (Localize.GetImageKey (name, "Tree")));

            this.toolAdd.Text = Localize.Get (name, "Add");
            this.toolAdd.TooltipText = Localize.Get (name, "Add");
            this.toolAdd.Image = GuiTool.GetImage (Localize.GetImageKey (name, "Add"));

            this.toolRemove.Text = Localize.Get (name, "Delete");
            this.toolRemove.TooltipText = Localize.Get (name, "Delete");
            this.toolRemove.Image = GuiTool.GetImage (Localize.GetImageKey (name, "Delete"));

            this.toolEdit.Text = Localize.Get (name, "Edit");
            this.toolEdit.TooltipText = Localize.Get (name, "Edit");
            this.toolEdit.Image = GuiTool.GetImage (Localize.GetImageKey (name, "Edit"));
        }
        
        public Toolbar Tools
        {
            get{ return tools; }
        }
        
        public PList List
        {
            get{ return list;}
        }

        public event ListEditorItemSelectHandler ItemSelect
        {
            add{ _handleItemSelect += value;}
            remove{ _handleItemSelect -= value;}
        }
         
        public event PListGetEditorDelegate GetCellEditor
        {
            add{ _handleGetCellEditor += value;}
            remove{ _handleGetCellEditor -= value;}
        }

        private IPCellEditor ListOnGetCellEditor (object sender, object listItem, object itemValue, IPCell cell)
        {
            if (_handleGetCellEditor != null)
                return _handleGetCellEditor (sender, listItem, itemValue, cell);
            else 
                return null;
        }

        public bool ReadOnly
        {
            get { return list.ReadOnly; }
            set { list.ReadOnly = value; }
        }

        public IList DataSource
        {
            get { return _source; }
            set
            {
                _source = value;
                Type type = null;
                if (_source is IListQuery)
                {
                    type = typeof(dwf.tool.ListExtendView<>);
                    Type typeItem = type.MakeGenericType (((IListQuery)_source).ItemType);
                    list.ListSource = (IList)ReflectionAccessor.CreateObject (typeItem, new Type[] { typeof(IList) }, new object[] { _source }, true);
                }
                else
                    list.ListSource = _source;
            }
        }

        public override void Dispose ()
        {
            base.Dispose ();
            addToolForm.Dispose ();
            fields.Dispose ();
        }

        private void AddToolFormOnAcceptClick (object sender, EventArgs e)
        {
            if (list.ListSource == null)
                return;
            list.ItemAdd (fields.FieldSource);
            addToolForm.Hide ();
        }

        private void ToolRemoveOnClick (object sender, EventArgs e)
        {
            if (list.Selection.Count == 0)
                return;
            for (int i = 0; i < list.Selection.Count; i++)
            {
                object o = list.Selection[i].Item;
                list.ItemRemove (o);
                if (list.ListSource != _source)
                    ((IList)_source).Remove (o);
                i--;
            }
        }

        private void ToolAddOnClick (object sender, EventArgs e)
        {
            if (list.ListSource == null)
                return;
            object newObject = list.ItemNew ();
            fields.EditState = EditListState.EditAny;
            fields.FieldSource = newObject;
            addToolForm.ButtonAccept.Sensitive = true;
            addToolForm.Lable.Text = toolAdd.Label;         
            addToolForm.Show (tools, new Point (0, tools.Allocation.Height));         
        }

        private void ToolEditOnClick (object sender, EventArgs e)
        {
            this.fields.EditState = EditListState.Edit;
            OnItemSelect (_cacheArg);
        }

        private void ToolTreeOnClick (object sender, EventArgs e)
        {
            list.OnTreeMode (toolTree.Active);
        }

        public void OnItemSelect (ListEditorItemSelectEventArg ea)
        {
            if (list.SelectedItem == null)
                return;
            ea.Cancel = false;
            ea.SelectedItem = list.SelectedItem;
            if (_handleItemSelect != null)
            {
                _handleItemSelect (this, ea);
            }
            if (ea.Cancel)
                return;
            addToolForm.ButtonAccept.Sensitive = false;
            addToolForm.Lable.Text = toolEdit.Label;
         
            fields.FieldSource = list.SelectedItem;
            addToolForm.Show (tools, new Point (0, toolAdd.Allocation.Height));
        }

        private void ListOnCellDoubleClick (object sender, PListHitTestEventArgs e)
        {
            OnItemSelect (_cacheArg);
        }

        private void ListOnSelectionChanged (object sender, EventArgs e)
        {
            if (GuiService.Main != null)
                GuiService.Main.ShowProperty (this, list.SelectedItem, true); 
        }

        #region IDockModule implementation
    
        public bool HideOnClose
        {
            get { return false; }
        }

        public DockType DockType
        {
            get { return DockType.Content; }
        }

        public System.Drawing.Image Image
        {
            get { return Localize.GetImage ("ListEditor", "List Editor");   }
        }
        #endregion

    }

    public delegate void ListEditorItemSelectHandler (object sender, ListEditorItemSelectEventArg arg);

    public class ListEditorItemSelectEventArg : CancelEventArgs
    {
        object selectedItem;
     
        public ListEditorItemSelectEventArg () : base()
        {
        }

        public object SelectedItem
        { 
            get { return selectedItem; } 
            set { selectedItem = value; }
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Kazakstan Kazakstan
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions