1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2019 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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Drawing;
|
---|
25 | using System.Linq;
|
---|
26 | using System.Reflection;
|
---|
27 | using System.Text;
|
---|
28 | using System.Windows.Forms;
|
---|
29 | using HeuristicLab.CodeEditor;
|
---|
30 | using HeuristicLab.Common.Resources;
|
---|
31 | using HeuristicLab.Core.Views;
|
---|
32 | using HeuristicLab.MainForm;
|
---|
33 | using HeuristicLab.PluginInfrastructure;
|
---|
34 |
|
---|
35 | namespace HeuristicLab.Operators.Programmable {
|
---|
36 |
|
---|
37 | [View("ProgrammableOperator View")]
|
---|
38 | [Content(typeof(ProgrammableOperator), true)]
|
---|
39 | public partial class ProgrammableOperatorView : NamedItemView {
|
---|
40 |
|
---|
41 | public ProgrammableOperator ProgrammableOperator {
|
---|
42 | get { return (ProgrammableOperator)base.Content; }
|
---|
43 | set { base.Content = (ProgrammableOperator)value; }
|
---|
44 | }
|
---|
45 |
|
---|
46 | public ProgrammableOperatorView() {
|
---|
47 | InitializeComponent();
|
---|
48 | namespacesTreeView.ImageList = new ImageList();
|
---|
49 | namespacesTreeView.ImageList.Images.Add(VSImageLibrary.Namespace);
|
---|
50 | assembliesTreeView.ImageList = new ImageList();
|
---|
51 | assembliesTreeView.ImageList.Images.Add(VSImageLibrary.Assembly);
|
---|
52 | assembliesTreeView.ImageList.Images.Add(VSImageLibrary.Module);
|
---|
53 | }
|
---|
54 |
|
---|
55 | protected override void RegisterContentEvents() {
|
---|
56 | base.RegisterContentEvents();
|
---|
57 | ProgrammableOperator.CodeChanged += ProgrammableOperator_CodeChanged;
|
---|
58 | ProgrammableOperator.SignatureChanged += ProgrammableOperator_SignatureChanged;
|
---|
59 | ProgrammableOperator.BreakpointChanged += ProgrammableOperator_BreakpointChanged;
|
---|
60 | }
|
---|
61 |
|
---|
62 | protected override void DeregisterContentEvents() {
|
---|
63 | ProgrammableOperator.CodeChanged -= ProgrammableOperator_CodeChanged;
|
---|
64 | ProgrammableOperator.SignatureChanged -= ProgrammableOperator_SignatureChanged;
|
---|
65 | ProgrammableOperator.BreakpointChanged -= ProgrammableOperator_BreakpointChanged;
|
---|
66 | base.DeregisterContentEvents();
|
---|
67 | }
|
---|
68 |
|
---|
69 | #region Content event handlers
|
---|
70 | void ProgrammableOperator_BreakpointChanged(object sender, EventArgs e) {
|
---|
71 | breakpointCheckBox.Checked = ProgrammableOperator.Breakpoint;
|
---|
72 | }
|
---|
73 | private void ProgrammableOperator_CodeChanged(object sender, EventArgs e) {
|
---|
74 | codeEditor.UserCode = ProgrammableOperator.Code;
|
---|
75 | }
|
---|
76 | private void ProgrammableOperator_SignatureChanged(object sender, EventArgs args) {
|
---|
77 | codeEditor.Prefix = GetGeneratedPrefix();
|
---|
78 | }
|
---|
79 | #endregion
|
---|
80 |
|
---|
81 | protected override void OnContentChanged() {
|
---|
82 | base.OnContentChanged();
|
---|
83 | if (ProgrammableOperator == null) {
|
---|
84 | codeEditor.UserCode = string.Empty;
|
---|
85 | codeEditor.ClearEditHistory();
|
---|
86 | assembliesTreeView.Nodes.Clear();
|
---|
87 | parameterCollectionView.Content = null;
|
---|
88 | } else {
|
---|
89 | codeEditor.Prefix = GetGeneratedPrefix();
|
---|
90 | codeEditor.Suffix = String.Format(" {0}{1} }}{1}}}", ProgrammableOperator.MethodSuffix, Environment.NewLine);
|
---|
91 | codeEditor.UserCode = ProgrammableOperator.Code;
|
---|
92 | if (codeEditor.UserCode == string.Empty)
|
---|
93 | codeEditor.UserCode = string.Format(" {0}", Environment.NewLine);
|
---|
94 | codeEditor.ClearEditHistory();
|
---|
95 | InitializeAssemblyList();
|
---|
96 | InitializeNamespacesList();
|
---|
97 | codeEditor.AddAssemblies(ProgrammableOperator.SelectedAssemblies);
|
---|
98 | codeEditor.ScrollAfterPrefix();
|
---|
99 | codeEditor.ShowCompileErrors(ProgrammableOperator.CompileErrors);
|
---|
100 | showCodeButton.Enabled = !string.IsNullOrEmpty(ProgrammableOperator.CompilationUnitCode);
|
---|
101 | parameterCollectionView.Content = ProgrammableOperator.Parameters;
|
---|
102 | }
|
---|
103 | UpdateCompilationLabel();
|
---|
104 | }
|
---|
105 |
|
---|
106 | private void UpdateCompilationLabel() {
|
---|
107 | if (ProgrammableOperator == null || ProgrammableOperator.CompileErrors == null) {
|
---|
108 | compilationLabel.ForeColor = SystemColors.ControlDarkDark;
|
---|
109 | compilationLabel.Text = "Not compiled";
|
---|
110 | } else if (ProgrammableOperator.CompileErrors.HasErrors) {
|
---|
111 | compilationLabel.ForeColor = Color.DarkRed;
|
---|
112 | compilationLabel.Text = "Compilation failed";
|
---|
113 | } else {
|
---|
114 | compilationLabel.ForeColor = Color.DarkGreen;
|
---|
115 | compilationLabel.Text = "Compilation successful";
|
---|
116 | }
|
---|
117 | }
|
---|
118 |
|
---|
119 | protected override void SetEnabledStateOfControls() {
|
---|
120 | base.SetEnabledStateOfControls();
|
---|
121 | breakpointCheckBox.Enabled = Content != null && !Locked;
|
---|
122 | parameterCollectionView.Enabled = Content != null;
|
---|
123 | assembliesTreeView.Enabled = Content != null && !ReadOnly;
|
---|
124 | namespacesTreeView.Enabled = Content != null && !ReadOnly;
|
---|
125 | compileButton.Enabled = Content != null && !ReadOnly;
|
---|
126 | codeEditor.Enabled = Content != null && !ReadOnly;
|
---|
127 | showCodeButton.Enabled = Content != null && !string.IsNullOrEmpty(ProgrammableOperator.CompilationUnitCode) && !ReadOnly;
|
---|
128 | }
|
---|
129 |
|
---|
130 | #region Child Control event handlers
|
---|
131 | private void compileButton_Click(object sender, EventArgs e) {
|
---|
132 | Recompile();
|
---|
133 | }
|
---|
134 | private void breakpointCheckBox_CheckedChanged(object sender, EventArgs e) {
|
---|
135 | if (ProgrammableOperator != null) ProgrammableOperator.Breakpoint = breakpointCheckBox.Checked;
|
---|
136 | }
|
---|
137 | private void showCodeButton_Click(object sender, EventArgs e) {
|
---|
138 | #if __MonoCS__
|
---|
139 | new TextDialog("CodeViewer", ProgrammableOperator.CompilationUnitCode, true).ShowDialog(this);
|
---|
140 | #else
|
---|
141 | new CodeViewer(ProgrammableOperator.CompilationUnitCode).ShowDialog(this);
|
---|
142 | #endif
|
---|
143 | }
|
---|
144 | private void codeEditor_Validated(object sender, EventArgs e) {
|
---|
145 | ProgrammableOperator.Code = codeEditor.UserCode;
|
---|
146 | }
|
---|
147 | private void assembliesTreeView_AfterCheck(object sender, TreeViewEventArgs e) {
|
---|
148 | if (initializing)
|
---|
149 | return;
|
---|
150 | Assembly a = e.Node.Tag as Assembly;
|
---|
151 | if (a == null && e.Node.Nodes.Count > 0) {
|
---|
152 | foreach (TreeNode n in e.Node.Nodes)
|
---|
153 | n.Checked = e.Node.Checked;
|
---|
154 | return;
|
---|
155 | } else {
|
---|
156 | if (e.Node.Checked) {
|
---|
157 | ProgrammableOperator.SelectAssembly(a);
|
---|
158 | codeEditor.AddAssembly(a);
|
---|
159 | } else {
|
---|
160 | ProgrammableOperator.UnselectAssembly(a);
|
---|
161 | codeEditor.RemoveAssembly(a);
|
---|
162 | }
|
---|
163 | }
|
---|
164 | InitializeNamespacesList();
|
---|
165 | }
|
---|
166 | private void namespacesTreeView_AfterCheck(object sender, TreeViewEventArgs e) {
|
---|
167 | if (initializing)
|
---|
168 | return;
|
---|
169 | if (e.Node.Checked) {
|
---|
170 | ProgrammableOperator.SelectNamespace(e.Node.FullPath);
|
---|
171 | } else {
|
---|
172 | ProgrammableOperator.UnselectNamespace(e.Node.FullPath);
|
---|
173 | }
|
---|
174 | }
|
---|
175 | #endregion
|
---|
176 |
|
---|
177 | #region global HotKeys
|
---|
178 | protected override bool ProcessCmdKey(ref Message msg, Keys keyData) {
|
---|
179 | if (keyData == Keys.F6) {
|
---|
180 | Control activeControl = ActiveControl;
|
---|
181 | compileButton.Focus();
|
---|
182 | Recompile();
|
---|
183 | if (activeControl != null)
|
---|
184 | activeControl.Focus();
|
---|
185 | return true;
|
---|
186 | }
|
---|
187 | return base.ProcessCmdKey(ref msg, keyData);
|
---|
188 | }
|
---|
189 | #endregion
|
---|
190 |
|
---|
191 | #region Auxiliary functions
|
---|
192 |
|
---|
193 | private string GetGeneratedPrefix() {
|
---|
194 | StringBuilder prefix = new StringBuilder();
|
---|
195 | prefix.Append("public class ").Append(ProgrammableOperator.CompiledTypeName).AppendLine(" {");
|
---|
196 | prefix.Append(" ").Append(ProgrammableOperator.Signature).AppendLine(" {");
|
---|
197 | return prefix.ToString();
|
---|
198 | }
|
---|
199 |
|
---|
200 | private void Recompile() {
|
---|
201 | this.Enabled = false;
|
---|
202 | try {
|
---|
203 | ProgrammableOperator.Compile();
|
---|
204 | } catch (Exception ex) {
|
---|
205 | ErrorHandling.ShowErrorDialog(this, ex);
|
---|
206 | }
|
---|
207 | this.Enabled = true;
|
---|
208 | UpdateCompilationLabel();
|
---|
209 | codeEditor.ShowCompileErrors(ProgrammableOperator.CompileErrors);
|
---|
210 | }
|
---|
211 |
|
---|
212 | private bool initializing = false;
|
---|
213 | private void InitializeAssemblyList() {
|
---|
214 | initializing = true;
|
---|
215 | assembliesTreeView.Enabled = false;
|
---|
216 | namespacesTreeView.Enabled = false;
|
---|
217 | assembliesTreeView.BeginUpdate();
|
---|
218 | assembliesTreeView.Nodes.Clear();
|
---|
219 | var selectedAssemblies = new HashSet<Assembly>(ProgrammableOperator.SelectedAssemblies);
|
---|
220 | foreach (var p in ProgrammableOperator.Plugins) {
|
---|
221 | var node = assembliesTreeView.Nodes.Add(p.Key);
|
---|
222 | node.Tag = p;
|
---|
223 | node.ImageIndex = 1;
|
---|
224 | foreach (var a in p.Value) {
|
---|
225 | var aNode = node.Nodes.Add(a.GetName().Name);
|
---|
226 | aNode.Tag = a;
|
---|
227 | aNode.ImageIndex = 0;
|
---|
228 | if (selectedAssemblies.Contains(a))
|
---|
229 | aNode.Checked = true;
|
---|
230 | }
|
---|
231 | if (node.Nodes.Count == 1 && node.Nodes[0].Name == node.Nodes[0].Name) {
|
---|
232 | node.Tag = node.Nodes[0].Tag;
|
---|
233 | node.ImageIndex = node.Nodes[0].ImageIndex;
|
---|
234 | node.Checked = node.Nodes[0].Checked;
|
---|
235 | node.Nodes.Clear();
|
---|
236 | } else if (node.Nodes.Count > 0 && node.Nodes.Cast<TreeNode>().All(n => n.Checked)) {
|
---|
237 | node.Checked = true;
|
---|
238 | }
|
---|
239 | }
|
---|
240 | assembliesTreeView.Sort();
|
---|
241 | assembliesTreeView.EndUpdate();
|
---|
242 | assembliesTreeView.Enabled = true;
|
---|
243 | namespacesTreeView.Enabled = true;
|
---|
244 | initializing = false;
|
---|
245 | }
|
---|
246 |
|
---|
247 | private void InitializeNamespacesList() {
|
---|
248 | initializing = true;
|
---|
249 | namespacesTreeView.Enabled = false;
|
---|
250 | namespacesTreeView.BeginUpdate();
|
---|
251 | TreeNode oldTree = new TreeNode("root");
|
---|
252 | CloneTreeNodeCollection(oldTree, namespacesTreeView.Nodes);
|
---|
253 | namespacesTreeView.Nodes.Clear();
|
---|
254 | var selectedNamespaces = new HashSet<string>(ProgrammableOperator.Namespaces);
|
---|
255 | foreach (var ns in ProgrammableOperator.GetAllNamespaces(true))
|
---|
256 | AddNamespace(namespacesTreeView.Nodes, ns, selectedNamespaces.Contains(ns), oldTree);
|
---|
257 | codeEditor.Prefix = GetGeneratedPrefix();
|
---|
258 | namespacesTreeView.Sort();
|
---|
259 | namespacesTreeView.EndUpdate();
|
---|
260 | namespacesTreeView.Enabled = true;
|
---|
261 | initializing = false;
|
---|
262 | }
|
---|
263 |
|
---|
264 | private void CloneTreeNodeCollection(TreeNode root, TreeNodeCollection nodes) {
|
---|
265 | foreach (TreeNode n in nodes) {
|
---|
266 | TreeNode newNode = root.Nodes.Add(n.Text, n.Text);
|
---|
267 | newNode.Checked = n.Checked;
|
---|
268 | newNode.ImageIndex = n.ImageIndex;
|
---|
269 | CloneTreeNodeCollection(newNode, n.Nodes);
|
---|
270 | if (n.IsExpanded)
|
---|
271 | newNode.Expand();
|
---|
272 | }
|
---|
273 | }
|
---|
274 |
|
---|
275 | private bool AddNamespace(TreeNodeCollection parentNodes, string ns, bool isSelected, TreeNode oldTree) {
|
---|
276 | int dotIndex = ns.IndexOf('.');
|
---|
277 | string prefix = ns;
|
---|
278 | if (dotIndex != -1)
|
---|
279 | prefix = ns.Substring(0, dotIndex);
|
---|
280 | TreeNode node = GetOrCreateNode(parentNodes, prefix);
|
---|
281 | TreeNode oldNode = MaybeGetNode(oldTree, prefix);
|
---|
282 | bool isNew = oldNode == null;
|
---|
283 | if (dotIndex != -1 && dotIndex + 1 < ns.Length) {
|
---|
284 | isNew = AddNamespace(node.Nodes, ns.Substring(dotIndex + 1, ns.Length - (dotIndex + 1)), isSelected, oldNode);
|
---|
285 | } else {
|
---|
286 | node.Checked = isSelected;
|
---|
287 | }
|
---|
288 | if (isNew || oldNode != null && oldNode.IsExpanded)
|
---|
289 | node.Expand();
|
---|
290 | if (isNew) {
|
---|
291 | namespacesTreeView.SelectedNode = node;
|
---|
292 | node.ImageIndex = 0;
|
---|
293 | } else {
|
---|
294 | node.ImageIndex = oldNode.ImageIndex;
|
---|
295 | }
|
---|
296 | return isNew;
|
---|
297 | }
|
---|
298 |
|
---|
299 | private static TreeNode MaybeGetNode(TreeNode parentNode, string key) {
|
---|
300 | if (parentNode == null)
|
---|
301 | return null;
|
---|
302 | if (parentNode.Nodes.ContainsKey(key))
|
---|
303 | return parentNode.Nodes[key];
|
---|
304 | return null;
|
---|
305 | }
|
---|
306 |
|
---|
307 | private static TreeNode GetOrCreateNode(TreeNodeCollection parentNodes, string key) {
|
---|
308 | TreeNode node = null;
|
---|
309 | if (parentNodes.ContainsKey(key)) {
|
---|
310 | node = parentNodes[key];
|
---|
311 | } else {
|
---|
312 | node = parentNodes.Add(key, key);
|
---|
313 | }
|
---|
314 | return node;
|
---|
315 | }
|
---|
316 |
|
---|
317 | #endregion
|
---|
318 | }
|
---|
319 | } |
---|