Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Scripting.Views/3.3/CSharpScriptView.cs @ 10731

Last change on this file since 10731 was 10731, checked in by abeham, 9 years ago

#2136:

  • Split Script into Script and CSharpScript
  • Split ScriptView into ScriptView and CSharpScriptView
File size: 5.5 KB
Line 
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
22using System;
23using System.Drawing;
24using System.Windows.Forms;
25using HeuristicLab.Common;
26using HeuristicLab.Common.Resources;
27using HeuristicLab.MainForm;
28
29namespace HeuristicLab.Scripting.Views {
30
31  [View("C# Script View")]
32  [Content(typeof(CSharpScript), true)]
33  public partial class CSharpScriptView : ScriptView {
34    protected bool Running { get; set; }
35
36    public new CSharpScript Content {
37      get { return (CSharpScript)base.Content; }
38      set { base.Content = value; }
39    }
40
41    public CSharpScriptView() {
42      InitializeComponent();
43    }
44
45    protected override void RegisterContentEvents() {
46      base.RegisterContentEvents();
47      Content.ScriptExecutionStarted += ContentOnScriptExecutionStarted;
48      Content.ScriptExecutionFinished += ContentOnScriptExecutionFinished;
49      Content.ConsoleOutputChanged += ContentOnConsoleOutputChanged;
50    }
51
52    protected override void DeregisterContentEvents() {
53      Content.ScriptExecutionStarted -= ContentOnScriptExecutionStarted;
54      Content.ScriptExecutionFinished -= ContentOnScriptExecutionFinished;
55      Content.ConsoleOutputChanged -= ContentOnConsoleOutputChanged;
56      base.DeregisterContentEvents();
57    }
58
59    #region Content event handlers
60    protected virtual void ContentOnScriptExecutionStarted(object sender, EventArgs e) {
61      if (InvokeRequired)
62        Invoke((Action<object, EventArgs>)ContentOnScriptExecutionStarted, sender, e);
63      else {
64        Locked = true;
65        ReadOnly = true;
66        startStopButton.Image = VSImageLibrary.Stop;
67        toolTip.SetToolTip(startStopButton, "Stop (Shift+F5)");
68        infoTabControl.SelectedTab = outputTabPage;
69      }
70    }
71    protected virtual void ContentOnScriptExecutionFinished(object sender, EventArgs<Exception> e) {
72      if (InvokeRequired)
73        Invoke((Action<object, EventArgs<Exception>>)ContentOnScriptExecutionFinished, sender, e);
74      else {
75        Locked = false;
76        ReadOnly = false;
77        startStopButton.Image = VSImageLibrary.Play;
78        toolTip.SetToolTip(startStopButton, "Run (F5)");
79        Running = false;
80        var ex = e.Value;
81        if (ex != null)
82          PluginInfrastructure.ErrorHandling.ShowErrorDialog(this, ex);
83      }
84    }
85    protected virtual void ContentOnConsoleOutputChanged(object sender, EventArgs<string> e) {
86      if (InvokeRequired)
87        Invoke((Action<object, EventArgs<string>>)ContentOnConsoleOutputChanged, sender, e);
88      else {
89        outputTextBox.AppendText(e.Value);
90      }
91    }
92    #endregion
93
94    protected override void OnContentChanged() {
95      base.OnContentChanged();
96      if (Content == null) {
97        codeEditor.UserCode = string.Empty;
98        variableStoreView.Content = null;
99      } else {
100        codeEditor.UserCode = Content.Code;
101        foreach (var asm in Content.GetAssemblies())
102          codeEditor.AddAssembly(asm);
103        variableStoreView.Content = Content.VariableStore;
104        if (Content.CompileErrors == null) {
105          compilationLabel.ForeColor = SystemColors.ControlDarkDark;
106          compilationLabel.Text = "Not compiled";
107        } else if (Content.CompileErrors.HasErrors) {
108          compilationLabel.ForeColor = Color.DarkRed;
109          compilationLabel.Text = "Compilation failed";
110        } else {
111          compilationLabel.ForeColor = Color.DarkGreen;
112          compilationLabel.Text = "Compilation successful";
113        }
114      }
115    }
116
117    protected override void SetEnabledStateOfControls() {
118      base.SetEnabledStateOfControls();
119      compileButton.Enabled = Content != null && !Locked && !ReadOnly;
120      startStopButton.Enabled = Content != null && (!Locked || Running);
121      codeEditor.Enabled = Content != null && !Locked && !ReadOnly;
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          Content.Execute();
131          Running = true;
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.Execute();
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) Compile();
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.