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

Last change on this file since 3454 was 3454, checked in by swagner, 12 years ago

Adapted views according the new read-only 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      parameterCollectionView.ReadOnly = ReadOnly;
109      assembliesTreeView.Enabled = Content != null && !ReadOnly;
110      namespacesTreeView.Enabled = Content != null && !ReadOnly;
111      compileButton.Enabled = Content != null && !ReadOnly;
112      codeEditor.Enabled = Content != null && !ReadOnly;
113    }
114
115
116    private string GetGeneratedPrefix() {
117      StringBuilder prefix = new StringBuilder();
118      foreach (var ns in ProgrammableOperator.GetSelectedAndValidNamespaces()) {
119        prefix.Append("using ").Append(ns).AppendLine(";");
120      }
121      prefix.AppendLine();
122      prefix.Append("public class ").Append(ProgrammableOperator.CompiledTypeName).AppendLine(" {");
123      prefix.Append("  ").Append(ProgrammableOperator.Signature).AppendLine(" {");
124      return prefix.ToString();
125    }
126
127    private void codeEditor_Validated(object sender, EventArgs e) {
128      ProgrammableOperator.Code = codeEditor.UserCode;
129    }
130
131    private void Recompile() {
132      this.Enabled = false;
133      try {
134        ProgrammableOperator.Compile();
135        MessageBox.Show("Compilation successful", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
136      } catch (Exception ex) {
137        Auxiliary.ShowErrorMessageBox(ex);
138      }
139      OnContentChanged();
140      this.Enabled = true;
141    }
142
143    private void compileButton_Click(object sender, EventArgs e) {
144      Recompile();
145    }
146
147    #region ProgrammableOperator Events
148    private void ProgrammableOperator_CodeChanged(object sender, EventArgs e) {
149      codeEditor.Text = ProgrammableOperator.Code;
150    }
151    private void ProgrammableOperator_SignatureChanged(object sender, EventArgs args) {
152      codeEditor.Prefix = GetGeneratedPrefix();
153    }
154    #endregion
155
156    private void assembliesTreeView_AfterCheck(object sender, TreeViewEventArgs e) {
157      if (initializing)
158        return;
159      Assembly a = e.Node.Tag as Assembly;
160      if (a == null && e.Node.Nodes.Count > 0) {
161        foreach (TreeNode n in e.Node.Nodes)
162          n.Checked = e.Node.Checked;
163        return;
164      } else {
165        if (e.Node.Checked) {
166          ProgrammableOperator.SelectAssembly(a);
167          codeEditor.AddAssembly(a);
168        } else {
169          ProgrammableOperator.UnselectAssembly(a);
170          codeEditor.RemoveAssembly(a);
171        }
172      }
173      InitializeNamespacesList();
174      codeEditor.Prefix = GetGeneratedPrefix();
175    }
176
177    private bool initializing = false;
178    private void InitializeAssemblyList() {
179      initializing = true;
180      assembliesTreeView.Enabled = false;
181      namespacesTreeView.Enabled = false;
182      assembliesTreeView.BeginUpdate();
183      assembliesTreeView.Nodes.Clear();
184      var selectedAssemblies = new HashSet<Assembly>(ProgrammableOperator.SelectedAssemblies);
185      foreach (var p in ProgrammableOperator.Plugins) {
186        var node = assembliesTreeView.Nodes.Add(p.Key);
187        node.Tag = p;
188        foreach (var a in p.Value) {
189          var aNode = node.Nodes.Add(a.GetName().Name);
190          aNode.Tag = a;
191          if (selectedAssemblies.Contains(a))
192            aNode.Checked = true;
193        }
194        if (node.Nodes.Count == 1 && node.Nodes[0].Name == node.Nodes[0].Name) {
195          node.Tag = node.Nodes[0].Tag;
196          node.Nodes.Clear();
197        } else if (node.Nodes.Count > 0 && node.Nodes.Cast<TreeNode>().All(n => n.Checked)) {
198          node.Checked = true;
199        }
200      }
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.EndUpdate();
219      namespacesTreeView.Enabled = true;
220      initializing = false;
221    }
222
223    private void CloneTreeNodeCollection(TreeNode root, TreeNodeCollection nodes) {
224      foreach (TreeNode n in nodes) {
225        TreeNode newNode = root.Nodes.Add(n.Text, n.Text);
226        newNode.Checked = n.Checked;
227        CloneTreeNodeCollection(newNode, n.Nodes);
228        if (n.IsExpanded)
229          newNode.Expand();
230      }
231    }
232
233    private bool AddNamespace(TreeNodeCollection parentNodes, string ns, bool isSelected, TreeNode oldTree) {
234      int dotIndex = ns.IndexOf('.');
235      string prefix = ns;
236      if (dotIndex != -1)
237        prefix = ns.Substring(0, dotIndex);
238      TreeNode node = GetOrCreateNode(parentNodes, prefix);
239      TreeNode oldNode = MaybeGetNode(oldTree, prefix);
240      bool isNew = oldNode == null;
241      if (dotIndex != -1 && dotIndex + 1 < ns.Length) {
242        isNew = AddNamespace(node.Nodes, ns.Substring(dotIndex + 1, ns.Length - (dotIndex + 1)), isSelected, oldNode);
243      } else {
244        node.Checked = isSelected;
245      }
246      if (isNew || oldNode != null && oldNode.IsExpanded)
247        node.Expand();
248      if (isNew)
249        namespacesTreeView.SelectedNode = node;
250      return isNew;
251    }
252
253    private static TreeNode MaybeGetNode(TreeNode parentNode, string key) {
254      if (parentNode == null)
255        return null;
256      if (parentNode.Nodes.ContainsKey(key))
257        return parentNode.Nodes[key];
258      return null;
259    }
260
261    private static TreeNode GetOrCreateNode(TreeNodeCollection parentNodes, string key) {
262      TreeNode node = null;
263      if (parentNodes.ContainsKey(key)) {
264        node = parentNodes[key];
265      } else {
266        node = parentNodes.Add(key, key);
267      }
268      return node;
269    }
270
271    private void namespacesTreeView_AfterCheck(object sender, TreeViewEventArgs e) {
272      if (initializing)
273        return;
274      if (e.Node.Checked) {
275        ProgrammableOperator.SelectNamespace(e.Node.FullPath);
276      } else {
277        ProgrammableOperator.UnselectNamespace(e.Node.FullPath);
278      }
279      codeEditor.Prefix = GetGeneratedPrefix();
280    }
281
282    private void showCodeButton_Click(object sender, EventArgs e) {
283      new CodeViewer(ProgrammableOperator.CompilationUnitCode).ShowDialog(this);
284    }
285  }
286}
Note: See TracBrowser for help on using the repository browser.