[2655] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[17097] | 3 | * Copyright (C) 2002-2019 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[2655] | 4 | *
|
---|
| 5 | * This file is part of HeuristicLab.
|
---|
| 6 | *
|
---|
| 7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
| 8 | * it under the terms of the GNU General Public License as published by
|
---|
| 9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
| 10 | * (at your option) any later version.
|
---|
| 11 | *
|
---|
| 12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 15 | * GNU General Public License for more details.
|
---|
| 16 | *
|
---|
| 17 | * You should have received a copy of the GNU General Public License
|
---|
| 18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
| 19 | */
|
---|
| 20 | #endregion
|
---|
| 21 |
|
---|
| 22 | using System;
|
---|
[4068] | 23 | using System.Collections.Generic;
|
---|
[12708] | 24 | using System.Drawing;
|
---|
[2818] | 25 | using System.Linq;
|
---|
[5848] | 26 | using System.Reflection;
|
---|
[2655] | 27 | using System.Text;
|
---|
| 28 | using System.Windows.Forms;
|
---|
[2739] | 29 | using HeuristicLab.Common;
|
---|
[2655] | 30 | using HeuristicLab.PluginInfrastructure;
|
---|
| 31 |
|
---|
[4068] | 32 | namespace HeuristicLab.Core.Views {
|
---|
[2655] | 33 | public partial class TypeSelector : UserControl {
|
---|
[2952] | 34 | protected List<TreeNode> treeNodes;
|
---|
| 35 | protected string currentSearchString;
|
---|
[3588] | 36 | protected TypeSelectorDialog typeSelectorDialog;
|
---|
[2952] | 37 |
|
---|
[5850] | 38 | protected IEnumerable<Type> baseTypes;
|
---|
| 39 | public IEnumerable<Type> BaseTypes {
|
---|
| 40 | get { return baseTypes; }
|
---|
[2655] | 41 | }
|
---|
[2676] | 42 | protected bool showNotInstantiableTypes;
|
---|
[2655] | 43 | public bool ShowNotInstantiableTypes {
|
---|
| 44 | get { return showNotInstantiableTypes; }
|
---|
| 45 | }
|
---|
[2676] | 46 | protected bool showGenericTypes;
|
---|
[2655] | 47 | public bool ShowGenericTypes {
|
---|
| 48 | get { return showGenericTypes; }
|
---|
| 49 | }
|
---|
| 50 | public string Caption {
|
---|
[2676] | 51 | get { return typesGroupBox.Text; }
|
---|
[2655] | 52 | set {
|
---|
| 53 | if (InvokeRequired)
|
---|
| 54 | Invoke(new Action<string>(delegate(string s) { Caption = s; }), value);
|
---|
| 55 | else
|
---|
[2676] | 56 | typesGroupBox.Text = value;
|
---|
[2655] | 57 | }
|
---|
| 58 | }
|
---|
| 59 | public TreeView TypesTreeView {
|
---|
| 60 | get { return typesTreeView; }
|
---|
| 61 | }
|
---|
[2676] | 62 | protected Type selectedType;
|
---|
[2655] | 63 | public Type SelectedType {
|
---|
| 64 | get { return selectedType; }
|
---|
| 65 | private set {
|
---|
| 66 | if (value != selectedType) {
|
---|
| 67 | selectedType = value;
|
---|
| 68 | OnSelectedTypeChanged();
|
---|
| 69 | }
|
---|
| 70 | }
|
---|
| 71 | }
|
---|
| 72 |
|
---|
| 73 | public TypeSelector() {
|
---|
| 74 | InitializeComponent();
|
---|
[2952] | 75 | treeNodes = new List<TreeNode>();
|
---|
| 76 | currentSearchString = string.Empty;
|
---|
[2655] | 77 | selectedType = null;
|
---|
| 78 | }
|
---|
| 79 |
|
---|
[5237] | 80 | protected override void Dispose(bool disposing) {
|
---|
| 81 | if (disposing) {
|
---|
| 82 | if (typeSelectorDialog != null) typeSelectorDialog.Dispose();
|
---|
| 83 | if (components != null) components.Dispose();
|
---|
| 84 | }
|
---|
| 85 | base.Dispose(disposing);
|
---|
| 86 | }
|
---|
| 87 |
|
---|
[2676] | 88 | public virtual void Configure(Type baseType, bool showNotInstantiableTypes, bool showGenericTypes) {
|
---|
[6365] | 89 | Configure(baseType, showNotInstantiableTypes, showGenericTypes, (t) => true);
|
---|
[5850] | 90 | }
|
---|
| 91 |
|
---|
[6363] | 92 | public virtual void Configure(Type baseType, bool showNotInstantiableTypes, bool showGenericTypes, Func<Type, bool> typeCondition) {
|
---|
| 93 | Configure(new List<Type>() { baseType }, showNotInstantiableTypes, showGenericTypes, true, typeCondition);
|
---|
| 94 | }
|
---|
| 95 |
|
---|
[5903] | 96 | public virtual void Configure(IEnumerable<Type> baseTypes, bool showNotInstantiableTypes, bool showGenericTypes, bool assignableToAllTypes) {
|
---|
[6363] | 97 | Configure(baseTypes, showNotInstantiableTypes, showGenericTypes, assignableToAllTypes, (t) => { return true; });
|
---|
| 98 | }
|
---|
| 99 |
|
---|
| 100 | public virtual void Configure(IEnumerable<Type> baseTypes, bool showNotInstantiableTypes, bool showGenericTypes, bool assignableToAllTypes, Func<Type, bool> typeCondition) {
|
---|
[5850] | 101 | if (baseTypes == null) throw new ArgumentNullException();
|
---|
[2655] | 102 | if (InvokeRequired)
|
---|
[6363] | 103 | Invoke(new Action<IEnumerable<Type>, bool, bool, bool, Func<Type, bool>>(Configure), baseTypes, showNotInstantiableTypes, showGenericTypes, assignableToAllTypes, typeCondition);
|
---|
[2655] | 104 | else {
|
---|
[5850] | 105 | this.baseTypes = baseTypes;
|
---|
[2655] | 106 | this.showNotInstantiableTypes = showNotInstantiableTypes;
|
---|
| 107 | this.showGenericTypes = showGenericTypes;
|
---|
[6954] | 108 | bool selectedTypeFound = false;
|
---|
[2655] | 109 |
|
---|
[3588] | 110 | typeParametersSplitContainer.Panel2Collapsed = !showGenericTypes;
|
---|
| 111 |
|
---|
[2952] | 112 | TreeNode selectedNode = typesTreeView.SelectedNode;
|
---|
[2655] | 113 | typesTreeView.Nodes.Clear();
|
---|
[2952] | 114 | treeNodes.Clear();
|
---|
[10647] | 115 |
|
---|
[2655] | 116 | imageList.Images.Clear();
|
---|
[5287] | 117 | imageList.Images.Add(HeuristicLab.Common.Resources.VSImageLibrary.Class); // default icon
|
---|
| 118 | imageList.Images.Add(HeuristicLab.Common.Resources.VSImageLibrary.Namespace); // plugins
|
---|
| 119 | imageList.Images.Add(HeuristicLab.Common.Resources.VSImageLibrary.Interface); // interfaces
|
---|
| 120 | imageList.Images.Add(HeuristicLab.Common.Resources.VSImageLibrary.Template); // generic types
|
---|
[10647] | 121 | // additional dictionary for image indexes as imageList.ContainsKey and imageList.IndexOfKey are very slow!
|
---|
| 122 | var imageNames = new Dictionary<string, int>();
|
---|
[2655] | 123 |
|
---|
| 124 | var plugins = from p in ApplicationManager.Manager.Plugins
|
---|
| 125 | orderby p.Name, p.Version ascending
|
---|
| 126 | select p;
|
---|
| 127 | foreach (IPluginDescription plugin in plugins) {
|
---|
| 128 | TreeNode pluginNode = new TreeNode();
|
---|
[5898] | 129 | pluginNode.Text = string.Format("{0} {1}.{2}", plugin.Name, plugin.Version.Major, plugin.Version.Minor);
|
---|
[2655] | 130 | pluginNode.ImageIndex = 1;
|
---|
| 131 | pluginNode.SelectedImageIndex = pluginNode.ImageIndex;
|
---|
| 132 | pluginNode.Tag = plugin;
|
---|
| 133 |
|
---|
[7111] | 134 | var types = from t in ApplicationManager.Manager.GetTypes(BaseTypes, plugin, !ShowNotInstantiableTypes, ShowGenericTypes, assignableToAllTypes)
|
---|
[6363] | 135 | where typeCondition(t)
|
---|
[2655] | 136 | orderby t.Name ascending
|
---|
| 137 | select t;
|
---|
| 138 | foreach (Type type in types) {
|
---|
[7123] | 139 | bool valid = (ShowNotInstantiableTypes || type.GetConstructor(Type.EmptyTypes) != null); //check for public default ctor
|
---|
[2655] | 140 | if (valid) {
|
---|
| 141 | TreeNode typeNode = new TreeNode();
|
---|
| 142 | string name = ItemAttribute.GetName(type);
|
---|
[2739] | 143 | typeNode.Text = name != null ? name : type.GetPrettyName();
|
---|
[2655] | 144 | typeNode.ImageIndex = 0;
|
---|
| 145 | if (type.IsInterface) typeNode.ImageIndex = 2;
|
---|
[3528] | 146 | else if (type.ContainsGenericParameters) typeNode.ImageIndex = 3;
|
---|
[10647] | 147 | else if (imageNames.ContainsKey(type.FullName)) typeNode.ImageIndex = imageNames[type.FullName];
|
---|
[7201] | 148 | else {
|
---|
| 149 | var image = ItemAttribute.GetImage(type);
|
---|
| 150 | if (image != null) {
|
---|
[10647] | 151 | imageList.Images.Add(image);
|
---|
| 152 | typeNode.ImageIndex = imageList.Images.Count - 1;
|
---|
| 153 | imageNames.Add(type.FullName, imageList.Images.Count - 1);
|
---|
[7201] | 154 | }
|
---|
[2655] | 155 | }
|
---|
| 156 | typeNode.SelectedImageIndex = typeNode.ImageIndex;
|
---|
| 157 | typeNode.Tag = type;
|
---|
| 158 | pluginNode.Nodes.Add(typeNode);
|
---|
[6954] | 159 | if (type.Equals(selectedType)) selectedTypeFound = true;
|
---|
[2655] | 160 | }
|
---|
| 161 | }
|
---|
| 162 | if (pluginNode.Nodes.Count > 0)
|
---|
[2952] | 163 | treeNodes.Add(pluginNode);
|
---|
[2655] | 164 | }
|
---|
[6954] | 165 | if (!selectedTypeFound) SelectedType = null;
|
---|
[2952] | 166 | foreach (TreeNode node in treeNodes)
|
---|
| 167 | typesTreeView.Nodes.Add((TreeNode)node.Clone());
|
---|
| 168 | RestoreSelectedNode(selectedNode);
|
---|
| 169 | Filter(searchTextBox.Text);
|
---|
[3588] | 170 |
|
---|
| 171 | UpdateTypeParameters();
|
---|
[2952] | 172 | }
|
---|
| 173 | }
|
---|
| 174 |
|
---|
| 175 | public virtual void Filter(string searchString) {
|
---|
| 176 | if (InvokeRequired)
|
---|
| 177 | Invoke(new Action<string>(Filter), searchString);
|
---|
| 178 | else {
|
---|
| 179 | searchString = searchString.ToLower();
|
---|
[12708] | 180 | TreeNode selectedNode = typesTreeView.SelectedNode;
|
---|
[4826] | 181 |
|
---|
[2952] | 182 | if (!searchString.Contains(currentSearchString)) {
|
---|
[4826] | 183 | typesTreeView.BeginUpdate();
|
---|
[2952] | 184 | // expand search -> restore all tree nodes
|
---|
| 185 | typesTreeView.Nodes.Clear();
|
---|
| 186 | foreach (TreeNode node in treeNodes)
|
---|
| 187 | typesTreeView.Nodes.Add((TreeNode)node.Clone());
|
---|
[4826] | 188 | typesTreeView.EndUpdate();
|
---|
[2952] | 189 | }
|
---|
| 190 |
|
---|
| 191 | // remove nodes
|
---|
[4826] | 192 | typesTreeView.BeginUpdate();
|
---|
[12708] | 193 | var searchTokens = searchString.Split(' ');
|
---|
[2952] | 194 | int i = 0;
|
---|
| 195 | while (i < typesTreeView.Nodes.Count) {
|
---|
[12708] | 196 | var node = typesTreeView.Nodes[i];
|
---|
| 197 | bool remove = FilterNode(node, searchTokens);
|
---|
| 198 | if (remove)
|
---|
| 199 | typesTreeView.Nodes.RemoveAt(i);
|
---|
| 200 | else i++;
|
---|
[2952] | 201 | }
|
---|
[3516] | 202 | typesTreeView.EndUpdate();
|
---|
[2952] | 203 | currentSearchString = searchString;
|
---|
| 204 |
|
---|
[12708] | 205 | // select first item
|
---|
| 206 | typesTreeView.BeginUpdate();
|
---|
| 207 | var firstNode = typesTreeView.Nodes.Count > 0 ? typesTreeView.Nodes[0] : null;
|
---|
| 208 | while (firstNode != null && !(firstNode.Tag is Type))
|
---|
| 209 | firstNode = firstNode.NextVisibleNode;
|
---|
| 210 | if (firstNode != null)
|
---|
| 211 | typesTreeView.SelectedNode = firstNode;
|
---|
[2952] | 212 |
|
---|
[2655] | 213 | if (typesTreeView.Nodes.Count == 0) {
|
---|
[2952] | 214 | SelectedType = null;
|
---|
[2655] | 215 | typesTreeView.Enabled = false;
|
---|
| 216 | } else {
|
---|
[2952] | 217 | SetTreeNodeVisibility();
|
---|
| 218 | typesTreeView.Enabled = true;
|
---|
[2655] | 219 | }
|
---|
[12708] | 220 | typesTreeView.EndUpdate();
|
---|
| 221 |
|
---|
| 222 | RestoreSelectedNode(selectedNode);
|
---|
[2952] | 223 | UpdateDescription();
|
---|
[2655] | 224 | }
|
---|
| 225 | }
|
---|
| 226 |
|
---|
[12708] | 227 | private bool FilterNode(TreeNode node, string[] searchTokens) {
|
---|
| 228 | if (node.Tag is IPluginDescription) { // Plugin node
|
---|
| 229 | int i = 0;
|
---|
| 230 | while (i < node.Nodes.Count) {
|
---|
| 231 | bool remove = FilterNode(node.Nodes[i], searchTokens);
|
---|
| 232 | if (remove)
|
---|
| 233 | node.Nodes.RemoveAt(i);
|
---|
| 234 | else i++;
|
---|
| 235 | }
|
---|
| 236 | return node.Nodes.Count == 0;
|
---|
[15131] | 237 | }
|
---|
| 238 | if (node.Tag is Type) { // Type node
|
---|
[12708] | 239 | var text = node.Text;
|
---|
| 240 | if (searchTokens.Any(searchToken => !text.ToLower().Contains(searchToken))) {
|
---|
| 241 | var typeTag = (Type)node.Tag;
|
---|
| 242 | if (typeTag == SelectedType) {
|
---|
| 243 | SelectedType = null;
|
---|
| 244 | typesTreeView.SelectedNode = null;
|
---|
| 245 | }
|
---|
| 246 | return true;
|
---|
| 247 | }
|
---|
| 248 | return false;
|
---|
| 249 | }
|
---|
| 250 | throw new InvalidOperationException("Encountered neither a plugin nor a type node during tree traversal.");
|
---|
| 251 | }
|
---|
| 252 |
|
---|
[2676] | 253 | public virtual object CreateInstanceOfSelectedType(params object[] args) {
|
---|
[2999] | 254 | if (SelectedType == null)
|
---|
| 255 | throw new InvalidOperationException("No type selected.");
|
---|
| 256 | else
|
---|
[2655] | 257 | return Activator.CreateInstance(SelectedType, args);
|
---|
| 258 | }
|
---|
| 259 |
|
---|
[3588] | 260 | protected virtual void UpdateTypeParameters() {
|
---|
| 261 | typeParametersListView.Items.Clear();
|
---|
| 262 | if ((SelectedType == null) || !SelectedType.ContainsGenericParameters) {
|
---|
| 263 | typeParametersGroupBox.Enabled = false;
|
---|
| 264 | typeParametersSplitContainer.Panel2Collapsed = true;
|
---|
| 265 | } else {
|
---|
| 266 | typeParametersGroupBox.Enabled = true;
|
---|
| 267 | typeParametersSplitContainer.Panel2Collapsed = false;
|
---|
| 268 | setTypeParameterButton.Enabled = false;
|
---|
| 269 |
|
---|
| 270 | foreach (Type param in SelectedType.GetGenericArguments()) {
|
---|
| 271 | if (param.IsGenericParameter) {
|
---|
| 272 | ListViewItem item = new ListViewItem();
|
---|
| 273 | item.Text = param.Name;
|
---|
| 274 |
|
---|
| 275 | item.ToolTipText = "Constraints:";
|
---|
| 276 | Type[] constraints = param.GetGenericParameterConstraints();
|
---|
| 277 | if (constraints.Length == 0) {
|
---|
| 278 | item.ToolTipText += " none";
|
---|
| 279 | } else {
|
---|
| 280 | foreach (Type constraint in constraints)
|
---|
| 281 | item.ToolTipText += " " + constraint.GetPrettyName();
|
---|
| 282 | }
|
---|
| 283 |
|
---|
| 284 | item.Tag = param;
|
---|
| 285 | typeParametersListView.Items.Add(item);
|
---|
| 286 | }
|
---|
| 287 | }
|
---|
| 288 | typeParametersListView.Columns[0].AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent);
|
---|
| 289 | }
|
---|
[2655] | 290 | }
|
---|
| 291 |
|
---|
[3588] | 292 | protected virtual void SetTypeParameter() {
|
---|
| 293 | if (typeSelectorDialog == null) {
|
---|
| 294 | typeSelectorDialog = new TypeSelectorDialog();
|
---|
| 295 | typeSelectorDialog.Caption = "Select Type of Generic Type Parameter";
|
---|
| 296 | }
|
---|
| 297 | Type param = typeParametersListView.SelectedItems[0].Tag as Type;
|
---|
[5850] | 298 | Type[] constraints = param.GetGenericParameterConstraints();
|
---|
[5848] | 299 | bool showNotInstantiableTypes = !param.GenericParameterAttributes.HasFlag(GenericParameterAttributes.DefaultConstructorConstraint);
|
---|
[5898] | 300 | typeSelectorDialog.TypeSelector.Configure(constraints, showNotInstantiableTypes, true, true);
|
---|
[3588] | 301 |
|
---|
| 302 | if (typeSelectorDialog.ShowDialog(this) == DialogResult.OK) {
|
---|
| 303 | Type selected = typeSelectorDialog.TypeSelector.SelectedType;
|
---|
| 304 | Type[] parameters = SelectedType.GetGenericArguments();
|
---|
| 305 | parameters[param.GenericParameterPosition] = selected;
|
---|
| 306 | SelectedType = SelectedType.GetGenericTypeDefinition().MakeGenericType(parameters);
|
---|
| 307 |
|
---|
| 308 | typeParametersListView.SelectedItems[0].Text = param.Name + ": " + selected.GetPrettyName();
|
---|
| 309 | typeParametersListView.Columns[0].AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent);
|
---|
| 310 | }
|
---|
| 311 | }
|
---|
| 312 |
|
---|
[2676] | 313 | protected virtual void UpdateDescription() {
|
---|
[2655] | 314 | descriptionTextBox.Text = string.Empty;
|
---|
| 315 |
|
---|
| 316 | if (typesTreeView.SelectedNode != null) {
|
---|
| 317 | IPluginDescription plugin = typesTreeView.SelectedNode.Tag as IPluginDescription;
|
---|
| 318 | if (plugin != null) {
|
---|
| 319 | StringBuilder sb = new StringBuilder();
|
---|
| 320 | sb.Append("Plugin: ").Append(plugin.Name).Append(Environment.NewLine);
|
---|
| 321 | sb.Append("Version: ").Append(plugin.Version.ToString()).Append(Environment.NewLine);
|
---|
| 322 | descriptionTextBox.Text = sb.ToString();
|
---|
| 323 | }
|
---|
| 324 | Type type = typesTreeView.SelectedNode.Tag as Type;
|
---|
| 325 | if (type != null) {
|
---|
| 326 | string description = ItemAttribute.GetDescription(type);
|
---|
| 327 | if (description != null)
|
---|
| 328 | descriptionTextBox.Text = description;
|
---|
| 329 | }
|
---|
[2952] | 330 | } else if (typesTreeView.Nodes.Count == 0) {
|
---|
| 331 | descriptionTextBox.Text = "No types found";
|
---|
[2655] | 332 | }
|
---|
| 333 | }
|
---|
| 334 |
|
---|
[3588] | 335 | #region Events
|
---|
| 336 | public event EventHandler SelectedTypeChanged;
|
---|
| 337 | protected virtual void OnSelectedTypeChanged() {
|
---|
| 338 | if (SelectedTypeChanged != null)
|
---|
| 339 | SelectedTypeChanged(this, EventArgs.Empty);
|
---|
| 340 | }
|
---|
| 341 | #endregion
|
---|
| 342 |
|
---|
| 343 | #region Control Events
|
---|
[2952] | 344 | protected virtual void searchTextBox_TextChanged(object sender, System.EventArgs e) {
|
---|
| 345 | Filter(searchTextBox.Text);
|
---|
| 346 | }
|
---|
| 347 |
|
---|
[2676] | 348 | protected virtual void typesTreeView_AfterSelect(object sender, TreeViewEventArgs e) {
|
---|
[2952] | 349 | if (typesTreeView.SelectedNode == null) SelectedType = null;
|
---|
| 350 | else SelectedType = typesTreeView.SelectedNode.Tag as Type;
|
---|
[3588] | 351 | UpdateTypeParameters();
|
---|
[2655] | 352 | UpdateDescription();
|
---|
| 353 | }
|
---|
[2676] | 354 | protected virtual void typesTreeView_ItemDrag(object sender, ItemDragEventArgs e) {
|
---|
[2655] | 355 | TreeNode node = (TreeNode)e.Item;
|
---|
| 356 | Type type = node.Tag as Type;
|
---|
[2999] | 357 | if ((type != null) && (!type.IsInterface) && (!type.IsAbstract) && (!type.HasElementType) && (!type.ContainsGenericParameters)) {
|
---|
| 358 | object o = Activator.CreateInstance(type);
|
---|
| 359 | DataObject data = new DataObject();
|
---|
[5837] | 360 | data.SetData(HeuristicLab.Common.Constants.DragDropDataFormat, o);
|
---|
[2999] | 361 | DoDragDrop(data, DragDropEffects.Copy);
|
---|
[2655] | 362 | }
|
---|
| 363 | }
|
---|
[2952] | 364 | protected virtual void typesTreeView_VisibleChanged(object sender, EventArgs e) {
|
---|
| 365 | if (Visible) SetTreeNodeVisibility();
|
---|
| 366 | }
|
---|
| 367 |
|
---|
[3588] | 368 | protected virtual void typeParametersListView_SelectedIndexChanged(object sender, EventArgs e) {
|
---|
| 369 | setTypeParameterButton.Enabled = typeParametersListView.SelectedItems.Count == 1;
|
---|
| 370 | }
|
---|
| 371 | protected virtual void typeParametersListView_DoubleClick(object sender, EventArgs e) {
|
---|
| 372 | if (typeParametersListView.SelectedItems.Count == 1)
|
---|
| 373 | SetTypeParameter();
|
---|
| 374 | }
|
---|
| 375 |
|
---|
| 376 | protected virtual void setTypeParameterButton_Click(object sender, EventArgs e) {
|
---|
| 377 | SetTypeParameter();
|
---|
| 378 | }
|
---|
[12708] | 379 | private void searchTextBox_KeyDown(object sender, KeyEventArgs e) {
|
---|
| 380 | if (typesTreeView.Nodes.Count == 0)
|
---|
| 381 | return;
|
---|
| 382 |
|
---|
| 383 | if (e.KeyCode == Keys.Up || e.KeyCode == Keys.Down) {
|
---|
| 384 | var selectedNode = typesTreeView.SelectedNode;
|
---|
| 385 |
|
---|
| 386 | if (selectedNode == null) { // nothing selected => select first
|
---|
| 387 | if (e.KeyCode == Keys.Down) typesTreeView.SelectedNode = typesTreeView.Nodes.Count > 0 ? typesTreeView.Nodes[0] : null;
|
---|
| 388 | } else {
|
---|
| 389 | if (e.KeyCode == Keys.Down && selectedNode.NextVisibleNode != null)
|
---|
| 390 | typesTreeView.SelectedNode = selectedNode.NextVisibleNode;
|
---|
| 391 | if (e.KeyCode == Keys.Up && selectedNode.PrevVisibleNode != null)
|
---|
| 392 | typesTreeView.SelectedNode = selectedNode.PrevVisibleNode;
|
---|
| 393 | }
|
---|
| 394 | e.Handled = true;
|
---|
| 395 | }
|
---|
| 396 | }
|
---|
| 397 | private void clearSearchButton_Click(object sender, EventArgs e) {
|
---|
| 398 | searchTextBox.Text = string.Empty;
|
---|
| 399 | searchTextBox.Focus();
|
---|
| 400 | }
|
---|
| 401 |
|
---|
| 402 | private TreeNode toolStripMenuNode = null;
|
---|
| 403 | private void typesTreeView_MouseDown(object sender, MouseEventArgs e) {
|
---|
| 404 | if (e.Button == MouseButtons.Right) {
|
---|
| 405 | Point coordinates = typesTreeView.PointToClient(Cursor.Position);
|
---|
| 406 | toolStripMenuNode = typesTreeView.GetNodeAt(coordinates);
|
---|
| 407 |
|
---|
| 408 | if (toolStripMenuNode != null && coordinates.X >= toolStripMenuNode.Bounds.Left &&
|
---|
| 409 | coordinates.X <= toolStripMenuNode.Bounds.Right) {
|
---|
| 410 | typesTreeView.SelectedNode = toolStripMenuNode;
|
---|
| 411 |
|
---|
| 412 | expandToolStripMenuItem.Enabled =
|
---|
| 413 | expandToolStripMenuItem.Visible = !toolStripMenuNode.IsExpanded && toolStripMenuNode.Nodes.Count > 0;
|
---|
| 414 | collapseToolStripMenuItem.Enabled = collapseToolStripMenuItem.Visible = toolStripMenuNode.IsExpanded;
|
---|
| 415 | } else {
|
---|
| 416 | expandToolStripMenuItem.Enabled = expandToolStripMenuItem.Visible = false;
|
---|
| 417 | collapseToolStripMenuItem.Enabled = collapseToolStripMenuItem.Visible = false;
|
---|
| 418 | }
|
---|
| 419 | expandAllToolStripMenuItem.Enabled =
|
---|
| 420 | expandAllToolStripMenuItem.Visible =
|
---|
| 421 | !typesTreeView.Nodes.OfType<TreeNode>().All(x => TreeNodeIsFullyExpanded(x));
|
---|
| 422 | collapseAllToolStripMenuItem.Enabled =
|
---|
| 423 | collapseAllToolStripMenuItem.Visible = typesTreeView.Nodes.OfType<TreeNode>().Any(x => x.IsExpanded);
|
---|
| 424 | if (contextMenuStrip.Items.Cast<ToolStripMenuItem>().Any(item => item.Enabled))
|
---|
| 425 | contextMenuStrip.Show(Cursor.Position);
|
---|
| 426 | }
|
---|
| 427 | }
|
---|
| 428 | private bool TreeNodeIsFullyExpanded(TreeNode node) {
|
---|
| 429 | return (node.Nodes.Count == 0) || (node.IsExpanded && node.Nodes.OfType<TreeNode>().All(x => TreeNodeIsFullyExpanded(x)));
|
---|
| 430 | }
|
---|
| 431 | private void expandToolStripMenuItem_Click(object sender, EventArgs e) {
|
---|
| 432 | typesTreeView.BeginUpdate();
|
---|
| 433 | if (toolStripMenuNode != null) toolStripMenuNode.ExpandAll();
|
---|
| 434 | typesTreeView.EndUpdate();
|
---|
| 435 | }
|
---|
| 436 | private void expandAllToolStripMenuItem_Click(object sender, EventArgs e) {
|
---|
| 437 | typesTreeView.BeginUpdate();
|
---|
| 438 | typesTreeView.ExpandAll();
|
---|
| 439 | typesTreeView.EndUpdate();
|
---|
| 440 | }
|
---|
| 441 | private void collapseToolStripMenuItem_Click(object sender, EventArgs e) {
|
---|
| 442 | typesTreeView.BeginUpdate();
|
---|
| 443 | if (toolStripMenuNode != null) toolStripMenuNode.Collapse();
|
---|
| 444 | typesTreeView.EndUpdate();
|
---|
| 445 | }
|
---|
| 446 | private void collapseAllToolStripMenuItem_Click(object sender, EventArgs e) {
|
---|
| 447 | typesTreeView.BeginUpdate();
|
---|
| 448 | typesTreeView.CollapseAll();
|
---|
| 449 | typesTreeView.EndUpdate();
|
---|
| 450 | }
|
---|
[3588] | 451 | #endregion
|
---|
| 452 |
|
---|
[2952] | 453 | #region Helpers
|
---|
| 454 | private void RestoreSelectedNode(TreeNode selectedNode) {
|
---|
| 455 | if (selectedNode != null) {
|
---|
| 456 | foreach (TreeNode node in typesTreeView.Nodes) {
|
---|
| 457 | if (node.Text.Equals(selectedNode.Text)) typesTreeView.SelectedNode = node;
|
---|
| 458 | foreach (TreeNode child in node.Nodes) {
|
---|
| 459 | if ((child.Text.Equals(selectedNode.Text)) && (child.Tag == selectedNode.Tag))
|
---|
| 460 | typesTreeView.SelectedNode = child;
|
---|
| 461 | }
|
---|
| 462 | }
|
---|
| 463 | if (typesTreeView.SelectedNode == null) SelectedType = null;
|
---|
| 464 | }
|
---|
| 465 | }
|
---|
| 466 | private void SetTreeNodeVisibility() {
|
---|
| 467 | TreeNode selectedNode = typesTreeView.SelectedNode;
|
---|
[2953] | 468 | if (string.IsNullOrEmpty(currentSearchString) && (typesTreeView.Nodes.Count > 1)) {
|
---|
[2952] | 469 | typesTreeView.CollapseAll();
|
---|
| 470 | if (selectedNode != null) typesTreeView.SelectedNode = selectedNode;
|
---|
| 471 | } else {
|
---|
| 472 | typesTreeView.ExpandAll();
|
---|
| 473 | }
|
---|
| 474 | if (selectedNode != null) selectedNode.EnsureVisible();
|
---|
| 475 | }
|
---|
| 476 | #endregion
|
---|
[2655] | 477 | }
|
---|
| 478 | }
|
---|