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