Free cookie consent management tool by TermsFeed Policy Generator

source: branches/SuccessProgressAnalysis/HeuristicLab.Operators.Programmable/3.3/ProgrammableOperatorView.cs @ 5598

Last change on this file since 5598 was 5287, checked in by abeham, 14 years ago

#1337

  • Renamed VS2008ImageLibrary resource to VSImageLibrary
  • Added Filter icon to the VS2010ImageLibrary folder and the resource manager
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(VSImageLibrary.Namespace);
50      assembliesTreeView.ImageList = new ImageList();
51      assembliesTreeView.ImageList.Images.Add(VSImageLibrary.Assembly);
52      assembliesTreeView.ImageList.Images.Add(VSImageLibrary.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      breakpointCheckBox.Enabled = Content != null && !Locked;
121      parameterCollectionView.Enabled = Content != null;
122      assembliesTreeView.Enabled = Content != null && !ReadOnly;
123      namespacesTreeView.Enabled = Content != null && !ReadOnly;
124      compileButton.Enabled = Content != null && !ReadOnly;
125      codeEditor.Enabled = Content != null && !ReadOnly;
126      showCodeButton.Enabled = Content != null && !string.IsNullOrEmpty(ProgrammableOperator.CompilationUnitCode) && !ReadOnly;
127    }
128
129    #region Child Control event handlers
130    private void compileButton_Click(object sender, EventArgs e) {
131      Recompile();
132    }
133    private void breakpointCheckBox_CheckedChanged(object sender, EventArgs e) {
134      if (ProgrammableOperator != null) ProgrammableOperator.Breakpoint = breakpointCheckBox.Checked;
135    }
136    private void showCodeButton_Click(object sender, EventArgs e) {
137      new CodeViewer(ProgrammableOperator.CompilationUnitCode).ShowDialog(this);
138    }
139    private void codeEditor_Validated(object sender, EventArgs e) {
140      ProgrammableOperator.Code = codeEditor.UserCode;
141    }
142    private void assembliesTreeView_AfterCheck(object sender, TreeViewEventArgs e) {
143      if (initializing)
144        return;
145      Assembly a = e.Node.Tag as Assembly;
146      if (a == null && e.Node.Nodes.Count > 0) {
147        foreach (TreeNode n in e.Node.Nodes)
148          n.Checked = e.Node.Checked;
149        return;
150      } else {
151        if (e.Node.Checked) {
152          ProgrammableOperator.SelectAssembly(a);
153          codeEditor.AddAssembly(a);
154        } else {
155          ProgrammableOperator.UnselectAssembly(a);
156          codeEditor.RemoveAssembly(a);
157        }
158      }
159      InitializeNamespacesList();
160      codeEditor.Prefix = GetGeneratedPrefix();
161    }
162    private void namespacesTreeView_AfterCheck(object sender, TreeViewEventArgs e) {
163      if (initializing)
164        return;
165      if (e.Node.Checked) {
166        ProgrammableOperator.SelectNamespace(e.Node.FullPath);
167      } else {
168        ProgrammableOperator.UnselectNamespace(e.Node.FullPath);
169      }
170      codeEditor.Prefix = GetGeneratedPrefix();
171    }
172    #endregion
173
174    #region global HotKeys
175    protected override bool ProcessCmdKey(ref Message msg, Keys keyData) {
176      if (keyData == Keys.F6) {
177        Control activeControl = ActiveControl;
178        compileButton.Focus();
179        Recompile();
180        if (activeControl != null)
181          activeControl.Focus();
182        return true;
183      }
184      return base.ProcessCmdKey(ref msg, keyData);
185    }
186    #endregion
187
188    #region Auxiliary functions
189
190    private string GetGeneratedPrefix() {
191      StringBuilder prefix = new StringBuilder();
192      foreach (var ns in ProgrammableOperator.GetSelectedAndValidNamespaces()) {
193        prefix.Append("using ").Append(ns).AppendLine(";");
194      }
195      prefix.AppendLine();
196      prefix.Append("public class ").Append(ProgrammableOperator.CompiledTypeName).AppendLine(" {");
197      prefix.Append("  ").Append(ProgrammableOperator.Signature).AppendLine(" {");
198      return prefix.ToString();
199    }
200
201    private void Recompile() {
202      this.Enabled = false;
203      try {
204        ProgrammableOperator.Compile();
205      } catch (Exception ex) {
206        ErrorHandling.ShowErrorDialog(this, ex);
207      }
208      OnContentChanged();
209      this.Enabled = true;
210    }
211
212    private bool initializing = false;
213    private void InitializeAssemblyList() {
214      initializing = true;
215      assembliesTreeView.Enabled = false;
216      namespacesTreeView.Enabled = false;
217      assembliesTreeView.BeginUpdate();
218      assembliesTreeView.Nodes.Clear();
219      var selectedAssemblies = new HashSet<Assembly>(ProgrammableOperator.SelectedAssemblies);
220      foreach (var p in ProgrammableOperator.Plugins) {
221        var node = assembliesTreeView.Nodes.Add(p.Key);
222        node.Tag = p;
223        node.ImageIndex = 1;
224        foreach (var a in p.Value) {
225          var aNode = node.Nodes.Add(a.GetName().Name);
226          aNode.Tag = a;
227          aNode.ImageIndex = 0;
228          if (selectedAssemblies.Contains(a))
229            aNode.Checked = true;
230        }
231        if (node.Nodes.Count == 1 && node.Nodes[0].Name == node.Nodes[0].Name) {
232          node.Tag = node.Nodes[0].Tag;
233          node.ImageIndex = node.Nodes[0].ImageIndex;
234          node.Checked = node.Nodes[0].Checked;
235          node.Nodes.Clear();
236        } else if (node.Nodes.Count > 0 && node.Nodes.Cast<TreeNode>().All(n => n.Checked)) {
237          node.Checked = true;
238        }
239      }
240      assembliesTreeView.Sort();
241      assembliesTreeView.EndUpdate();
242      assembliesTreeView.Enabled = true;
243      namespacesTreeView.Enabled = true;
244      initializing = false;
245    }
246
247    private void InitializeNamespacesList() {
248      initializing = true;
249      namespacesTreeView.Enabled = false;
250      namespacesTreeView.BeginUpdate();
251      TreeNode oldTree = new TreeNode("root");
252      CloneTreeNodeCollection(oldTree, namespacesTreeView.Nodes);
253      namespacesTreeView.Nodes.Clear();
254      var selectedNamespaces = new HashSet<string>(ProgrammableOperator.Namespaces);
255      foreach (var ns in ProgrammableOperator.GetAllNamespaces(true))
256        AddNamespace(namespacesTreeView.Nodes, ns, selectedNamespaces.Contains(ns), oldTree);
257      codeEditor.Prefix = GetGeneratedPrefix();
258      namespacesTreeView.Sort();
259      namespacesTreeView.EndUpdate();
260      namespacesTreeView.Enabled = true;
261      initializing = false;
262    }
263
264    private void CloneTreeNodeCollection(TreeNode root, TreeNodeCollection nodes) {
265      foreach (TreeNode n in nodes) {
266        TreeNode newNode = root.Nodes.Add(n.Text, n.Text);
267        newNode.Checked = n.Checked;
268        newNode.ImageIndex = n.ImageIndex;
269        CloneTreeNodeCollection(newNode, n.Nodes);
270        if (n.IsExpanded)
271          newNode.Expand();
272      }
273    }
274
275    private bool AddNamespace(TreeNodeCollection parentNodes, string ns, bool isSelected, TreeNode oldTree) {
276      int dotIndex = ns.IndexOf('.');
277      string prefix = ns;
278      if (dotIndex != -1)
279        prefix = ns.Substring(0, dotIndex);
280      TreeNode node = GetOrCreateNode(parentNodes, prefix);
281      TreeNode oldNode = MaybeGetNode(oldTree, prefix);
282      bool isNew = oldNode == null;
283      if (dotIndex != -1 && dotIndex + 1 < ns.Length) {
284        isNew = AddNamespace(node.Nodes, ns.Substring(dotIndex + 1, ns.Length - (dotIndex + 1)), isSelected, oldNode);
285      } else {
286        node.Checked = isSelected;
287      }
288      if (isNew || oldNode != null && oldNode.IsExpanded)
289        node.Expand();
290      if (isNew) {
291        namespacesTreeView.SelectedNode = node;
292        node.ImageIndex = 0;
293      } else {
294        node.ImageIndex = oldNode.ImageIndex;
295      }
296      return isNew;
297    }
298
299    private static TreeNode MaybeGetNode(TreeNode parentNode, string key) {
300      if (parentNode == null)
301        return null;
302      if (parentNode.Nodes.ContainsKey(key))
303        return parentNode.Nodes[key];
304      return null;
305    }
306
307    private static TreeNode GetOrCreateNode(TreeNodeCollection parentNodes, string key) {
308      TreeNode node = null;
309      if (parentNodes.ContainsKey(key)) {
310        node = parentNodes[key];
311      } else {
312        node = parentNodes.Add(key, key);
313      }
314      return node;
315    }
316
317    #endregion
318  }
319}
Note: See TracBrowser for help on using the repository browser.