External/Aga.Controls/Tree/NodeControls/NodeComboBox.cs
changeset 345 0c551e8818e0
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/External/Aga.Controls/Tree/NodeControls/NodeComboBox.cs	Sun May 27 15:16:19 2012 +0000
     1.3 @@ -0,0 +1,232 @@
     1.4 +using System;
     1.5 +using System.Collections.Generic;
     1.6 +using System.Text;
     1.7 +using System.Drawing;
     1.8 +using System.Windows.Forms;
     1.9 +using System.Reflection;
    1.10 +using System.ComponentModel;
    1.11 +using System.Drawing.Design;
    1.12 +
    1.13 +namespace Aga.Controls.Tree.NodeControls
    1.14 +{
    1.15 +	public class NodeComboBox : BaseTextControl
    1.16 +	{
    1.17 +		#region Properties
    1.18 +
    1.19 +		private int _editorWidth = 100;
    1.20 +		[DefaultValue(100)]
    1.21 +		public int EditorWidth
    1.22 +		{
    1.23 +			get { return _editorWidth; }
    1.24 +			set { _editorWidth = value; }
    1.25 +		}
    1.26 +
    1.27 +		private int _editorHeight = 100;
    1.28 +		[DefaultValue(100)]
    1.29 +		public int EditorHeight
    1.30 +		{
    1.31 +			get { return _editorHeight; }
    1.32 +			set { _editorHeight = value; }
    1.33 +		}
    1.34 +
    1.35 +		private List<object> _dropDownItems;
    1.36 +		[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1002:DoNotExposeGenericLists")]
    1.37 +		[Editor(typeof(StringCollectionEditor), typeof(UITypeEditor)), DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
    1.38 +		public List<object> DropDownItems
    1.39 +		{
    1.40 +			get { return _dropDownItems; }
    1.41 +		}
    1.42 +
    1.43 +		#endregion
    1.44 +
    1.45 +		public event EventHandler<EditEventArgs> CreatingEditor;
    1.46 +
    1.47 +		public NodeComboBox()
    1.48 +		{
    1.49 +			_dropDownItems = new List<object>();
    1.50 +		}
    1.51 +
    1.52 +		protected override Size CalculateEditorSize(EditorContext context)
    1.53 +		{
    1.54 +			if (Parent.UseColumns)
    1.55 +			{
    1.56 +				if (context.Editor is CheckedListBox)
    1.57 +					return new Size(context.Bounds.Size.Width, EditorHeight);
    1.58 +				else
    1.59 +					return context.Bounds.Size;
    1.60 +			}
    1.61 +			else
    1.62 +			{
    1.63 +				if (context.Editor is CheckedListBox)
    1.64 +					return new Size(EditorWidth, EditorHeight);
    1.65 +				else
    1.66 +					return new Size(EditorWidth, context.Bounds.Height);
    1.67 +			}
    1.68 +		}
    1.69 +
    1.70 +		protected override Control CreateEditor(TreeNodeAdv node)
    1.71 +		{
    1.72 +			Control c;
    1.73 +			object value = GetValue(node);
    1.74 +			if (IsCheckedListBoxRequired(node))
    1.75 +				c = CreateCheckedListBox(node);
    1.76 +			else
    1.77 +				c = CreateCombo(node);
    1.78 +			OnCreatingEditor(new EditEventArgs(node, c));
    1.79 +			return c;
    1.80 +		}
    1.81 +
    1.82 +		protected override void DisposeEditor(Control editor)
    1.83 +		{
    1.84 +		}
    1.85 +
    1.86 +		protected virtual void OnCreatingEditor(EditEventArgs args)
    1.87 +		{
    1.88 +			if (CreatingEditor != null)
    1.89 +				CreatingEditor(this, args);
    1.90 +		}
    1.91 +
    1.92 +		protected virtual bool IsCheckedListBoxRequired(TreeNodeAdv node)
    1.93 +		{
    1.94 +			object value = GetValue(node);
    1.95 +			if (value != null)
    1.96 +			{
    1.97 +				Type t = value.GetType();
    1.98 +				object[] arr = t.GetCustomAttributes(typeof(FlagsAttribute), false);
    1.99 +				return (t.IsEnum && arr.Length == 1);
   1.100 +			}
   1.101 +			return false;
   1.102 +		}
   1.103 +
   1.104 +		private Control CreateCombo(TreeNodeAdv node)
   1.105 +		{
   1.106 +			ComboBox comboBox = new ComboBox();
   1.107 +			if (DropDownItems != null)
   1.108 +				comboBox.Items.AddRange(DropDownItems.ToArray());
   1.109 +			comboBox.SelectedItem = GetValue(node);
   1.110 +			comboBox.DropDownStyle = ComboBoxStyle.DropDownList;
   1.111 +			comboBox.DropDownClosed += new EventHandler(EditorDropDownClosed);
   1.112 +			SetEditControlProperties(comboBox, node);
   1.113 +			return comboBox;
   1.114 +		}
   1.115 +
   1.116 +		private Control CreateCheckedListBox(TreeNodeAdv node)
   1.117 +		{
   1.118 +			CheckedListBox listBox = new CheckedListBox();
   1.119 +			listBox.CheckOnClick = true;
   1.120 +
   1.121 +			object value = GetValue(node);
   1.122 +			Type enumType = GetEnumType(node);
   1.123 +			foreach (object obj in Enum.GetValues(enumType))
   1.124 +			{
   1.125 +				object[] attributes = enumType.GetField(obj.ToString()).GetCustomAttributes(typeof(BrowsableAttribute), false);
   1.126 +				if (attributes.Length == 0 || ((BrowsableAttribute)attributes[0]).Browsable)
   1.127 +					listBox.Items.Add(obj, IsContain(value, obj));
   1.128 +			}
   1.129 +
   1.130 +			SetEditControlProperties(listBox, node);
   1.131 +			if (CreatingEditor != null)
   1.132 +				CreatingEditor(this, new EditEventArgs(node, listBox));
   1.133 +			return listBox;
   1.134 +		}
   1.135 +
   1.136 +		protected virtual Type GetEnumType(TreeNodeAdv node)
   1.137 +		{
   1.138 +			object value = GetValue(node);
   1.139 +			return value.GetType();
   1.140 +		}
   1.141 +
   1.142 +		private bool IsContain(object value, object enumElement)
   1.143 +		{
   1.144 +			if (value == null || enumElement == null)
   1.145 +				return false;
   1.146 +			if (value.GetType().IsEnum)
   1.147 +			{
   1.148 +				int i1 = (int)value;
   1.149 +				int i2 = (int)enumElement;
   1.150 +				return (i1 & i2) == i2;
   1.151 +			}
   1.152 +			else
   1.153 +			{
   1.154 +				var arr = value as object[];
   1.155 +				foreach (object obj in arr)
   1.156 +					if ((int)obj == (int)enumElement)
   1.157 +						return true;
   1.158 +				return false;
   1.159 +			}
   1.160 +		}
   1.161 +
   1.162 +		protected override string FormatLabel(object obj)
   1.163 +		{
   1.164 +			var arr = obj as object[];
   1.165 +			if (arr != null)
   1.166 +			{
   1.167 +				StringBuilder sb = new StringBuilder();
   1.168 +				foreach (object t in arr)
   1.169 +				{
   1.170 +					if (sb.Length > 0)
   1.171 +						sb.Append(", ");
   1.172 +					sb.Append(t);
   1.173 +				}
   1.174 +				return sb.ToString();
   1.175 +			}
   1.176 +			else
   1.177 +				return base.FormatLabel(obj);
   1.178 +		}
   1.179 +
   1.180 +		void EditorDropDownClosed(object sender, EventArgs e)
   1.181 +		{
   1.182 +			EndEdit(true);
   1.183 +		}
   1.184 +
   1.185 +		public override void UpdateEditor(Control control)
   1.186 +		{
   1.187 +			if (control is ComboBox)
   1.188 +				(control as ComboBox).DroppedDown = true;
   1.189 +		}
   1.190 +
   1.191 +		protected override void DoApplyChanges(TreeNodeAdv node, Control editor)
   1.192 +		{
   1.193 +			var combo = editor as ComboBox;
   1.194 +			if (combo != null)
   1.195 +			{
   1.196 +				if (combo.DropDownStyle == ComboBoxStyle.DropDown)
   1.197 +					SetValue(node, combo.Text);
   1.198 +				else
   1.199 +					SetValue(node, combo.SelectedItem);
   1.200 +			}
   1.201 +			else
   1.202 +			{
   1.203 +				var listBox = editor as CheckedListBox;
   1.204 +				Type type = GetEnumType(node);
   1.205 +				if (IsFlags(type))
   1.206 +				{
   1.207 +					int res = 0;
   1.208 +					foreach (object obj in listBox.CheckedItems)
   1.209 +						res |= (int)obj;
   1.210 +					object val = Enum.ToObject(type, res);
   1.211 +					SetValue(node, val);
   1.212 +				}
   1.213 +				else
   1.214 +				{
   1.215 +					List<object> list = new List<object>();
   1.216 +					foreach (object obj in listBox.CheckedItems)
   1.217 +						list.Add(obj);
   1.218 +					SetValue(node, list.ToArray());
   1.219 +				}
   1.220 +			}
   1.221 +		}
   1.222 +
   1.223 +		private bool IsFlags(Type type)
   1.224 +		{
   1.225 +			object[] atr = type.GetCustomAttributes(typeof(FlagsAttribute), false);
   1.226 +			return atr.Length == 1;
   1.227 +		}
   1.228 +
   1.229 +		public override void MouseUp(TreeNodeAdvMouseEventArgs args)
   1.230 +		{
   1.231 +			if (args.Node != null && args.Node.IsSelected) //Workaround of specific ComboBox control behavior
   1.232 +				base.MouseUp(args);
   1.233 +		}
   1.234 +	}
   1.235 +}