Free cookie consent management tool by TermsFeed Policy Generator

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

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

Operator architecture refactoring (#95)

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