Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.GP/3.3/FunctionTreeView.cs @ 2584

Last change on this file since 2584 was 2237, checked in by gkronber, 15 years ago

Deleted unused field. #323

File size: 3.6 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.Windows.Forms;
24using HeuristicLab.Core;
25using HeuristicLab.PluginInfrastructure;
26using HeuristicLab.GP.Interfaces;
27
28namespace HeuristicLab.GP {
29  public partial class FunctionTreeView : ViewBase {
30    private IFunctionTree functionTree;
31
32    public FunctionTreeView() {
33      InitializeComponent();
34    }
35
36    public FunctionTreeView(IFunctionTree functionTree)
37      : this() {
38      this.functionTree = functionTree;
39      Refresh();
40    }
41
42    protected override void UpdateControls() {
43      funTreeView.Nodes.Clear();
44      TreeNode rootNode = new TreeNode();
45      rootNode.Name = functionTree.ToString();
46      rootNode.Text = functionTree.ToString();
47      rootNode.Tag = functionTree;
48      treeNodeContextMenu.MenuItems.Clear();
49      DiscoveryService discoveryService = new DiscoveryService();
50      IFunctionTreeSerializer[] exporters = discoveryService.GetInstances<IFunctionTreeSerializer>();
51      foreach (IFunctionTreeSerializer exporter in exporters) {
52        string result;
53        // register a menu item for the exporter
54        MenuItem item = new MenuItem("Copy to clip-board (" + exporter.Name + ")",
55          MakeExporterDelegate(exporter), Shortcut.None);
56        // try to export the whole tree
57        if (exporter.TryExport(functionTree, out result)) {
58          // if it worked enable the context-menu item
59          item.Enabled = true;
60        } else {
61          item.Enabled = false;
62        }
63        treeNodeContextMenu.MenuItems.Add(item);
64      }
65      rootNode.ContextMenu = treeNodeContextMenu;
66      funTreeView.Nodes.Add(rootNode);
67
68      foreach (IFunctionTree subTree in functionTree.SubTrees) {
69        CreateTree(rootNode, subTree);
70      }
71      funTreeView.ExpandAll();
72    }
73
74    private EventHandler MakeExporterDelegate(IFunctionTreeSerializer exporter) {
75      return delegate(object source, EventArgs args) {
76        TreeNode node = funTreeView.SelectedNode;
77        if (node == null || node.Tag == null) return;
78        Clipboard.SetText(exporter.Export((IFunctionTree)node.Tag));
79      };
80    }
81
82    private void CreateTree(TreeNode rootNode, IFunctionTree functionTree) {
83      TreeNode node = new TreeNode();
84      node.Name = functionTree.ToString();
85      node.Text = functionTree.ToString();
86      node.Tag = functionTree;
87      node.ContextMenu = treeNodeContextMenu;
88      rootNode.Nodes.Add(node);
89      foreach (IFunctionTree subTree in functionTree.SubTrees) {
90        CreateTree(node, subTree);
91      }
92    }
93
94    private void funTreeView_MouseUp(object sender, MouseEventArgs e) {
95      if (e.Button == MouseButtons.Right) {
96        // Select the clicked node
97        funTreeView.SelectedNode = funTreeView.GetNodeAt(e.X, e.Y);
98      }
99    }
100  }
101}
Note: See TracBrowser for help on using the repository browser.