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.Collections.Generic;
|
---|
24 | using System.ComponentModel;
|
---|
25 | using System.Drawing;
|
---|
26 | using System.Data;
|
---|
27 | using System.Linq;
|
---|
28 | using System.Text;
|
---|
29 | using System.Windows.Forms;
|
---|
30 | using HeuristicLab.Core;
|
---|
31 | using HeuristicLab.PluginInfrastructure;
|
---|
32 | using HeuristicLab.Data;
|
---|
33 |
|
---|
34 | namespace HeuristicLab.GP {
|
---|
35 | public partial class FunctionTreeView : ViewBase {
|
---|
36 | private IFunctionTree functionTree;
|
---|
37 |
|
---|
38 | private IFunctionTree selectedBranch;
|
---|
39 | private IVariable selectedVariable;
|
---|
40 | private IFunctionTreeNameGenerator nameGenerator;
|
---|
41 | private IFunctionTreeNameGenerator[] allNameGenerators;
|
---|
42 | private MenuItem representationsMenu;
|
---|
43 |
|
---|
44 | public FunctionTreeView() {
|
---|
45 | nameGenerator = new DefaultFunctionTreeNameGenerator();
|
---|
46 | InitializeComponent();
|
---|
47 |
|
---|
48 | allNameGenerators = ApplicationManager.Manager.GetInstances<IFunctionTreeNameGenerator>().ToArray();
|
---|
49 | representationsMenu = new MenuItem();
|
---|
50 | representationsMenu.Text = "Tree representation";
|
---|
51 | representationsMenu.Name = "Tree representation";
|
---|
52 | foreach (IFunctionTreeNameGenerator generator in allNameGenerators) {
|
---|
53 | MenuItem mi = new MenuItem(generator.Name, MakeNameGeneratorDelegate(generator), Shortcut.None);
|
---|
54 | if (generator is DefaultFunctionTreeNameGenerator) mi.Checked = true;
|
---|
55 | else mi.Checked = false;
|
---|
56 | mi.Tag = generator;
|
---|
57 | representationsMenu.MenuItems.Add(mi);
|
---|
58 | }
|
---|
59 | }
|
---|
60 |
|
---|
61 | public FunctionTreeView(IFunctionTree functionTree)
|
---|
62 | : this() {
|
---|
63 | this.functionTree = functionTree;
|
---|
64 | Refresh();
|
---|
65 | }
|
---|
66 |
|
---|
67 | protected override void UpdateControls() {
|
---|
68 | funTreeView.Nodes.Clear();
|
---|
69 | TreeNode rootNode = new TreeNode();
|
---|
70 | rootNode.Name = functionTree.Function.Name;
|
---|
71 | rootNode.Text = nameGenerator.GetName(functionTree);
|
---|
72 | rootNode.Tag = functionTree;
|
---|
73 | treeNodeContextMenu.MenuItems.Clear();
|
---|
74 | treeNodeContextMenu.MenuItems.Add(representationsMenu);
|
---|
75 | IFunctionTreeExporter[] exporters = ApplicationManager.Manager.GetInstances<IFunctionTreeExporter>().ToArray();
|
---|
76 | foreach (IFunctionTreeExporter exporter in exporters) {
|
---|
77 | string result;
|
---|
78 | // register a menu item for the exporter
|
---|
79 | MenuItem item = new MenuItem("Copy to clip-board (" + exporter.Name + ")",
|
---|
80 | MakeExporterDelegate(exporter), Shortcut.None);
|
---|
81 | // try to export the whole tree
|
---|
82 | if (exporter.TryExport(functionTree, out result)) {
|
---|
83 | // if it worked enable the context-menu item
|
---|
84 | item.Enabled = true;
|
---|
85 | } else {
|
---|
86 | item.Enabled = false;
|
---|
87 | }
|
---|
88 | treeNodeContextMenu.MenuItems.Add(item);
|
---|
89 | }
|
---|
90 | rootNode.ContextMenu = treeNodeContextMenu;
|
---|
91 | funTreeView.Nodes.Add(rootNode);
|
---|
92 |
|
---|
93 | foreach (IFunctionTree subTree in functionTree.SubTrees) {
|
---|
94 | CreateTree(rootNode, subTree);
|
---|
95 | }
|
---|
96 | funTreeView.ExpandAll();
|
---|
97 | }
|
---|
98 |
|
---|
99 | private EventHandler MakeNameGeneratorDelegate(IFunctionTreeNameGenerator generator) {
|
---|
100 | return delegate(object source, EventArgs args) {
|
---|
101 | IFunctionTreeNameGenerator g = (IFunctionTreeNameGenerator)((MenuItem)source).Tag;
|
---|
102 | this.nameGenerator = g;
|
---|
103 | foreach (MenuItem otherMenuItem in representationsMenu.MenuItems) otherMenuItem.Checked = false;
|
---|
104 | ((MenuItem)source).Checked = true;
|
---|
105 | UpdateControls();
|
---|
106 | };
|
---|
107 | }
|
---|
108 |
|
---|
109 | private EventHandler MakeExporterDelegate(IFunctionTreeExporter exporter) {
|
---|
110 | return delegate(object source, EventArgs args) {
|
---|
111 | TreeNode node = funTreeView.SelectedNode;
|
---|
112 | if (node == null || node.Tag == null) return;
|
---|
113 | Clipboard.SetText(exporter.Export((IFunctionTree)node.Tag));
|
---|
114 | };
|
---|
115 | }
|
---|
116 |
|
---|
117 | private void CreateTree(TreeNode rootNode, IFunctionTree functionTree) {
|
---|
118 | TreeNode node = new TreeNode();
|
---|
119 | node.Name = functionTree.Function.Name;
|
---|
120 | node.Text = nameGenerator.GetName(functionTree);
|
---|
121 | node.Tag = functionTree;
|
---|
122 | node.ContextMenu = treeNodeContextMenu;
|
---|
123 | rootNode.Nodes.Add(node);
|
---|
124 | foreach (IFunctionTree subTree in functionTree.SubTrees) {
|
---|
125 | CreateTree(node, subTree);
|
---|
126 | }
|
---|
127 | }
|
---|
128 |
|
---|
129 | private void functionTreeView_AfterSelect(object sender, TreeViewEventArgs e) {
|
---|
130 | variablesListBox.Items.Clear();
|
---|
131 | variablesSplitContainer.Panel2.Controls.Clear();
|
---|
132 | templateTextBox.Clear();
|
---|
133 | editButton.Enabled = false;
|
---|
134 | if (funTreeView.SelectedNode != null && funTreeView.SelectedNode.Tag != null) {
|
---|
135 | IFunctionTree selectedBranch = (IFunctionTree)funTreeView.SelectedNode.Tag;
|
---|
136 | UpdateVariablesList(selectedBranch);
|
---|
137 | templateTextBox.Text = nameGenerator.GetName(selectedBranch);
|
---|
138 | this.selectedBranch = selectedBranch;
|
---|
139 | editButton.Enabled = true;
|
---|
140 | }
|
---|
141 | }
|
---|
142 |
|
---|
143 | private void UpdateVariablesList(IFunctionTree functionTree) {
|
---|
144 | foreach (IVariable variable in functionTree.LocalVariables) {
|
---|
145 | variablesListBox.Items.Add(variable.Name);
|
---|
146 | }
|
---|
147 | }
|
---|
148 |
|
---|
149 | private void variablesListBox_SelectedIndexChanged(object sender, EventArgs e) {
|
---|
150 | // in case we had an event-handler registered for a different variable => unregister the event-handler
|
---|
151 | if (selectedVariable != null) {
|
---|
152 | selectedVariable.Value.Changed -= new EventHandler(selectedVariable_ValueChanged);
|
---|
153 | }
|
---|
154 | if (variablesListBox.SelectedItem != null) {
|
---|
155 | string selectedVariableName = (string)variablesListBox.SelectedItem;
|
---|
156 | selectedVariable = selectedBranch.GetLocalVariable(selectedVariableName);
|
---|
157 | variablesSplitContainer.Panel2.Controls.Clear();
|
---|
158 | Control editor = (Control)selectedVariable.CreateView();
|
---|
159 | variablesSplitContainer.Panel2.Controls.Add(editor);
|
---|
160 | editor.Dock = DockStyle.Fill;
|
---|
161 | // register an event handler that updates the treenode when the value of the variable is changed by the user
|
---|
162 | selectedVariable.Value.Changed += new EventHandler(selectedVariable_ValueChanged);
|
---|
163 | } else {
|
---|
164 | variablesSplitContainer.Panel2.Controls.Clear();
|
---|
165 | }
|
---|
166 | }
|
---|
167 |
|
---|
168 | void selectedVariable_ValueChanged(object sender, EventArgs e) {
|
---|
169 | if (funTreeView.SelectedNode != null && funTreeView.SelectedNode.Tag != null) {
|
---|
170 | TreeNode node = funTreeView.SelectedNode;
|
---|
171 | node.Text = nameGenerator.GetName(functionTree);
|
---|
172 | }
|
---|
173 | }
|
---|
174 |
|
---|
175 | protected virtual void editButton_Click(object sender, EventArgs e) {
|
---|
176 | ControlManager.Manager.ShowControl(selectedBranch.Function.CreateView());
|
---|
177 | }
|
---|
178 |
|
---|
179 | private void funTreeView_MouseUp(object sender, MouseEventArgs e) {
|
---|
180 | if (e.Button == MouseButtons.Right) {
|
---|
181 | // Select the clicked node
|
---|
182 | funTreeView.SelectedNode = funTreeView.GetNodeAt(e.X, e.Y);
|
---|
183 | }
|
---|
184 | }
|
---|
185 | }
|
---|
186 | }
|
---|