[2] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[7259] | 3 | * Copyright (C) 2002-2012 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[2] | 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;
|
---|
[5042] | 24 | using System.Drawing;
|
---|
[4068] | 25 | using System.Linq;
|
---|
| 26 | using System.Reflection;
|
---|
[2] | 27 | using System.Text;
|
---|
| 28 | using System.Windows.Forms;
|
---|
[2799] | 29 | using HeuristicLab.CodeEditor;
|
---|
[4068] | 30 | using HeuristicLab.Common.Resources;
|
---|
[2520] | 31 | using HeuristicLab.Core.Views;
|
---|
[2897] | 32 | using HeuristicLab.MainForm;
|
---|
[3758] | 33 | using HeuristicLab.PluginInfrastructure;
|
---|
[2] | 34 |
|
---|
| 35 | namespace HeuristicLab.Operators.Programmable {
|
---|
[5042] | 36 |
|
---|
[2917] | 37 | [View("ProgrammableOperator View")]
|
---|
[2897] | 38 | [Content(typeof(ProgrammableOperator), true)]
|
---|
[2799] | 39 | public partial class ProgrammableOperatorView : NamedItemView {
|
---|
| 40 |
|
---|
[2] | 41 | public ProgrammableOperator ProgrammableOperator {
|
---|
[2799] | 42 | get { return (ProgrammableOperator)base.Content; }
|
---|
[2897] | 43 | set { base.Content = (ProgrammableOperator)value; }
|
---|
[2] | 44 | }
|
---|
| 45 |
|
---|
| 46 | public ProgrammableOperatorView() {
|
---|
| 47 | InitializeComponent();
|
---|
[3903] | 48 | namespacesTreeView.ImageList = new ImageList();
|
---|
[5287] | 49 | namespacesTreeView.ImageList.Images.Add(VSImageLibrary.Namespace);
|
---|
[3903] | 50 | assembliesTreeView.ImageList = new ImageList();
|
---|
[5287] | 51 | assembliesTreeView.ImageList.Images.Add(VSImageLibrary.Assembly);
|
---|
| 52 | assembliesTreeView.ImageList.Images.Add(VSImageLibrary.Module);
|
---|
[2] | 53 | }
|
---|
[2799] | 54 |
|
---|
| 55 | protected override void RegisterContentEvents() {
|
---|
| 56 | base.RegisterContentEvents();
|
---|
[2897] | 57 | ProgrammableOperator.CodeChanged += ProgrammableOperator_CodeChanged;
|
---|
| 58 | ProgrammableOperator.SignatureChanged += ProgrammableOperator_SignatureChanged;
|
---|
[5042] | 59 | ProgrammableOperator.BreakpointChanged += ProgrammableOperator_BreakpointChanged;
|
---|
[2] | 60 | }
|
---|
| 61 |
|
---|
[2897] | 62 | protected override void DeregisterContentEvents() {
|
---|
| 63 | ProgrammableOperator.CodeChanged -= ProgrammableOperator_CodeChanged;
|
---|
| 64 | ProgrammableOperator.SignatureChanged -= ProgrammableOperator_SignatureChanged;
|
---|
[5042] | 65 | ProgrammableOperator.BreakpointChanged -= ProgrammableOperator_BreakpointChanged;
|
---|
[2897] | 66 | base.DeregisterContentEvents();
|
---|
| 67 | }
|
---|
[2799] | 68 |
|
---|
[5042] | 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.Text = ProgrammableOperator.Code;
|
---|
| 75 | }
|
---|
| 76 | private void ProgrammableOperator_SignatureChanged(object sender, EventArgs args) {
|
---|
| 77 | codeEditor.Prefix = GetGeneratedPrefix();
|
---|
| 78 | }
|
---|
| 79 | #endregion
|
---|
| 80 |
|
---|
[2799] | 81 | protected override void OnContentChanged() {
|
---|
[2897] | 82 | base.OnContentChanged();
|
---|
[2] | 83 | if (ProgrammableOperator == null) {
|
---|
[2799] | 84 | codeEditor.Text = "";
|
---|
| 85 | assembliesTreeView.Nodes.Clear();
|
---|
[3014] | 86 | parameterCollectionView.Content = null;
|
---|
[2] | 87 | } else {
|
---|
[2799] | 88 | codeEditor.Prefix = GetGeneratedPrefix();
|
---|
[4838] | 89 | codeEditor.Suffix = String.Format(" {0}\n }}\n}}", ProgrammableOperator.MethodSuffix);
|
---|
[2799] | 90 | codeEditor.UserCode = ProgrammableOperator.Code;
|
---|
| 91 | if (codeEditor.UserCode == "")
|
---|
[3903] | 92 | codeEditor.UserCode = " \n \n \n \n";
|
---|
[2799] | 93 | InitializeAssemblyList();
|
---|
| 94 | InitializeNamespacesList();
|
---|
| 95 | foreach (var a in ProgrammableOperator.SelectedAssemblies) {
|
---|
| 96 | codeEditor.AddAssembly(a);
|
---|
| 97 | }
|
---|
| 98 | codeEditor.ScrollAfterPrefix();
|
---|
[2897] | 99 | codeEditor.ShowCompileErrors(ProgrammableOperator.CompileErrors, "ProgrammableOperator");
|
---|
[3903] | 100 | showCodeButton.Enabled =
|
---|
| 101 | ProgrammableOperator.CompilationUnitCode != null &&
|
---|
[3008] | 102 | ProgrammableOperator.CompilationUnitCode.Length > 0;
|
---|
[3014] | 103 | parameterCollectionView.Content = ProgrammableOperator.Parameters;
|
---|
[5042] | 104 | if (ProgrammableOperator.CompileErrors == null) {
|
---|
| 105 | compilationLabel.ForeColor = SystemColors.ControlDarkDark;
|
---|
[5218] | 106 | compilationLabel.Text = "Not compiled";
|
---|
[5042] | 107 | } else if (ProgrammableOperator.CompileErrors.HasErrors) {
|
---|
| 108 | compilationLabel.ForeColor = Color.DarkRed;
|
---|
[5218] | 109 | compilationLabel.Text = "Compilation failed";
|
---|
[5042] | 110 | } else {
|
---|
| 111 | compilationLabel.ForeColor = Color.DarkGreen;
|
---|
[5218] | 112 | compilationLabel.Text = "Compilation successful";
|
---|
[5042] | 113 | }
|
---|
| 114 |
|
---|
[2] | 115 | }
|
---|
| 116 | }
|
---|
| 117 |
|
---|
[3906] | 118 | protected override void SetEnabledStateOfControls() {
|
---|
| 119 | base.SetEnabledStateOfControls();
|
---|
[5214] | 120 | breakpointCheckBox.Enabled = Content != null && !Locked;
|
---|
[3454] | 121 | parameterCollectionView.Enabled = Content != null;
|
---|
| 122 | assembliesTreeView.Enabled = Content != null && !ReadOnly;
|
---|
| 123 | namespacesTreeView.Enabled = Content != null && !ReadOnly;
|
---|
| 124 | compileButton.Enabled = Content != null && !ReadOnly;
|
---|
| 125 | codeEditor.Enabled = Content != null && !ReadOnly;
|
---|
[5042] | 126 | showCodeButton.Enabled = Content != null && !string.IsNullOrEmpty(ProgrammableOperator.CompilationUnitCode) && !ReadOnly;
|
---|
[3454] | 127 | }
|
---|
| 128 |
|
---|
[5042] | 129 | #region Child Control event handlers
|
---|
[2799] | 130 | private void compileButton_Click(object sender, EventArgs e) {
|
---|
| 131 | Recompile();
|
---|
| 132 | }
|
---|
[5042] | 133 | private void breakpointCheckBox_CheckedChanged(object sender, EventArgs e) {
|
---|
| 134 | if (ProgrammableOperator != null) ProgrammableOperator.Breakpoint = breakpointCheckBox.Checked;
|
---|
[2] | 135 | }
|
---|
[5042] | 136 | private void showCodeButton_Click(object sender, EventArgs e) {
|
---|
| 137 | new CodeViewer(ProgrammableOperator.CompilationUnitCode).ShowDialog(this);
|
---|
[2897] | 138 | }
|
---|
[5042] | 139 | private void codeEditor_Validated(object sender, EventArgs e) {
|
---|
| 140 | ProgrammableOperator.Code = codeEditor.UserCode;
|
---|
| 141 | }
|
---|
[2799] | 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 | }
|
---|
[5042] | 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
|
---|
[2799] | 173 |
|
---|
[5042] | 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();
|
---|
[5287] | 205 | } catch (Exception ex) {
|
---|
[5042] | 206 | ErrorHandling.ShowErrorDialog(this, ex);
|
---|
| 207 | }
|
---|
| 208 | OnContentChanged();
|
---|
| 209 | this.Enabled = true;
|
---|
| 210 | }
|
---|
| 211 |
|
---|
[2799] | 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;
|
---|
[3903] | 223 | node.ImageIndex = 1;
|
---|
[2799] | 224 | foreach (var a in p.Value) {
|
---|
| 225 | var aNode = node.Nodes.Add(a.GetName().Name);
|
---|
| 226 | aNode.Tag = a;
|
---|
[3903] | 227 | aNode.ImageIndex = 0;
|
---|
[2799] | 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;
|
---|
[3903] | 233 | node.ImageIndex = node.Nodes[0].ImageIndex;
|
---|
| 234 | node.Checked = node.Nodes[0].Checked;
|
---|
[2799] | 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 | }
|
---|
[3903] | 240 | assembliesTreeView.Sort();
|
---|
[2799] | 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();
|
---|
[3903] | 258 | namespacesTreeView.Sort();
|
---|
[2799] | 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;
|
---|
[3903] | 268 | newNode.ImageIndex = n.ImageIndex;
|
---|
[2799] | 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();
|
---|
[3903] | 290 | if (isNew) {
|
---|
[2799] | 291 | namespacesTreeView.SelectedNode = node;
|
---|
[3903] | 292 | node.ImageIndex = 0;
|
---|
| 293 | } else {
|
---|
| 294 | node.ImageIndex = oldNode.ImageIndex;
|
---|
| 295 | }
|
---|
[2799] | 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 |
|
---|
[5042] | 317 | #endregion
|
---|
[2] | 318 | }
|
---|
[2799] | 319 | } |
---|