1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2013 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.Reflection;
|
---|
26 | using System.Text;
|
---|
27 | using System.Windows.Forms;
|
---|
28 | using HeuristicLab.Common;
|
---|
29 | using HeuristicLab.PluginInfrastructure;
|
---|
30 |
|
---|
31 | namespace HeuristicLab.Core.Views {
|
---|
32 | public partial class TypeSelector : UserControl {
|
---|
33 | protected List<TreeNode> treeNodes;
|
---|
34 | protected string currentSearchString;
|
---|
35 | protected TypeSelectorDialog typeSelectorDialog;
|
---|
36 |
|
---|
37 | protected IEnumerable<Type> baseTypes;
|
---|
38 | public IEnumerable<Type> BaseTypes {
|
---|
39 | get { return baseTypes; }
|
---|
40 | }
|
---|
41 | protected bool showNotInstantiableTypes;
|
---|
42 | public bool ShowNotInstantiableTypes {
|
---|
43 | get { return showNotInstantiableTypes; }
|
---|
44 | }
|
---|
45 | protected bool showGenericTypes;
|
---|
46 | public bool ShowGenericTypes {
|
---|
47 | get { return showGenericTypes; }
|
---|
48 | }
|
---|
49 | public string Caption {
|
---|
50 | get { return typesGroupBox.Text; }
|
---|
51 | set {
|
---|
52 | if (InvokeRequired)
|
---|
53 | Invoke(new Action<string>(delegate(string s) { Caption = s; }), value);
|
---|
54 | else
|
---|
55 | typesGroupBox.Text = value;
|
---|
56 | }
|
---|
57 | }
|
---|
58 | public TreeView TypesTreeView {
|
---|
59 | get { return typesTreeView; }
|
---|
60 | }
|
---|
61 | protected Type selectedType;
|
---|
62 | public Type SelectedType {
|
---|
63 | get { return selectedType; }
|
---|
64 | private set {
|
---|
65 | if (value != selectedType) {
|
---|
66 | selectedType = value;
|
---|
67 | OnSelectedTypeChanged();
|
---|
68 | }
|
---|
69 | }
|
---|
70 | }
|
---|
71 |
|
---|
72 | public TypeSelector() {
|
---|
73 | InitializeComponent();
|
---|
74 | treeNodes = new List<TreeNode>();
|
---|
75 | currentSearchString = string.Empty;
|
---|
76 | selectedType = null;
|
---|
77 | }
|
---|
78 |
|
---|
79 | protected override void Dispose(bool disposing) {
|
---|
80 | if (disposing) {
|
---|
81 | if (typeSelectorDialog != null) typeSelectorDialog.Dispose();
|
---|
82 | if (components != null) components.Dispose();
|
---|
83 | }
|
---|
84 | base.Dispose(disposing);
|
---|
85 | }
|
---|
86 |
|
---|
87 | public virtual void Configure(Type baseType, bool showNotInstantiableTypes, bool showGenericTypes) {
|
---|
88 | Configure(baseType, showNotInstantiableTypes, showGenericTypes, (t) => true);
|
---|
89 | }
|
---|
90 |
|
---|
91 | public virtual void Configure(Type baseType, bool showNotInstantiableTypes, bool showGenericTypes, Func<Type, bool> typeCondition) {
|
---|
92 | Configure(new List<Type>() { baseType }, showNotInstantiableTypes, showGenericTypes, true, typeCondition);
|
---|
93 | }
|
---|
94 |
|
---|
95 | public virtual void Configure(IEnumerable<Type> baseTypes, bool showNotInstantiableTypes, bool showGenericTypes, bool assignableToAllTypes) {
|
---|
96 | Configure(baseTypes, showNotInstantiableTypes, showGenericTypes, assignableToAllTypes, (t) => { return true; });
|
---|
97 | }
|
---|
98 |
|
---|
99 | public virtual void Configure(IEnumerable<Type> baseTypes, bool showNotInstantiableTypes, bool showGenericTypes, bool assignableToAllTypes, Func<Type, bool> typeCondition) {
|
---|
100 | if (baseTypes == null) throw new ArgumentNullException();
|
---|
101 | if (InvokeRequired)
|
---|
102 | Invoke(new Action<IEnumerable<Type>, bool, bool, bool, Func<Type, bool>>(Configure), baseTypes, showNotInstantiableTypes, showGenericTypes, assignableToAllTypes, typeCondition);
|
---|
103 | else {
|
---|
104 | this.baseTypes = baseTypes;
|
---|
105 | this.showNotInstantiableTypes = showNotInstantiableTypes;
|
---|
106 | this.showGenericTypes = showGenericTypes;
|
---|
107 | bool selectedTypeFound = false;
|
---|
108 |
|
---|
109 | typeParametersSplitContainer.Panel2Collapsed = !showGenericTypes;
|
---|
110 |
|
---|
111 | TreeNode selectedNode = typesTreeView.SelectedNode;
|
---|
112 | typesTreeView.Nodes.Clear();
|
---|
113 | treeNodes.Clear();
|
---|
114 |
|
---|
115 | imageList.Images.Clear();
|
---|
116 | imageList.Images.Add(HeuristicLab.Common.Resources.VSImageLibrary.Class); // default icon
|
---|
117 | imageList.Images.Add(HeuristicLab.Common.Resources.VSImageLibrary.Namespace); // plugins
|
---|
118 | imageList.Images.Add(HeuristicLab.Common.Resources.VSImageLibrary.Interface); // interfaces
|
---|
119 | imageList.Images.Add(HeuristicLab.Common.Resources.VSImageLibrary.Template); // generic types
|
---|
120 | // additional dictionary for image indexes as imageList.ContainsKey and imageList.IndexOfKey are very slow!
|
---|
121 | var imageNames = new Dictionary<string, int>();
|
---|
122 |
|
---|
123 | var plugins = from p in ApplicationManager.Manager.Plugins
|
---|
124 | orderby p.Name, p.Version ascending
|
---|
125 | select p;
|
---|
126 | foreach (IPluginDescription plugin in plugins) {
|
---|
127 | TreeNode pluginNode = new TreeNode();
|
---|
128 | pluginNode.Text = string.Format("{0} {1}.{2}", plugin.Name, plugin.Version.Major, plugin.Version.Minor);
|
---|
129 | pluginNode.ImageIndex = 1;
|
---|
130 | pluginNode.SelectedImageIndex = pluginNode.ImageIndex;
|
---|
131 | pluginNode.Tag = plugin;
|
---|
132 |
|
---|
133 | var types = from t in ApplicationManager.Manager.GetTypes(BaseTypes, plugin, !ShowNotInstantiableTypes, ShowGenericTypes, assignableToAllTypes)
|
---|
134 | where typeCondition(t)
|
---|
135 | orderby t.Name ascending
|
---|
136 | select t;
|
---|
137 | foreach (Type type in types) {
|
---|
138 | bool valid = (ShowNotInstantiableTypes || type.GetConstructor(Type.EmptyTypes) != null); //check for public default ctor
|
---|
139 | if (valid) {
|
---|
140 | TreeNode typeNode = new TreeNode();
|
---|
141 | string name = ItemAttribute.GetName(type);
|
---|
142 | typeNode.Text = name != null ? name : type.GetPrettyName();
|
---|
143 | typeNode.ImageIndex = 0;
|
---|
144 | if (type.IsInterface) typeNode.ImageIndex = 2;
|
---|
145 | else if (type.ContainsGenericParameters) typeNode.ImageIndex = 3;
|
---|
146 | else if (imageNames.ContainsKey(type.FullName)) typeNode.ImageIndex = imageNames[type.FullName];
|
---|
147 | else {
|
---|
148 | var image = ItemAttribute.GetImage(type);
|
---|
149 | if (image != null) {
|
---|
150 | imageList.Images.Add(image);
|
---|
151 | typeNode.ImageIndex = imageList.Images.Count - 1;
|
---|
152 | imageNames.Add(type.FullName, imageList.Images.Count - 1);
|
---|
153 | }
|
---|
154 | }
|
---|
155 | typeNode.SelectedImageIndex = typeNode.ImageIndex;
|
---|
156 | typeNode.Tag = type;
|
---|
157 | pluginNode.Nodes.Add(typeNode);
|
---|
158 | if (type.Equals(selectedType)) selectedTypeFound = true;
|
---|
159 | }
|
---|
160 | }
|
---|
161 | if (pluginNode.Nodes.Count > 0)
|
---|
162 | treeNodes.Add(pluginNode);
|
---|
163 | }
|
---|
164 | if (!selectedTypeFound) SelectedType = null;
|
---|
165 | foreach (TreeNode node in treeNodes)
|
---|
166 | typesTreeView.Nodes.Add((TreeNode)node.Clone());
|
---|
167 | RestoreSelectedNode(selectedNode);
|
---|
168 | Filter(searchTextBox.Text);
|
---|
169 |
|
---|
170 | UpdateTypeParameters();
|
---|
171 | }
|
---|
172 | }
|
---|
173 |
|
---|
174 | public virtual void Filter(string searchString) {
|
---|
175 | if (InvokeRequired)
|
---|
176 | Invoke(new Action<string>(Filter), searchString);
|
---|
177 | else {
|
---|
178 | searchString = searchString.ToLower();
|
---|
179 |
|
---|
180 | if (!searchString.Contains(currentSearchString)) {
|
---|
181 | typesTreeView.BeginUpdate();
|
---|
182 | // expand search -> restore all tree nodes
|
---|
183 | TreeNode selectedNode = typesTreeView.SelectedNode;
|
---|
184 | typesTreeView.Nodes.Clear();
|
---|
185 | foreach (TreeNode node in treeNodes)
|
---|
186 | typesTreeView.Nodes.Add((TreeNode)node.Clone());
|
---|
187 | RestoreSelectedNode(selectedNode);
|
---|
188 | typesTreeView.EndUpdate();
|
---|
189 | }
|
---|
190 |
|
---|
191 |
|
---|
192 | // remove nodes
|
---|
193 | typesTreeView.BeginUpdate();
|
---|
194 | int i = 0;
|
---|
195 | while (i < typesTreeView.Nodes.Count) {
|
---|
196 | int j = 0;
|
---|
197 | while (j < typesTreeView.Nodes[i].Nodes.Count) {
|
---|
198 | if (!typesTreeView.Nodes[i].Nodes[j].Text.ToLower().Contains(searchString)) {
|
---|
199 | if ((typesTreeView.Nodes[i].Nodes[j].Tag as Type).Equals(selectedType))
|
---|
200 | SelectedType = null;
|
---|
201 | typesTreeView.Nodes[i].Nodes[j].Remove();
|
---|
202 | } else
|
---|
203 | j++;
|
---|
204 | }
|
---|
205 | if (typesTreeView.Nodes[i].Nodes.Count == 0)
|
---|
206 | typesTreeView.Nodes[i].Remove();
|
---|
207 | else
|
---|
208 | i++;
|
---|
209 | }
|
---|
210 | typesTreeView.EndUpdate();
|
---|
211 | currentSearchString = searchString;
|
---|
212 |
|
---|
213 | // if there is just one type node left, select by default
|
---|
214 | if (typesTreeView.Nodes.Count == 1) {
|
---|
215 | if (typesTreeView.Nodes[0].Nodes.Count == 1) {
|
---|
216 | typesTreeView.SelectedNode = typesTreeView.Nodes[0].Nodes[0];
|
---|
217 | }
|
---|
218 | }
|
---|
219 |
|
---|
220 | if (typesTreeView.Nodes.Count == 0) {
|
---|
221 | SelectedType = null;
|
---|
222 | typesTreeView.Enabled = false;
|
---|
223 | } else {
|
---|
224 | SetTreeNodeVisibility();
|
---|
225 | typesTreeView.Enabled = true;
|
---|
226 | }
|
---|
227 | UpdateDescription();
|
---|
228 | }
|
---|
229 | }
|
---|
230 |
|
---|
231 | public virtual object CreateInstanceOfSelectedType(params object[] args) {
|
---|
232 | if (SelectedType == null)
|
---|
233 | throw new InvalidOperationException("No type selected.");
|
---|
234 | else
|
---|
235 | return Activator.CreateInstance(SelectedType, args);
|
---|
236 | }
|
---|
237 |
|
---|
238 | protected virtual void UpdateTypeParameters() {
|
---|
239 | typeParametersListView.Items.Clear();
|
---|
240 | if ((SelectedType == null) || !SelectedType.ContainsGenericParameters) {
|
---|
241 | typeParametersGroupBox.Enabled = false;
|
---|
242 | typeParametersSplitContainer.Panel2Collapsed = true;
|
---|
243 | } else {
|
---|
244 | typeParametersGroupBox.Enabled = true;
|
---|
245 | typeParametersSplitContainer.Panel2Collapsed = false;
|
---|
246 | setTypeParameterButton.Enabled = false;
|
---|
247 |
|
---|
248 | foreach (Type param in SelectedType.GetGenericArguments()) {
|
---|
249 | if (param.IsGenericParameter) {
|
---|
250 | ListViewItem item = new ListViewItem();
|
---|
251 | item.Text = param.Name;
|
---|
252 |
|
---|
253 | item.ToolTipText = "Constraints:";
|
---|
254 | Type[] constraints = param.GetGenericParameterConstraints();
|
---|
255 | if (constraints.Length == 0) {
|
---|
256 | item.ToolTipText += " none";
|
---|
257 | } else {
|
---|
258 | foreach (Type constraint in constraints)
|
---|
259 | item.ToolTipText += " " + constraint.GetPrettyName();
|
---|
260 | }
|
---|
261 |
|
---|
262 | item.Tag = param;
|
---|
263 | typeParametersListView.Items.Add(item);
|
---|
264 | }
|
---|
265 | }
|
---|
266 | typeParametersListView.Columns[0].AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent);
|
---|
267 | }
|
---|
268 | }
|
---|
269 |
|
---|
270 | protected virtual void SetTypeParameter() {
|
---|
271 | if (typeSelectorDialog == null) {
|
---|
272 | typeSelectorDialog = new TypeSelectorDialog();
|
---|
273 | typeSelectorDialog.Caption = "Select Type of Generic Type Parameter";
|
---|
274 | }
|
---|
275 | Type param = typeParametersListView.SelectedItems[0].Tag as Type;
|
---|
276 | Type[] constraints = param.GetGenericParameterConstraints();
|
---|
277 | bool showNotInstantiableTypes = !param.GenericParameterAttributes.HasFlag(GenericParameterAttributes.DefaultConstructorConstraint);
|
---|
278 | typeSelectorDialog.TypeSelector.Configure(constraints, showNotInstantiableTypes, true, true);
|
---|
279 |
|
---|
280 | if (typeSelectorDialog.ShowDialog(this) == DialogResult.OK) {
|
---|
281 | Type selected = typeSelectorDialog.TypeSelector.SelectedType;
|
---|
282 | Type[] parameters = SelectedType.GetGenericArguments();
|
---|
283 | parameters[param.GenericParameterPosition] = selected;
|
---|
284 | SelectedType = SelectedType.GetGenericTypeDefinition().MakeGenericType(parameters);
|
---|
285 |
|
---|
286 | typeParametersListView.SelectedItems[0].Text = param.Name + ": " + selected.GetPrettyName();
|
---|
287 | typeParametersListView.Columns[0].AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent);
|
---|
288 | }
|
---|
289 | }
|
---|
290 |
|
---|
291 | protected virtual void UpdateDescription() {
|
---|
292 | descriptionTextBox.Text = string.Empty;
|
---|
293 |
|
---|
294 | if (typesTreeView.SelectedNode != null) {
|
---|
295 | IPluginDescription plugin = typesTreeView.SelectedNode.Tag as IPluginDescription;
|
---|
296 | if (plugin != null) {
|
---|
297 | StringBuilder sb = new StringBuilder();
|
---|
298 | sb.Append("Plugin: ").Append(plugin.Name).Append(Environment.NewLine);
|
---|
299 | sb.Append("Version: ").Append(plugin.Version.ToString()).Append(Environment.NewLine);
|
---|
300 | descriptionTextBox.Text = sb.ToString();
|
---|
301 | }
|
---|
302 | Type type = typesTreeView.SelectedNode.Tag as Type;
|
---|
303 | if (type != null) {
|
---|
304 | string description = ItemAttribute.GetDescription(type);
|
---|
305 | if (description != null)
|
---|
306 | descriptionTextBox.Text = description;
|
---|
307 | }
|
---|
308 | } else if (typesTreeView.Nodes.Count == 0) {
|
---|
309 | descriptionTextBox.Text = "No types found";
|
---|
310 | }
|
---|
311 | }
|
---|
312 |
|
---|
313 | #region Events
|
---|
314 | public event EventHandler SelectedTypeChanged;
|
---|
315 | protected virtual void OnSelectedTypeChanged() {
|
---|
316 | if (SelectedTypeChanged != null)
|
---|
317 | SelectedTypeChanged(this, EventArgs.Empty);
|
---|
318 | }
|
---|
319 | #endregion
|
---|
320 |
|
---|
321 | #region Control Events
|
---|
322 | protected virtual void searchTextBox_TextChanged(object sender, System.EventArgs e) {
|
---|
323 | Filter(searchTextBox.Text);
|
---|
324 | }
|
---|
325 |
|
---|
326 | protected virtual void typesTreeView_AfterSelect(object sender, TreeViewEventArgs e) {
|
---|
327 | if (typesTreeView.SelectedNode == null) SelectedType = null;
|
---|
328 | else SelectedType = typesTreeView.SelectedNode.Tag as Type;
|
---|
329 | UpdateTypeParameters();
|
---|
330 | UpdateDescription();
|
---|
331 | }
|
---|
332 | protected virtual void typesTreeView_ItemDrag(object sender, ItemDragEventArgs e) {
|
---|
333 | TreeNode node = (TreeNode)e.Item;
|
---|
334 | Type type = node.Tag as Type;
|
---|
335 | if ((type != null) && (!type.IsInterface) && (!type.IsAbstract) && (!type.HasElementType) && (!type.ContainsGenericParameters)) {
|
---|
336 | object o = Activator.CreateInstance(type);
|
---|
337 | DataObject data = new DataObject();
|
---|
338 | data.SetData(HeuristicLab.Common.Constants.DragDropDataFormat, o);
|
---|
339 | DoDragDrop(data, DragDropEffects.Copy);
|
---|
340 | }
|
---|
341 | }
|
---|
342 | protected virtual void typesTreeView_VisibleChanged(object sender, EventArgs e) {
|
---|
343 | if (Visible) SetTreeNodeVisibility();
|
---|
344 | }
|
---|
345 |
|
---|
346 | protected virtual void typeParametersListView_SelectedIndexChanged(object sender, EventArgs e) {
|
---|
347 | setTypeParameterButton.Enabled = typeParametersListView.SelectedItems.Count == 1;
|
---|
348 | }
|
---|
349 | protected virtual void typeParametersListView_DoubleClick(object sender, EventArgs e) {
|
---|
350 | if (typeParametersListView.SelectedItems.Count == 1)
|
---|
351 | SetTypeParameter();
|
---|
352 | }
|
---|
353 |
|
---|
354 | protected virtual void setTypeParameterButton_Click(object sender, EventArgs e) {
|
---|
355 | SetTypeParameter();
|
---|
356 | }
|
---|
357 | #endregion
|
---|
358 |
|
---|
359 | #region Helpers
|
---|
360 | private void RestoreSelectedNode(TreeNode selectedNode) {
|
---|
361 | if (selectedNode != null) {
|
---|
362 | foreach (TreeNode node in typesTreeView.Nodes) {
|
---|
363 | if (node.Text.Equals(selectedNode.Text)) typesTreeView.SelectedNode = node;
|
---|
364 | foreach (TreeNode child in node.Nodes) {
|
---|
365 | if ((child.Text.Equals(selectedNode.Text)) && (child.Tag == selectedNode.Tag))
|
---|
366 | typesTreeView.SelectedNode = child;
|
---|
367 | }
|
---|
368 | }
|
---|
369 | if (typesTreeView.SelectedNode == null) SelectedType = null;
|
---|
370 | }
|
---|
371 | }
|
---|
372 | private void SetTreeNodeVisibility() {
|
---|
373 | TreeNode selectedNode = typesTreeView.SelectedNode;
|
---|
374 | if (string.IsNullOrEmpty(currentSearchString) && (typesTreeView.Nodes.Count > 1)) {
|
---|
375 | typesTreeView.CollapseAll();
|
---|
376 | if (selectedNode != null) typesTreeView.SelectedNode = selectedNode;
|
---|
377 | } else {
|
---|
378 | typesTreeView.ExpandAll();
|
---|
379 | }
|
---|
380 | if (selectedNode != null) selectedNode.EnsureVisible();
|
---|
381 | }
|
---|
382 | #endregion
|
---|
383 | }
|
---|
384 | }
|
---|