source: branches/OptimizationNetworks/HeuristicLab.Optimization.Networks.Views/3.3/Core.Networks.Views/NetworkItemSelector.cs @ 11528

Last change on this file since 11528 was 11528, checked in by swagner, 8 years ago

#2205: Implemented review comments

  • adapted formatting of multi-line LINQ queries
  • renamed Entity to NetworkItem
File size: 5.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2014 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 HeuristicLab.Common;
23using System;
24using System.Windows.Forms;
25
26namespace HeuristicLab.Core.Networks.Views {
27  public partial class NetworkItemSelector : UserControl {
28    protected Func<INetworkItem, bool> networkItemCondition;
29    protected INetworkItem root;
30    public INetworkItem Root {
31      get { return root; }
32    }
33    public string Caption {
34      get { return networkItemsGroupBox.Text; }
35      set {
36        if (InvokeRequired)
37          Invoke(new Action<string>(s => { Caption = s; }), value);
38        else
39          networkItemsGroupBox.Text = value;
40      }
41    }
42    public TreeView NetworkItemsTreeView {
43      get { return networkItemsTreeView; }
44    }
45    protected INetworkItem selectedNetworkItem;
46    public INetworkItem SelectedNetworkItem {
47      get {
48        return selectedNetworkItem;
49      }
50      protected set {
51        if (selectedNetworkItem != value) {
52          selectedNetworkItem = value;
53          OnSelectedNetworkItemChanged();
54        }
55      }
56    }
57
58    public NetworkItemSelector() {
59      InitializeComponent();
60      root = null;
61      networkItemCondition = null;
62      selectedNetworkItem = null;
63    }
64
65    public virtual void Configure(INetworkItem root) {
66      Configure(root, null, (e) => true);
67    }
68    public virtual void Configure(INetworkItem root, INetworkItem selectedNetworkItem) {
69      Configure(root, selectedNetworkItem, (e) => true);
70    }
71    public virtual void Configure(INetworkItem root, INetworkItem selectedNetworkItem, Type networkItemType) {
72      Configure(root, selectedNetworkItem, (e) => { return networkItemType.IsAssignableFrom(e.GetType()); });
73    }
74    public virtual void Configure(INetworkItem root, INetworkItem selectedNetworkItem, Func<INetworkItem, bool> networkItemCondition) {
75      if (root == null) throw new ArgumentNullException("root");
76      if (networkItemCondition == null) throw new ArgumentNullException("networkItemCondition");
77      if (InvokeRequired)
78        Invoke(new Action<INetworkItem, INetworkItem, Func<INetworkItem, bool>>(Configure), root, selectedNetworkItem, networkItemCondition);
79      else {
80        this.root = root;
81        this.networkItemCondition = networkItemCondition;
82        this.selectedNetworkItem = null;
83        networkItemsTreeView.Nodes.Clear();
84        imageList.Images.Clear();
85
86        TreeNode selectedNode;
87        var n = CreateTreeNode(Root, selectedNetworkItem, networkItemCondition, out selectedNode);
88        if (n != null)
89          networkItemsTreeView.Nodes.Add(n);
90        if (selectedNode != null) {
91          networkItemsTreeView.SelectedNode = selectedNode;
92          selectedNode.EnsureVisible();
93        }
94      }
95    }
96
97    public event EventHandler SelectedNetworkItemChanged;
98    protected virtual void OnSelectedNetworkItemChanged() {
99      var handler = SelectedNetworkItemChanged;
100      if (handler != null) SelectedNetworkItemChanged(this, EventArgs.Empty);
101    }
102
103    #region Control Events
104    protected virtual void networkItemsTreeView_AfterSelect(object sender, TreeViewEventArgs e) {
105      if (networkItemsTreeView.SelectedNode == null)
106        SelectedNetworkItem = null;
107      else {
108        var selected = (INetworkItem)networkItemsTreeView.SelectedNode.Tag;
109        SelectedNetworkItem = ((networkItemCondition != null) && !networkItemCondition(selected)) ? null : selected;
110      }
111    }
112    #endregion
113
114    #region Helpers
115    protected virtual TreeNode CreateTreeNode(INetworkItem networkItem, INetworkItem selectedNetworkItem, Func<INetworkItem, bool> networkItemCondition, out TreeNode selectedNode) {
116      selectedNode = null;
117      if (networkItem == null) return null;
118
119      TreeNode node = new TreeNode(networkItem.Name);
120      node.Tag = networkItem;
121      foreach (var child in networkItem.Children) {
122        TreeNode selected;
123        var n = CreateTreeNode(child, selectedNetworkItem, networkItemCondition, out selected);
124        if (n != null) {
125          node.Nodes.Add(n);
126          if (selected != null) selectedNode = selected;
127        }
128
129      }
130      if (!networkItemCondition(networkItem) && (node.Nodes.Count == 0))
131        return null;
132
133      imageList.Images.Add(networkItem.ItemImage);
134      node.ImageIndex = imageList.Images.Count - 1;
135      node.SelectedImageIndex = node.ImageIndex;
136      node.ToolTipText = networkItem.GetType().GetPrettyName();
137
138      if (networkItem == selectedNetworkItem) selectedNode = node;
139      return node;
140    }
141    #endregion
142  }
143}
Note: See TracBrowser for help on using the repository browser.