Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 4848 was 4838, checked in by epitzer, 14 years ago

Better reflect single successor operation generation inside user visible code. (#1275)

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