[2790] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[12012] | 3 | * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[2790] | 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;
|
---|
[12215] | 23 | using System.Collections;
|
---|
[12184] | 24 | using System.Collections.Generic;
|
---|
[12206] | 25 | using System.Drawing;
|
---|
[2520] | 26 | using System.Linq;
|
---|
| 27 | using System.Windows.Forms;
|
---|
[2790] | 28 | using HeuristicLab.Core;
|
---|
[2546] | 29 | using HeuristicLab.PluginInfrastructure;
|
---|
[2520] | 30 |
|
---|
| 31 | namespace HeuristicLab.Optimizer {
|
---|
[2546] | 32 | internal partial class NewItemDialog : Form {
|
---|
[12200] | 33 | private bool isInitialized;
|
---|
| 34 |
|
---|
[12284] | 35 | private readonly List<TreeNode> treeNodes;
|
---|
[12184] | 36 | private string currentSearchString;
|
---|
[2546] | 37 |
|
---|
[12184] | 38 | private Type selectedType;
|
---|
| 39 | public Type SelectedType {
|
---|
| 40 | get { return selectedType; }
|
---|
| 41 | private set {
|
---|
| 42 | if (value != selectedType) {
|
---|
| 43 | selectedType = value;
|
---|
| 44 | OnSelectedTypeChanged();
|
---|
| 45 | }
|
---|
| 46 | }
|
---|
| 47 | }
|
---|
| 48 |
|
---|
[2546] | 49 | private IItem item;
|
---|
| 50 | public IItem Item {
|
---|
| 51 | get { return item; }
|
---|
[2520] | 52 | }
|
---|
| 53 |
|
---|
[2546] | 54 | public NewItemDialog() {
|
---|
[12184] | 55 | InitializeComponent();
|
---|
| 56 | treeNodes = new List<TreeNode>();
|
---|
| 57 | currentSearchString = string.Empty;
|
---|
[2546] | 58 | item = null;
|
---|
[12184] | 59 | SelectedTypeChanged += this_SelectedTypeChanged;
|
---|
[2520] | 60 | }
|
---|
[2546] | 61 |
|
---|
| 62 | private void NewItemDialog_Load(object sender, EventArgs e) {
|
---|
[12200] | 63 | if (isInitialized) return;
|
---|
| 64 |
|
---|
[12184] | 65 | var categories =
|
---|
[12215] | 66 | from type in ApplicationManager.Manager.GetTypes(typeof(IItem))
|
---|
| 67 | let category = CreatableAttribute.GetCategory(type)
|
---|
| 68 | let name = ItemAttribute.GetName(type)
|
---|
[12246] | 69 | let priority = CreatableAttribute.GetPriority(type)
|
---|
[12215] | 70 | let version = ItemAttribute.GetVersion(type)
|
---|
| 71 | where CreatableAttribute.IsCreatable(type)
|
---|
[12246] | 72 | orderby category, priority, name, version ascending
|
---|
[12215] | 73 | group type by category into categoryGroup
|
---|
| 74 | select categoryGroup;
|
---|
[2546] | 75 |
|
---|
[12215] | 76 | var rootNode = CreateCategoryTree(categories);
|
---|
| 77 | CreateItemNodes(rootNode, categories);
|
---|
| 78 |
|
---|
| 79 | foreach (TreeNode topNode in rootNode.Nodes)
|
---|
| 80 | treeNodes.Add(topNode);
|
---|
| 81 | foreach (var node in treeNodes)
|
---|
| 82 | typesTreeView.Nodes.Add((TreeNode)node.Clone());
|
---|
| 83 |
|
---|
| 84 | typesTreeView.TreeViewNodeSorter = new ItemTreeNodeComparer();
|
---|
| 85 | typesTreeView.Sort();
|
---|
| 86 |
|
---|
| 87 | isInitialized = true;
|
---|
| 88 | }
|
---|
| 89 |
|
---|
| 90 | private TreeNode CreateCategoryTree(IEnumerable<IGrouping<string, Type>> categories) {
|
---|
[12184] | 91 | imageList.Images.Add(HeuristicLab.Common.Resources.VSImageLibrary.Class); // default icon
|
---|
| 92 | imageList.Images.Add(HeuristicLab.Common.Resources.VSImageLibrary.Namespace); // plugins
|
---|
| 93 |
|
---|
[12217] | 94 | var rootNode = new TreeNode();
|
---|
[12215] | 95 |
|
---|
[12184] | 96 | foreach (var category in categories) {
|
---|
[12217] | 97 | var fullName = category.Key;
|
---|
| 98 | var tokens = fullName.Split('#');
|
---|
| 99 | var name = tokens.Last();
|
---|
| 100 | var parents = tokens.Take(tokens.Length - 1);
|
---|
[12215] | 101 |
|
---|
[12284] | 102 | var categoryNode = new TreeNode(name, imageIndex: 1, selectedImageIndex: 1) {
|
---|
[12217] | 103 | Name = fullName,
|
---|
| 104 | Tag = fullName
|
---|
[12184] | 105 | };
|
---|
| 106 |
|
---|
[12217] | 107 | var parentNode = FindOrCreateParentNode(rootNode, parents);
|
---|
[12215] | 108 | if (parentNode != null)
|
---|
| 109 | parentNode.Nodes.Add(categoryNode);
|
---|
| 110 | else
|
---|
| 111 | rootNode.Nodes.Add(categoryNode);
|
---|
| 112 | }
|
---|
[12184] | 113 |
|
---|
[12215] | 114 | return rootNode;
|
---|
| 115 | }
|
---|
| 116 | private TreeNode FindOrCreateParentNode(TreeNode node, IEnumerable<string> parentCategories) {
|
---|
| 117 | TreeNode parentNode = null;
|
---|
[12217] | 118 | string fullName = null;
|
---|
[12215] | 119 | foreach (string parentCategory in parentCategories) {
|
---|
[12217] | 120 | fullName = fullName == null ? parentCategory : fullName + "#" + parentCategory;
|
---|
| 121 | parentNode = node.Nodes.Find(fullName, searchAllChildren: false).SingleOrDefault();
|
---|
[12215] | 122 | if (parentNode == null) {
|
---|
[12284] | 123 | parentNode = new TreeNode(parentCategory, imageIndex: 1, selectedImageIndex: 1) {
|
---|
[12217] | 124 | Name = fullName,
|
---|
| 125 | Tag = fullName
|
---|
[12184] | 126 | };
|
---|
[12215] | 127 | node.Nodes.Add(parentNode);
|
---|
| 128 | }
|
---|
| 129 | node = parentNode;
|
---|
| 130 | }
|
---|
| 131 | return parentNode;
|
---|
| 132 | }
|
---|
| 133 | private void CreateItemNodes(TreeNode node, IEnumerable<IGrouping<string, Type>> categories) {
|
---|
| 134 | foreach (var category in categories) {
|
---|
[12217] | 135 | var categoryNode = node.Nodes.Find(category.Key, searchAllChildren: true).Single();
|
---|
[12215] | 136 | foreach (var creatable in category) {
|
---|
| 137 | var itemNode = CreateItemNode(creatable);
|
---|
[12217] | 138 | itemNode.Name = itemNode.Name + ":" + category.Key;
|
---|
[12184] | 139 | categoryNode.Nodes.Add(itemNode);
|
---|
[2546] | 140 | }
|
---|
| 141 | }
|
---|
| 142 | }
|
---|
[12215] | 143 | private TreeNode CreateItemNode(Type creatable) {
|
---|
| 144 | string name = ItemAttribute.GetName(creatable);
|
---|
| 145 |
|
---|
| 146 | var itemNode = new TreeNode(name) {
|
---|
| 147 | ImageIndex = 0,
|
---|
[12217] | 148 | Tag = creatable,
|
---|
| 149 | Name = name
|
---|
[12215] | 150 | };
|
---|
| 151 |
|
---|
| 152 | var image = ItemAttribute.GetImage(creatable);
|
---|
| 153 | if (image != null) {
|
---|
| 154 | imageList.Images.Add(image);
|
---|
| 155 | itemNode.ImageIndex = imageList.Images.Count - 1;
|
---|
| 156 | }
|
---|
| 157 | itemNode.SelectedImageIndex = itemNode.ImageIndex;
|
---|
| 158 |
|
---|
| 159 | return itemNode;
|
---|
| 160 | }
|
---|
| 161 |
|
---|
[2546] | 162 | private void NewItemDialog_Shown(object sender, EventArgs e) {
|
---|
[12206] | 163 | searchTextBox.Text = string.Empty;
|
---|
| 164 | searchTextBox.Focus();
|
---|
[12239] | 165 | SelectedType = null;
|
---|
| 166 | typesTreeView.SelectedNode = null;
|
---|
| 167 | typesTreeView.CollapseAll();
|
---|
| 168 | UpdateDescription();
|
---|
[2546] | 169 | }
|
---|
| 170 |
|
---|
[12184] | 171 | public virtual void Filter(string searchString) {
|
---|
| 172 | if (InvokeRequired) {
|
---|
| 173 | Invoke(new Action<string>(Filter), searchString);
|
---|
| 174 | } else {
|
---|
| 175 | searchString = searchString.ToLower();
|
---|
| 176 |
|
---|
| 177 | if (!searchString.Contains(currentSearchString)) {
|
---|
| 178 | typesTreeView.BeginUpdate();
|
---|
[12239] | 179 | // expand search -> restore all tree nodes
|
---|
[12184] | 180 | var selectedNode = typesTreeView.SelectedNode;
|
---|
| 181 | typesTreeView.Nodes.Clear();
|
---|
| 182 | foreach (TreeNode node in treeNodes)
|
---|
| 183 | typesTreeView.Nodes.Add((TreeNode)node.Clone());
|
---|
| 184 | RestoreSelectedNode(selectedNode);
|
---|
| 185 | typesTreeView.EndUpdate();
|
---|
[12200] | 186 | }
|
---|
[12184] | 187 |
|
---|
[12200] | 188 | // remove nodes
|
---|
| 189 | typesTreeView.BeginUpdate();
|
---|
[12217] | 190 | var searchTokens = searchString.Split(' ');
|
---|
| 191 |
|
---|
[12200] | 192 | int i = 0;
|
---|
| 193 | while (i < typesTreeView.Nodes.Count) {
|
---|
[12217] | 194 | var categoryNode = typesTreeView.Nodes[i];
|
---|
| 195 | bool remove = FilterNode(categoryNode, searchTokens);
|
---|
| 196 | if (remove)
|
---|
| 197 | typesTreeView.Nodes.RemoveAt(i);
|
---|
| 198 | else i++;
|
---|
[12200] | 199 | }
|
---|
| 200 | typesTreeView.EndUpdate();
|
---|
| 201 | currentSearchString = searchString;
|
---|
[12184] | 202 |
|
---|
[12235] | 203 | // select first item
|
---|
[12239] | 204 | var firstNode = FirstVisibleNode;
|
---|
| 205 | while (firstNode != null && !(firstNode.Tag is Type))
|
---|
| 206 | firstNode = firstNode.NextVisibleNode;
|
---|
| 207 | if (firstNode != null)
|
---|
| 208 | typesTreeView.SelectedNode = firstNode;
|
---|
[12200] | 209 |
|
---|
| 210 | if (typesTreeView.Nodes.Count == 0) {
|
---|
| 211 | SelectedType = null;
|
---|
| 212 | typesTreeView.Enabled = false;
|
---|
| 213 | } else {
|
---|
| 214 | SetTreeNodeVisibility();
|
---|
| 215 | typesTreeView.Enabled = true;
|
---|
| 216 | }
|
---|
| 217 | UpdateDescription();
|
---|
[12184] | 218 | }
|
---|
[2546] | 219 | }
|
---|
| 220 |
|
---|
[12217] | 221 | private bool FilterNode(TreeNode node, string[] searchTokens) {
|
---|
[12238] | 222 | if (node.Tag is string) { // Category node
|
---|
[12217] | 223 | int i = 0;
|
---|
| 224 | while (i < node.Nodes.Count) {
|
---|
| 225 | bool remove = FilterNode(node.Nodes[i], searchTokens);
|
---|
| 226 | if (remove)
|
---|
| 227 | node.Nodes.RemoveAt(i);
|
---|
| 228 | else i++;
|
---|
| 229 | }
|
---|
| 230 | return node.Nodes.Count == 0;
|
---|
[12284] | 231 | } if (node.Tag is Type) { // Type node
|
---|
[12217] | 232 | var text = node.Text;
|
---|
| 233 | if (searchTokens.Any(searchToken => !text.ToLower().Contains(searchToken))) {
|
---|
| 234 | var typeTag = (Type)node.Tag;
|
---|
[12284] | 235 | if (typeTag == SelectedType) {
|
---|
[12217] | 236 | SelectedType = null;
|
---|
[12238] | 237 | typesTreeView.SelectedNode = null;
|
---|
| 238 | }
|
---|
[12217] | 239 | return true;
|
---|
| 240 | }
|
---|
| 241 | return false;
|
---|
| 242 | }
|
---|
[12284] | 243 | throw new InvalidOperationException("Encountered neither a category nor a creatable node during tree traversal.");
|
---|
[12217] | 244 | }
|
---|
| 245 |
|
---|
[12184] | 246 | protected virtual void UpdateDescription() {
|
---|
[12239] | 247 | itemDescriptionTextBox.Text = string.Empty;
|
---|
| 248 | pluginDescriptionTextBox.Text = string.Empty;
|
---|
| 249 | pluginTextBox.Text = string.Empty;
|
---|
| 250 | versionTextBox.Text = string.Empty;
|
---|
[12184] | 251 |
|
---|
| 252 | if (typesTreeView.SelectedNode != null) {
|
---|
| 253 | string category = typesTreeView.SelectedNode.Tag as string;
|
---|
| 254 | if (category != null) {
|
---|
[12239] | 255 | itemDescriptionTextBox.Text = category;
|
---|
[12184] | 256 | }
|
---|
| 257 | Type type = typesTreeView.SelectedNode.Tag as Type;
|
---|
| 258 | if (type != null) {
|
---|
| 259 | string description = ItemAttribute.GetDescription(type);
|
---|
[12203] | 260 | var version = ItemAttribute.GetVersion(type);
|
---|
| 261 | var plugin = ApplicationManager.Manager.GetDeclaringPlugin(type);
|
---|
[12184] | 262 | if (description != null)
|
---|
[12239] | 263 | itemDescriptionTextBox.Text = description;
|
---|
| 264 | if (plugin != null) {
|
---|
[12203] | 265 | pluginTextBox.Text = plugin.Name;
|
---|
[12239] | 266 | pluginDescriptionTextBox.Text = plugin.Description;
|
---|
| 267 | }
|
---|
[12203] | 268 | if (version != null)
|
---|
| 269 | versionTextBox.Text = version.ToString();
|
---|
[12184] | 270 | }
|
---|
| 271 | } else if (typesTreeView.Nodes.Count == 0) {
|
---|
[12239] | 272 | itemDescriptionTextBox.Text = "No types found";
|
---|
[12184] | 273 | }
|
---|
| 274 | }
|
---|
| 275 |
|
---|
| 276 | #region Events
|
---|
| 277 | public event EventHandler SelectedTypeChanged;
|
---|
| 278 | protected virtual void OnSelectedTypeChanged() {
|
---|
| 279 | if (SelectedTypeChanged != null)
|
---|
| 280 | SelectedTypeChanged(this, EventArgs.Empty);
|
---|
| 281 | }
|
---|
| 282 | #endregion
|
---|
| 283 |
|
---|
| 284 | #region Control Events
|
---|
[12201] | 285 | protected virtual void searchTextBox_TextChanged(object sender, EventArgs e) {
|
---|
[12184] | 286 | Filter(searchTextBox.Text);
|
---|
| 287 | }
|
---|
| 288 |
|
---|
| 289 | protected virtual void itemsTreeView_AfterSelect(object sender, TreeViewEventArgs e) {
|
---|
| 290 | if (typesTreeView.SelectedNode == null) SelectedType = null;
|
---|
| 291 | else SelectedType = typesTreeView.SelectedNode.Tag as Type;
|
---|
| 292 | UpdateDescription();
|
---|
| 293 | }
|
---|
| 294 |
|
---|
| 295 | protected virtual void itemsTreeView_VisibleChanged(object sender, EventArgs e) {
|
---|
| 296 | if (Visible) SetTreeNodeVisibility();
|
---|
| 297 | }
|
---|
| 298 | #endregion
|
---|
| 299 |
|
---|
| 300 | #region Helpers
|
---|
| 301 | private void RestoreSelectedNode(TreeNode selectedNode) {
|
---|
| 302 | if (selectedNode != null) {
|
---|
[12217] | 303 | var node = typesTreeView.Nodes.Find(selectedNode.Name, searchAllChildren: true).SingleOrDefault();
|
---|
| 304 | typesTreeView.SelectedNode = node;
|
---|
[12184] | 305 | if (typesTreeView.SelectedNode == null) SelectedType = null;
|
---|
| 306 | }
|
---|
| 307 | }
|
---|
| 308 | private void SetTreeNodeVisibility() {
|
---|
| 309 | TreeNode selectedNode = typesTreeView.SelectedNode;
|
---|
[12203] | 310 | if (string.IsNullOrEmpty(currentSearchString) && (typesTreeView.Nodes.Count > 1)) {
|
---|
| 311 | typesTreeView.CollapseAll();
|
---|
| 312 | if (selectedNode != null) typesTreeView.SelectedNode = selectedNode;
|
---|
| 313 | } else {
|
---|
| 314 | typesTreeView.ExpandAll();
|
---|
[12184] | 315 | }
|
---|
[12203] | 316 | if (selectedNode != null) selectedNode.EnsureVisible();
|
---|
[12184] | 317 | }
|
---|
| 318 | #endregion
|
---|
| 319 |
|
---|
[2546] | 320 | private void okButton_Click(object sender, EventArgs e) {
|
---|
[12184] | 321 | if (SelectedType != null) {
|
---|
| 322 | item = (IItem)Activator.CreateInstance(SelectedType);
|
---|
[2546] | 323 | DialogResult = DialogResult.OK;
|
---|
| 324 | Close();
|
---|
| 325 | }
|
---|
| 326 | }
|
---|
[12184] | 327 | private void itemTreeView_DoubleClick(object sender, EventArgs e) {
|
---|
| 328 | if (SelectedType != null) {
|
---|
| 329 | item = (IItem)Activator.CreateInstance(SelectedType);
|
---|
[2546] | 330 | DialogResult = DialogResult.OK;
|
---|
| 331 | Close();
|
---|
| 332 | }
|
---|
| 333 | }
|
---|
[12184] | 334 | private void this_SelectedTypeChanged(object sender, EventArgs e) {
|
---|
| 335 | okButton.Enabled = SelectedType != null;
|
---|
| 336 | }
|
---|
[12201] | 337 |
|
---|
| 338 | private void expandAllButton_Click(object sender, EventArgs e) {
|
---|
| 339 | typesTreeView.ExpandAll();
|
---|
| 340 | }
|
---|
| 341 | private void collapseAllButton_Click(object sender, EventArgs e) {
|
---|
| 342 | typesTreeView.CollapseAll();
|
---|
| 343 | }
|
---|
[12206] | 344 |
|
---|
| 345 | private TreeNode toolStripMenuNode = null;
|
---|
| 346 | private void typesTreeView_MouseDown(object sender, MouseEventArgs e) {
|
---|
| 347 | if (e.Button == MouseButtons.Right) {
|
---|
| 348 | Point coordinates = typesTreeView.PointToClient(Cursor.Position);
|
---|
| 349 | toolStripMenuNode = typesTreeView.GetNodeAt(coordinates);
|
---|
| 350 |
|
---|
| 351 | if (toolStripMenuNode != null && coordinates.X >= toolStripMenuNode.Bounds.Left &&
|
---|
| 352 | coordinates.X <= toolStripMenuNode.Bounds.Right) {
|
---|
| 353 | typesTreeView.SelectedNode = toolStripMenuNode;
|
---|
| 354 |
|
---|
| 355 | expandToolStripMenuItem.Enabled =
|
---|
| 356 | expandToolStripMenuItem.Visible = !toolStripMenuNode.IsExpanded && toolStripMenuNode.Nodes.Count > 0;
|
---|
| 357 | collapseToolStripMenuItem.Enabled = collapseToolStripMenuItem.Visible = toolStripMenuNode.IsExpanded;
|
---|
| 358 | } else {
|
---|
| 359 | expandToolStripMenuItem.Enabled = expandToolStripMenuItem.Visible = false;
|
---|
| 360 | collapseToolStripMenuItem.Enabled = collapseToolStripMenuItem.Visible = false;
|
---|
| 361 | }
|
---|
| 362 | expandAllToolStripMenuItem.Enabled =
|
---|
| 363 | expandAllToolStripMenuItem.Visible =
|
---|
| 364 | !typesTreeView.Nodes.OfType<TreeNode>().All(x => TreeNodeIsFullyExpanded(x));
|
---|
| 365 | collapseAllToolStripMenuItem.Enabled =
|
---|
| 366 | collapseAllToolStripMenuItem.Visible = typesTreeView.Nodes.OfType<TreeNode>().Any(x => x.IsExpanded);
|
---|
| 367 | if (contextMenuStrip.Items.Cast<ToolStripMenuItem>().Any(item => item.Enabled))
|
---|
| 368 | contextMenuStrip.Show(Cursor.Position);
|
---|
| 369 | }
|
---|
| 370 | }
|
---|
| 371 | private bool TreeNodeIsFullyExpanded(TreeNode node) {
|
---|
| 372 | return (node.Nodes.Count == 0) || (node.IsExpanded && node.Nodes.OfType<TreeNode>().All(x => TreeNodeIsFullyExpanded(x)));
|
---|
| 373 | }
|
---|
| 374 |
|
---|
| 375 | private void expandToolStripMenuItem_Click(object sender, EventArgs e) {
|
---|
| 376 | if (toolStripMenuNode != null) toolStripMenuNode.ExpandAll();
|
---|
| 377 | }
|
---|
| 378 | private void expandAllToolStripMenuItem_Click(object sender, EventArgs e) {
|
---|
| 379 | typesTreeView.ExpandAll();
|
---|
| 380 | }
|
---|
| 381 | private void collapseToolStripMenuItem_Click(object sender, EventArgs e) {
|
---|
| 382 | if (toolStripMenuNode != null) toolStripMenuNode.Collapse();
|
---|
| 383 | }
|
---|
| 384 | private void collapseAllToolStripMenuItem_Click(object sender, EventArgs e) {
|
---|
| 385 | typesTreeView.CollapseAll();
|
---|
| 386 | }
|
---|
[12207] | 387 |
|
---|
| 388 | private void clearSearchButton_Click(object sender, EventArgs e) {
|
---|
| 389 | searchTextBox.Text = string.Empty;
|
---|
| 390 | searchTextBox.Focus();
|
---|
| 391 | }
|
---|
[12215] | 392 |
|
---|
| 393 | private void searchTextBox_KeyDown(object sender, KeyEventArgs e) {
|
---|
[12234] | 394 | if (typesTreeView.Nodes.Count == 0)
|
---|
| 395 | return;
|
---|
| 396 |
|
---|
[12215] | 397 | if (e.KeyCode == Keys.Up || e.KeyCode == Keys.Down) {
|
---|
[12234] | 398 | var selectedNode = typesTreeView.SelectedNode;
|
---|
[12215] | 399 |
|
---|
[12234] | 400 | if (selectedNode == null) { // nothing selected => select first or last
|
---|
| 401 | if (e.KeyCode == Keys.Down) typesTreeView.SelectedNode = FirstVisibleNode;
|
---|
| 402 | if (e.KeyCode == Keys.Up) typesTreeView.SelectedNode = LastVisibleNode;
|
---|
| 403 | } else {
|
---|
| 404 | if (e.KeyCode == Keys.Down)
|
---|
| 405 | typesTreeView.SelectedNode = selectedNode.NextVisibleNode ?? FirstVisibleNode; // select next or cycle to first
|
---|
| 406 | if (e.KeyCode == Keys.Up)
|
---|
| 407 | typesTreeView.SelectedNode = selectedNode.PrevVisibleNode ?? LastVisibleNode; // select prev or cycle to last
|
---|
[12215] | 408 | }
|
---|
| 409 | e.Handled = true;
|
---|
| 410 | }
|
---|
| 411 | }
|
---|
| 412 |
|
---|
[12234] | 413 | private TreeNode FirstVisibleNode {
|
---|
| 414 | get {
|
---|
[12235] | 415 | return typesTreeView.Nodes.Count > 0 ? typesTreeView.Nodes[0] : null;
|
---|
[12234] | 416 | }
|
---|
| 417 | }
|
---|
| 418 | private TreeNode LastVisibleNode {
|
---|
| 419 | get {
|
---|
| 420 | var node = FirstVisibleNode;
|
---|
[12235] | 421 | while (node != null && node.NextVisibleNode != null) node = node.NextVisibleNode;
|
---|
[12234] | 422 | return node;
|
---|
| 423 | }
|
---|
| 424 | }
|
---|
| 425 |
|
---|
[12215] | 426 | private class ItemTreeNodeComparer : IComparer {
|
---|
| 427 | public int Compare(object x, object y) {
|
---|
| 428 | var lhs = (TreeNode)x;
|
---|
| 429 | var rhs = (TreeNode)y;
|
---|
| 430 |
|
---|
| 431 | if (lhs.Tag is string && rhs.Tag is string) {
|
---|
| 432 | return lhs.Name.CompareTo(rhs.Name);
|
---|
| 433 | } else if (lhs.Tag is string) {
|
---|
| 434 | return -1;
|
---|
| 435 | } else
|
---|
| 436 | return 1;
|
---|
| 437 | }
|
---|
| 438 | }
|
---|
[2520] | 439 | }
|
---|
| 440 | }
|
---|