[645] | 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 |
|
---|
| 22 | using System;
|
---|
| 23 | using System.Windows.Forms;
|
---|
| 24 | using HeuristicLab.Core;
|
---|
| 25 | using HeuristicLab.PluginInfrastructure;
|
---|
[2210] | 26 | using HeuristicLab.GP.Interfaces;
|
---|
[645] | 27 |
|
---|
| 28 | namespace 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();
|
---|
[2236] | 45 | rootNode.Name = functionTree.ToString();
|
---|
| 46 | rootNode.Text = functionTree.ToString();
|
---|
[645] | 47 | rootNode.Tag = functionTree;
|
---|
[655] | 48 | treeNodeContextMenu.MenuItems.Clear();
|
---|
[654] | 49 | DiscoveryService discoveryService = new DiscoveryService();
|
---|
[2216] | 50 | IFunctionTreeSerializer[] exporters = discoveryService.GetInstances<IFunctionTreeSerializer>();
|
---|
| 51 | foreach (IFunctionTreeSerializer exporter in exporters) {
|
---|
[654] | 52 | string result;
|
---|
| 53 | // register a menu item for the exporter
|
---|
[1227] | 54 | MenuItem item = new MenuItem("Copy to clip-board (" + exporter.Name + ")",
|
---|
| 55 | MakeExporterDelegate(exporter), Shortcut.None);
|
---|
[654] | 56 | // try to export the whole tree
|
---|
[1227] | 57 | if (exporter.TryExport(functionTree, out result)) {
|
---|
[654] | 58 | // if it worked enable the context-menu item
|
---|
| 59 | item.Enabled = true;
|
---|
| 60 | } else {
|
---|
| 61 | item.Enabled = false;
|
---|
| 62 | }
|
---|
[655] | 63 | treeNodeContextMenu.MenuItems.Add(item);
|
---|
[654] | 64 | }
|
---|
[655] | 65 | rootNode.ContextMenu = treeNodeContextMenu;
|
---|
[645] | 66 | funTreeView.Nodes.Add(rootNode);
|
---|
| 67 |
|
---|
[1227] | 68 | foreach (IFunctionTree subTree in functionTree.SubTrees) {
|
---|
[645] | 69 | CreateTree(rootNode, subTree);
|
---|
| 70 | }
|
---|
| 71 | funTreeView.ExpandAll();
|
---|
| 72 | }
|
---|
| 73 |
|
---|
[2216] | 74 | private EventHandler MakeExporterDelegate(IFunctionTreeSerializer exporter) {
|
---|
[1227] | 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 |
|
---|
[645] | 82 | private void CreateTree(TreeNode rootNode, IFunctionTree functionTree) {
|
---|
| 83 | TreeNode node = new TreeNode();
|
---|
[2235] | 84 | node.Name = functionTree.ToString();
|
---|
| 85 | node.Text = functionTree.ToString();
|
---|
[645] | 86 | node.Tag = functionTree;
|
---|
[655] | 87 | node.ContextMenu = treeNodeContextMenu;
|
---|
[645] | 88 | rootNode.Nodes.Add(node);
|
---|
[1227] | 89 | foreach (IFunctionTree subTree in functionTree.SubTrees) {
|
---|
[645] | 90 | CreateTree(node, subTree);
|
---|
| 91 | }
|
---|
| 92 | }
|
---|
| 93 |
|
---|
| 94 | private void funTreeView_MouseUp(object sender, MouseEventArgs e) {
|
---|
[1227] | 95 | if (e.Button == MouseButtons.Right) {
|
---|
[645] | 96 | // Select the clicked node
|
---|
| 97 | funTreeView.SelectedNode = funTreeView.GetNodeAt(e.X, e.Y);
|
---|
| 98 | }
|
---|
| 99 | }
|
---|
| 100 | }
|
---|
| 101 | }
|
---|