Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Operators.Programmable/3.2/ProgrammableOperatorView.cs @ 3580

Last change on this file since 3580 was 2720, checked in by epitzer, 15 years ago

disable elements while updating (or compiling) (#842)

File size: 11.6 KB
RevLine 
[2]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.Text;
[2660]28using System.Linq;
[2]29using System.Windows.Forms;
30using HeuristicLab.Core;
[51]31using HeuristicLab.Operators;
[2660]32using System.CodeDom.Compiler;
33using System.Reflection;
34using HeuristicLab.CodeEditor;
[2]35
36namespace HeuristicLab.Operators.Programmable {
[2660]37
[2]38  public partial class ProgrammableOperatorView : ViewBase {
[2660]39
[2]40    public ProgrammableOperator ProgrammableOperator {
41      get { return (ProgrammableOperator)Item; }
42      set { base.Item = value; }
43    }
44
45    public ProgrammableOperatorView() {
46      InitializeComponent();
47    }
[2660]48
[2]49    public ProgrammableOperatorView(ProgrammableOperator programmableOperator)
50      : this() {
51      ProgrammableOperator = programmableOperator;
52    }
[2668]53
[2]54    protected override void RemoveItemEvents() {
55      operatorBaseVariableInfosView.Operator = null;
56      operatorBaseVariablesView.Operator = null;
57      constrainedItemBaseView.ConstrainedItem = null;
58      ProgrammableOperator.CodeChanged -= new EventHandler(ProgrammableOperator_CodeChanged);
59      ProgrammableOperator.DescriptionChanged -= new EventHandler(ProgrammableOperator_DescriptionChanged);
60      base.RemoveItemEvents();
61    }
62
63    protected override void AddItemEvents() {
64      base.AddItemEvents();
65      operatorBaseVariableInfosView.Operator = ProgrammableOperator;
66      operatorBaseVariablesView.Operator = ProgrammableOperator;
67      constrainedItemBaseView.ConstrainedItem = ProgrammableOperator;
68      ProgrammableOperator.CodeChanged += new EventHandler(ProgrammableOperator_CodeChanged);
69      ProgrammableOperator.DescriptionChanged += new EventHandler(ProgrammableOperator_DescriptionChanged);
70    }
71
72    protected override void UpdateControls() {
73      base.UpdateControls();
74      if (ProgrammableOperator == null) {
[2660]75        codeEditor.Text = "";
76        codeEditor.Enabled = false;
[2]77        addVariableInfoButton.Enabled = false;
78        removeVariableInfoButton.Enabled = false;
79        descriptionTextBox.Text = "";
80        descriptionTextBox.Enabled = false;
[2660]81        codeEditor.Prefix = @"using System
82
83public class Operator {
[2668]84  public static IOperation Execute(IOperator op, IScope scope, parameters ...) {
85";
[2660]86        codeEditor.Suffix = @"
87    return null;
88  }
[2668]89}";
90        assembliesTreeView.Nodes.Clear();
91      } else {
[2660]92        codeEditor.Enabled = true;
[2]93        addVariableInfoButton.Enabled = true;
94        removeVariableInfoButton.Enabled = operatorBaseVariableInfosView.SelectedVariableInfos.Count > 0;
95        descriptionTextBox.Text = ProgrammableOperator.Description;
96        descriptionTextBox.Enabled = true;
[2660]97        codeEditor.Prefix = GetGeneratedPrefix();
98        codeEditor.Suffix = @"
99    return null;
100  }
101}";
102        codeEditor.UserCode = ProgrammableOperator.Code;
103        if (codeEditor.UserCode == "")
104          codeEditor.UserCode = "\n\n\n";
105        InitializeAssemblyList();
106        InitializeNamespacesList();
107        foreach (var a in ProgrammableOperator.SelectedAssemblies) {
108          codeEditor.AddAssembly(a);
109        }
110        codeEditor.ScrollAfterPrefix();
[2668]111      }
[2660]112    }
113
[2668]114
[2660]115    private string GetGeneratedPrefix() {
116      StringBuilder prefix = new StringBuilder();
117      foreach (var ns in ProgrammableOperator.GetSelectedAndValidNamespaces()) {
118        prefix.Append("using ").Append(ns).AppendLine(";");
[2]119      }
[2660]120      prefix.AppendLine();
121      prefix.Append("public class ").Append(ProgrammableOperator.CompiledTypeName).AppendLine(" {");
[2668]122      prefix.Append("  ").Append(ProgrammableOperator.Signature).AppendLine(" {");
[2660]123      return prefix.ToString();
[2]124    }
125
126    private void operatorBaseVariableInfosView_SelectedVariableInfosChanged(object sender, EventArgs e) {
127      removeVariableInfoButton.Enabled = operatorBaseVariableInfosView.SelectedVariableInfos.Count > 0;
128    }
[2660]129    private void codeEditor_Validated(object sender, EventArgs e) {
130      ProgrammableOperator.Code = codeEditor.UserCode;
[2]131    }
132    private void descriptionTextBox_Validated(object sender, EventArgs e) {
133      ProgrammableOperator.SetDescription(descriptionTextBox.Text);
134    }
135
136    private void addVariableInfoButton_Click(object sender, EventArgs e) {
137      AddVariableInfoDialog dialog = new AddVariableInfoDialog();
138      if (dialog.ShowDialog(this) == DialogResult.OK) {
[2660]139        if (ProgrammableOperator.GetVariableInfo(dialog.VariableInfo.FormalName) != null) {
[2]140          Auxiliary.ShowErrorMessageBox("A variable info with the same formal name already exists.");
[2660]141        } else {
[2]142          ProgrammableOperator.AddVariableInfo(dialog.VariableInfo);
[2668]143          Recompile();
[2660]144        }
[2]145      }
146      dialog.Dispose();
147    }
[2660]148
[2]149    private void removeVariableInfoButton_Click(object sender, EventArgs e) {
150      IVariableInfo[] selected = new IVariableInfo[operatorBaseVariableInfosView.SelectedVariableInfos.Count];
151      operatorBaseVariableInfosView.SelectedVariableInfos.CopyTo(selected, 0);
152      for (int i = 0; i < selected.Length; i++)
153        ProgrammableOperator.RemoveVariableInfo(selected[i].FormalName);
[2668]154      Recompile();
[2]155    }
156
[2660]157    private void Recompile() {
[2720]158      this.Enabled = false;
[2660]159      try {
[2668]160        ProgrammableOperator.Compile();
[2660]161        MessageBox.Show("Compilation successful", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
162      } catch (Exception ex) {
163        Auxiliary.ShowErrorMessageBox(ex);
164      }
165      UpdateControls();
[2668]166      codeEditor.ShowCompileErrors(ProgrammableOperator.CompileErrors, "ProgrammableOperator");
[2720]167      this.Enabled = true;
[2660]168    }
169
170    private void compileButton_Click(object sender, EventArgs e) {
171      Recompile();
[2668]172    }
[2660]173
[2]174    #region ProgrammableOperator Events
175    private void ProgrammableOperator_CodeChanged(object sender, EventArgs e) {
[2660]176      codeEditor.Text = ProgrammableOperator.Code;
[2]177    }
178    private void ProgrammableOperator_DescriptionChanged(object sender, EventArgs e) {
179      descriptionTextBox.Text = ProgrammableOperator.Description;
180    }
181    #endregion
[2660]182
[2668]183    private void assembliesTreeView_AfterCheck(object sender, TreeViewEventArgs e) {
[2660]184      if (initializing)
185        return;
[2668]186      Assembly a = e.Node.Tag as Assembly;
187      if (a == null && e.Node.Nodes.Count > 0) {
188        foreach (TreeNode n in e.Node.Nodes)
189          n.Checked = e.Node.Checked;
190        return;
[2660]191      } else {
[2668]192        if (e.Node.Checked) {
193          ProgrammableOperator.SelectAssembly(a);
194          codeEditor.AddAssembly(a);
195        } else {
196          ProgrammableOperator.UnselectAssembly(a);
197          codeEditor.RemoveAssembly(a);
198        }
[2660]199      }
[2668]200      InitializeNamespacesList();
[2660]201      codeEditor.Prefix = GetGeneratedPrefix();
202    }
203
204    private bool initializing = false;
205    private void InitializeAssemblyList() {
[2668]206      initializing = true;
[2720]207      assembliesTreeView.Enabled = false;
208      namespacesTreeView.Enabled = false;
[2668]209      assembliesTreeView.BeginUpdate();
210      assembliesTreeView.Nodes.Clear();
[2660]211      var selectedAssemblies = new HashSet<Assembly>(ProgrammableOperator.SelectedAssemblies);
[2668]212      foreach (var p in ProgrammableOperator.Plugins) {
213        var node = assembliesTreeView.Nodes.Add(p.Key);
214        node.Tag = p;
215        foreach (var a in p.Value) {
216          var aNode = node.Nodes.Add(a.GetName().Name);
217          aNode.Tag = a;
218          if (selectedAssemblies.Contains(a))
219            aNode.Checked = true;
220        }
221        if (node.Nodes.Count == 1 && node.Nodes[0].Name == node.Nodes[0].Name) {
222          node.Tag = node.Nodes[0].Tag;
223          node.Nodes.Clear();
224        } else if (node.Nodes.Count > 0 && node.Nodes.Cast<TreeNode>().All(n => n.Checked)) {
225          node.Checked = true;
226        }
[2660]227      }
[2668]228      assembliesTreeView.EndUpdate();
[2720]229      assembliesTreeView.Enabled = true;
230      namespacesTreeView.Enabled = true;
[2660]231      initializing = false;
232    }
233
234    private void InitializeNamespacesList() {
235      initializing = true;
[2720]236      namespacesTreeView.Enabled = false;
237      namespacesTreeView.BeginUpdate();
[2680]238      TreeNode oldTree = new TreeNode("root");
239      CloneTreeNodeCollection(oldTree, namespacesTreeView.Nodes);
[2668]240      namespacesTreeView.Nodes.Clear();
[2660]241      var selectedNamespaces = new HashSet<string>(ProgrammableOperator.Namespaces);
[2668]242      foreach (var ns in ProgrammableOperator.GetAllNamespaces(true))
[2680]243        AddNamespace(namespacesTreeView.Nodes, ns, selectedNamespaces.Contains(ns), oldTree);
[2660]244      codeEditor.Prefix = GetGeneratedPrefix();
[2720]245      namespacesTreeView.EndUpdate();
246      namespacesTreeView.Enabled = true;
[2660]247      initializing = false;
248    }
249
[2680]250    private void CloneTreeNodeCollection(TreeNode root, TreeNodeCollection nodes) {
251      foreach (TreeNode n in nodes) {
252        TreeNode newNode = root.Nodes.Add(n.Text, n.Text);
253        newNode.Checked = n.Checked;
254        CloneTreeNodeCollection(newNode, n.Nodes);
255        if (n.IsExpanded)
256          newNode.Expand();
257      }
258    }
259
260    private bool AddNamespace(TreeNodeCollection parentNodes, string ns, bool isSelected, TreeNode oldTree) {
[2668]261      int dotIndex = ns.IndexOf('.');
262      string prefix = ns;
263      if (dotIndex != -1)
264        prefix = ns.Substring(0, dotIndex);
[2680]265      TreeNode node = GetOrCreateNode(parentNodes, prefix);
266      TreeNode oldNode = MaybeGetNode(oldTree, prefix);
267      bool isNew = oldNode == null;
268      if (dotIndex != -1 && dotIndex + 1 < ns.Length) {
269        isNew = AddNamespace(node.Nodes, ns.Substring(dotIndex + 1, ns.Length - (dotIndex + 1)), isSelected, oldNode);
270      } else {
271        node.Checked = isSelected;
272      }
273      if (isNew || oldNode != null && oldNode.IsExpanded)
274        node.Expand();
275      if (isNew)
276        namespacesTreeView.SelectedNode = node;
277      return isNew;
278    }
279
280    private static TreeNode MaybeGetNode(TreeNode parentNode, string key) {
281      if (parentNode == null)
282        return null;
283      if (parentNode.Nodes.ContainsKey(key))
284        return parentNode.Nodes[key];
285      return null;
286    }
287
288    private static TreeNode GetOrCreateNode(TreeNodeCollection parentNodes, string key) {
[2668]289      TreeNode node = null;
[2680]290      if (parentNodes.ContainsKey(key)) {
291        node = parentNodes[key];
[2668]292      } else {
[2680]293        node = parentNodes.Add(key, key);
[2668]294      }
[2680]295      return node;
[2668]296    }
297
298    private void namespacesTreeView_AfterCheck(object sender, TreeViewEventArgs e) {
[2660]299      if (initializing)
300        return;
[2668]301      if (e.Node.Checked) {
302        ProgrammableOperator.SelectNamespace(e.Node.FullPath);
303      } else {
304        ProgrammableOperator.UnselectNamespace(e.Node.FullPath);
[2660]305      }
306      codeEditor.Prefix = GetGeneratedPrefix();
307    }
308
309    private void showCodeButton_Click(object sender, EventArgs e) {
310      new CodeViewer(ProgrammableOperator.CompilationUnitCode).ShowDialog(this);
311    }
312
313
314  }
[2668]315}
Note: See TracBrowser for help on using the repository browser.