Free cookie consent management tool by TermsFeed Policy Generator

source: branches/GpPluginsRefactoringBranch/HeuristicLab.GP/FunctionView.cs @ 654

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

worked on exporters and display of function-names in the functionView (#177)

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