Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.GP/3.4/FunctionView.cs @ 1939

Last change on this file since 1939 was 1529, checked in by gkronber, 16 years ago

Moved source files of plugins AdvancedOptimizationFrontEnd ... Grid into version-specific sub-folders. #576

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