Free cookie consent management tool by TermsFeed Policy Generator

source: branches/NewItemDialog/HeuristicLab.Optimizer/3.3/NewItemDialog.cs @ 12205

Last change on this file since 12205 was 12205, checked in by pfleck, 9 years ago

#2025 Corrected index indices of items.

File size: 9.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 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.Collections.Generic;
24using System.Linq;
25using System.Windows.Forms;
26using HeuristicLab.Core;
27using HeuristicLab.PluginInfrastructure;
28
29namespace HeuristicLab.Optimizer {
30  internal partial class NewItemDialog : Form {
31    private bool isInitialized;
32
33    private List<TreeNode> treeNodes;
34    private string currentSearchString;
35
36    private Type selectedType;
37    public Type SelectedType {
38      get { return selectedType; }
39      private set {
40        if (value != selectedType) {
41          selectedType = value;
42          OnSelectedTypeChanged();
43        }
44      }
45    }
46
47    private IItem item;
48    public IItem Item {
49      get { return item; }
50    }
51
52    public NewItemDialog() {
53      InitializeComponent();
54      treeNodes = new List<TreeNode>();
55      currentSearchString = string.Empty;
56      item = null;
57      SelectedTypeChanged += this_SelectedTypeChanged;
58    }
59
60    private void NewItemDialog_Load(object sender, EventArgs e) {
61      if (isInitialized) return;
62
63      var categories =
64        from t in ApplicationManager.Manager.GetTypes(typeof(IItem))
65        where CreatableAttribute.IsCreatable(t)
66        orderby CreatableAttribute.GetCategory(t), ItemAttribute.GetName(t), ItemAttribute.GetVersion(t) ascending
67        group t by CreatableAttribute.GetCategory(t) into c
68        select c;
69
70      imageList.Images.Add(HeuristicLab.Common.Resources.VSImageLibrary.Class);      // default icon
71      imageList.Images.Add(HeuristicLab.Common.Resources.VSImageLibrary.Namespace);  // plugins
72
73      foreach (var category in categories) {
74        var categoryNode = new TreeNode(category.Key) {
75          ImageIndex = 1,
76          Tag = category.Key,
77        };
78        categoryNode.SelectedImageIndex = categoryNode.ImageIndex;
79
80        foreach (var creatable in category) {
81          string name = ItemAttribute.GetName(creatable);
82
83          var itemNode = new TreeNode(name) {
84            ImageIndex = 0,
85            Tag = creatable
86          };
87          categoryNode.Nodes.Add(itemNode);
88          var image = ItemAttribute.GetImage(creatable);
89          if (image != null) {
90            imageList.Images.Add(image);
91            itemNode.ImageIndex = imageList.Images.Count - 1;
92          }
93          itemNode.SelectedImageIndex = itemNode.ImageIndex;
94        }
95        if (categoryNode.Nodes.Count > 0)
96          treeNodes.Add(categoryNode);
97      }
98      foreach (var node in treeNodes)
99        typesTreeView.Nodes.Add((TreeNode)node.Clone());
100      isInitialized = true;
101    }
102    private void NewItemDialog_Shown(object sender, EventArgs e) {
103      SelectedType = null;
104    }
105
106    public virtual void Filter(string searchString) {
107      if (InvokeRequired) {
108        Invoke(new Action<string>(Filter), searchString);
109      } else {
110        searchString = searchString.ToLower();
111
112        if (!searchString.Contains(currentSearchString)) {
113          typesTreeView.BeginUpdate();
114          // expand saerch -> restore all tree nodes
115          var selectedNode = typesTreeView.SelectedNode;
116          typesTreeView.Nodes.Clear();
117          foreach (TreeNode node in treeNodes)
118            typesTreeView.Nodes.Add((TreeNode)node.Clone());
119          RestoreSelectedNode(selectedNode);
120          typesTreeView.EndUpdate();
121        }
122
123        // remove nodes
124        typesTreeView.BeginUpdate();
125        int i = 0;
126        var searchTokens = searchString.Split(' ');
127        while (i < typesTreeView.Nodes.Count) {
128          int j = 0;
129          while (j < typesTreeView.Nodes[i].Nodes.Count) {
130            if (searchTokens.Any(searchToken => !typesTreeView.Nodes[i].Nodes[j].Text.ToLower().Contains(searchToken))) {
131              if ((typesTreeView.Nodes[i].Nodes[j].Tag as Type).Equals(SelectedType))
132                SelectedType = null;
133              typesTreeView.Nodes[i].Nodes[j].Remove();
134            } else
135              j++;
136          }
137          if (typesTreeView.Nodes[i].Nodes.Count == 0)
138            typesTreeView.Nodes[i].Remove();
139          else
140            i++;
141        }
142        typesTreeView.EndUpdate();
143        currentSearchString = searchString;
144
145        // if there is just one type node left, select by default
146        if (typesTreeView.Nodes.Count == 1) {
147          if (typesTreeView.Nodes[0].Nodes.Count == 1) {
148            typesTreeView.SelectedNode = typesTreeView.Nodes[0].Nodes[0];
149          }
150        }
151
152        if (typesTreeView.Nodes.Count == 0) {
153          SelectedType = null;
154          typesTreeView.Enabled = false;
155        } else {
156          SetTreeNodeVisibility();
157          typesTreeView.Enabled = true;
158        }
159        UpdateDescription();
160      }
161    }
162
163    protected virtual void UpdateDescription() {
164      descriptionTextBox.Text = string.Empty;
165
166      if (typesTreeView.SelectedNode != null) {
167        string category = typesTreeView.SelectedNode.Tag as string;
168        if (category != null) {
169          descriptionTextBox.Text = category;
170          pluginTextBox.Text = string.Empty;
171          versionTextBox.Text = string.Empty;
172        }
173        Type type = typesTreeView.SelectedNode.Tag as Type;
174        if (type != null) {
175          string description = ItemAttribute.GetDescription(type);
176          var version = ItemAttribute.GetVersion(type);
177          var plugin = ApplicationManager.Manager.GetDeclaringPlugin(type);
178          if (description != null)
179            descriptionTextBox.Text = description;
180          if (plugin != null)
181            pluginTextBox.Text = plugin.Name;
182          if (version != null)
183            versionTextBox.Text = version.ToString();
184        }
185      } else if (typesTreeView.Nodes.Count == 0) {
186        descriptionTextBox.Text = "No types found";
187        pluginTextBox.Text = string.Empty;
188        versionTextBox.Text = string.Empty;
189      }
190    }
191
192    #region Events
193    public event EventHandler SelectedTypeChanged;
194    protected virtual void OnSelectedTypeChanged() {
195      if (SelectedTypeChanged != null)
196        SelectedTypeChanged(this, EventArgs.Empty);
197    }
198    #endregion
199
200    #region Control Events
201    protected virtual void searchTextBox_TextChanged(object sender, EventArgs e) {
202      Filter(searchTextBox.Text);
203    }
204
205    protected virtual void itemsTreeView_AfterSelect(object sender, TreeViewEventArgs e) {
206      if (typesTreeView.SelectedNode == null) SelectedType = null;
207      else SelectedType = typesTreeView.SelectedNode.Tag as Type;
208      UpdateDescription();
209    }
210
211    protected virtual void itemsTreeView_VisibleChanged(object sender, EventArgs e) {
212      if (Visible) SetTreeNodeVisibility();
213    }
214    #endregion
215
216    #region Helpers
217    private void RestoreSelectedNode(TreeNode selectedNode) {
218      if (selectedNode != null) {
219        foreach (TreeNode node in typesTreeView.Nodes) {
220          if (node.Text.Equals(selectedNode.Text)) typesTreeView.SelectedNode = node;
221          foreach (TreeNode child in node.Nodes) {
222            if ((child.Text.Equals(selectedNode.Text)) && (child.Tag == selectedNode.Tag))
223              typesTreeView.SelectedNode = child;
224          }
225        }
226        if (typesTreeView.SelectedNode == null) SelectedType = null;
227      }
228    }
229    private void SetTreeNodeVisibility() {
230      TreeNode selectedNode = typesTreeView.SelectedNode;
231      if (string.IsNullOrEmpty(currentSearchString) && (typesTreeView.Nodes.Count > 1)) {
232        typesTreeView.CollapseAll();
233        if (selectedNode != null) typesTreeView.SelectedNode = selectedNode;
234      } else {
235        typesTreeView.ExpandAll();
236      }
237      if (selectedNode != null) selectedNode.EnsureVisible();
238    }
239    #endregion
240
241    private void okButton_Click(object sender, EventArgs e) {
242      if (SelectedType != null) {
243        item = (IItem)Activator.CreateInstance(SelectedType);
244        DialogResult = DialogResult.OK;
245        Close();
246      }
247    }
248    private void itemTreeView_DoubleClick(object sender, EventArgs e) {
249      if (SelectedType != null) {
250        item = (IItem)Activator.CreateInstance(SelectedType);
251        DialogResult = DialogResult.OK;
252        Close();
253      }
254    }
255    private void this_SelectedTypeChanged(object sender, EventArgs e) {
256      okButton.Enabled = SelectedType != null;
257    }
258
259    private void expandAllButton_Click(object sender, EventArgs e) {
260      typesTreeView.ExpandAll();
261    }
262    private void collapseAllButton_Click(object sender, EventArgs e) {
263      typesTreeView.CollapseAll();
264    }
265  }
266}
Note: See TracBrowser for help on using the repository browser.