Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.GP/3.3/FunctionView.cs @ 1905

Last change on this file since 1905 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
RevLine 
[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
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;
[654]40    private IFunctionTreeNameGenerator nameGenerator;
41    private IFunctionTreeNameGenerator[] allNameGenerators;
[655]42    private MenuItem representationsMenu;
[645]43
44    public FunctionTreeView() {
[654]45      nameGenerator = new DefaultFunctionTreeNameGenerator();
[645]46      InitializeComponent();
[654]47
48      DiscoveryService discoveryService = new DiscoveryService();
49      allNameGenerators = discoveryService.GetInstances<IFunctionTreeNameGenerator>();
[655]50      representationsMenu = new MenuItem();
51      representationsMenu.Text = "Tree representation";
52      representationsMenu.Name = "Tree representation";
[1227]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;
[655]56        else mi.Checked = false;
57        mi.Tag = generator;
58        representationsMenu.MenuItems.Add(mi);
[654]59      }
[645]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;
[654]72      rootNode.Text = nameGenerator.GetName(functionTree);
[645]73      rootNode.Tag = functionTree;
[655]74      treeNodeContextMenu.MenuItems.Clear();
75      treeNodeContextMenu.MenuItems.Add(representationsMenu);
[654]76      DiscoveryService discoveryService = new DiscoveryService();
77      IFunctionTreeExporter[] exporters = discoveryService.GetInstances<IFunctionTreeExporter>();
[1227]78      foreach (IFunctionTreeExporter exporter in exporters) {
[654]79        string result;
80        // register a menu item for the exporter
[1227]81        MenuItem item = new MenuItem("Copy to clip-board (" + exporter.Name + ")",
82          MakeExporterDelegate(exporter), Shortcut.None);
[654]83        // try to export the whole tree
[1227]84        if (exporter.TryExport(functionTree, out result)) {
[654]85          // if it worked enable the context-menu item
86          item.Enabled = true;
87        } else {
88          item.Enabled = false;
89        }
[655]90        treeNodeContextMenu.MenuItems.Add(item);
[654]91      }
[655]92      rootNode.ContextMenu = treeNodeContextMenu;
[645]93      funTreeView.Nodes.Add(rootNode);
94
[1227]95      foreach (IFunctionTree subTree in functionTree.SubTrees) {
[645]96        CreateTree(rootNode, subTree);
97      }
98      funTreeView.ExpandAll();
99    }
100
[1227]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
[645]119    private void CreateTree(TreeNode rootNode, IFunctionTree functionTree) {
120      TreeNode node = new TreeNode();
121      node.Name = functionTree.Function.Name;
[654]122      node.Text = nameGenerator.GetName(functionTree);
[645]123      node.Tag = functionTree;
[655]124      node.ContextMenu = treeNodeContextMenu;
[645]125      rootNode.Nodes.Add(node);
[1227]126      foreach (IFunctionTree subTree in functionTree.SubTrees) {
[645]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;
[1227]136      if (funTreeView.SelectedNode != null && funTreeView.SelectedNode.Tag != null) {
[645]137        IFunctionTree selectedBranch = (IFunctionTree)funTreeView.SelectedNode.Tag;
138        UpdateVariablesList(selectedBranch);
[654]139        templateTextBox.Text = nameGenerator.GetName(selectedBranch);
[645]140        this.selectedBranch = selectedBranch;
141        editButton.Enabled = true;
142      }
143    }
144
145    private void UpdateVariablesList(IFunctionTree functionTree) {
[1227]146      foreach (IVariable variable in functionTree.LocalVariables) {
[645]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
[1227]153      if (selectedVariable != null) {
[645]154        selectedVariable.Value.Changed -= new EventHandler(selectedVariable_ValueChanged);
155      }
[1227]156      if (variablesListBox.SelectedItem != null) {
[645]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) {
[1227]171      if (funTreeView.SelectedNode != null && funTreeView.SelectedNode.Tag != null) {
[645]172        TreeNode node = funTreeView.SelectedNode;
[654]173        node.Text = nameGenerator.GetName(functionTree);
[645]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) {
[1227]182      if (e.Button == MouseButtons.Right) {
[645]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.