Free cookie consent management tool by TermsFeed Policy Generator

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

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

Committing first results of the refactoring of HeuristicLab.Core and related plugins (#95)

File size: 8.0 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2008 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.PluginInfrastructure;
31using HeuristicLab.MainForm;
32
33namespace HeuristicLab.Core.Views {
34  public partial class TypeSelector : UserControl {
35    private Type baseType;
36    public Type BaseType {
37      get { return baseType; }
38    }
39    private bool showNotInstantiableTypes;
40    public bool ShowNotInstantiableTypes {
41      get { return showNotInstantiableTypes; }
42    }
43    private bool showGenericTypes;
44    public bool ShowGenericTypes {
45      get { return showGenericTypes; }
46    }
47    public string Caption {
48      get { return groupBox.Text; }
49      set {
50        if (InvokeRequired)
51          Invoke(new Action<string>(delegate(string s) { Caption = s; }), value);
52        else
53          groupBox.Text = value;
54      }
55    }
56    public TreeView TypesTreeView {
57      get { return typesTreeView; }
58    }
59    private Type selectedType;
60    public Type SelectedType {
61      get { return selectedType; }
62      private set {
63        if (value != selectedType) {
64          selectedType = value;
65          OnSelectedTypeChanged();
66        }
67      }
68    }
69
70    public TypeSelector() {
71      InitializeComponent();
72      selectedType = null;
73    }
74
75    public void Configure(Type baseType, bool showNotInstantiableTypes, bool showGenericTypes) {
76      if (baseType == null) throw new ArgumentNullException();
77      if (InvokeRequired)
78        Invoke(new Action<Type, bool, bool>(Configure), baseType, showNotInstantiableTypes, showGenericTypes);
79      else {
80        this.baseType = baseType;
81        this.showNotInstantiableTypes = showNotInstantiableTypes;
82        this.showGenericTypes = showGenericTypes;
83
84        typesTreeView.Nodes.Clear();
85        imageList.Images.Clear();
86        imageList.Images.Add(HeuristicLab.Common.Resources.VS2008ImageLibrary.Class);      // default icon
87        imageList.Images.Add(HeuristicLab.Common.Resources.VS2008ImageLibrary.Namespace);  // plugins
88        imageList.Images.Add(HeuristicLab.Common.Resources.VS2008ImageLibrary.Interface);  // interfaces
89        imageList.Images.Add(HeuristicLab.Common.Resources.VS2008ImageLibrary.OrgChart);   // abstract types
90        imageList.Images.Add(HeuristicLab.Common.Resources.VS2008ImageLibrary.Template);   // generic types
91
92        TreeNode selectedNode = null;
93        var plugins = from p in ApplicationManager.Manager.Plugins
94                      orderby p.Name, p.Version ascending
95                      select p;
96        foreach (IPluginDescription plugin in plugins) {
97          TreeNode pluginNode = new TreeNode();
98          pluginNode.Text = plugin.Name;
99          pluginNode.ImageIndex = 1;
100          pluginNode.SelectedImageIndex = pluginNode.ImageIndex;
101          pluginNode.Tag = plugin;
102
103          var types = from t in ApplicationManager.Manager.GetTypes(BaseType, plugin, false)
104                      orderby t.Name ascending
105                      select t;
106          foreach (Type type in types) {
107            bool valid = true;
108            valid = valid && (ShowGenericTypes || !type.ContainsGenericParameters);
109            valid = valid && (ShowNotInstantiableTypes || !type.IsAbstract);
110            valid = valid && (ShowNotInstantiableTypes || !type.IsInterface);
111            valid = valid && (ShowNotInstantiableTypes || !type.HasElementType);
112            if (valid) {
113              TreeNode typeNode = new TreeNode();
114              string name = ItemAttribute.GetName(type);
115              typeNode.Text = name != null ? name : type.Name;
116              typeNode.ImageIndex = 0;
117              if (type.IsInterface) typeNode.ImageIndex = 2;
118              else if (type.IsAbstract) typeNode.ImageIndex = 3;
119              else if (type.ContainsGenericParameters) typeNode.ImageIndex = 4;
120              else if (imageList.Images.ContainsKey(type.FullName)) typeNode.ImageIndex = imageList.Images.IndexOfKey(type.FullName);
121              else if (typeof(IItem).IsAssignableFrom(type)) {
122                try {
123                  IItem item = (IItem)Activator.CreateInstance(type);
124                  imageList.Images.Add(type.FullName, item.ItemImage);
125                  typeNode.ImageIndex = imageList.Images.IndexOfKey(type.FullName);
126                }
127                catch (Exception) {
128                }
129              }
130              typeNode.SelectedImageIndex = typeNode.ImageIndex;
131              typeNode.Tag = type;
132              if (type == SelectedType) selectedNode = typeNode;
133              pluginNode.Nodes.Add(typeNode);
134            }
135          }
136          if (pluginNode.Nodes.Count > 0)
137            typesTreeView.Nodes.Add(pluginNode);
138        }
139        if (typesTreeView.Nodes.Count == 0) {
140          typesTreeView.Nodes.Add("No types of base type \"" + BaseType.Name + "\" found");
141          typesTreeView.Enabled = false;
142        }
143        if (selectedNode != null) {
144          typesTreeView.SelectedNode = selectedNode;
145          selectedNode.EnsureVisible();
146        } else {
147          SelectedType = null;
148        }
149      }
150    }
151
152    public object CreateInstanceOfSelectedType(params object[] args) {
153      if (SelectedType != null) {
154      try {
155        return Activator.CreateInstance(SelectedType, args);
156      } catch(Exception) { }
157      }
158      return null;
159    }
160
161    public event EventHandler SelectedTypeChanged;
162    protected virtual void OnSelectedTypeChanged() {
163      if (SelectedTypeChanged != null)
164        SelectedTypeChanged(this, EventArgs.Empty);
165    }
166
167    private void UpdateDescription() {
168      descriptionTextBox.Text = string.Empty;
169
170      if (typesTreeView.SelectedNode != null) {
171        IPluginDescription plugin = typesTreeView.SelectedNode.Tag as IPluginDescription;
172        if (plugin != null) {
173          StringBuilder sb = new StringBuilder();
174          sb.Append("Plugin: ").Append(plugin.Name).Append(Environment.NewLine);
175          sb.Append("Version: ").Append(plugin.Version.ToString()).Append(Environment.NewLine);
176          sb.Append("Build Date: ").Append(plugin.BuildDate.ToString());
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    private 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    private 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.Link);
205        } catch (Exception) {
206        }
207      }
208    }
209  }
210}
Note: See TracBrowser for help on using the repository browser.