Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 5182 was 5042, checked in by epitzer, 14 years ago

Add breakpoint to ProgrammableOperator (#1322)

this patch also adds a shortcut (F6) to recompile, makes minor GUI improvements and gets rid of resource files

File size: 12.1 KB
RevLine 
[2]1#region License Information
2/* HeuristicLab
[2911]3 * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[2]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;
[5042]24using System.Drawing;
[4068]25using System.Linq;
26using System.Reflection;
[2]27using System.Text;
28using System.Windows.Forms;
[2799]29using HeuristicLab.CodeEditor;
[4068]30using HeuristicLab.Common.Resources;
[2520]31using HeuristicLab.Core.Views;
[2897]32using HeuristicLab.MainForm;
[3758]33using HeuristicLab.PluginInfrastructure;
[2]34
35namespace HeuristicLab.Operators.Programmable {
[5042]36
[2917]37  [View("ProgrammableOperator View")]
[2897]38  [Content(typeof(ProgrammableOperator), true)]
[2799]39  public partial class ProgrammableOperatorView : NamedItemView {
40
[2]41    public ProgrammableOperator ProgrammableOperator {
[2799]42      get { return (ProgrammableOperator)base.Content; }
[2897]43      set { base.Content = (ProgrammableOperator)value; }
[2]44    }
45
46    public ProgrammableOperatorView() {
47      InitializeComponent();
[3903]48      namespacesTreeView.ImageList = new ImageList();
49      namespacesTreeView.ImageList.Images.Add(VS2008ImageLibrary.Namespace);
50      assembliesTreeView.ImageList = new ImageList();
51      assembliesTreeView.ImageList.Images.Add(VS2008ImageLibrary.Assembly);
52      assembliesTreeView.ImageList.Images.Add(VS2008ImageLibrary.Module);
[2]53    }
[2799]54
55    protected override void RegisterContentEvents() {
56      base.RegisterContentEvents();
[2897]57      ProgrammableOperator.CodeChanged += ProgrammableOperator_CodeChanged;
58      ProgrammableOperator.SignatureChanged += ProgrammableOperator_SignatureChanged;
[5042]59      ProgrammableOperator.BreakpointChanged += ProgrammableOperator_BreakpointChanged;
[2]60    }
61
[2897]62    protected override void DeregisterContentEvents() {
63      ProgrammableOperator.CodeChanged -= ProgrammableOperator_CodeChanged;
64      ProgrammableOperator.SignatureChanged -= ProgrammableOperator_SignatureChanged;
[5042]65      ProgrammableOperator.BreakpointChanged -= ProgrammableOperator_BreakpointChanged;
[2897]66      base.DeregisterContentEvents();
67    }
[2799]68
[5042]69    #region Content event handlers
70    void ProgrammableOperator_BreakpointChanged(object sender, EventArgs e) {
71      breakpointCheckBox.Checked = ProgrammableOperator.Breakpoint;
72    }
73    private void ProgrammableOperator_CodeChanged(object sender, EventArgs e) {
74      codeEditor.Text = ProgrammableOperator.Code;
75    }
76    private void ProgrammableOperator_SignatureChanged(object sender, EventArgs args) {
77      codeEditor.Prefix = GetGeneratedPrefix();
78    }
79    #endregion
80
[2799]81    protected override void OnContentChanged() {
[2897]82      base.OnContentChanged();
[2]83      if (ProgrammableOperator == null) {
[2799]84        codeEditor.Text = "";
85        assembliesTreeView.Nodes.Clear();
[3014]86        parameterCollectionView.Content = null;
[2]87      } else {
[2799]88        codeEditor.Prefix = GetGeneratedPrefix();
[4838]89        codeEditor.Suffix = String.Format("    {0}\n  }}\n}}", ProgrammableOperator.MethodSuffix);
[2799]90        codeEditor.UserCode = ProgrammableOperator.Code;
91        if (codeEditor.UserCode == "")
[3903]92          codeEditor.UserCode = "    \n    \n    \n    \n";
[2799]93        InitializeAssemblyList();
94        InitializeNamespacesList();
95        foreach (var a in ProgrammableOperator.SelectedAssemblies) {
96          codeEditor.AddAssembly(a);
97        }
98        codeEditor.ScrollAfterPrefix();
[2897]99        codeEditor.ShowCompileErrors(ProgrammableOperator.CompileErrors, "ProgrammableOperator");
[3903]100        showCodeButton.Enabled =
101          ProgrammableOperator.CompilationUnitCode != null &&
[3008]102          ProgrammableOperator.CompilationUnitCode.Length > 0;
[3014]103        parameterCollectionView.Content = ProgrammableOperator.Parameters;
[5042]104        if (ProgrammableOperator.CompileErrors == null) {
105          compilationLabel.ForeColor = SystemColors.ControlDarkDark;
106          compilationLabel.Text = "not compiled";
107        } else if (ProgrammableOperator.CompileErrors.HasErrors) {
108          compilationLabel.ForeColor = Color.DarkRed;
109          compilationLabel.Text = "compilation failed";
110        } else {
111          compilationLabel.ForeColor = Color.DarkGreen;
112          compilationLabel.Text = "compilation successful";
113        }
114
[2]115      }
116    }
117
[3906]118    protected override void SetEnabledStateOfControls() {
119      base.SetEnabledStateOfControls();
[3454]120      parameterCollectionView.Enabled = Content != null;
121      assembliesTreeView.Enabled = Content != null && !ReadOnly;
122      namespacesTreeView.Enabled = Content != null && !ReadOnly;
123      compileButton.Enabled = Content != null && !ReadOnly;
124      codeEditor.Enabled = Content != null && !ReadOnly;
[5042]125      showCodeButton.Enabled = Content != null && !string.IsNullOrEmpty(ProgrammableOperator.CompilationUnitCode) && !ReadOnly;
[3454]126    }
127
[5042]128    #region Child Control event handlers
[2799]129    private void compileButton_Click(object sender, EventArgs e) {
130      Recompile();
131    }
[5042]132    private void breakpointCheckBox_CheckedChanged(object sender, EventArgs e) {
133      if (ProgrammableOperator != null) ProgrammableOperator.Breakpoint = breakpointCheckBox.Checked;
[2]134    }
[5042]135    private void showCodeButton_Click(object sender, EventArgs e) {
136      new CodeViewer(ProgrammableOperator.CompilationUnitCode).ShowDialog(this);
[2897]137    }
[5042]138    private void codeEditor_Validated(object sender, EventArgs e) {
139      ProgrammableOperator.Code = codeEditor.UserCode;
140    }
[2799]141    private void assembliesTreeView_AfterCheck(object sender, TreeViewEventArgs e) {
142      if (initializing)
143        return;
144      Assembly a = e.Node.Tag as Assembly;
145      if (a == null && e.Node.Nodes.Count > 0) {
146        foreach (TreeNode n in e.Node.Nodes)
147          n.Checked = e.Node.Checked;
148        return;
149      } else {
150        if (e.Node.Checked) {
151          ProgrammableOperator.SelectAssembly(a);
152          codeEditor.AddAssembly(a);
153        } else {
154          ProgrammableOperator.UnselectAssembly(a);
155          codeEditor.RemoveAssembly(a);
156        }
157      }
158      InitializeNamespacesList();
159      codeEditor.Prefix = GetGeneratedPrefix();
160    }
[5042]161    private void namespacesTreeView_AfterCheck(object sender, TreeViewEventArgs e) {
162      if (initializing)
163        return;
164      if (e.Node.Checked) {
165        ProgrammableOperator.SelectNamespace(e.Node.FullPath);
166      } else {
167        ProgrammableOperator.UnselectNamespace(e.Node.FullPath);
168      }
169      codeEditor.Prefix = GetGeneratedPrefix();
170    }
171    #endregion
[2799]172
[5042]173    #region global HotKeys
174    protected override bool ProcessCmdKey(ref Message msg, Keys keyData) {
175      if (keyData == Keys.F6) {
176        Control activeControl = ActiveControl;
177        compileButton.Focus();
178        Recompile();
179        if (activeControl != null)
180          activeControl.Focus();
181        return true;
182      }
183      return base.ProcessCmdKey(ref msg, keyData);
184    }
185    #endregion
186
187    #region Auxiliary functions
188
189    private string GetGeneratedPrefix() {
190      StringBuilder prefix = new StringBuilder();
191      foreach (var ns in ProgrammableOperator.GetSelectedAndValidNamespaces()) {
192        prefix.Append("using ").Append(ns).AppendLine(";");
193      }
194      prefix.AppendLine();
195      prefix.Append("public class ").Append(ProgrammableOperator.CompiledTypeName).AppendLine(" {");
196      prefix.Append("  ").Append(ProgrammableOperator.Signature).AppendLine(" {");
197      return prefix.ToString();
198    }
199
200    private void Recompile() {
201      this.Enabled = false;
202      try {
203        ProgrammableOperator.Compile();
204      } catch (Exception ex) {
205        ErrorHandling.ShowErrorDialog(this, ex);
206      }
207      OnContentChanged();
208      this.Enabled = true;
209    }
210
[2799]211    private bool initializing = false;
212    private void InitializeAssemblyList() {
213      initializing = true;
214      assembliesTreeView.Enabled = false;
215      namespacesTreeView.Enabled = false;
216      assembliesTreeView.BeginUpdate();
217      assembliesTreeView.Nodes.Clear();
218      var selectedAssemblies = new HashSet<Assembly>(ProgrammableOperator.SelectedAssemblies);
219      foreach (var p in ProgrammableOperator.Plugins) {
220        var node = assembliesTreeView.Nodes.Add(p.Key);
221        node.Tag = p;
[3903]222        node.ImageIndex = 1;
[2799]223        foreach (var a in p.Value) {
224          var aNode = node.Nodes.Add(a.GetName().Name);
225          aNode.Tag = a;
[3903]226          aNode.ImageIndex = 0;
[2799]227          if (selectedAssemblies.Contains(a))
228            aNode.Checked = true;
229        }
230        if (node.Nodes.Count == 1 && node.Nodes[0].Name == node.Nodes[0].Name) {
231          node.Tag = node.Nodes[0].Tag;
[3903]232          node.ImageIndex = node.Nodes[0].ImageIndex;
233          node.Checked = node.Nodes[0].Checked;
[2799]234          node.Nodes.Clear();
235        } else if (node.Nodes.Count > 0 && node.Nodes.Cast<TreeNode>().All(n => n.Checked)) {
236          node.Checked = true;
237        }
238      }
[3903]239      assembliesTreeView.Sort();
[2799]240      assembliesTreeView.EndUpdate();
241      assembliesTreeView.Enabled = true;
242      namespacesTreeView.Enabled = true;
243      initializing = false;
244    }
245
246    private void InitializeNamespacesList() {
247      initializing = true;
248      namespacesTreeView.Enabled = false;
249      namespacesTreeView.BeginUpdate();
250      TreeNode oldTree = new TreeNode("root");
251      CloneTreeNodeCollection(oldTree, namespacesTreeView.Nodes);
252      namespacesTreeView.Nodes.Clear();
253      var selectedNamespaces = new HashSet<string>(ProgrammableOperator.Namespaces);
254      foreach (var ns in ProgrammableOperator.GetAllNamespaces(true))
255        AddNamespace(namespacesTreeView.Nodes, ns, selectedNamespaces.Contains(ns), oldTree);
256      codeEditor.Prefix = GetGeneratedPrefix();
[3903]257      namespacesTreeView.Sort();
[2799]258      namespacesTreeView.EndUpdate();
259      namespacesTreeView.Enabled = true;
260      initializing = false;
261    }
262
263    private void CloneTreeNodeCollection(TreeNode root, TreeNodeCollection nodes) {
264      foreach (TreeNode n in nodes) {
265        TreeNode newNode = root.Nodes.Add(n.Text, n.Text);
266        newNode.Checked = n.Checked;
[3903]267        newNode.ImageIndex = n.ImageIndex;
[2799]268        CloneTreeNodeCollection(newNode, n.Nodes);
269        if (n.IsExpanded)
270          newNode.Expand();
271      }
272    }
273
274    private bool AddNamespace(TreeNodeCollection parentNodes, string ns, bool isSelected, TreeNode oldTree) {
275      int dotIndex = ns.IndexOf('.');
276      string prefix = ns;
277      if (dotIndex != -1)
278        prefix = ns.Substring(0, dotIndex);
279      TreeNode node = GetOrCreateNode(parentNodes, prefix);
280      TreeNode oldNode = MaybeGetNode(oldTree, prefix);
281      bool isNew = oldNode == null;
282      if (dotIndex != -1 && dotIndex + 1 < ns.Length) {
283        isNew = AddNamespace(node.Nodes, ns.Substring(dotIndex + 1, ns.Length - (dotIndex + 1)), isSelected, oldNode);
284      } else {
285        node.Checked = isSelected;
286      }
287      if (isNew || oldNode != null && oldNode.IsExpanded)
288        node.Expand();
[3903]289      if (isNew) {
[2799]290        namespacesTreeView.SelectedNode = node;
[3903]291        node.ImageIndex = 0;
292      } else {
293        node.ImageIndex = oldNode.ImageIndex;
294      }
[2799]295      return isNew;
296    }
297
298    private static TreeNode MaybeGetNode(TreeNode parentNode, string key) {
299      if (parentNode == null)
300        return null;
301      if (parentNode.Nodes.ContainsKey(key))
302        return parentNode.Nodes[key];
303      return null;
304    }
305
306    private static TreeNode GetOrCreateNode(TreeNodeCollection parentNodes, string key) {
307      TreeNode node = null;
308      if (parentNodes.ContainsKey(key)) {
309        node = parentNodes[key];
310      } else {
311        node = parentNodes.Add(key, key);
312      }
313      return node;
314    }
315
[5042]316    #endregion
[2]317  }
[2799]318}
Note: See TracBrowser for help on using the repository browser.