Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Core.Views/3.3/TypeSelector.cs @ 2792

Last change on this file since 2792 was 2790, checked in by swagner, 14 years ago

Operator architecture refactoring (#95)

  • implemented reviewers' comments
  • added additional plugins HeuristicLab.Evolutionary, HeuristicLab.Permutation, HeuristicLab.Selection, and HeuristicLab.Routing.TSP
File size: 8.1 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 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.ComponentModel;
25using System.Drawing;
26using System.Data;
27using System.Text;
28using System.Windows.Forms;
29using System.Linq;
30using HeuristicLab.Common;
31using HeuristicLab.PluginInfrastructure;
32using HeuristicLab.MainForm;
33
34namespace HeuristicLab.Core.Views {
35  public partial class TypeSelector : UserControl {
36    protected Type baseType;
37    public Type BaseType {
38      get { return baseType; }
39    }
40    protected bool showNotInstantiableTypes;
41    public bool ShowNotInstantiableTypes {
42      get { return showNotInstantiableTypes; }
43    }
44    protected bool showGenericTypes;
45    public bool ShowGenericTypes {
46      get { return showGenericTypes; }
47    }
48    public string Caption {
49      get { return typesGroupBox.Text; }
50      set {
51        if (InvokeRequired)
52          Invoke(new Action<string>(delegate(string s) { Caption = s; }), value);
53        else
54          typesGroupBox.Text = value;
55      }
56    }
57    public TreeView TypesTreeView {
58      get { return typesTreeView; }
59    }
60    protected Type selectedType;
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();
73      selectedType = null;
74    }
75
76    public virtual void Configure(Type baseType, bool showNotInstantiableTypes, bool showGenericTypes) {
77      if (baseType == null) throw new ArgumentNullException();
78      if (InvokeRequired)
79        Invoke(new Action<Type, bool, bool>(Configure), baseType, showNotInstantiableTypes, showGenericTypes);
80      else {
81        this.baseType = baseType;
82        this.showNotInstantiableTypes = showNotInstantiableTypes;
83        this.showGenericTypes = showGenericTypes;
84
85        typesTreeView.Nodes.Clear();
86        imageList.Images.Clear();
87        imageList.Images.Add(HeuristicLab.Common.Resources.VS2008ImageLibrary.Class);      // default icon
88        imageList.Images.Add(HeuristicLab.Common.Resources.VS2008ImageLibrary.Namespace);  // plugins
89        imageList.Images.Add(HeuristicLab.Common.Resources.VS2008ImageLibrary.Interface);  // interfaces
90        imageList.Images.Add(HeuristicLab.Common.Resources.VS2008ImageLibrary.OrgChart);   // abstract types
91        imageList.Images.Add(HeuristicLab.Common.Resources.VS2008ImageLibrary.Template);   // generic types
92
93        TreeNode selectedNode = null;
94        var plugins = from p in ApplicationManager.Manager.Plugins
95                      orderby p.Name, p.Version ascending
96                      select p;
97        foreach (IPluginDescription plugin in plugins) {
98          TreeNode pluginNode = new TreeNode();
99          pluginNode.Text = plugin.Name;
100          pluginNode.ImageIndex = 1;
101          pluginNode.SelectedImageIndex = pluginNode.ImageIndex;
102          pluginNode.Tag = plugin;
103
104          var types = from t in ApplicationManager.Manager.GetTypes(BaseType, plugin, false)
105                      orderby t.Name ascending
106                      select t;
107          foreach (Type type in types) {
108            bool valid = true;
109            valid = valid && (ShowGenericTypes || !type.ContainsGenericParameters);
110            valid = valid && (ShowNotInstantiableTypes || !type.IsAbstract);
111            valid = valid && (ShowNotInstantiableTypes || !type.IsInterface);
112            valid = valid && (ShowNotInstantiableTypes || !type.HasElementType);
113            if (valid) {
114              TreeNode typeNode = new TreeNode();
115              string name = ItemAttribute.GetName(type);
116              typeNode.Text = name != null ? name : type.GetPrettyName();
117              typeNode.ImageIndex = 0;
118              if (type.IsInterface) typeNode.ImageIndex = 2;
119              else if (type.IsAbstract) typeNode.ImageIndex = 3;
120              else if (type.ContainsGenericParameters) typeNode.ImageIndex = 4;
121              else if (imageList.Images.ContainsKey(type.FullName)) typeNode.ImageIndex = imageList.Images.IndexOfKey(type.FullName);
122              else if (typeof(IItem).IsAssignableFrom(type)) {
123                try {
124                  IItem item = (IItem)Activator.CreateInstance(type);
125                  imageList.Images.Add(type.FullName, item.ItemImage);
126                  typeNode.ImageIndex = imageList.Images.IndexOfKey(type.FullName);
127                }
128                catch (Exception) {
129                }
130              }
131              typeNode.SelectedImageIndex = typeNode.ImageIndex;
132              typeNode.Tag = type;
133              if (type == SelectedType) selectedNode = typeNode;
134              pluginNode.Nodes.Add(typeNode);
135            }
136          }
137          if (pluginNode.Nodes.Count > 0)
138            typesTreeView.Nodes.Add(pluginNode);
139        }
140        if (typesTreeView.Nodes.Count == 0) {
141          typesTreeView.Nodes.Add("No types of base type \"" + BaseType.GetPrettyName() + "\" found");
142          typesTreeView.Enabled = false;
143        }
144        if (selectedNode != null) {
145          typesTreeView.SelectedNode = selectedNode;
146          selectedNode.EnsureVisible();
147        } else {
148          SelectedType = null;
149        }
150      }
151    }
152
153    public virtual object CreateInstanceOfSelectedType(params object[] args) {
154      if (SelectedType != null) {
155      try {
156        return Activator.CreateInstance(SelectedType, args);
157      } catch(Exception) { }
158      }
159      return null;
160    }
161
162    public event EventHandler SelectedTypeChanged;
163    protected virtual void OnSelectedTypeChanged() {
164      if (SelectedTypeChanged != null)
165        SelectedTypeChanged(this, EventArgs.Empty);
166    }
167
168    protected virtual void UpdateDescription() {
169      descriptionTextBox.Text = string.Empty;
170
171      if (typesTreeView.SelectedNode != null) {
172        IPluginDescription plugin = typesTreeView.SelectedNode.Tag as IPluginDescription;
173        if (plugin != null) {
174          StringBuilder sb = new StringBuilder();
175          sb.Append("Plugin: ").Append(plugin.Name).Append(Environment.NewLine);
176          sb.Append("Version: ").Append(plugin.Version.ToString()).Append(Environment.NewLine);
177          descriptionTextBox.Text = sb.ToString();
178        }
179        Type type = typesTreeView.SelectedNode.Tag as Type;
180        if (type != null) {
181          string description = ItemAttribute.GetDescription(type);
182          if (description != null)
183            descriptionTextBox.Text = description;
184        }
185      }
186    }
187
188    protected virtual void typesTreeView_AfterSelect(object sender, TreeViewEventArgs e) {
189      if (typesTreeView.SelectedNode != null) {
190        SelectedType = typesTreeView.SelectedNode.Tag as Type;
191      }
192      UpdateDescription();
193    }
194
195    protected virtual void typesTreeView_ItemDrag(object sender, ItemDragEventArgs e) {
196      TreeNode node = (TreeNode)e.Item;
197      Type type = node.Tag as Type;
198      if (type != null) {
199        try {
200          object o = Activator.CreateInstance(type);
201          DataObject data = new DataObject();
202          data.SetData("Type", type);
203          data.SetData("Value", o);
204          DoDragDrop(data, DragDropEffects.Copy);
205        } catch (Exception) {
206        }
207      }
208    }
209  }
210}
Note: See TracBrowser for help on using the repository browser.