Free cookie consent management tool by TermsFeed Policy Generator

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

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

Merged changes from GP-refactoring branch back into the trunk #713.

File size: 5.1 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    private IFunctionTreeNameGenerator nameGenerator;
33    private IFunctionTreeNameGenerator[] allNameGenerators;
34    private MenuItem representationsMenu;
35
36    public FunctionTreeView() {
37      nameGenerator = new DefaultFunctionTreeNameGenerator();
38      InitializeComponent();
39
40      DiscoveryService discoveryService = new DiscoveryService();
41      allNameGenerators = discoveryService.GetInstances<IFunctionTreeNameGenerator>();
42      representationsMenu = new MenuItem();
43      representationsMenu.Text = "Tree representation";
44      representationsMenu.Name = "Tree representation";
45      foreach (IFunctionTreeNameGenerator generator in allNameGenerators) {
46        MenuItem mi = new MenuItem(generator.Name, MakeNameGeneratorDelegate(generator), Shortcut.None);
47        if (generator is DefaultFunctionTreeNameGenerator) mi.Checked = true;
48        else mi.Checked = false;
49        mi.Tag = generator;
50        representationsMenu.MenuItems.Add(mi);
51      }
52    }
53
54    public FunctionTreeView(IFunctionTree functionTree)
55      : this() {
56      this.functionTree = functionTree;
57      Refresh();
58    }
59
60    protected override void UpdateControls() {
61      funTreeView.Nodes.Clear();
62      TreeNode rootNode = new TreeNode();
63      rootNode.Name = functionTree.Function.Name;
64      rootNode.Text = nameGenerator.GetName(functionTree);
65      rootNode.Tag = functionTree;
66      treeNodeContextMenu.MenuItems.Clear();
67      treeNodeContextMenu.MenuItems.Add(representationsMenu);
68      DiscoveryService discoveryService = new DiscoveryService();
69      IFunctionTreeSerializer[] exporters = discoveryService.GetInstances<IFunctionTreeSerializer>();
70      foreach (IFunctionTreeSerializer exporter in exporters) {
71        string result;
72        // register a menu item for the exporter
73        MenuItem item = new MenuItem("Copy to clip-board (" + exporter.Name + ")",
74          MakeExporterDelegate(exporter), Shortcut.None);
75        // try to export the whole tree
76        if (exporter.TryExport(functionTree, out result)) {
77          // if it worked enable the context-menu item
78          item.Enabled = true;
79        } else {
80          item.Enabled = false;
81        }
82        treeNodeContextMenu.MenuItems.Add(item);
83      }
84      rootNode.ContextMenu = treeNodeContextMenu;
85      funTreeView.Nodes.Add(rootNode);
86
87      foreach (IFunctionTree subTree in functionTree.SubTrees) {
88        CreateTree(rootNode, subTree);
89      }
90      funTreeView.ExpandAll();
91    }
92
93    private EventHandler MakeNameGeneratorDelegate(IFunctionTreeNameGenerator generator) {
94      return delegate(object source, EventArgs args) {
95        IFunctionTreeNameGenerator g = (IFunctionTreeNameGenerator)((MenuItem)source).Tag;
96        this.nameGenerator = g;
97        foreach (MenuItem otherMenuItem in representationsMenu.MenuItems) otherMenuItem.Checked = false;
98        ((MenuItem)source).Checked = true;
99        UpdateControls();
100      };
101    }
102
103    private EventHandler MakeExporterDelegate(IFunctionTreeSerializer exporter) {
104      return delegate(object source, EventArgs args) {
105        TreeNode node = funTreeView.SelectedNode;
106        if (node == null || node.Tag == null) return;
107        Clipboard.SetText(exporter.Export((IFunctionTree)node.Tag));
108      };
109    }
110
111    private void CreateTree(TreeNode rootNode, IFunctionTree functionTree) {
112      TreeNode node = new TreeNode();
113      node.Name = functionTree.Function.Name;
114      node.Text = nameGenerator.GetName(functionTree);
115      node.Tag = functionTree;
116      node.ContextMenu = treeNodeContextMenu;
117      rootNode.Nodes.Add(node);
118      foreach (IFunctionTree subTree in functionTree.SubTrees) {
119        CreateTree(node, subTree);
120      }
121    }
122
123    private void funTreeView_MouseUp(object sender, MouseEventArgs e) {
124      if (e.Button == MouseButtons.Right) {
125        // Select the clicked node
126        funTreeView.SelectedNode = funTreeView.GetNodeAt(e.X, e.Y);
127      }
128    }
129  }
130}
Note: See TracBrowser for help on using the repository browser.