[7636] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[12012] | 3 | * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[7636] | 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) {
|
---|
[11807] | 84 | codeEditor.UserCode = string.Empty;
|
---|
[7636] | 85 | assembliesTreeView.Nodes.Clear();
|
---|
| 86 | parameterCollectionView.Content = null;
|
---|
| 87 | } else {
|
---|
| 88 | codeEditor.Prefix = GetGeneratedPrefix();
|
---|
[11807] | 89 | codeEditor.Suffix = String.Format(" {0}{1} }}{1}}}", ProgrammableOperator.MethodSuffix, Environment.NewLine);
|
---|
[7636] | 90 | codeEditor.UserCode = ProgrammableOperator.Code;
|
---|
[11807] | 91 | if (codeEditor.UserCode == string.Empty)
|
---|
| 92 | codeEditor.UserCode = string.Format(" {0}", Environment.NewLine);
|
---|
[7636] | 93 | InitializeAssemblyList();
|
---|
| 94 | InitializeNamespacesList();
|
---|
[11807] | 95 | codeEditor.AddAssemblies(ProgrammableOperator.SelectedAssemblies);
|
---|
[7636] | 96 | codeEditor.ScrollAfterPrefix();
|
---|
[11807] | 97 | codeEditor.ShowCompileErrors(ProgrammableOperator.CompileErrors);
|
---|
| 98 | showCodeButton.Enabled = !string.IsNullOrEmpty(ProgrammableOperator.CompilationUnitCode);
|
---|
[7636] | 99 | parameterCollectionView.Content = ProgrammableOperator.Parameters;
|
---|
| 100 | if (ProgrammableOperator.CompileErrors == null) {
|
---|
| 101 | compilationLabel.ForeColor = SystemColors.ControlDarkDark;
|
---|
| 102 | compilationLabel.Text = "Not compiled";
|
---|
| 103 | } else if (ProgrammableOperator.CompileErrors.HasErrors) {
|
---|
| 104 | compilationLabel.ForeColor = Color.DarkRed;
|
---|
| 105 | compilationLabel.Text = "Compilation failed";
|
---|
| 106 | } else {
|
---|
| 107 | compilationLabel.ForeColor = Color.DarkGreen;
|
---|
| 108 | compilationLabel.Text = "Compilation successful";
|
---|
| 109 | }
|
---|
| 110 |
|
---|
| 111 | }
|
---|
| 112 | }
|
---|
| 113 |
|
---|
| 114 | protected override void SetEnabledStateOfControls() {
|
---|
| 115 | base.SetEnabledStateOfControls();
|
---|
| 116 | breakpointCheckBox.Enabled = Content != null && !Locked;
|
---|
| 117 | parameterCollectionView.Enabled = Content != null;
|
---|
| 118 | assembliesTreeView.Enabled = Content != null && !ReadOnly;
|
---|
| 119 | namespacesTreeView.Enabled = Content != null && !ReadOnly;
|
---|
| 120 | compileButton.Enabled = Content != null && !ReadOnly;
|
---|
| 121 | codeEditor.Enabled = Content != null && !ReadOnly;
|
---|
| 122 | showCodeButton.Enabled = Content != null && !string.IsNullOrEmpty(ProgrammableOperator.CompilationUnitCode) && !ReadOnly;
|
---|
| 123 | }
|
---|
| 124 |
|
---|
| 125 | #region Child Control event handlers
|
---|
| 126 | private void compileButton_Click(object sender, EventArgs e) {
|
---|
| 127 | Recompile();
|
---|
| 128 | }
|
---|
| 129 | private void breakpointCheckBox_CheckedChanged(object sender, EventArgs e) {
|
---|
| 130 | if (ProgrammableOperator != null) ProgrammableOperator.Breakpoint = breakpointCheckBox.Checked;
|
---|
| 131 | }
|
---|
| 132 | private void showCodeButton_Click(object sender, EventArgs e) {
|
---|
[12473] | 133 | #if __MonoCS__
|
---|
| 134 | new TextDialog("CodeViewer", ProgrammableOperator.CompilationUnitCode, true).ShowDialog(this);
|
---|
| 135 | #else
|
---|
[7636] | 136 | new CodeViewer(ProgrammableOperator.CompilationUnitCode).ShowDialog(this);
|
---|
[12473] | 137 | #endif
|
---|
[7636] | 138 | }
|
---|
| 139 | private void codeEditor_Validated(object sender, EventArgs e) {
|
---|
| 140 | ProgrammableOperator.Code = codeEditor.UserCode;
|
---|
| 141 | }
|
---|
| 142 | private void assembliesTreeView_AfterCheck(object sender, TreeViewEventArgs e) {
|
---|
| 143 | if (initializing)
|
---|
| 144 | return;
|
---|
| 145 | Assembly a = e.Node.Tag as Assembly;
|
---|
| 146 | if (a == null && e.Node.Nodes.Count > 0) {
|
---|
| 147 | foreach (TreeNode n in e.Node.Nodes)
|
---|
| 148 | n.Checked = e.Node.Checked;
|
---|
| 149 | return;
|
---|
| 150 | } else {
|
---|
| 151 | if (e.Node.Checked) {
|
---|
| 152 | ProgrammableOperator.SelectAssembly(a);
|
---|
| 153 | codeEditor.AddAssembly(a);
|
---|
| 154 | } else {
|
---|
| 155 | ProgrammableOperator.UnselectAssembly(a);
|
---|
| 156 | codeEditor.RemoveAssembly(a);
|
---|
| 157 | }
|
---|
| 158 | }
|
---|
| 159 | InitializeNamespacesList();
|
---|
| 160 | codeEditor.Prefix = GetGeneratedPrefix();
|
---|
| 161 | }
|
---|
| 162 | private void namespacesTreeView_AfterCheck(object sender, TreeViewEventArgs e) {
|
---|
| 163 | if (initializing)
|
---|
| 164 | return;
|
---|
| 165 | if (e.Node.Checked) {
|
---|
| 166 | ProgrammableOperator.SelectNamespace(e.Node.FullPath);
|
---|
| 167 | } else {
|
---|
| 168 | ProgrammableOperator.UnselectNamespace(e.Node.FullPath);
|
---|
| 169 | }
|
---|
| 170 | codeEditor.Prefix = GetGeneratedPrefix();
|
---|
| 171 | }
|
---|
| 172 | #endregion
|
---|
| 173 |
|
---|
| 174 | #region global HotKeys
|
---|
| 175 | protected override bool ProcessCmdKey(ref Message msg, Keys keyData) {
|
---|
| 176 | if (keyData == Keys.F6) {
|
---|
| 177 | Control activeControl = ActiveControl;
|
---|
| 178 | compileButton.Focus();
|
---|
| 179 | Recompile();
|
---|
| 180 | if (activeControl != null)
|
---|
| 181 | activeControl.Focus();
|
---|
| 182 | return true;
|
---|
| 183 | }
|
---|
| 184 | return base.ProcessCmdKey(ref msg, keyData);
|
---|
| 185 | }
|
---|
| 186 | #endregion
|
---|
| 187 |
|
---|
| 188 | #region Auxiliary functions
|
---|
| 189 |
|
---|
| 190 | private string GetGeneratedPrefix() {
|
---|
| 191 | StringBuilder prefix = new StringBuilder();
|
---|
| 192 | foreach (var ns in ProgrammableOperator.GetSelectedAndValidNamespaces()) {
|
---|
| 193 | prefix.Append("using ").Append(ns).AppendLine(";");
|
---|
| 194 | }
|
---|
| 195 | prefix.AppendLine();
|
---|
| 196 | prefix.Append("public class ").Append(ProgrammableOperator.CompiledTypeName).AppendLine(" {");
|
---|
| 197 | prefix.Append(" ").Append(ProgrammableOperator.Signature).AppendLine(" {");
|
---|
| 198 | return prefix.ToString();
|
---|
| 199 | }
|
---|
| 200 |
|
---|
| 201 | private void Recompile() {
|
---|
| 202 | this.Enabled = false;
|
---|
| 203 | try {
|
---|
| 204 | ProgrammableOperator.Compile();
|
---|
[12473] | 205 | }
|
---|
| 206 | catch (Exception ex) {
|
---|
[13338] | 207 | MainFormManager.MainForm.ShowError(ex.Message, ex);
|
---|
[7636] | 208 | }
|
---|
| 209 | OnContentChanged();
|
---|
| 210 | this.Enabled = true;
|
---|
| 211 | }
|
---|
| 212 |
|
---|
| 213 | private bool initializing = false;
|
---|
| 214 | private void InitializeAssemblyList() {
|
---|
| 215 | initializing = true;
|
---|
| 216 | assembliesTreeView.Enabled = false;
|
---|
| 217 | namespacesTreeView.Enabled = false;
|
---|
| 218 | assembliesTreeView.BeginUpdate();
|
---|
| 219 | assembliesTreeView.Nodes.Clear();
|
---|
| 220 | var selectedAssemblies = new HashSet<Assembly>(ProgrammableOperator.SelectedAssemblies);
|
---|
| 221 | foreach (var p in ProgrammableOperator.Plugins) {
|
---|
| 222 | var node = assembliesTreeView.Nodes.Add(p.Key);
|
---|
| 223 | node.Tag = p;
|
---|
| 224 | node.ImageIndex = 1;
|
---|
| 225 | foreach (var a in p.Value) {
|
---|
| 226 | var aNode = node.Nodes.Add(a.GetName().Name);
|
---|
| 227 | aNode.Tag = a;
|
---|
| 228 | aNode.ImageIndex = 0;
|
---|
| 229 | if (selectedAssemblies.Contains(a))
|
---|
| 230 | aNode.Checked = true;
|
---|
| 231 | }
|
---|
| 232 | if (node.Nodes.Count == 1 && node.Nodes[0].Name == node.Nodes[0].Name) {
|
---|
| 233 | node.Tag = node.Nodes[0].Tag;
|
---|
| 234 | node.ImageIndex = node.Nodes[0].ImageIndex;
|
---|
| 235 | node.Checked = node.Nodes[0].Checked;
|
---|
| 236 | node.Nodes.Clear();
|
---|
| 237 | } else if (node.Nodes.Count > 0 && node.Nodes.Cast<TreeNode>().All(n => n.Checked)) {
|
---|
| 238 | node.Checked = true;
|
---|
| 239 | }
|
---|
| 240 | }
|
---|
| 241 | assembliesTreeView.Sort();
|
---|
| 242 | assembliesTreeView.EndUpdate();
|
---|
| 243 | assembliesTreeView.Enabled = true;
|
---|
| 244 | namespacesTreeView.Enabled = true;
|
---|
| 245 | initializing = false;
|
---|
| 246 | }
|
---|
| 247 |
|
---|
| 248 | private void InitializeNamespacesList() {
|
---|
| 249 | initializing = true;
|
---|
| 250 | namespacesTreeView.Enabled = false;
|
---|
| 251 | namespacesTreeView.BeginUpdate();
|
---|
| 252 | TreeNode oldTree = new TreeNode("root");
|
---|
| 253 | CloneTreeNodeCollection(oldTree, namespacesTreeView.Nodes);
|
---|
| 254 | namespacesTreeView.Nodes.Clear();
|
---|
| 255 | var selectedNamespaces = new HashSet<string>(ProgrammableOperator.Namespaces);
|
---|
| 256 | foreach (var ns in ProgrammableOperator.GetAllNamespaces(true))
|
---|
| 257 | AddNamespace(namespacesTreeView.Nodes, ns, selectedNamespaces.Contains(ns), oldTree);
|
---|
| 258 | codeEditor.Prefix = GetGeneratedPrefix();
|
---|
| 259 | namespacesTreeView.Sort();
|
---|
| 260 | namespacesTreeView.EndUpdate();
|
---|
| 261 | namespacesTreeView.Enabled = true;
|
---|
| 262 | initializing = false;
|
---|
| 263 | }
|
---|
| 264 |
|
---|
| 265 | private void CloneTreeNodeCollection(TreeNode root, TreeNodeCollection nodes) {
|
---|
| 266 | foreach (TreeNode n in nodes) {
|
---|
| 267 | TreeNode newNode = root.Nodes.Add(n.Text, n.Text);
|
---|
| 268 | newNode.Checked = n.Checked;
|
---|
| 269 | newNode.ImageIndex = n.ImageIndex;
|
---|
| 270 | CloneTreeNodeCollection(newNode, n.Nodes);
|
---|
| 271 | if (n.IsExpanded)
|
---|
| 272 | newNode.Expand();
|
---|
| 273 | }
|
---|
| 274 | }
|
---|
| 275 |
|
---|
| 276 | private bool AddNamespace(TreeNodeCollection parentNodes, string ns, bool isSelected, TreeNode oldTree) {
|
---|
| 277 | int dotIndex = ns.IndexOf('.');
|
---|
| 278 | string prefix = ns;
|
---|
| 279 | if (dotIndex != -1)
|
---|
| 280 | prefix = ns.Substring(0, dotIndex);
|
---|
| 281 | TreeNode node = GetOrCreateNode(parentNodes, prefix);
|
---|
| 282 | TreeNode oldNode = MaybeGetNode(oldTree, prefix);
|
---|
| 283 | bool isNew = oldNode == null;
|
---|
| 284 | if (dotIndex != -1 && dotIndex + 1 < ns.Length) {
|
---|
| 285 | isNew = AddNamespace(node.Nodes, ns.Substring(dotIndex + 1, ns.Length - (dotIndex + 1)), isSelected, oldNode);
|
---|
| 286 | } else {
|
---|
| 287 | node.Checked = isSelected;
|
---|
| 288 | }
|
---|
| 289 | if (isNew || oldNode != null && oldNode.IsExpanded)
|
---|
| 290 | node.Expand();
|
---|
| 291 | if (isNew) {
|
---|
| 292 | namespacesTreeView.SelectedNode = node;
|
---|
| 293 | node.ImageIndex = 0;
|
---|
| 294 | } else {
|
---|
| 295 | node.ImageIndex = oldNode.ImageIndex;
|
---|
| 296 | }
|
---|
| 297 | return isNew;
|
---|
| 298 | }
|
---|
| 299 |
|
---|
| 300 | private static TreeNode MaybeGetNode(TreeNode parentNode, string key) {
|
---|
| 301 | if (parentNode == null)
|
---|
| 302 | return null;
|
---|
| 303 | if (parentNode.Nodes.ContainsKey(key))
|
---|
| 304 | return parentNode.Nodes[key];
|
---|
| 305 | return null;
|
---|
| 306 | }
|
---|
| 307 |
|
---|
| 308 | private static TreeNode GetOrCreateNode(TreeNodeCollection parentNodes, string key) {
|
---|
| 309 | TreeNode node = null;
|
---|
| 310 | if (parentNodes.ContainsKey(key)) {
|
---|
| 311 | node = parentNodes[key];
|
---|
| 312 | } else {
|
---|
| 313 | node = parentNodes.Add(key, key);
|
---|
| 314 | }
|
---|
| 315 | return node;
|
---|
| 316 | }
|
---|
| 317 |
|
---|
| 318 | #endregion
|
---|
| 319 | }
|
---|
| 320 | } |
---|