Free cookie consent management tool by TermsFeed Policy Generator

source: branches/irace/HeuristicLab.Scripting.Views/3.3/CSharpScriptView.cs @ 12394

Last change on this file since 12394 was 11171, checked in by ascheibe, 10 years ago

#2115 merged r11170 (copyright update) into trunk

File size: 4.7 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2014 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.Windows.Forms;
24using HeuristicLab.Common;
25using HeuristicLab.Common.Resources;
26using HeuristicLab.MainForm;
27
28namespace HeuristicLab.Scripting.Views {
29
30  [View("C# Script View")]
31  [Content(typeof(CSharpScript), true)]
32  public partial class CSharpScriptView : ScriptView {
33    protected bool Running { get; set; }
34
35    public new CSharpScript Content {
36      get { return (CSharpScript)base.Content; }
37      set { base.Content = value; }
38    }
39
40    public CSharpScriptView() {
41      InitializeComponent();
42    }
43
44    protected override void RegisterContentEvents() {
45      base.RegisterContentEvents();
46      Content.ScriptExecutionStarted += ContentOnScriptExecutionStarted;
47      Content.ScriptExecutionFinished += ContentOnScriptExecutionFinished;
48      Content.ConsoleOutputChanged += ContentOnConsoleOutputChanged;
49    }
50
51    protected override void DeregisterContentEvents() {
52      Content.ScriptExecutionStarted -= ContentOnScriptExecutionStarted;
53      Content.ScriptExecutionFinished -= ContentOnScriptExecutionFinished;
54      Content.ConsoleOutputChanged -= ContentOnConsoleOutputChanged;
55      base.DeregisterContentEvents();
56    }
57
58    #region Content event handlers
59    protected virtual void ContentOnScriptExecutionStarted(object sender, EventArgs e) {
60      if (InvokeRequired)
61        Invoke((Action<object, EventArgs>)ContentOnScriptExecutionStarted, sender, e);
62      else {
63        Locked = true;
64        ReadOnly = true;
65        startStopButton.Image = VSImageLibrary.Stop;
66        toolTip.SetToolTip(startStopButton, "Stop (Shift+F5)");
67        infoTabControl.SelectedTab = outputTabPage;
68      }
69    }
70    protected virtual void ContentOnScriptExecutionFinished(object sender, EventArgs<Exception> e) {
71      if (InvokeRequired)
72        Invoke((Action<object, EventArgs<Exception>>)ContentOnScriptExecutionFinished, sender, e);
73      else {
74        Locked = false;
75        ReadOnly = false;
76        startStopButton.Image = VSImageLibrary.Play;
77        toolTip.SetToolTip(startStopButton, "Run (F5)");
78        Running = false;
79        var ex = e.Value;
80        if (ex != null)
81          PluginInfrastructure.ErrorHandling.ShowErrorDialog(this, ex);
82      }
83    }
84    protected virtual void ContentOnConsoleOutputChanged(object sender, EventArgs<string> e) {
85      if (InvokeRequired)
86        Invoke((Action<object, EventArgs<string>>)ContentOnConsoleOutputChanged, sender, e);
87      else {
88        outputTextBox.AppendText(e.Value);
89      }
90    }
91    #endregion
92
93    protected override void OnContentChanged() {
94      base.OnContentChanged();
95      if (Content == null) {
96        variableStoreView.Content = null;
97      } else {
98        variableStoreView.Content = Content.VariableStore;
99      }
100    }
101
102    protected override void SetEnabledStateOfControls() {
103      base.SetEnabledStateOfControls();
104      startStopButton.Enabled = Content != null && (!Locked || Running);
105    }
106
107    protected virtual void StartStopButtonOnClick(object sender, EventArgs e) {
108      if (Running) {
109        Content.Kill();
110      } else
111        if (Compile()) {
112          outputTextBox.Clear();
113          Content.Execute();
114          Running = true;
115        }
116    }
117
118    #region global HotKeys
119    protected override bool ProcessCmdKey(ref Message msg, Keys keyData) {
120      switch (keyData) {
121        case Keys.F5:
122          if (Content != null && !Locked && !Running) {
123            if (Compile()) {
124              outputTextBox.Clear();
125              Content.Execute();
126              Running = true;
127            }
128          }
129          return true;
130        case Keys.F5 | Keys.Shift:
131          if (Running) Content.Kill();
132          return true;
133        case Keys.F6:
134          if (!Running) Compile();
135          return true;
136      }
137      return base.ProcessCmdKey(ref msg, keyData); ;
138    }
139    #endregion
140  }
141}
Note: See TracBrowser for help on using the repository browser.