[10332] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
| 3 | * Copyright (C) 2002-2013 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.CodeDom.Compiler;
|
---|
| 24 | using System.Drawing;
|
---|
[10506] | 25 | using System.Globalization;
|
---|
[10332] | 26 | using System.Linq;
|
---|
| 27 | using System.Windows.Forms;
|
---|
| 28 | using HeuristicLab.Common;
|
---|
| 29 | using HeuristicLab.Common.Resources;
|
---|
| 30 | using HeuristicLab.Core.Views;
|
---|
| 31 | using HeuristicLab.MainForm;
|
---|
| 32 |
|
---|
[10506] | 33 | namespace HeuristicLab.Scripting.Views {
|
---|
[10332] | 34 |
|
---|
[10401] | 35 | [View("Script View")]
|
---|
| 36 | [Content(typeof(Script), true)]
|
---|
| 37 | public partial class ScriptView : NamedItemView {
|
---|
[10332] | 38 | private bool running;
|
---|
| 39 |
|
---|
[10401] | 40 | public new Script Content {
|
---|
| 41 | get { return (Script)base.Content; }
|
---|
| 42 | set { base.Content = (Script)value; }
|
---|
[10332] | 43 | }
|
---|
| 44 |
|
---|
[10401] | 45 | public ScriptView() {
|
---|
[10332] | 46 | InitializeComponent();
|
---|
[10506] | 47 | errorListView.SmallImageList.Images.AddRange(new Image[] { VSImageLibrary.Warning, VSImageLibrary.Error });
|
---|
| 48 | AdjustErrorListViewColumnSizes();
|
---|
[10332] | 49 | }
|
---|
| 50 |
|
---|
| 51 | protected override void RegisterContentEvents() {
|
---|
| 52 | base.RegisterContentEvents();
|
---|
| 53 | Content.CodeChanged += Content_CodeChanged;
|
---|
| 54 | Content.ScriptExecutionStarted += Content_ScriptExecutionStarted;
|
---|
| 55 | Content.ScriptExecutionFinished += Content_ScriptExecutionFinished;
|
---|
| 56 | Content.ConsoleOutputChanged += Content_ConsoleOutputChanged;
|
---|
| 57 | }
|
---|
| 58 |
|
---|
| 59 | protected override void DeregisterContentEvents() {
|
---|
| 60 | Content.CodeChanged -= Content_CodeChanged;
|
---|
| 61 | Content.ScriptExecutionStarted -= Content_ScriptExecutionStarted;
|
---|
| 62 | Content.ScriptExecutionFinished -= Content_ScriptExecutionFinished;
|
---|
| 63 | Content.ConsoleOutputChanged -= Content_ConsoleOutputChanged;
|
---|
| 64 | base.DeregisterContentEvents();
|
---|
| 65 | }
|
---|
| 66 |
|
---|
| 67 | #region Content event handlers
|
---|
| 68 | private void Content_CodeChanged(object sender, EventArgs e) {
|
---|
| 69 | codeEditor.UserCode = Content.Code;
|
---|
| 70 | }
|
---|
| 71 | private void Content_ScriptExecutionStarted(object sender, EventArgs e) {
|
---|
[10506] | 72 | if (InvokeRequired)
|
---|
| 73 | Invoke((Action<object, EventArgs>)Content_ScriptExecutionStarted, sender, e);
|
---|
| 74 | else {
|
---|
| 75 | Locked = true;
|
---|
| 76 | ReadOnly = true;
|
---|
| 77 | startStopButton.Image = VSImageLibrary.Stop;
|
---|
[10727] | 78 | toolTip.SetToolTip(startStopButton, "Stop (Shift+F5)");
|
---|
[10506] | 79 | infoTabControl.SelectedTab = outputTabPage;
|
---|
| 80 | }
|
---|
[10332] | 81 | }
|
---|
[10506] | 82 | private void Content_ScriptExecutionFinished(object sender, EventArgs<Exception> e) {
|
---|
| 83 | if (InvokeRequired)
|
---|
| 84 | Invoke((Action<object, EventArgs<Exception>>)Content_ScriptExecutionFinished, sender, e);
|
---|
| 85 | else {
|
---|
| 86 | Locked = false;
|
---|
| 87 | ReadOnly = false;
|
---|
| 88 | startStopButton.Image = VSImageLibrary.Play;
|
---|
[10727] | 89 | toolTip.SetToolTip(startStopButton, "Run (F5)");
|
---|
[10506] | 90 | running = false;
|
---|
| 91 | var ex = e.Value;
|
---|
| 92 | if (ex != null)
|
---|
| 93 | PluginInfrastructure.ErrorHandling.ShowErrorDialog(this, ex);
|
---|
| 94 | }
|
---|
[10332] | 95 | }
|
---|
| 96 | private void Content_ConsoleOutputChanged(object sender, EventArgs<string> e) {
|
---|
[10506] | 97 | if (InvokeRequired)
|
---|
| 98 | Invoke((Action<object, EventArgs<string>>)Content_ConsoleOutputChanged, sender, e);
|
---|
[10332] | 99 | else {
|
---|
| 100 | outputTextBox.AppendText(e.Value);
|
---|
| 101 | }
|
---|
| 102 | }
|
---|
| 103 | #endregion
|
---|
| 104 |
|
---|
| 105 | protected override void OnContentChanged() {
|
---|
| 106 | base.OnContentChanged();
|
---|
| 107 | if (Content == null) {
|
---|
| 108 | codeEditor.UserCode = string.Empty;
|
---|
| 109 | variableStoreView.Content = null;
|
---|
| 110 | } else {
|
---|
| 111 | codeEditor.UserCode = Content.Code;
|
---|
[10358] | 112 | foreach (var asm in Content.GetAssemblies())
|
---|
| 113 | codeEditor.AddAssembly(asm);
|
---|
[10332] | 114 | variableStoreView.Content = Content.VariableStore;
|
---|
| 115 | if (Content.CompileErrors == null) {
|
---|
| 116 | compilationLabel.ForeColor = SystemColors.ControlDarkDark;
|
---|
| 117 | compilationLabel.Text = "Not compiled";
|
---|
| 118 | } else if (Content.CompileErrors.HasErrors) {
|
---|
| 119 | compilationLabel.ForeColor = Color.DarkRed;
|
---|
| 120 | compilationLabel.Text = "Compilation failed";
|
---|
| 121 | } else {
|
---|
| 122 | compilationLabel.ForeColor = Color.DarkGreen;
|
---|
| 123 | compilationLabel.Text = "Compilation successful";
|
---|
| 124 | }
|
---|
| 125 | }
|
---|
| 126 | }
|
---|
| 127 |
|
---|
| 128 | protected override void SetEnabledStateOfControls() {
|
---|
| 129 | base.SetEnabledStateOfControls();
|
---|
[10506] | 130 | compileButton.Enabled = Content != null && !Locked && !ReadOnly;
|
---|
[10401] | 131 | startStopButton.Enabled = Content != null && (!Locked || running);
|
---|
[10358] | 132 | codeEditor.Enabled = Content != null && !Locked && !ReadOnly;
|
---|
[10332] | 133 | }
|
---|
| 134 |
|
---|
| 135 | #region Child Control event handlers
|
---|
[10506] | 136 | private void compileButton_Click(object sender, EventArgs e) {
|
---|
| 137 | Compile();
|
---|
| 138 | }
|
---|
| 139 |
|
---|
[10332] | 140 | private void startStopButton_Click(object sender, EventArgs e) {
|
---|
| 141 | if (running) {
|
---|
| 142 | Content.Kill();
|
---|
| 143 | } else
|
---|
| 144 | if (Compile()) {
|
---|
| 145 | outputTextBox.Clear();
|
---|
| 146 | Content.Execute();
|
---|
| 147 | running = true;
|
---|
| 148 | }
|
---|
| 149 | }
|
---|
| 150 |
|
---|
| 151 | private void codeEditor_TextEditorTextChanged(object sender, EventArgs e) {
|
---|
| 152 | Content.Code = codeEditor.UserCode;
|
---|
| 153 | }
|
---|
| 154 | #endregion
|
---|
| 155 |
|
---|
| 156 | #region global HotKeys
|
---|
| 157 | protected override bool ProcessCmdKey(ref Message msg, Keys keyData) {
|
---|
[10506] | 158 | switch (keyData) {
|
---|
| 159 | case Keys.F5:
|
---|
[10727] | 160 | if (Content != null && !Locked && !running) {
|
---|
[10506] | 161 | if (Compile()) {
|
---|
| 162 | outputTextBox.Clear();
|
---|
| 163 | Content.Execute();
|
---|
| 164 | running = true;
|
---|
[10642] | 165 | }
|
---|
[10506] | 166 | }
|
---|
| 167 | break;
|
---|
| 168 | case Keys.F5 | Keys.Shift:
|
---|
| 169 | if (running) Content.Kill();
|
---|
| 170 | break;
|
---|
| 171 | case Keys.F6:
|
---|
[10727] | 172 | if (!running) Compile();
|
---|
[10506] | 173 | break;
|
---|
[10332] | 174 | }
|
---|
| 175 | return base.ProcessCmdKey(ref msg, keyData);
|
---|
| 176 | }
|
---|
| 177 | #endregion
|
---|
| 178 |
|
---|
| 179 | #region Auxiliary functions
|
---|
| 180 | private bool Compile() {
|
---|
| 181 | ReadOnly = true;
|
---|
[10358] | 182 | Locked = true;
|
---|
[10332] | 183 | errorListView.Items.Clear();
|
---|
[10506] | 184 | outputTextBox.Text = "Compiling ... ";
|
---|
[10332] | 185 | try {
|
---|
| 186 | Content.Compile();
|
---|
| 187 | outputTextBox.AppendText("Compilation succeeded.");
|
---|
| 188 | return true;
|
---|
| 189 | } catch {
|
---|
| 190 | outputTextBox.AppendText("Compilation failed.");
|
---|
| 191 | return false;
|
---|
| 192 | } finally {
|
---|
[10506] | 193 | ShowCompilationResults();
|
---|
[10642] | 194 | if (Content.CompileErrors.Count > 0)
|
---|
| 195 | infoTabControl.SelectedTab = errorListTabPage;
|
---|
[10332] | 196 | ReadOnly = false;
|
---|
[10358] | 197 | Locked = false;
|
---|
[10506] | 198 | OnContentChanged();
|
---|
[10332] | 199 | }
|
---|
| 200 | }
|
---|
| 201 | #endregion
|
---|
| 202 |
|
---|
[10506] | 203 | private void ShowCompilationResults() {
|
---|
[10332] | 204 | if (Content.CompileErrors.Count == 0) return;
|
---|
[10506] | 205 | var msgs = Content.CompileErrors.OfType<CompilerError>()
|
---|
| 206 | .OrderBy(x => x.IsWarning)
|
---|
| 207 | .ThenBy(x => x.Line)
|
---|
| 208 | .ThenBy(x => x.Column);
|
---|
[10332] | 209 | foreach (var m in msgs) {
|
---|
[10506] | 210 | var item = new ListViewItem();
|
---|
| 211 | item.SubItems.AddRange(new[] {
|
---|
[10332] | 212 | m.IsWarning ? "Warning" : "Error",
|
---|
| 213 | m.ErrorNumber,
|
---|
[10506] | 214 | m.Line.ToString(CultureInfo.InvariantCulture),
|
---|
| 215 | m.Column.ToString(CultureInfo.InvariantCulture),
|
---|
[10332] | 216 | m.ErrorText
|
---|
| 217 | });
|
---|
[10506] | 218 | item.ImageIndex = m.IsWarning ? 0 : 1;
|
---|
[10332] | 219 | errorListView.Items.Add(item);
|
---|
| 220 | }
|
---|
[10506] | 221 | AdjustErrorListViewColumnSizes();
|
---|
[10332] | 222 | }
|
---|
[10506] | 223 |
|
---|
| 224 | private void AdjustErrorListViewColumnSizes() {
|
---|
| 225 | foreach (ColumnHeader ch in errorListView.Columns)
|
---|
| 226 | // adjusts the column width to the width of the column
|
---|
| 227 | // header or the column content, whichever is greater
|
---|
| 228 | ch.Width = -2;
|
---|
| 229 | }
|
---|
[10332] | 230 | }
|
---|
| 231 | } |
---|