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
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.Text;
28using System.Linq;
29using System.Windows.Forms;
30using HeuristicLab.Core;
31using HeuristicLab.Operators;
32using System.CodeDom.Compiler;
33using System.Reflection;
34using HeuristicLab.CodeEditor;
35
36namespace HeuristicLab.Operators.Programmable {
37
38  public partial class ProgrammableOperatorView : ViewBase {
39
40    public ProgrammableOperator ProgrammableOperator {
41      get { return (ProgrammableOperator)Item; }
42      set { base.Item = value; }
43    }
44
45    public ProgrammableOperatorView() {
46      InitializeComponent();
47    }
48
49    public ProgrammableOperatorView(ProgrammableOperator programmableOperator)
50      : this() {
51      ProgrammableOperator = programmableOperator;
52    }
53
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) {
75        codeEditor.Text = "";
76        codeEditor.Enabled = false;
77        addVariableInfoButton.Enabled = false;
78        removeVariableInfoButton.Enabled = false;
79        descriptionTextBox.Text = "";
80        descriptionTextBox.Enabled = false;
81        codeEditor.Prefix = @"using System
82
83public class Operator {
84  public static IOperation Execute(IOperator op, IScope scope, parameters ...) {
85";
86        codeEditor.Suffix = @"
87    return null;
88  }
89}";
90        assembliesTreeView.Nodes.Clear();
91      } else {
92        codeEditor.Enabled = true;
93        addVariableInfoButton.Enabled = true;
94        removeVariableInfoButton.Enabled = operatorBaseVariableInfosView.SelectedVariableInfos.Count > 0;
95        descriptionTextBox.Text = ProgrammableOperator.Description;
96        descriptionTextBox.Enabled = true;
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();
111      }
112    }
113
114
115    private string GetGeneratedPrefix() {
116      StringBuilder prefix = new StringBuilder();
117      foreach (var ns in ProgrammableOperator.GetSelectedAndValidNamespaces()) {
118        prefix.Append("using ").Append(ns).AppendLine(";");
119      }
120      prefix.AppendLine();
121      prefix.Append("public class ").Append(ProgrammableOperator.CompiledTypeName).AppendLine(" {");
122      prefix.Append("  ").Append(ProgrammableOperator.Signature).AppendLine(" {");
123      return prefix.ToString();
124    }
125
126    private void operatorBaseVariableInfosView_SelectedVariableInfosChanged(object sender, EventArgs e) {
127      removeVariableInfoButton.Enabled = operatorBaseVariableInfosView.SelectedVariableInfos.Count > 0;
128    }
129    private void codeEditor_Validated(object sender, EventArgs e) {
130      ProgrammableOperator.Code = codeEditor.UserCode;
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) {
139        if (ProgrammableOperator.GetVariableInfo(dialog.VariableInfo.FormalName) != null) {
140          Auxiliary.ShowErrorMessageBox("A variable info with the same formal name already exists.");
141        } else {
142          ProgrammableOperator.AddVariableInfo(dialog.VariableInfo);
143          Recompile();
144        }
145      }
146      dialog.Dispose();
147    }
148
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);
154      Recompile();
155    }
156
157    private void Recompile() {
158      this.Enabled = false;
159      try {
160        ProgrammableOperator.Compile();
161        MessageBox.Show("Compilation successful", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
162      } catch (Exception ex) {
163        Auxiliary.ShowErrorMessageBox(ex);
164      }
165      UpdateControls();
166      codeEditor.ShowCompileErrors(ProgrammableOperator.CompileErrors, "ProgrammableOperator");
167      this.Enabled = true;
168    }
169
170    private void compileButton_Click(object sender, EventArgs e) {
171      Recompile();
172    }
173
174    #region ProgrammableOperator Events
175    private void ProgrammableOperator_CodeChanged(object sender, EventArgs e) {
176      codeEditor.Text = ProgrammableOperator.Code;
177    }
178    private void ProgrammableOperator_DescriptionChanged(object sender, EventArgs e) {
179      descriptionTextBox.Text = ProgrammableOperator.Description;
180    }
181    #endregion
182
183    private void assembliesTreeView_AfterCheck(object sender, TreeViewEventArgs e) {
184      if (initializing)
185        return;
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;
191      } else {
192        if (e.Node.Checked) {
193          ProgrammableOperator.SelectAssembly(a);
194          codeEditor.AddAssembly(a);
195        } else {
196          ProgrammableOperator.UnselectAssembly(a);
197          codeEditor.RemoveAssembly(a);
198        }
199      }
200      InitializeNamespacesList();
201      codeEditor.Prefix = GetGeneratedPrefix();
202    }
203
204    private bool initializing = false;
205    private void InitializeAssemblyList() {
206      initializing = true;
207      assembliesTreeView.Enabled = false;
208      namespacesTreeView.Enabled = false;
209      assembliesTreeView.BeginUpdate();
210      assembliesTreeView.Nodes.Clear();
211      var selectedAssemblies = new HashSet<Assembly>(ProgrammableOperator.SelectedAssemblies);
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        }
227      }
228      assembliesTreeView.EndUpdate();
229      assembliesTreeView.Enabled = true;
230      namespacesTreeView.Enabled = true;
231      initializing = false;
232    }
233
234    private void InitializeNamespacesList() {
235      initializing = true;
236      namespacesTreeView.Enabled = false;
237      namespacesTreeView.BeginUpdate();
238      TreeNode oldTree = new TreeNode("root");
239      CloneTreeNodeCollection(oldTree, namespacesTreeView.Nodes);
240      namespacesTreeView.Nodes.Clear();
241      var selectedNamespaces = new HashSet<string>(ProgrammableOperator.Namespaces);
242      foreach (var ns in ProgrammableOperator.GetAllNamespaces(true))
243        AddNamespace(namespacesTreeView.Nodes, ns, selectedNamespaces.Contains(ns), oldTree);
244      codeEditor.Prefix = GetGeneratedPrefix();
245      namespacesTreeView.EndUpdate();
246      namespacesTreeView.Enabled = true;
247      initializing = false;
248    }
249
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) {
261      int dotIndex = ns.IndexOf('.');
262      string prefix = ns;
263      if (dotIndex != -1)
264        prefix = ns.Substring(0, dotIndex);
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) {
289      TreeNode node = null;
290      if (parentNodes.ContainsKey(key)) {
291        node = parentNodes[key];
292      } else {
293        node = parentNodes.Add(key, key);
294      }
295      return node;
296    }
297
298    private void namespacesTreeView_AfterCheck(object sender, TreeViewEventArgs e) {
299      if (initializing)
300        return;
301      if (e.Node.Checked) {
302        ProgrammableOperator.SelectNamespace(e.Node.FullPath);
303      } else {
304        ProgrammableOperator.UnselectNamespace(e.Node.FullPath);
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  }
315}
Note: See TracBrowser for help on using the repository browser.