Free cookie consent management tool by TermsFeed Policy Generator

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

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

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