Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.GP/FunctionView.cs @ 820

Last change on this file since 820 was 656, checked in by gkronber, 16 years ago

merged changesets r644:647 and r651:655 from the GpPluginsRefactoringBranch back into the trunk (#177)

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