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