Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Operators.Programmable/3.3/ProgrammableOperatorView.cs @ 2901

Last change on this file since 2901 was 2897, checked in by epitzer, 15 years ago

Finished initial port of ProgrammableOperator to HL 3.3 (#842)

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