Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 3580 was 3455, checked in by swagner, 15 years ago

Removed manual propagation of ReadOnly property (#973)

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