Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2025

  • Collapse tree per default when opening.
  • Added Plugin and Version textboxes.
  • Moved expand/collapse-all buttons.
File size: 9.9 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      imageList.Images.Add(HeuristicLab.Common.Resources.VSImageLibrary.Interface);  // interfacesitemsListView.SmallImageList = new ImageList();
73      imageList.Images.Add(HeuristicLab.Common.Resources.VSImageLibrary.Template);   // generic types
74
75      foreach (var category in categories) {
76        var categoryNode = new TreeNode(category.Key) {
77          ImageIndex = 1,
78          Tag = category.Key,
79        };
80        categoryNode.SelectedImageIndex = categoryNode.ImageIndex;
81
82        foreach (var creatable in category) {
83          string name = ItemAttribute.GetName(creatable);
84          string version = ItemAttribute.GetVersion(creatable).ToString();
85          string description = ItemAttribute.GetDescription(creatable);
86
87          var itemNode = new TreeNode(name) {
88            ImageIndex = 0,
89            Tag = creatable
90          };
91          itemNode.SelectedImageIndex = itemNode.ImageIndex;
92          categoryNode.Nodes.Add(itemNode);
93
94          /*
95          ListViewItem item = new ListViewItem(new string[] { name, version, description }, group);
96          item.ImageIndex = 0;
97          Image image = ItemAttribute.GetImage(creatable);
98          if (image != null) {
99            itemsListView.SmallImageList.Images.Add(image);
100            item.ImageIndex = itemsListView.SmallImageList.Images.Count - 1;
101          }
102          item.Tag = creatable;
103          itemsListView.Items.Add(item);*/
104        }
105        if (categoryNode.Nodes.Count > 0)
106          treeNodes.Add(categoryNode);
107      }
108      foreach (var node in treeNodes)
109        typesTreeView.Nodes.Add((TreeNode)node.Clone());
110      isInitialized = true;
111    }
112    private void NewItemDialog_Shown(object sender, EventArgs e) {
113      SelectedType = null;
114    }
115
116    public virtual void Filter(string searchString) {
117      if (InvokeRequired) {
118        Invoke(new Action<string>(Filter), searchString);
119      } else {
120        searchString = searchString.ToLower();
121
122        if (!searchString.Contains(currentSearchString)) {
123          typesTreeView.BeginUpdate();
124          // expand saerch -> restore all tree nodes
125          var selectedNode = typesTreeView.SelectedNode;
126          typesTreeView.Nodes.Clear();
127          foreach (TreeNode node in treeNodes)
128            typesTreeView.Nodes.Add((TreeNode)node.Clone());
129          RestoreSelectedNode(selectedNode);
130          typesTreeView.EndUpdate();
131        }
132
133        // remove nodes
134        typesTreeView.BeginUpdate();
135        int i = 0;
136        var searchTokens = searchString.Split(' ');
137        while (i < typesTreeView.Nodes.Count) {
138          int j = 0;
139          while (j < typesTreeView.Nodes[i].Nodes.Count) {
140            if (searchTokens.Any(searchToken => !typesTreeView.Nodes[i].Nodes[j].Text.ToLower().Contains(searchToken))) {
141              if ((typesTreeView.Nodes[i].Nodes[j].Tag as Type).Equals(SelectedType))
142                SelectedType = null;
143              typesTreeView.Nodes[i].Nodes[j].Remove();
144            } else
145              j++;
146          }
147          if (typesTreeView.Nodes[i].Nodes.Count == 0)
148            typesTreeView.Nodes[i].Remove();
149          else
150            i++;
151        }
152        typesTreeView.EndUpdate();
153        currentSearchString = searchString;
154
155        // if there is just one type node left, select by default
156        if (typesTreeView.Nodes.Count == 1) {
157          if (typesTreeView.Nodes[0].Nodes.Count == 1) {
158            typesTreeView.SelectedNode = typesTreeView.Nodes[0].Nodes[0];
159          }
160        }
161
162        if (typesTreeView.Nodes.Count == 0) {
163          SelectedType = null;
164          typesTreeView.Enabled = false;
165        } else {
166          SetTreeNodeVisibility();
167          typesTreeView.Enabled = true;
168        }
169        UpdateDescription();
170      }
171    }
172
173    protected virtual void UpdateDescription() {
174      descriptionTextBox.Text = string.Empty;
175
176      if (typesTreeView.SelectedNode != null) {
177        string category = typesTreeView.SelectedNode.Tag as string;
178        if (category != null) {
179          descriptionTextBox.Text = category;
180          pluginTextBox.Text = string.Empty;
181          versionTextBox.Text = string.Empty;
182        }
183        Type type = typesTreeView.SelectedNode.Tag as Type;
184        if (type != null) {
185          string description = ItemAttribute.GetDescription(type);
186          var version = ItemAttribute.GetVersion(type);
187          var plugin = ApplicationManager.Manager.GetDeclaringPlugin(type);
188          if (description != null)
189            descriptionTextBox.Text = description;
190          if (plugin != null)
191            pluginTextBox.Text = plugin.Name;
192          if (version != null)
193            versionTextBox.Text = version.ToString();
194        }
195      } else if (typesTreeView.Nodes.Count == 0) {
196        descriptionTextBox.Text = "No types found";
197        pluginTextBox.Text = string.Empty;
198        versionTextBox.Text = string.Empty;
199      }
200    }
201
202    #region Events
203    public event EventHandler SelectedTypeChanged;
204    protected virtual void OnSelectedTypeChanged() {
205      if (SelectedTypeChanged != null)
206        SelectedTypeChanged(this, EventArgs.Empty);
207    }
208    #endregion
209
210    #region Control Events
211    protected virtual void searchTextBox_TextChanged(object sender, EventArgs e) {
212      Filter(searchTextBox.Text);
213    }
214
215    protected virtual void itemsTreeView_AfterSelect(object sender, TreeViewEventArgs e) {
216      if (typesTreeView.SelectedNode == null) SelectedType = null;
217      else SelectedType = typesTreeView.SelectedNode.Tag as Type;
218      UpdateDescription();
219    }
220
221    protected virtual void itemsTreeView_VisibleChanged(object sender, EventArgs e) {
222      if (Visible) SetTreeNodeVisibility();
223    }
224    #endregion
225
226    #region Helpers
227    private void RestoreSelectedNode(TreeNode selectedNode) {
228      if (selectedNode != null) {
229        foreach (TreeNode node in typesTreeView.Nodes) {
230          if (node.Text.Equals(selectedNode.Text)) typesTreeView.SelectedNode = node;
231          foreach (TreeNode child in node.Nodes) {
232            if ((child.Text.Equals(selectedNode.Text)) && (child.Tag == selectedNode.Tag))
233              typesTreeView.SelectedNode = child;
234          }
235        }
236        if (typesTreeView.SelectedNode == null) SelectedType = null;
237      }
238    }
239    private void SetTreeNodeVisibility() {
240      TreeNode selectedNode = typesTreeView.SelectedNode;
241      if (string.IsNullOrEmpty(currentSearchString) && (typesTreeView.Nodes.Count > 1)) {
242        typesTreeView.CollapseAll();
243        if (selectedNode != null) typesTreeView.SelectedNode = selectedNode;
244      } else {
245        typesTreeView.ExpandAll();
246      }
247      if (selectedNode != null) selectedNode.EnsureVisible();
248    }
249    #endregion
250
251    private void okButton_Click(object sender, EventArgs e) {
252      if (SelectedType != null) {
253        item = (IItem)Activator.CreateInstance(SelectedType);
254        DialogResult = DialogResult.OK;
255        Close();
256      }
257    }
258    private void itemTreeView_DoubleClick(object sender, EventArgs e) {
259      if (SelectedType != null) {
260        item = (IItem)Activator.CreateInstance(SelectedType);
261        DialogResult = DialogResult.OK;
262        Close();
263      }
264    }
265    private void this_SelectedTypeChanged(object sender, EventArgs e) {
266      okButton.Enabled = SelectedType != null;
267    }
268
269    private void expandAllButton_Click(object sender, EventArgs e) {
270      typesTreeView.ExpandAll();
271    }
272    private void collapseAllButton_Click(object sender, EventArgs e) {
273      typesTreeView.CollapseAll();
274    }
275  }
276}
Note: See TracBrowser for help on using the repository browser.