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
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.ComponentModel;
25using System.Drawing;
26using System.Data;
27using System.Text;
28using System.Linq;
29using System.Windows.Forms;
30using HeuristicLab.Common;
31using HeuristicLab.Core;
32using HeuristicLab.Operators;
33using System.CodeDom.Compiler;
34using System.Reflection;
35using HeuristicLab.CodeEditor;
36using HeuristicLab.Core.Views;
37using HeuristicLab.Operators.Views;
38using HeuristicLab.MainForm;
39using HeuristicLab.PluginInfrastructure;
40using HeuristicLab.Common.Resources;
41
42namespace HeuristicLab.Operators.Programmable {
43  [View("ProgrammableOperator View")]
44  [Content(typeof(ProgrammableOperator), true)]
45  public partial class ProgrammableOperatorView : NamedItemView {
46
47    public ProgrammableOperator ProgrammableOperator {
48      get { return (ProgrammableOperator)base.Content; }
49      set { base.Content = (ProgrammableOperator)value; }
50    }
51
52    public ProgrammableOperatorView() {
53      InitializeComponent();
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);
59    }
60
61    protected override void RegisterContentEvents() {
62      base.RegisterContentEvents();
63      ProgrammableOperator.CodeChanged += ProgrammableOperator_CodeChanged;
64      ProgrammableOperator.SignatureChanged += ProgrammableOperator_SignatureChanged;
65    }
66
67    protected override void DeregisterContentEvents() {
68      ProgrammableOperator.CodeChanged -= ProgrammableOperator_CodeChanged;
69      ProgrammableOperator.SignatureChanged -= ProgrammableOperator_SignatureChanged;
70      base.DeregisterContentEvents();
71    }
72
73    protected override void OnContentChanged() {
74      base.OnContentChanged();
75      if (ProgrammableOperator == null) {
76        codeEditor.Text = "";
77        assembliesTreeView.Nodes.Clear();
78        parameterCollectionView.Content = null;
79      } else {
80        codeEditor.Prefix = GetGeneratedPrefix();
81        codeEditor.Suffix = @"    return null;
82  }
83}";
84        codeEditor.UserCode = ProgrammableOperator.Code;
85        if (codeEditor.UserCode == "")
86          codeEditor.UserCode = "    \n    \n    \n    \n";
87        InitializeAssemblyList();
88        InitializeNamespacesList();
89        foreach (var a in ProgrammableOperator.SelectedAssemblies) {
90          codeEditor.AddAssembly(a);
91        }
92        codeEditor.ScrollAfterPrefix();
93        codeEditor.ShowCompileErrors(ProgrammableOperator.CompileErrors, "ProgrammableOperator");
94        showCodeButton.Enabled =
95          ProgrammableOperator.CompilationUnitCode != null &&
96          ProgrammableOperator.CompilationUnitCode.Length > 0;
97        parameterCollectionView.Content = ProgrammableOperator.Parameters;
98      }
99    }
100
101    protected override void SetEnabledStateOfControls() {
102      base.SetEnabledStateOfControls();
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
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    }
125
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) {
132        ErrorHandling.ShowErrorDialog(this, ex);
133      }
134      OnContentChanged();
135      this.Enabled = true;
136    }
137
138    private void compileButton_Click(object sender, EventArgs e) {
139      Recompile();
140    }
141
142    #region ProgrammableOperator Events
143    private void ProgrammableOperator_CodeChanged(object sender, EventArgs e) {
144      codeEditor.Text = ProgrammableOperator.Code;
145    }
146    private void ProgrammableOperator_SignatureChanged(object sender, EventArgs args) {
147      codeEditor.Prefix = GetGeneratedPrefix();
148    }
149    #endregion
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;
183        node.ImageIndex = 1;
184        foreach (var a in p.Value) {
185          var aNode = node.Nodes.Add(a.GetName().Name);
186          aNode.Tag = a;
187          aNode.ImageIndex = 0;
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;
193          node.ImageIndex = node.Nodes[0].ImageIndex;
194          node.Checked = node.Nodes[0].Checked;
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      }
200      assembliesTreeView.Sort();
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();
218      namespacesTreeView.Sort();
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;
228        newNode.ImageIndex = n.ImageIndex;
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();
250      if (isNew) {
251        namespacesTreeView.SelectedNode = node;
252        node.ImageIndex = 0;
253      } else {
254        node.ImageIndex = oldNode.ImageIndex;
255      }
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    }
291  }
292}
Note: See TracBrowser for help on using the repository browser.