Click here to Skip to main content
15,881,455 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.4K   7.6K   110  
Docking windows container, extended listview, extended property editor.
 using System;
using System.Collections;
using System.ComponentModel;
using System.Reflection;
using dwf.tool;
using Gtk;
using Gdk;

namespace dwf.gui
{
	public class OptionEditor : Gtk.VBox, ILocalizable
	{
		private object data;
		private PListGetEditorDelegate _handle;
        private PList tree = new PList();
        private HPaned hpaned = new HPaned();
        private Toolbar tools = new Toolbar();
        private ToolButton label = new ToolButton(null, "label");
        private VPaned vpaned = new VPaned();
        private FieldsEditor fields = new FieldsEditor();
        private ListEditor list = new ListEditor();

		public OptionEditor () : base()
		{			
			this.hpaned.Name = "split";
			this.hpaned.Pack1 (this.tree, false, true);
			this.hpaned.Pack2 (this.vpaned, true, true);
			this.hpaned.Position = 200;
				
			this.tree.Name = "tree";
			this.tree.Mode = PListMode.Tree;
			this.tree.SelectionChanged += TreeOnAfterSelect;
			
			this.vpaned.Name = "split2";
			this.vpaned.Pack2 (this.fields, true, true);			
			this.vpaned.Pack1 (this.list, true, true);
			this.vpaned.Position = 269;
			
			this.list.DataSource = null;
			this.list.Name = "list";
			this.list.ReadOnly = true;
			this.list.List.SelectionChanged += ListOnSelectionChanged;
			this.list.ItemSelect += new ListEditorItemSelectHandler (ListOnItemSelect);
			
			this.fields.Fields.HideEmpty = false;         
			this.fields.Fields.EditMode = EditModes.ByClick;
			this.fields.Fields.ReadOnly = false;
			this.fields.DataSource = null;
			this.fields.Name = "fields";            
			
			this.tools.ToolbarStyle = ToolbarStyle.Icons;
			this.tools.IconSize = IconSize.SmallToolbar;
			this.tools.IconSize = IconSize.Menu;
			//this.tools.Ha
			this.tools.Add( this.label);
			this.tools.Name = "tools";

			this.label.Name = "label";
			this.label.Label = " ";
			
			this.PackStart(tools, false, false, 0);
			this.PackStart(hpaned, true, true, 0);
			this.Name = "OptionEditor";
			this.ShowAll();
			
			Localizing ();	
			
		}

		public event PListGetEditorDelegate GetCellEditor {
			add {
				this._handle += value;
			}
			remove {
				this._handle -= value;
			}
		}

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

		public bool ViewTree {
			get { return !(hpaned.Position==0); }
			set {
				if(value)
					hpaned.Position = 0;
				else 
					hpaned.Position = 200;
			}
		}
		
		public object Value
		{
			get{ return data; }
			set{
				if(data == value)
					return;
				data = value;
				InitializeTree (data.ToString());
			}
		}
		
		public Node InitSetting (Node iter, object baseSet, PropertyInfo field)
		{
			Node tn = InitNode (iter, Localize.Get(field.DeclaringType.FullName, field.Name), baseSet);
			return tn;
		}

		private void InitializeTree (string Name)
		{
			tree.Nodes.Clear();
			if (data == null) {
				fields.DataSource = null;
				return;
			}
           
			Node tn = new Node(Name,	Name, false, data);
			tree.Nodes.Add(tn);
			tree.SelectedItem = tn;
		}

		private Node InitNode (Node par, string text, object tag)
		{
			Node node = new Node("123");
			node.Header = text;
			node.Tag = tag;
			node.Group = par;
			//node
			return  node;
		}

		private void TreeOnAfterSelect (object sender, EventArgs e)
		{
			Node item = tree.SelectedItem as Node;
			if(item ==null)
				return;
			
			label.Label = item.Header;
			object tag = item.Tag;
			bool check = item.Check;
			if (tag is IList) {
				list.DataSource = (IList)tag;
				vpaned.Position =vpaned.Allocation.Height/2;
			} else {
				fields.DataSource = tag;			
				vpaned.Position = 0;
			}
			if (check)
				return;
			if (tag == null)
				return;
         
			object obj = tag;
         
			foreach (PropertyInfo fInf in obj.GetType ().GetProperties ()) {
				if (TypeService.GetBrowsable (fInf))
				if (TypeService.IsList (fInf.PropertyType)) {
					InitSetting (item, ReflectionAccessor.GetValue( fInf, obj, null), fInf);
				}
			}
			item.Check = true;
		}

		public bool ViewTool {
			get { return tools.Visible; }
			set { tools.Visible = value; }
		}

		private void ToolSaveOnClick (object sender, EventArgs e)
		{
			if (this.data is IFSerialize) {
				IFSerialize ifl = this.data as IFSerialize;
				ifl.SaveDirectory (Environment.CurrentDirectory);
			}
		}

		private void ListOnItemSelect (object sender, ListEditorItemSelectEventArg e)
		{
			e.Cancel = true;
			
			Node selected = tree.SelectedItem as Node;
			
			int count = tree.Nodes.Count;
			
			for (int i = 0; i< count; i ++)
			{	
				Node ch = tree.Nodes[i];
			
				if(ch.Tag == e.SelectedItem)
				{					
					tree.SelectedItem = ch;
					return;
				}
			}
			
			Node tn = InitNode(selected, e.SelectedItem.ToString (), e.SelectedItem);
			
			tree.SelectedItem = tn;
		}

		private void ListOnSelectionChanged (object sender, EventArgs e)
		{
			fields.DataSource = list.List.SelectedItem;
		}

        #region ILocalizable implementation
		public void Localizing ()
		{
			this.Name = Localize.Get ("OptionEditor", "Option Editor");
		}
        #endregion
		
		public override void Dispose ()
		{
			base.Dispose ();
		}
		
		//private TreeStore store;
    	
	}
 
}

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