Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 12014 was 12012, checked in by ascheibe, 10 years ago

#2212 merged r12008, r12009, r12010 back into trunk

File size: 12.1 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 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.UserCode = 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.UserCode = string.Empty;
85        assembliesTreeView.Nodes.Clear();
86        parameterCollectionView.Content = null;
87      } else {
88        codeEditor.Prefix = GetGeneratedPrefix();
89        codeEditor.Suffix = String.Format("    {0}{1}  }}{1}}}", ProgrammableOperator.MethodSuffix, Environment.NewLine);
90        codeEditor.UserCode = ProgrammableOperator.Code;
91        if (codeEditor.UserCode == string.Empty)
92          codeEditor.UserCode = string.Format("    {0}", Environment.NewLine);
93        InitializeAssemblyList();
94        InitializeNamespacesList();
95        codeEditor.AddAssemblies(ProgrammableOperator.SelectedAssemblies);
96        codeEditor.ScrollAfterPrefix();
97        codeEditor.ShowCompileErrors(ProgrammableOperator.CompileErrors);
98        showCodeButton.Enabled = !string.IsNullOrEmpty(ProgrammableOperator.CompilationUnitCode);
99        parameterCollectionView.Content = ProgrammableOperator.Parameters;
100        if (ProgrammableOperator.CompileErrors == null) {
101          compilationLabel.ForeColor = SystemColors.ControlDarkDark;
102          compilationLabel.Text = "Not compiled";
103        } else if (ProgrammableOperator.CompileErrors.HasErrors) {
104          compilationLabel.ForeColor = Color.DarkRed;
105          compilationLabel.Text = "Compilation failed";
106        } else {
107          compilationLabel.ForeColor = Color.DarkGreen;
108          compilationLabel.Text = "Compilation successful";
109        }
110
111      }
112    }
113
114    protected override void SetEnabledStateOfControls() {
115      base.SetEnabledStateOfControls();
116      breakpointCheckBox.Enabled = Content != null && !Locked;
117      parameterCollectionView.Enabled = Content != null;
118      assembliesTreeView.Enabled = Content != null && !ReadOnly;
119      namespacesTreeView.Enabled = Content != null && !ReadOnly;
120      compileButton.Enabled = Content != null && !ReadOnly;
121      codeEditor.Enabled = Content != null && !ReadOnly;
122      showCodeButton.Enabled = Content != null && !string.IsNullOrEmpty(ProgrammableOperator.CompilationUnitCode) && !ReadOnly;
123    }
124
125    #region Child Control event handlers
126    private void compileButton_Click(object sender, EventArgs e) {
127      Recompile();
128    }
129    private void breakpointCheckBox_CheckedChanged(object sender, EventArgs e) {
130      if (ProgrammableOperator != null) ProgrammableOperator.Breakpoint = breakpointCheckBox.Checked;
131    }
132    private void showCodeButton_Click(object sender, EventArgs e) {
133      new CodeViewer(ProgrammableOperator.CompilationUnitCode).ShowDialog(this);
134    }
135    private void codeEditor_Validated(object sender, EventArgs e) {
136      ProgrammableOperator.Code = codeEditor.UserCode;
137    }
138    private void assembliesTreeView_AfterCheck(object sender, TreeViewEventArgs e) {
139      if (initializing)
140        return;
141      Assembly a = e.Node.Tag as Assembly;
142      if (a == null && e.Node.Nodes.Count > 0) {
143        foreach (TreeNode n in e.Node.Nodes)
144          n.Checked = e.Node.Checked;
145        return;
146      } else {
147        if (e.Node.Checked) {
148          ProgrammableOperator.SelectAssembly(a);
149          codeEditor.AddAssembly(a);
150        } else {
151          ProgrammableOperator.UnselectAssembly(a);
152          codeEditor.RemoveAssembly(a);
153        }
154      }
155      InitializeNamespacesList();
156      codeEditor.Prefix = GetGeneratedPrefix();
157    }
158    private void namespacesTreeView_AfterCheck(object sender, TreeViewEventArgs e) {
159      if (initializing)
160        return;
161      if (e.Node.Checked) {
162        ProgrammableOperator.SelectNamespace(e.Node.FullPath);
163      } else {
164        ProgrammableOperator.UnselectNamespace(e.Node.FullPath);
165      }
166      codeEditor.Prefix = GetGeneratedPrefix();
167    }
168    #endregion
169
170    #region global HotKeys
171    protected override bool ProcessCmdKey(ref Message msg, Keys keyData) {
172      if (keyData == Keys.F6) {
173        Control activeControl = ActiveControl;
174        compileButton.Focus();
175        Recompile();
176        if (activeControl != null)
177          activeControl.Focus();
178        return true;
179      }
180      return base.ProcessCmdKey(ref msg, keyData);
181    }
182    #endregion
183
184    #region Auxiliary functions
185
186    private string GetGeneratedPrefix() {
187      StringBuilder prefix = new StringBuilder();
188      foreach (var ns in ProgrammableOperator.GetSelectedAndValidNamespaces()) {
189        prefix.Append("using ").Append(ns).AppendLine(";");
190      }
191      prefix.AppendLine();
192      prefix.Append("public class ").Append(ProgrammableOperator.CompiledTypeName).AppendLine(" {");
193      prefix.Append("  ").Append(ProgrammableOperator.Signature).AppendLine(" {");
194      return prefix.ToString();
195    }
196
197    private void Recompile() {
198      this.Enabled = false;
199      try {
200        ProgrammableOperator.Compile();
201      } catch (Exception ex) {
202        ErrorHandling.ShowErrorDialog(this, ex);
203      }
204      OnContentChanged();
205      this.Enabled = true;
206    }
207
208    private bool initializing = false;
209    private void InitializeAssemblyList() {
210      initializing = true;
211      assembliesTreeView.Enabled = false;
212      namespacesTreeView.Enabled = false;
213      assembliesTreeView.BeginUpdate();
214      assembliesTreeView.Nodes.Clear();
215      var selectedAssemblies = new HashSet<Assembly>(ProgrammableOperator.SelectedAssemblies);
216      foreach (var p in ProgrammableOperator.Plugins) {
217        var node = assembliesTreeView.Nodes.Add(p.Key);
218        node.Tag = p;
219        node.ImageIndex = 1;
220        foreach (var a in p.Value) {
221          var aNode = node.Nodes.Add(a.GetName().Name);
222          aNode.Tag = a;
223          aNode.ImageIndex = 0;
224          if (selectedAssemblies.Contains(a))
225            aNode.Checked = true;
226        }
227        if (node.Nodes.Count == 1 && node.Nodes[0].Name == node.Nodes[0].Name) {
228          node.Tag = node.Nodes[0].Tag;
229          node.ImageIndex = node.Nodes[0].ImageIndex;
230          node.Checked = node.Nodes[0].Checked;
231          node.Nodes.Clear();
232        } else if (node.Nodes.Count > 0 && node.Nodes.Cast<TreeNode>().All(n => n.Checked)) {
233          node.Checked = true;
234        }
235      }
236      assembliesTreeView.Sort();
237      assembliesTreeView.EndUpdate();
238      assembliesTreeView.Enabled = true;
239      namespacesTreeView.Enabled = true;
240      initializing = false;
241    }
242
243    private void InitializeNamespacesList() {
244      initializing = true;
245      namespacesTreeView.Enabled = false;
246      namespacesTreeView.BeginUpdate();
247      TreeNode oldTree = new TreeNode("root");
248      CloneTreeNodeCollection(oldTree, namespacesTreeView.Nodes);
249      namespacesTreeView.Nodes.Clear();
250      var selectedNamespaces = new HashSet<string>(ProgrammableOperator.Namespaces);
251      foreach (var ns in ProgrammableOperator.GetAllNamespaces(true))
252        AddNamespace(namespacesTreeView.Nodes, ns, selectedNamespaces.Contains(ns), oldTree);
253      codeEditor.Prefix = GetGeneratedPrefix();
254      namespacesTreeView.Sort();
255      namespacesTreeView.EndUpdate();
256      namespacesTreeView.Enabled = true;
257      initializing = false;
258    }
259
260    private void CloneTreeNodeCollection(TreeNode root, TreeNodeCollection nodes) {
261      foreach (TreeNode n in nodes) {
262        TreeNode newNode = root.Nodes.Add(n.Text, n.Text);
263        newNode.Checked = n.Checked;
264        newNode.ImageIndex = n.ImageIndex;
265        CloneTreeNodeCollection(newNode, n.Nodes);
266        if (n.IsExpanded)
267          newNode.Expand();
268      }
269    }
270
271    private bool AddNamespace(TreeNodeCollection parentNodes, string ns, bool isSelected, TreeNode oldTree) {
272      int dotIndex = ns.IndexOf('.');
273      string prefix = ns;
274      if (dotIndex != -1)
275        prefix = ns.Substring(0, dotIndex);
276      TreeNode node = GetOrCreateNode(parentNodes, prefix);
277      TreeNode oldNode = MaybeGetNode(oldTree, prefix);
278      bool isNew = oldNode == null;
279      if (dotIndex != -1 && dotIndex + 1 < ns.Length) {
280        isNew = AddNamespace(node.Nodes, ns.Substring(dotIndex + 1, ns.Length - (dotIndex + 1)), isSelected, oldNode);
281      } else {
282        node.Checked = isSelected;
283      }
284      if (isNew || oldNode != null && oldNode.IsExpanded)
285        node.Expand();
286      if (isNew) {
287        namespacesTreeView.SelectedNode = node;
288        node.ImageIndex = 0;
289      } else {
290        node.ImageIndex = oldNode.ImageIndex;
291      }
292      return isNew;
293    }
294
295    private static TreeNode MaybeGetNode(TreeNode parentNode, string key) {
296      if (parentNode == null)
297        return null;
298      if (parentNode.Nodes.ContainsKey(key))
299        return parentNode.Nodes[key];
300      return null;
301    }
302
303    private static TreeNode GetOrCreateNode(TreeNodeCollection parentNodes, string key) {
304      TreeNode node = null;
305      if (parentNodes.ContainsKey(key)) {
306        node = parentNodes[key];
307      } else {
308        node = parentNodes.Add(key, key);
309      }
310      return node;
311    }
312
313    #endregion
314  }
315}
Note: See TracBrowser for help on using the repository browser.