Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Scripting.Views/3.3/CSharpScriptView.cs @ 13149

Last change on this file since 13149 was 12009, checked in by ascheibe, 10 years ago

#2212 updated copyright year

File size: 5.6 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 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
22using System;
23using System.Drawing;
24using System.Threading;
25using System.Windows.Forms;
26using HeuristicLab.Common;
27using HeuristicLab.Common.Resources;
28using HeuristicLab.MainForm;
29
30namespace HeuristicLab.Scripting.Views {
31
32  [View("C# Script View")]
33  [Content(typeof(CSharpScript), true)]
34  public partial class CSharpScriptView : ScriptView {
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
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;
56    }
57
58    protected override void DeregisterContentEvents() {
59      Content.ScriptExecutionStarted -= ContentOnScriptExecutionStarted;
60      Content.ScriptExecutionFinished -= ContentOnScriptExecutionFinished;
61      Content.ConsoleOutputChanged -= ContentOnConsoleOutputChanged;
62      base.DeregisterContentEvents();
63    }
64
65    #region Content event handlers
66    protected virtual void ContentOnScriptExecutionStarted(object sender, EventArgs e) {
67      if (InvokeRequired)
68        Invoke((Action<object, EventArgs>)ContentOnScriptExecutionStarted, sender, e);
69      else {
70        Locked = true;
71        ReadOnly = true;
72        startStopButton.Image = VSImageLibrary.Stop;
73        toolTip.SetToolTip(startStopButton, "Stop (Shift+F5)");
74        UpdateInfoTextLabel(ScriptExecutionStartedMessage, SystemColors.ControlText);
75        infoTabControl.SelectedTab = outputTabPage;
76      }
77    }
78    protected virtual void ContentOnScriptExecutionFinished(object sender, EventArgs<Exception> e) {
79      if (InvokeRequired)
80        Invoke((Action<object, EventArgs<Exception>>)ContentOnScriptExecutionFinished, sender, e);
81      else {
82        Locked = false;
83        ReadOnly = false;
84        startStopButton.Image = VSImageLibrary.Play;
85        toolTip.SetToolTip(startStopButton, "Run (F5)");
86
87        var ex = e.Value;
88        if (ex == null) {
89          UpdateInfoTextLabel(ScriptExecutionSuccessfulMessage, Color.DarkGreen);
90        } else if (ex is ThreadAbortException) {
91          // the execution was canceled by the user
92          UpdateInfoTextLabel(ScriptExecutionCanceledMessage, Color.DarkOrange);
93        } else {
94          UpdateInfoTextLabel(ScriptExecutionFailedMessage, Color.DarkRed);
95          PluginInfrastructure.ErrorHandling.ShowErrorDialog(this, ex);
96        }
97
98        Running = false;
99      }
100    }
101    protected virtual void ContentOnConsoleOutputChanged(object sender, EventArgs<string> e) {
102      if (InvokeRequired)
103        Invoke((Action<object, EventArgs<string>>)ContentOnConsoleOutputChanged, sender, e);
104      else {
105        outputTextBox.AppendText(e.Value);
106      }
107    }
108    #endregion
109
110    protected override void OnContentChanged() {
111      base.OnContentChanged();
112      if (Content == null) {
113        variableStoreView.Content = null;
114      } else {
115        variableStoreView.Content = Content.VariableStore;
116      }
117    }
118
119    protected override void SetEnabledStateOfControls() {
120      base.SetEnabledStateOfControls();
121      startStopButton.Enabled = Content != null && (!Locked || Running);
122    }
123
124    protected virtual void StartStopButtonOnClick(object sender, EventArgs e) {
125      if (Running) {
126        Content.Kill();
127      } else
128        if (Compile()) {
129          outputTextBox.Clear();
130          Running = true;
131          Content.ExecuteAsync();
132        }
133    }
134
135    #region global HotKeys
136    protected override bool ProcessCmdKey(ref Message msg, Keys keyData) {
137      switch (keyData) {
138        case Keys.F5:
139          if (Content != null && !Locked && !Running) {
140            if (Compile()) {
141              outputTextBox.Clear();
142              Content.ExecuteAsync();
143              Running = true;
144            }
145          }
146          return true;
147        case Keys.F5 | Keys.Shift:
148          if (Running) Content.Kill();
149          return true;
150        case Keys.F6:
151          if (!Running) base.ProcessCmdKey(ref msg, keyData);
152          return true;
153      }
154      return base.ProcessCmdKey(ref msg, keyData);
155    }
156    #endregion
157  }
158}
Note: See TracBrowser for help on using the repository browser.