[10731] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[12012] | 3 | * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[10731] | 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;
|
---|
[11836] | 23 | using System.Drawing;
|
---|
| 24 | using System.Threading;
|
---|
[10731] | 25 | using System.Windows.Forms;
|
---|
| 26 | using HeuristicLab.Common;
|
---|
| 27 | using HeuristicLab.Common.Resources;
|
---|
| 28 | using HeuristicLab.MainForm;
|
---|
| 29 |
|
---|
| 30 | namespace HeuristicLab.Scripting.Views {
|
---|
| 31 |
|
---|
| 32 | [View("C# Script View")]
|
---|
| 33 | [Content(typeof(CSharpScript), true)]
|
---|
| 34 | public partial class CSharpScriptView : ScriptView {
|
---|
[11836] | 35 | private const string ScriptExecutionStartedMessage = "Script execution started";
|
---|
| 36 | private const string ScriptExecutionCanceledMessage = "Script execution canceled";
|
---|
| 37 | private const string ScriptExecutionSuccessfulMessage = "Script execution successful";
|
---|
| 38 | private const string ScriptExecutionFailedMessage = "Script execution failed";
|
---|
| 39 |
|
---|
[10731] | 40 | protected bool Running { get; set; }
|
---|
| 41 |
|
---|
| 42 | public new CSharpScript Content {
|
---|
| 43 | get { return (CSharpScript)base.Content; }
|
---|
| 44 | set { base.Content = value; }
|
---|
| 45 | }
|
---|
| 46 |
|
---|
| 47 | public CSharpScriptView() {
|
---|
| 48 | InitializeComponent();
|
---|
| 49 | }
|
---|
| 50 |
|
---|
| 51 | protected override void RegisterContentEvents() {
|
---|
| 52 | base.RegisterContentEvents();
|
---|
| 53 | Content.ScriptExecutionStarted += ContentOnScriptExecutionStarted;
|
---|
| 54 | Content.ScriptExecutionFinished += ContentOnScriptExecutionFinished;
|
---|
| 55 | Content.ConsoleOutputChanged += ContentOnConsoleOutputChanged;
|
---|
[13024] | 56 | Content.ExecutionTimeChanged += ContentOnExecutionTimeChanged;
|
---|
[10731] | 57 | }
|
---|
| 58 |
|
---|
| 59 | protected override void DeregisterContentEvents() {
|
---|
| 60 | Content.ScriptExecutionStarted -= ContentOnScriptExecutionStarted;
|
---|
| 61 | Content.ScriptExecutionFinished -= ContentOnScriptExecutionFinished;
|
---|
| 62 | Content.ConsoleOutputChanged -= ContentOnConsoleOutputChanged;
|
---|
[13024] | 63 | Content.ExecutionTimeChanged -= ContentOnExecutionTimeChanged;
|
---|
[10731] | 64 | base.DeregisterContentEvents();
|
---|
| 65 | }
|
---|
| 66 |
|
---|
| 67 | #region Content event handlers
|
---|
| 68 | protected virtual void ContentOnScriptExecutionStarted(object sender, EventArgs e) {
|
---|
| 69 | if (InvokeRequired)
|
---|
| 70 | Invoke((Action<object, EventArgs>)ContentOnScriptExecutionStarted, sender, e);
|
---|
| 71 | else {
|
---|
| 72 | Locked = true;
|
---|
| 73 | ReadOnly = true;
|
---|
| 74 | startStopButton.Image = VSImageLibrary.Stop;
|
---|
| 75 | toolTip.SetToolTip(startStopButton, "Stop (Shift+F5)");
|
---|
[11836] | 76 | UpdateInfoTextLabel(ScriptExecutionStartedMessage, SystemColors.ControlText);
|
---|
[10731] | 77 | infoTabControl.SelectedTab = outputTabPage;
|
---|
| 78 | }
|
---|
| 79 | }
|
---|
| 80 | protected virtual void ContentOnScriptExecutionFinished(object sender, EventArgs<Exception> e) {
|
---|
| 81 | if (InvokeRequired)
|
---|
| 82 | Invoke((Action<object, EventArgs<Exception>>)ContentOnScriptExecutionFinished, sender, e);
|
---|
| 83 | else {
|
---|
| 84 | Locked = false;
|
---|
| 85 | ReadOnly = false;
|
---|
| 86 | startStopButton.Image = VSImageLibrary.Play;
|
---|
| 87 | toolTip.SetToolTip(startStopButton, "Run (F5)");
|
---|
[11836] | 88 |
|
---|
[10731] | 89 | var ex = e.Value;
|
---|
[11836] | 90 | if (ex == null) {
|
---|
| 91 | UpdateInfoTextLabel(ScriptExecutionSuccessfulMessage, Color.DarkGreen);
|
---|
| 92 | } else if (ex is ThreadAbortException) {
|
---|
| 93 | // the execution was canceled by the user
|
---|
| 94 | UpdateInfoTextLabel(ScriptExecutionCanceledMessage, Color.DarkOrange);
|
---|
| 95 | } else {
|
---|
| 96 | UpdateInfoTextLabel(ScriptExecutionFailedMessage, Color.DarkRed);
|
---|
[10731] | 97 | PluginInfrastructure.ErrorHandling.ShowErrorDialog(this, ex);
|
---|
[11836] | 98 | }
|
---|
| 99 |
|
---|
| 100 | Running = false;
|
---|
[10731] | 101 | }
|
---|
| 102 | }
|
---|
| 103 | protected virtual void ContentOnConsoleOutputChanged(object sender, EventArgs<string> e) {
|
---|
| 104 | if (InvokeRequired)
|
---|
| 105 | Invoke((Action<object, EventArgs<string>>)ContentOnConsoleOutputChanged, sender, e);
|
---|
| 106 | else {
|
---|
| 107 | outputTextBox.AppendText(e.Value);
|
---|
| 108 | }
|
---|
| 109 | }
|
---|
[13024] | 110 | protected virtual void ContentOnExecutionTimeChanged(object sender, EventArgs eventArgs) {
|
---|
| 111 | if (InvokeRequired)
|
---|
| 112 | Invoke((Action<object, EventArgs>)ContentOnExecutionTimeChanged, sender, eventArgs);
|
---|
| 113 | else {
|
---|
| 114 | executionTimeTextBox.Text = Content == null ? "-" : Content.ExecutionTime.ToString();
|
---|
| 115 | }
|
---|
| 116 | }
|
---|
[10731] | 117 | #endregion
|
---|
| 118 |
|
---|
| 119 | protected override void OnContentChanged() {
|
---|
| 120 | base.OnContentChanged();
|
---|
| 121 | if (Content == null) {
|
---|
| 122 | variableStoreView.Content = null;
|
---|
[13024] | 123 | executionTimeTextBox.Text = "-";
|
---|
[10731] | 124 | } else {
|
---|
| 125 | variableStoreView.Content = Content.VariableStore;
|
---|
[13024] | 126 | executionTimeTextBox.Text = Content.ExecutionTime.ToString();
|
---|
[10731] | 127 | }
|
---|
| 128 | }
|
---|
| 129 |
|
---|
| 130 | protected override void SetEnabledStateOfControls() {
|
---|
| 131 | base.SetEnabledStateOfControls();
|
---|
| 132 | startStopButton.Enabled = Content != null && (!Locked || Running);
|
---|
| 133 | }
|
---|
| 134 |
|
---|
| 135 | protected virtual void StartStopButtonOnClick(object sender, EventArgs e) {
|
---|
| 136 | if (Running) {
|
---|
| 137 | Content.Kill();
|
---|
| 138 | } else
|
---|
| 139 | if (Compile()) {
|
---|
[13024] | 140 | outputTextBox.Clear();
|
---|
| 141 | Running = true;
|
---|
| 142 | Content.ExecuteAsync();
|
---|
| 143 | }
|
---|
[10731] | 144 | }
|
---|
| 145 |
|
---|
| 146 | #region global HotKeys
|
---|
| 147 | protected override bool ProcessCmdKey(ref Message msg, Keys keyData) {
|
---|
| 148 | switch (keyData) {
|
---|
| 149 | case Keys.F5:
|
---|
| 150 | if (Content != null && !Locked && !Running) {
|
---|
| 151 | if (Compile()) {
|
---|
| 152 | outputTextBox.Clear();
|
---|
[11734] | 153 | Content.ExecuteAsync();
|
---|
[10731] | 154 | Running = true;
|
---|
| 155 | }
|
---|
| 156 | }
|
---|
| 157 | return true;
|
---|
| 158 | case Keys.F5 | Keys.Shift:
|
---|
| 159 | if (Running) Content.Kill();
|
---|
| 160 | return true;
|
---|
| 161 | case Keys.F6:
|
---|
[11657] | 162 | if (!Running) base.ProcessCmdKey(ref msg, keyData);
|
---|
[10731] | 163 | return true;
|
---|
| 164 | }
|
---|
[11657] | 165 | return base.ProcessCmdKey(ref msg, keyData);
|
---|
[10731] | 166 | }
|
---|
| 167 | #endregion
|
---|
| 168 | }
|
---|
| 169 | } |
---|