Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ParameterBinding/HeuristicLab.Operators.Programmable/3.3/ProgrammableOperatorView.cs @ 12147

Last change on this file since 12147 was 4477, checked in by gkronber, 14 years ago

Merged r4458, r4459,r4462,r4464 from data analysis exploration branch into trunk. #1142

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 = @"    return null;
74  }
75}";
76        codeEditor.UserCode = ProgrammableOperator.Code;
77        if (codeEditor.UserCode == "")
78          codeEditor.UserCode = "    \n    \n    \n    \n";
79        InitializeAssemblyList();
80        InitializeNamespacesList();
81        foreach (var a in ProgrammableOperator.SelectedAssemblies) {
82          codeEditor.AddAssembly(a);
83        }
84        codeEditor.ScrollAfterPrefix();
85        codeEditor.ShowCompileErrors(ProgrammableOperator.CompileErrors, "ProgrammableOperator");
86        showCodeButton.Enabled =
87          ProgrammableOperator.CompilationUnitCode != null &&
88          ProgrammableOperator.CompilationUnitCode.Length > 0;
89        parameterCollectionView.Content = ProgrammableOperator.Parameters;
90      }
91    }
92
93    protected override void SetEnabledStateOfControls() {
94      base.SetEnabledStateOfControls();
95      parameterCollectionView.Enabled = Content != null;
96      assembliesTreeView.Enabled = Content != null && !ReadOnly;
97      namespacesTreeView.Enabled = Content != null && !ReadOnly;
98      compileButton.Enabled = Content != null && !ReadOnly;
99      codeEditor.Enabled = Content != null && !ReadOnly;
100    }
101
102
103    private string GetGeneratedPrefix() {
104      StringBuilder prefix = new StringBuilder();
105      foreach (var ns in ProgrammableOperator.GetSelectedAndValidNamespaces()) {
106        prefix.Append("using ").Append(ns).AppendLine(";");
107      }
108      prefix.AppendLine();
109      prefix.Append("public class ").Append(ProgrammableOperator.CompiledTypeName).AppendLine(" {");
110      prefix.Append("  ").Append(ProgrammableOperator.Signature).AppendLine(" {");
111      return prefix.ToString();
112    }
113
114    private void codeEditor_Validated(object sender, EventArgs e) {
115      ProgrammableOperator.Code = codeEditor.UserCode;
116    }
117
118    private void Recompile() {
119      this.Enabled = false;
120      try {
121        ProgrammableOperator.Compile();
122        MessageBox.Show("Compilation successful", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
123      }
124      catch (Exception ex) {
125        ErrorHandling.ShowErrorDialog(this, ex);
126      }
127      OnContentChanged();
128      this.Enabled = true;
129    }
130
131    private void compileButton_Click(object sender, EventArgs e) {
132      Recompile();
133    }
134
135    #region ProgrammableOperator Events
136    private void ProgrammableOperator_CodeChanged(object sender, EventArgs e) {
137      codeEditor.Text = ProgrammableOperator.Code;
138    }
139    private void ProgrammableOperator_SignatureChanged(object sender, EventArgs args) {
140      codeEditor.Prefix = GetGeneratedPrefix();
141    }
142    #endregion
143
144    private void assembliesTreeView_AfterCheck(object sender, TreeViewEventArgs e) {
145      if (initializing)
146        return;
147      Assembly a = e.Node.Tag as Assembly;
148      if (a == null && e.Node.Nodes.Count > 0) {
149        foreach (TreeNode n in e.Node.Nodes)
150          n.Checked = e.Node.Checked;
151        return;
152      } else {
153        if (e.Node.Checked) {
154          ProgrammableOperator.SelectAssembly(a);
155          codeEditor.AddAssembly(a);
156        } else {
157          ProgrammableOperator.UnselectAssembly(a);
158          codeEditor.RemoveAssembly(a);
159        }
160      }
161      InitializeNamespacesList();
162      codeEditor.Prefix = GetGeneratedPrefix();
163    }
164
165    private bool initializing = false;
166    private void InitializeAssemblyList() {
167      initializing = true;
168      assembliesTreeView.Enabled = false;
169      namespacesTreeView.Enabled = false;
170      assembliesTreeView.BeginUpdate();
171      assembliesTreeView.Nodes.Clear();
172      var selectedAssemblies = new HashSet<Assembly>(ProgrammableOperator.SelectedAssemblies);
173      foreach (var p in ProgrammableOperator.Plugins) {
174        var node = assembliesTreeView.Nodes.Add(p.Key);
175        node.Tag = p;
176        node.ImageIndex = 1;
177        foreach (var a in p.Value) {
178          var aNode = node.Nodes.Add(a.GetName().Name);
179          aNode.Tag = a;
180          aNode.ImageIndex = 0;
181          if (selectedAssemblies.Contains(a))
182            aNode.Checked = true;
183        }
184        if (node.Nodes.Count == 1 && node.Nodes[0].Name == node.Nodes[0].Name) {
185          node.Tag = node.Nodes[0].Tag;
186          node.ImageIndex = node.Nodes[0].ImageIndex;
187          node.Checked = node.Nodes[0].Checked;
188          node.Nodes.Clear();
189        } else if (node.Nodes.Count > 0 && node.Nodes.Cast<TreeNode>().All(n => n.Checked)) {
190          node.Checked = true;
191        }
192      }
193      assembliesTreeView.Sort();
194      assembliesTreeView.EndUpdate();
195      assembliesTreeView.Enabled = true;
196      namespacesTreeView.Enabled = true;
197      initializing = false;
198    }
199
200    private void InitializeNamespacesList() {
201      initializing = true;
202      namespacesTreeView.Enabled = false;
203      namespacesTreeView.BeginUpdate();
204      TreeNode oldTree = new TreeNode("root");
205      CloneTreeNodeCollection(oldTree, namespacesTreeView.Nodes);
206      namespacesTreeView.Nodes.Clear();
207      var selectedNamespaces = new HashSet<string>(ProgrammableOperator.Namespaces);
208      foreach (var ns in ProgrammableOperator.GetAllNamespaces(true))
209        AddNamespace(namespacesTreeView.Nodes, ns, selectedNamespaces.Contains(ns), oldTree);
210      codeEditor.Prefix = GetGeneratedPrefix();
211      namespacesTreeView.Sort();
212      namespacesTreeView.EndUpdate();
213      namespacesTreeView.Enabled = true;
214      initializing = false;
215    }
216
217    private void CloneTreeNodeCollection(TreeNode root, TreeNodeCollection nodes) {
218      foreach (TreeNode n in nodes) {
219        TreeNode newNode = root.Nodes.Add(n.Text, n.Text);
220        newNode.Checked = n.Checked;
221        newNode.ImageIndex = n.ImageIndex;
222        CloneTreeNodeCollection(newNode, n.Nodes);
223        if (n.IsExpanded)
224          newNode.Expand();
225      }
226    }
227
228    private bool AddNamespace(TreeNodeCollection parentNodes, string ns, bool isSelected, TreeNode oldTree) {
229      int dotIndex = ns.IndexOf('.');
230      string prefix = ns;
231      if (dotIndex != -1)
232        prefix = ns.Substring(0, dotIndex);
233      TreeNode node = GetOrCreateNode(parentNodes, prefix);
234      TreeNode oldNode = MaybeGetNode(oldTree, prefix);
235      bool isNew = oldNode == null;
236      if (dotIndex != -1 && dotIndex + 1 < ns.Length) {
237        isNew = AddNamespace(node.Nodes, ns.Substring(dotIndex + 1, ns.Length - (dotIndex + 1)), isSelected, oldNode);
238      } else {
239        node.Checked = isSelected;
240      }
241      if (isNew || oldNode != null && oldNode.IsExpanded)
242        node.Expand();
243      if (isNew) {
244        namespacesTreeView.SelectedNode = node;
245        node.ImageIndex = 0;
246      } else {
247        node.ImageIndex = oldNode.ImageIndex;
248      }
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.