Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 4949 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
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.Linq;
25using System.Reflection;
26using System.Text;
27using System.Windows.Forms;
28using HeuristicLab.CodeEditor;
29using HeuristicLab.Common.Resources;
30using HeuristicLab.Core.Views;
31using HeuristicLab.MainForm;
32using HeuristicLab.PluginInfrastructure;
33
34namespace HeuristicLab.Operators.Programmable {
35  [View("ProgrammableOperator View")]
36  [Content(typeof(ProgrammableOperator), true)]
37  public partial class ProgrammableOperatorView : NamedItemView {
38
39    public ProgrammableOperator ProgrammableOperator {
40      get { return (ProgrammableOperator)base.Content; }
41      set { base.Content = (ProgrammableOperator)value; }
42    }
43
44    public ProgrammableOperatorView() {
45      InitializeComponent();
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);
51    }
52
53    protected override void RegisterContentEvents() {
54      base.RegisterContentEvents();
55      ProgrammableOperator.CodeChanged += ProgrammableOperator_CodeChanged;
56      ProgrammableOperator.SignatureChanged += ProgrammableOperator_SignatureChanged;
57    }
58
59    protected override void DeregisterContentEvents() {
60      ProgrammableOperator.CodeChanged -= ProgrammableOperator_CodeChanged;
61      ProgrammableOperator.SignatureChanged -= ProgrammableOperator_SignatureChanged;
62      base.DeregisterContentEvents();
63    }
64
65    protected override void OnContentChanged() {
66      base.OnContentChanged();
67      if (ProgrammableOperator == null) {
68        codeEditor.Text = "";
69        assembliesTreeView.Nodes.Clear();
70        parameterCollectionView.Content = null;
71      } else {
72        codeEditor.Prefix = GetGeneratedPrefix();
73        codeEditor.Suffix = String.Format("    {0}\n  }}\n}}", ProgrammableOperator.MethodSuffix);
74        codeEditor.UserCode = ProgrammableOperator.Code;
75        if (codeEditor.UserCode == "")
76          codeEditor.UserCode = "    \n    \n    \n    \n";
77        InitializeAssemblyList();
78        InitializeNamespacesList();
79        foreach (var a in ProgrammableOperator.SelectedAssemblies) {
80          codeEditor.AddAssembly(a);
81        }
82        codeEditor.ScrollAfterPrefix();
83        codeEditor.ShowCompileErrors(ProgrammableOperator.CompileErrors, "ProgrammableOperator");
84        showCodeButton.Enabled =
85          ProgrammableOperator.CompilationUnitCode != null &&
86          ProgrammableOperator.CompilationUnitCode.Length > 0;
87        parameterCollectionView.Content = ProgrammableOperator.Parameters;
88      }
89    }
90
91    protected override void SetEnabledStateOfControls() {
92      base.SetEnabledStateOfControls();
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
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    }
115
116    private void Recompile() {
117      this.Enabled = false;
118      try {
119        ProgrammableOperator.Compile();
120        MessageBox.Show("Compilation successful", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
121      } catch (Exception ex) {
122        ErrorHandling.ShowErrorDialog(this, ex);
123      }
124      OnContentChanged();
125      this.Enabled = true;
126    }
127
128    private void compileButton_Click(object sender, EventArgs e) {
129      Recompile();
130    }
131
132    #region ProgrammableOperator Events
133    private void ProgrammableOperator_CodeChanged(object sender, EventArgs e) {
134      codeEditor.Text = ProgrammableOperator.Code;
135    }
136    private void ProgrammableOperator_SignatureChanged(object sender, EventArgs args) {
137      codeEditor.Prefix = GetGeneratedPrefix();
138    }
139    #endregion
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;
173        node.ImageIndex = 1;
174        foreach (var a in p.Value) {
175          var aNode = node.Nodes.Add(a.GetName().Name);
176          aNode.Tag = a;
177          aNode.ImageIndex = 0;
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;
183          node.ImageIndex = node.Nodes[0].ImageIndex;
184          node.Checked = node.Nodes[0].Checked;
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      }
190      assembliesTreeView.Sort();
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();
208      namespacesTreeView.Sort();
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;
218        newNode.ImageIndex = n.ImageIndex;
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();
240      if (isNew) {
241        namespacesTreeView.SelectedNode = node;
242        node.ImageIndex = 0;
243      } else {
244        node.ImageIndex = oldNode.ImageIndex;
245      }
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    }
281  }
282}
Note: See TracBrowser for help on using the repository browser.