SharpDisplay: Migrating to new robust client scheme.
2 using System.Collections.Generic;
5 using System.Windows.Forms;
6 using System.Reflection;
7 using System.ComponentModel;
8 using System.Drawing.Design;
10 namespace Aga.Controls.Tree.NodeControls
12 public class NodeComboBox : BaseTextControl
16 private int _editorWidth = 100;
18 public int EditorWidth
20 get { return _editorWidth; }
21 set { _editorWidth = value; }
24 private int _editorHeight = 100;
26 public int EditorHeight
28 get { return _editorHeight; }
29 set { _editorHeight = value; }
32 private List<object> _dropDownItems;
33 [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1002:DoNotExposeGenericLists")]
34 [Editor(typeof(StringCollectionEditor), typeof(UITypeEditor)), DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
35 public List<object> DropDownItems
37 get { return _dropDownItems; }
42 public event EventHandler<EditEventArgs> CreatingEditor;
46 _dropDownItems = new List<object>();
49 protected override Size CalculateEditorSize(EditorContext context)
51 if (Parent.UseColumns)
53 if (context.Editor is CheckedListBox)
54 return new Size(context.Bounds.Size.Width, EditorHeight);
56 return context.Bounds.Size;
60 if (context.Editor is CheckedListBox)
61 return new Size(EditorWidth, EditorHeight);
63 return new Size(EditorWidth, context.Bounds.Height);
67 protected override Control CreateEditor(TreeNodeAdv node)
70 object value = GetValue(node);
71 if (IsCheckedListBoxRequired(node))
72 c = CreateCheckedListBox(node);
74 c = CreateCombo(node);
75 OnCreatingEditor(new EditEventArgs(node, c));
79 protected override void DisposeEditor(Control editor)
83 protected virtual void OnCreatingEditor(EditEventArgs args)
85 if (CreatingEditor != null)
86 CreatingEditor(this, args);
89 protected virtual bool IsCheckedListBoxRequired(TreeNodeAdv node)
91 object value = GetValue(node);
94 Type t = value.GetType();
95 object[] arr = t.GetCustomAttributes(typeof(FlagsAttribute), false);
96 return (t.IsEnum && arr.Length == 1);
101 private Control CreateCombo(TreeNodeAdv node)
103 ComboBox comboBox = new ComboBox();
104 if (DropDownItems != null)
105 comboBox.Items.AddRange(DropDownItems.ToArray());
106 comboBox.SelectedItem = GetValue(node);
107 comboBox.DropDownStyle = ComboBoxStyle.DropDownList;
108 comboBox.DropDownClosed += new EventHandler(EditorDropDownClosed);
109 SetEditControlProperties(comboBox, node);
113 private Control CreateCheckedListBox(TreeNodeAdv node)
115 CheckedListBox listBox = new CheckedListBox();
116 listBox.CheckOnClick = true;
118 object value = GetValue(node);
119 Type enumType = GetEnumType(node);
120 foreach (object obj in Enum.GetValues(enumType))
122 object[] attributes = enumType.GetField(obj.ToString()).GetCustomAttributes(typeof(BrowsableAttribute), false);
123 if (attributes.Length == 0 || ((BrowsableAttribute)attributes[0]).Browsable)
124 listBox.Items.Add(obj, IsContain(value, obj));
127 SetEditControlProperties(listBox, node);
128 if (CreatingEditor != null)
129 CreatingEditor(this, new EditEventArgs(node, listBox));
133 protected virtual Type GetEnumType(TreeNodeAdv node)
135 object value = GetValue(node);
136 return value.GetType();
139 private bool IsContain(object value, object enumElement)
141 if (value == null || enumElement == null)
143 if (value.GetType().IsEnum)
146 int i2 = (int)enumElement;
147 return (i1 & i2) == i2;
151 var arr = value as object[];
152 foreach (object obj in arr)
153 if ((int)obj == (int)enumElement)
159 protected override string FormatLabel(object obj)
161 var arr = obj as object[];
164 StringBuilder sb = new StringBuilder();
165 foreach (object t in arr)
171 return sb.ToString();
174 return base.FormatLabel(obj);
177 void EditorDropDownClosed(object sender, EventArgs e)
182 public override void UpdateEditor(Control control)
184 if (control is ComboBox)
185 (control as ComboBox).DroppedDown = true;
188 protected override void DoApplyChanges(TreeNodeAdv node, Control editor)
190 var combo = editor as ComboBox;
193 if (combo.DropDownStyle == ComboBoxStyle.DropDown)
194 SetValue(node, combo.Text);
196 SetValue(node, combo.SelectedItem);
200 var listBox = editor as CheckedListBox;
201 Type type = GetEnumType(node);
205 foreach (object obj in listBox.CheckedItems)
207 object val = Enum.ToObject(type, res);
212 List<object> list = new List<object>();
213 foreach (object obj in listBox.CheckedItems)
215 SetValue(node, list.ToArray());
220 private bool IsFlags(Type type)
222 object[] atr = type.GetCustomAttributes(typeof(FlagsAttribute), false);
223 return atr.Length == 1;
226 public override void MouseUp(TreeNodeAdvMouseEventArgs args)
228 if (args.Node != null && args.Node.IsSelected) //Workaround of specific ComboBox control behavior