Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3.2/sources/HeuristicLab.GP/3.3/FunctionTreeView.cs @ 4062

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

Copied refactored plugin infrastructure from branch and merged changeset r2586:2589 from branch into the trunk. #799

File size: 3.5 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      foreach (IFunctionTreeSerializer exporter in ApplicationManager.Manager.GetInstances<IFunctionTreeSerializer>()) {
50        string result;
51        // register a menu item for the exporter
52        MenuItem item = new MenuItem("Copy to clip-board (" + exporter.Name + ")",
53          MakeExporterDelegate(exporter), Shortcut.None);
54        // try to export the whole tree
55        if (exporter.TryExport(functionTree, out result)) {
56          // if it worked enable the context-menu item
57          item.Enabled = true;
58        } else {
59          item.Enabled = false;
60        }
61        treeNodeContextMenu.MenuItems.Add(item);
62      }
63      rootNode.ContextMenu = treeNodeContextMenu;
64      funTreeView.Nodes.Add(rootNode);
65
66      foreach (IFunctionTree subTree in functionTree.SubTrees) {
67        CreateTree(rootNode, subTree);
68      }
69      funTreeView.ExpandAll();
70    }
71
72    private EventHandler MakeExporterDelegate(IFunctionTreeSerializer exporter) {
73      return delegate(object source, EventArgs args) {
74        TreeNode node = funTreeView.SelectedNode;
75        if (node == null || node.Tag == null) return;
76        Clipboard.SetText(exporter.Export((IFunctionTree)node.Tag));
77      };
78    }
79
80    private void CreateTree(TreeNode rootNode, IFunctionTree functionTree) {
81      TreeNode node = new TreeNode();
82      node.Name = functionTree.ToString();
83      node.Text = functionTree.ToString();
84      node.Tag = functionTree;
85      node.ContextMenu = treeNodeContextMenu;
86      rootNode.Nodes.Add(node);
87      foreach (IFunctionTree subTree in functionTree.SubTrees) {
88        CreateTree(node, subTree);
89      }
90    }
91
92    private void funTreeView_MouseUp(object sender, MouseEventArgs e) {
93      if (e.Button == MouseButtons.Right) {
94        // Select the clicked node
95        funTreeView.SelectedNode = funTreeView.GetNodeAt(e.X, e.Y);
96      }
97    }
98  }
99}
Note: See TracBrowser for help on using the repository browser.