[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();
|
---|
[2591] | 49 | foreach (IFunctionTreeSerializer exporter in ApplicationManager.Manager.GetInstances<IFunctionTreeSerializer>()) {
|
---|
[654] | 50 | string result;
|
---|
| 51 | // register a menu item for the exporter
|
---|
[1227] | 52 | MenuItem item = new MenuItem("Copy to clip-board (" + exporter.Name + ")",
|
---|
| 53 | MakeExporterDelegate(exporter), Shortcut.None);
|
---|
[654] | 54 | // try to export the whole tree
|
---|
[1227] | 55 | if (exporter.TryExport(functionTree, out result)) {
|
---|
[654] | 56 | // if it worked enable the context-menu item
|
---|
| 57 | item.Enabled = true;
|
---|
| 58 | } else {
|
---|
| 59 | item.Enabled = false;
|
---|
| 60 | }
|
---|
[655] | 61 | treeNodeContextMenu.MenuItems.Add(item);
|
---|
[654] | 62 | }
|
---|
[655] | 63 | rootNode.ContextMenu = treeNodeContextMenu;
|
---|
[645] | 64 | funTreeView.Nodes.Add(rootNode);
|
---|
| 65 |
|
---|
[1227] | 66 | foreach (IFunctionTree subTree in functionTree.SubTrees) {
|
---|
[645] | 67 | CreateTree(rootNode, subTree);
|
---|
| 68 | }
|
---|
| 69 | funTreeView.ExpandAll();
|
---|
| 70 | }
|
---|
| 71 |
|
---|
[2216] | 72 | private EventHandler MakeExporterDelegate(IFunctionTreeSerializer exporter) {
|
---|
[1227] | 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 |
|
---|
[645] | 80 | private void CreateTree(TreeNode rootNode, IFunctionTree functionTree) {
|
---|
| 81 | TreeNode node = new TreeNode();
|
---|
[2235] | 82 | node.Name = functionTree.ToString();
|
---|
| 83 | node.Text = functionTree.ToString();
|
---|
[645] | 84 | node.Tag = functionTree;
|
---|
[655] | 85 | node.ContextMenu = treeNodeContextMenu;
|
---|
[645] | 86 | rootNode.Nodes.Add(node);
|
---|
[1227] | 87 | foreach (IFunctionTree subTree in functionTree.SubTrees) {
|
---|
[645] | 88 | CreateTree(node, subTree);
|
---|
| 89 | }
|
---|
| 90 | }
|
---|
| 91 |
|
---|
| 92 | private void funTreeView_MouseUp(object sender, MouseEventArgs e) {
|
---|
[1227] | 93 | if (e.Button == MouseButtons.Right) {
|
---|
[645] | 94 | // Select the clicked node
|
---|
| 95 | funTreeView.SelectedNode = funTreeView.GetNodeAt(e.X, e.Y);
|
---|
| 96 | }
|
---|
| 97 | }
|
---|
| 98 | }
|
---|
| 99 | }
|
---|