Free cookie consent management tool by TermsFeed Policy Generator

Changeset 2952 for trunk/sources


Ignore:
Timestamp:
03/07/10 03:10:38 (14 years ago)
Author:
swagner
Message:

Implemented searching in TypeSelector (#894)

Location:
trunk/sources/HeuristicLab.Core.Views/3.3
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Core.Views/3.3/TypeSelector.Designer.cs

    r2924 r2952  
    5050      this.typesGroupBox = new System.Windows.Forms.GroupBox();
    5151      this.splitContainer = new System.Windows.Forms.SplitContainer();
     52      this.searchLabel = new System.Windows.Forms.Label();
     53      this.searchTextBox = new System.Windows.Forms.TextBox();
    5254      this.descriptionTextBox = new System.Windows.Forms.TextBox();
     55      this.toolTip = new System.Windows.Forms.ToolTip(this.components);
    5356      this.typesGroupBox.SuspendLayout();
    5457      this.splitContainer.Panel1.SuspendLayout();
     
    6568      this.typesTreeView.ImageIndex = 0;
    6669      this.typesTreeView.ImageList = this.imageList;
    67       this.typesTreeView.Location = new System.Drawing.Point(3, 3);
     70      this.typesTreeView.Location = new System.Drawing.Point(3, 29);
    6871      this.typesTreeView.Name = "typesTreeView";
    6972      this.typesTreeView.SelectedImageIndex = 0;
    7073      this.typesTreeView.ShowNodeToolTips = true;
    71       this.typesTreeView.Size = new System.Drawing.Size(291, 192);
    72       this.typesTreeView.TabIndex = 0;
     74      this.typesTreeView.Size = new System.Drawing.Size(291, 166);
     75      this.typesTreeView.TabIndex = 2;
     76      this.typesTreeView.VisibleChanged += new System.EventHandler(this.typesTreeView_VisibleChanged);
    7377      this.typesTreeView.AfterSelect += new System.Windows.Forms.TreeViewEventHandler(this.typesTreeView_AfterSelect);
    7478      this.typesTreeView.ItemDrag += new System.Windows.Forms.ItemDragEventHandler(this.typesTreeView_ItemDrag);
     
    100104      // splitContainer.Panel1
    101105      //
     106      this.splitContainer.Panel1.Controls.Add(this.searchLabel);
     107      this.splitContainer.Panel1.Controls.Add(this.searchTextBox);
    102108      this.splitContainer.Panel1.Controls.Add(this.typesTreeView);
    103109      //
     
    108114      this.splitContainer.SplitterDistance = 198;
    109115      this.splitContainer.TabIndex = 2;
     116      //
     117      // searchLabel
     118      //
     119      this.searchLabel.Image = HeuristicLab.Common.Resources.VS2008ImageLibrary.Zoom;
     120      this.searchLabel.Location = new System.Drawing.Point(3, 3);
     121      this.searchLabel.Name = "searchLabel";
     122      this.searchLabel.Size = new System.Drawing.Size(20, 20);
     123      this.searchLabel.TabIndex = 0;
     124      this.toolTip.SetToolTip(this.searchLabel, "Enter string to search for types");
     125      //
     126      // searchTextBox
     127      //
     128      this.searchTextBox.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
     129                  | System.Windows.Forms.AnchorStyles.Right)));
     130      this.searchTextBox.Location = new System.Drawing.Point(29, 3);
     131      this.searchTextBox.Name = "searchTextBox";
     132      this.searchTextBox.Size = new System.Drawing.Size(265, 20);
     133      this.searchTextBox.TabIndex = 1;
     134      this.toolTip.SetToolTip(this.searchTextBox, "Enter string to search for types");
     135      this.searchTextBox.TextChanged += new System.EventHandler(this.searchTextBox_TextChanged);
    110136      //
    111137      // descriptionTextBox
     
    132158      this.typesGroupBox.ResumeLayout(false);
    133159      this.splitContainer.Panel1.ResumeLayout(false);
     160      this.splitContainer.Panel1.PerformLayout();
    134161      this.splitContainer.Panel2.ResumeLayout(false);
    135162      this.splitContainer.Panel2.PerformLayout();
     
    146173    protected System.Windows.Forms.TreeView typesTreeView;
    147174    protected System.Windows.Forms.SplitContainer splitContainer;
     175    protected System.Windows.Forms.Label searchLabel;
     176    protected System.Windows.Forms.TextBox searchTextBox;
     177    protected System.Windows.Forms.ToolTip toolTip;
    148178
    149179  }
  • trunk/sources/HeuristicLab.Core.Views/3.3/TypeSelector.cs

    r2818 r2952  
    2626using HeuristicLab.Common;
    2727using HeuristicLab.PluginInfrastructure;
     28using System.Collections.Generic;
    2829
    2930namespace HeuristicLab.Core.Views {
    3031  public partial class TypeSelector : UserControl {
     32    protected List<TreeNode> treeNodes;
     33    protected string currentSearchString;
     34
    3135    protected Type baseType;
    3236    public Type BaseType {
     
    6670    public TypeSelector() {
    6771      InitializeComponent();
     72      treeNodes = new List<TreeNode>();
     73      currentSearchString = string.Empty;
    6874      selectedType = null;
    6975    }
     
    7884        this.showGenericTypes = showGenericTypes;
    7985
     86        TreeNode selectedNode = typesTreeView.SelectedNode;
    8087        typesTreeView.Nodes.Clear();
     88        treeNodes.Clear();
    8189        imageList.Images.Clear();
    8290        imageList.Images.Add(HeuristicLab.Common.Resources.VS2008ImageLibrary.Class);      // default icon
     
    8694        imageList.Images.Add(HeuristicLab.Common.Resources.VS2008ImageLibrary.Template);   // generic types
    8795
    88         TreeNode selectedNode = null;
    8996        var plugins = from p in ApplicationManager.Manager.Plugins
    9097                      orderby p.Name, p.Version ascending
     
    126133              typeNode.SelectedImageIndex = typeNode.ImageIndex;
    127134              typeNode.Tag = type;
    128               if (type == SelectedType) selectedNode = typeNode;
    129135              pluginNode.Nodes.Add(typeNode);
    130136            }
    131137          }
    132138          if (pluginNode.Nodes.Count > 0)
    133             typesTreeView.Nodes.Add(pluginNode);
    134         }
     139            treeNodes.Add(pluginNode);
     140        }
     141        foreach (TreeNode node in treeNodes)
     142          typesTreeView.Nodes.Add((TreeNode)node.Clone());
     143        RestoreSelectedNode(selectedNode);
     144        Filter(searchTextBox.Text);
     145      }
     146    }
     147
     148    public virtual void Filter(string searchString) {
     149      if (InvokeRequired)
     150        Invoke(new Action<string>(Filter), searchString);
     151      else {
     152        searchString = searchString.ToLower();
     153
     154        typesTreeView.BeginUpdate();
     155        if (!searchString.Contains(currentSearchString)) {
     156          // expand search -> restore all tree nodes
     157          TreeNode selectedNode = typesTreeView.SelectedNode;
     158          typesTreeView.Nodes.Clear();
     159          foreach (TreeNode node in treeNodes)
     160            typesTreeView.Nodes.Add((TreeNode)node.Clone());
     161          RestoreSelectedNode(selectedNode);
     162        }
     163
     164        // remove nodes
     165        int i = 0;
     166        while (i < typesTreeView.Nodes.Count) {
     167          int j = 0;
     168          while (j < typesTreeView.Nodes[i].Nodes.Count) {
     169            if (!typesTreeView.Nodes[i].Nodes[j].Text.ToLower().Contains(searchString))
     170              typesTreeView.Nodes[i].Nodes[j].Remove();
     171            else
     172              j++;
     173          }
     174          if (typesTreeView.Nodes[i].Nodes.Count == 0)
     175            typesTreeView.Nodes[i].Remove();
     176          else
     177            i++;
     178        }
     179        currentSearchString = searchString;
     180
     181        // if there is just one type node left, select by default
     182        if (typesTreeView.Nodes.Count == 1) {
     183          if (typesTreeView.Nodes[0].Nodes.Count == 1) {
     184            typesTreeView.SelectedNode = typesTreeView.Nodes[0].Nodes[0];
     185          }
     186        }
     187
    135188        if (typesTreeView.Nodes.Count == 0) {
    136           typesTreeView.Nodes.Add("No types of base type \"" + BaseType.GetPrettyName() + "\" found");
     189          SelectedType = null;
    137190          typesTreeView.Enabled = false;
    138         }
    139         if (selectedNode != null) {
    140           typesTreeView.SelectedNode = selectedNode;
    141           selectedNode.EnsureVisible();
    142191        } else {
    143           SelectedType = null;
    144         }
     192          SetTreeNodeVisibility();
     193          typesTreeView.Enabled = true;
     194        }
     195        UpdateDescription();
     196        typesTreeView.EndUpdate();
    145197      }
    146198    }
     
    178230            descriptionTextBox.Text = description;
    179231        }
    180       }
     232      } else if (typesTreeView.Nodes.Count == 0) {
     233        descriptionTextBox.Text = "No types found";
     234      }
     235    }
     236
     237    protected virtual void searchTextBox_TextChanged(object sender, System.EventArgs e) {
     238      Filter(searchTextBox.Text);
    181239    }
    182240
    183241    protected virtual void typesTreeView_AfterSelect(object sender, TreeViewEventArgs e) {
    184       if (typesTreeView.SelectedNode != null) {
    185         SelectedType = typesTreeView.SelectedNode.Tag as Type;
    186       }
     242      if (typesTreeView.SelectedNode == null) SelectedType = null;
     243      else SelectedType = typesTreeView.SelectedNode.Tag as Type;
    187244      UpdateDescription();
    188245    }
     
    202259      }
    203260    }
     261
     262    protected virtual void typesTreeView_VisibleChanged(object sender, EventArgs e) {
     263      if (Visible) SetTreeNodeVisibility();
     264    }
     265
     266    #region Helpers
     267    private void RestoreSelectedNode(TreeNode selectedNode) {
     268      if (selectedNode != null) {
     269        foreach (TreeNode node in typesTreeView.Nodes) {
     270          if (node.Text.Equals(selectedNode.Text)) typesTreeView.SelectedNode = node;
     271          foreach (TreeNode child in node.Nodes) {
     272            if ((child.Text.Equals(selectedNode.Text)) && (child.Tag == selectedNode.Tag))
     273              typesTreeView.SelectedNode = child;
     274          }
     275        }
     276        if (typesTreeView.SelectedNode == null) SelectedType = null;
     277      }
     278    }
     279    private void SetTreeNodeVisibility() {
     280      TreeNode selectedNode = typesTreeView.SelectedNode;
     281      if (string.IsNullOrEmpty(currentSearchString)) {
     282        typesTreeView.CollapseAll();
     283        if (selectedNode != null) typesTreeView.SelectedNode = selectedNode;
     284      } else {
     285        typesTreeView.ExpandAll();
     286      }
     287      if (selectedNode != null) selectedNode.EnsureVisible();
     288    }
     289    #endregion
    204290  }
    205291}
Note: See TracChangeset for help on using the changeset viewer.