Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 4047 was 3906, checked in by mkommend, 15 years ago

forgot to update ProgrammeableOperatorView (ticket #1020)

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