Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 5042 was 5042, checked in by epitzer, 12 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
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 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.Drawing;
25using System.Linq;
26using System.Reflection;
27using System.Text;
28using System.Windows.Forms;
29using HeuristicLab.CodeEditor;
30using HeuristicLab.Common.Resources;
31using HeuristicLab.Core.Views;
32using HeuristicLab.MainForm;
33using HeuristicLab.PluginInfrastructure;
34
35namespace HeuristicLab.Operators.Programmable {
36
37  [View("ProgrammableOperator View")]
38  [Content(typeof(ProgrammableOperator), true)]
39  public partial class ProgrammableOperatorView : NamedItemView {
40
41    public ProgrammableOperator ProgrammableOperator {
42      get { return (ProgrammableOperator)base.Content; }
43      set { base.Content = (ProgrammableOperator)value; }
44    }
45
46    public ProgrammableOperatorView() {
47      InitializeComponent();
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);
53    }
54
55    protected override void RegisterContentEvents() {
56      base.RegisterContentEvents();
57      ProgrammableOperator.CodeChanged += ProgrammableOperator_CodeChanged;
58      ProgrammableOperator.SignatureChanged += ProgrammableOperator_SignatureChanged;
59      ProgrammableOperator.BreakpointChanged += ProgrammableOperator_BreakpointChanged;
60    }
61
62    protected override void DeregisterContentEvents() {
63      ProgrammableOperator.CodeChanged -= ProgrammableOperator_CodeChanged;
64      ProgrammableOperator.SignatureChanged -= ProgrammableOperator_SignatureChanged;
65      ProgrammableOperator.BreakpointChanged -= ProgrammableOperator_BreakpointChanged;
66      base.DeregisterContentEvents();
67    }
68
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
81    protected override void OnContentChanged() {
82      base.OnContentChanged();
83      if (ProgrammableOperator == null) {
84        codeEditor.Text = "";
85        assembliesTreeView.Nodes.Clear();
86        parameterCollectionView.Content = null;
87      } else {
88        codeEditor.Prefix = GetGeneratedPrefix();
89        codeEditor.Suffix = String.Format("    {0}\n  }}\n}}", ProgrammableOperator.MethodSuffix);
90        codeEditor.UserCode = ProgrammableOperator.Code;
91        if (codeEditor.UserCode == "")
92          codeEditor.UserCode = "    \n    \n    \n    \n";
93        InitializeAssemblyList();
94        InitializeNamespacesList();
95        foreach (var a in ProgrammableOperator.SelectedAssemblies) {
96          codeEditor.AddAssembly(a);
97        }
98        codeEditor.ScrollAfterPrefix();
99        codeEditor.ShowCompileErrors(ProgrammableOperator.CompileErrors, "ProgrammableOperator");
100        showCodeButton.Enabled =
101          ProgrammableOperator.CompilationUnitCode != null &&
102          ProgrammableOperator.CompilationUnitCode.Length > 0;
103        parameterCollectionView.Content = ProgrammableOperator.Parameters;
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
115      }
116    }
117
118    protected override void SetEnabledStateOfControls() {
119      base.SetEnabledStateOfControls();
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;
125      showCodeButton.Enabled = Content != null && !string.IsNullOrEmpty(ProgrammableOperator.CompilationUnitCode) && !ReadOnly;
126    }
127
128    #region Child Control event handlers
129    private void compileButton_Click(object sender, EventArgs e) {
130      Recompile();
131    }
132    private void breakpointCheckBox_CheckedChanged(object sender, EventArgs e) {
133      if (ProgrammableOperator != null) ProgrammableOperator.Breakpoint = breakpointCheckBox.Checked;
134    }
135    private void showCodeButton_Click(object sender, EventArgs e) {
136      new CodeViewer(ProgrammableOperator.CompilationUnitCode).ShowDialog(this);
137    }
138    private void codeEditor_Validated(object sender, EventArgs e) {
139      ProgrammableOperator.Code = codeEditor.UserCode;
140    }
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    }
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
172
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
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;
222        node.ImageIndex = 1;
223        foreach (var a in p.Value) {
224          var aNode = node.Nodes.Add(a.GetName().Name);
225          aNode.Tag = a;
226          aNode.ImageIndex = 0;
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;
232          node.ImageIndex = node.Nodes[0].ImageIndex;
233          node.Checked = node.Nodes[0].Checked;
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      }
239      assembliesTreeView.Sort();
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();
257      namespacesTreeView.Sort();
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;
267        newNode.ImageIndex = n.ImageIndex;
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();
289      if (isNew) {
290        namespacesTreeView.SelectedNode = node;
291        node.ImageIndex = 0;
292      } else {
293        node.ImageIndex = oldNode.ImageIndex;
294      }
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
316    #endregion
317  }
318}
Note: See TracBrowser for help on using the repository browser.