Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Scripting.Views/3.3/ExecutableScriptView.cs @ 16688

Last change on this file since 16688 was 15584, checked in by swagner, 7 years ago

#2640: Updated year of copyrights in license headers on stable

File size: 5.5 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2018 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  [View("Executable Script View")]
32  [Content(typeof(ExecutableScript), true)]
33  public partial class ExecutableScriptView : ScriptView {
34    private const string ScriptExecutionStartedMessage = "Script execution started";
35    private const string ScriptExecutionCanceledMessage = "Script execution canceled";
36    private const string ScriptExecutionSuccessfulMessage = "Script execution successful";
37    private const string ScriptExecutionFailedMessage = "Script execution failed";
38
39    protected bool Running {
40      get { return Content.Running; }
41    }
42
43    public new ExecutableScript Content {
44      get { return (ExecutableScript)base.Content; }
45      set { base.Content = value; }
46    }
47
48    public ExecutableScriptView() {
49      InitializeComponent();
50    }
51
52    protected override void RegisterContentEvents() {
53      base.RegisterContentEvents();
54      Content.ScriptExecutionStarted += ContentOnScriptExecutionStarted;
55      Content.ScriptExecutionFinished += ContentOnScriptExecutionFinished;
56      Content.ExecutionTimeChanged += ContentOnExecutionTimeChanged;
57    }
58
59    protected override void DeregisterContentEvents() {
60      Content.ScriptExecutionStarted -= ContentOnScriptExecutionStarted;
61      Content.ScriptExecutionFinished -= ContentOnScriptExecutionFinished;
62      Content.ExecutionTimeChanged -= ContentOnExecutionTimeChanged;
63      base.DeregisterContentEvents();
64    }
65
66    #region Event Handlers
67    protected virtual void ContentOnScriptExecutionStarted(object sender, EventArgs e) {
68      if (InvokeRequired)
69        Invoke((Action<object, EventArgs>)ContentOnScriptExecutionStarted, sender, e);
70      else {
71        Locked = true;
72        ReadOnly = true;
73        startStopButton.Image = VSImageLibrary.Stop;
74        toolTip.SetToolTip(startStopButton, "Stop (Shift+F5)");
75        UpdateInfoTextLabel(ScriptExecutionStartedMessage, SystemColors.ControlText);
76        infoTabControl.SelectedTab = outputTabPage;
77      }
78    }
79    protected virtual void ContentOnScriptExecutionFinished(object sender, EventArgs<Exception> e) {
80      if (InvokeRequired)
81        Invoke((Action<object, EventArgs<Exception>>)ContentOnScriptExecutionFinished, sender, e);
82      else {
83        Locked = false;
84        ReadOnly = false;
85        startStopButton.Image = VSImageLibrary.Play;
86        toolTip.SetToolTip(startStopButton, "Run (F5)");
87
88        var ex = e.Value;
89        if (ex == null) {
90          UpdateInfoTextLabel(ScriptExecutionSuccessfulMessage, Color.DarkGreen);
91        } else if (ex is ThreadAbortException) {
92          // the execution was canceled by the user
93          UpdateInfoTextLabel(ScriptExecutionCanceledMessage, Color.DarkOrange);
94        } else {
95          UpdateInfoTextLabel(ScriptExecutionFailedMessage, Color.DarkRed);
96          PluginInfrastructure.ErrorHandling.ShowErrorDialog(this, ex);
97        }
98      }
99    }
100    protected virtual void ContentOnExecutionTimeChanged(object sender, EventArgs eventArgs) {
101      if (InvokeRequired)
102        Invoke((Action<object, EventArgs>)ContentOnExecutionTimeChanged, sender, eventArgs);
103      else {
104        executionTimeTextBox.Text = Content == null ? "-" : Content.ExecutionTime.ToString();
105      }
106    }
107    #endregion
108
109    protected override void OnContentChanged() {
110      base.OnContentChanged();
111      if (Content == null) {
112        executionTimeTextBox.Text = "-";
113      } else {
114        executionTimeTextBox.Text = Content.ExecutionTime.ToString();
115      }
116    }
117
118    protected override void SetEnabledStateOfControls() {
119      base.SetEnabledStateOfControls();
120      startStopButton.Enabled = Content != null && (!Locked || Running);
121    }
122
123    protected override bool ProcessCmdKey(ref Message msg, Keys keyData) {
124      switch (keyData) {
125        case Keys.F5:
126          if (Content != null && !Locked && !Running) {
127            if (Compile()) {
128              outputTextBox.Clear();
129              Content.ExecuteAsync();
130            }
131          }
132          return true;
133        case Keys.F5 | Keys.Shift:
134          if (Running) Content.Kill();
135          return true;
136        case Keys.F6:
137          if (!Running) base.ProcessCmdKey(ref msg, keyData);
138          return true;
139      }
140      return base.ProcessCmdKey(ref msg, keyData);
141    }
142
143    private void startStopButton_Click(object sender, EventArgs e) {
144      if (Running) {
145        Content.Kill();
146      } else if (Compile()) {
147        outputTextBox.Clear();
148        Content.ExecuteAsync();
149      }
150    }
151  }
152}
Note: See TracBrowser for help on using the repository browser.