Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Core.Views/3.3/TypeSelector.cs @ 2952

Last change on this file since 2952 was 2952, checked in by swagner, 14 years ago

Implemented searching in TypeSelector (#894)

File size: 11.0 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
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
22using System;
23using System.Linq;
24using System.Text;
25using System.Windows.Forms;
26using HeuristicLab.Common;
27using HeuristicLab.PluginInfrastructure;
28using System.Collections.Generic;
29
30namespace HeuristicLab.Core.Views {
31  public partial class TypeSelector : UserControl {
32    protected List<TreeNode> treeNodes;
33    protected string currentSearchString;
34
35    protected Type baseType;
36    public Type BaseType {
37      get { return baseType; }
38    }
39    protected bool showNotInstantiableTypes;
40    public bool ShowNotInstantiableTypes {
41      get { return showNotInstantiableTypes; }
42    }
43    protected bool showGenericTypes;
44    public bool ShowGenericTypes {
45      get { return showGenericTypes; }
46    }
47    public string Caption {
48      get { return typesGroupBox.Text; }
49      set {
50        if (InvokeRequired)
51          Invoke(new Action<string>(delegate(string s) { Caption = s; }), value);
52        else
53          typesGroupBox.Text = value;
54      }
55    }
56    public TreeView TypesTreeView {
57      get { return typesTreeView; }
58    }
59    protected Type selectedType;
60    public Type SelectedType {
61      get { return selectedType; }
62      private set {
63        if (value != selectedType) {
64          selectedType = value;
65          OnSelectedTypeChanged();
66        }
67      }
68    }
69
70    public TypeSelector() {
71      InitializeComponent();
72      treeNodes = new List<TreeNode>();
73      currentSearchString = string.Empty;
74      selectedType = null;
75    }
76
77    public virtual void Configure(Type baseType, bool showNotInstantiableTypes, bool showGenericTypes) {
78      if (baseType == null) throw new ArgumentNullException();
79      if (InvokeRequired)
80        Invoke(new Action<Type, bool, bool>(Configure), baseType, showNotInstantiableTypes, showGenericTypes);
81      else {
82        this.baseType = baseType;
83        this.showNotInstantiableTypes = showNotInstantiableTypes;
84        this.showGenericTypes = showGenericTypes;
85
86        TreeNode selectedNode = typesTreeView.SelectedNode;
87        typesTreeView.Nodes.Clear();
88        treeNodes.Clear();
89        imageList.Images.Clear();
90        imageList.Images.Add(HeuristicLab.Common.Resources.VS2008ImageLibrary.Class);      // default icon
91        imageList.Images.Add(HeuristicLab.Common.Resources.VS2008ImageLibrary.Namespace);  // plugins
92        imageList.Images.Add(HeuristicLab.Common.Resources.VS2008ImageLibrary.Interface);  // interfaces
93        imageList.Images.Add(HeuristicLab.Common.Resources.VS2008ImageLibrary.OrgChart);   // abstract types
94        imageList.Images.Add(HeuristicLab.Common.Resources.VS2008ImageLibrary.Template);   // generic types
95
96        var plugins = from p in ApplicationManager.Manager.Plugins
97                      orderby p.Name, p.Version ascending
98                      select p;
99        foreach (IPluginDescription plugin in plugins) {
100          TreeNode pluginNode = new TreeNode();
101          pluginNode.Text = plugin.Name;
102          pluginNode.ImageIndex = 1;
103          pluginNode.SelectedImageIndex = pluginNode.ImageIndex;
104          pluginNode.Tag = plugin;
105
106          var types = from t in ApplicationManager.Manager.GetTypes(BaseType, plugin, false)
107                      orderby t.Name ascending
108                      select t;
109          foreach (Type type in types) {
110            bool valid = true;
111            valid = valid && (ShowGenericTypes || !type.ContainsGenericParameters);
112            valid = valid && (ShowNotInstantiableTypes || !type.IsAbstract);
113            valid = valid && (ShowNotInstantiableTypes || !type.IsInterface);
114            valid = valid && (ShowNotInstantiableTypes || !type.HasElementType);
115            if (valid) {
116              TreeNode typeNode = new TreeNode();
117              string name = ItemAttribute.GetName(type);
118              typeNode.Text = name != null ? name : type.GetPrettyName();
119              typeNode.ImageIndex = 0;
120              if (type.IsInterface) typeNode.ImageIndex = 2;
121              else if (type.IsAbstract) typeNode.ImageIndex = 3;
122              else if (type.ContainsGenericParameters) typeNode.ImageIndex = 4;
123              else if (imageList.Images.ContainsKey(type.FullName)) typeNode.ImageIndex = imageList.Images.IndexOfKey(type.FullName);
124              else if (typeof(IItem).IsAssignableFrom(type)) {
125                try {
126                  IItem item = (IItem)Activator.CreateInstance(type);
127                  imageList.Images.Add(type.FullName, item.ItemImage);
128                  typeNode.ImageIndex = imageList.Images.IndexOfKey(type.FullName);
129                }
130                catch (Exception) {
131                }
132              }
133              typeNode.SelectedImageIndex = typeNode.ImageIndex;
134              typeNode.Tag = type;
135              pluginNode.Nodes.Add(typeNode);
136            }
137          }
138          if (pluginNode.Nodes.Count > 0)
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
188        if (typesTreeView.Nodes.Count == 0) {
189          SelectedType = null;
190          typesTreeView.Enabled = false;
191        } else {
192          SetTreeNodeVisibility();
193          typesTreeView.Enabled = true;
194        }
195        UpdateDescription();
196        typesTreeView.EndUpdate();
197      }
198    }
199
200    public virtual object CreateInstanceOfSelectedType(params object[] args) {
201      if (SelectedType != null) {
202      try {
203        return Activator.CreateInstance(SelectedType, args);
204      } catch(Exception) { }
205      }
206      return null;
207    }
208
209    public event EventHandler SelectedTypeChanged;
210    protected virtual void OnSelectedTypeChanged() {
211      if (SelectedTypeChanged != null)
212        SelectedTypeChanged(this, EventArgs.Empty);
213    }
214
215    protected virtual void UpdateDescription() {
216      descriptionTextBox.Text = string.Empty;
217
218      if (typesTreeView.SelectedNode != null) {
219        IPluginDescription plugin = typesTreeView.SelectedNode.Tag as IPluginDescription;
220        if (plugin != null) {
221          StringBuilder sb = new StringBuilder();
222          sb.Append("Plugin: ").Append(plugin.Name).Append(Environment.NewLine);
223          sb.Append("Version: ").Append(plugin.Version.ToString()).Append(Environment.NewLine);
224          descriptionTextBox.Text = sb.ToString();
225        }
226        Type type = typesTreeView.SelectedNode.Tag as Type;
227        if (type != null) {
228          string description = ItemAttribute.GetDescription(type);
229          if (description != null)
230            descriptionTextBox.Text = description;
231        }
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);
239    }
240
241    protected virtual void typesTreeView_AfterSelect(object sender, TreeViewEventArgs e) {
242      if (typesTreeView.SelectedNode == null) SelectedType = null;
243      else SelectedType = typesTreeView.SelectedNode.Tag as Type;
244      UpdateDescription();
245    }
246
247    protected virtual void typesTreeView_ItemDrag(object sender, ItemDragEventArgs e) {
248      TreeNode node = (TreeNode)e.Item;
249      Type type = node.Tag as Type;
250      if (type != null) {
251        try {
252          object o = Activator.CreateInstance(type);
253          DataObject data = new DataObject();
254          data.SetData("Type", type);
255          data.SetData("Value", o);
256          DoDragDrop(data, DragDropEffects.Copy);
257        } catch (Exception) {
258        }
259      }
260    }
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
290  }
291}
Note: See TracBrowser for help on using the repository browser.