source: trunk/sources/HeuristicLab.HLScript.Views/3.3/ScriptView.cs @ 10401

Last change on this file since 10401 was 10401, checked in by jkarder, 6 years ago

#2136:

  • renamed some HLScript files
  • fixed a bug where variables could be renamed while a script was running
  • the complete source code is now always visible
  • removed "show generated code" feature
File size: 7.2 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.CodeDom.Compiler;
24using System.Collections.Generic;
25using System.Drawing;
26using System.Linq;
27using System.Windows.Forms;
28using HeuristicLab.Common;
29using HeuristicLab.Common.Resources;
30using HeuristicLab.Core.Views;
31using HeuristicLab.MainForm;
32
33namespace HeuristicLab.HLScript.Views {
34
35  [View("Script View")]
36  [Content(typeof(Script), true)]
37  public partial class ScriptView : NamedItemView {
38    private bool running;
39
40    public new Script Content {
41      get { return (Script)base.Content; }
42      set { base.Content = (Script)value; }
43    }
44
45    public ScriptView() {
46      InitializeComponent();
47    }
48
49    protected override void RegisterContentEvents() {
50      base.RegisterContentEvents();
51      Content.CodeChanged += Content_CodeChanged;
52      Content.ScriptExecutionStarted += Content_ScriptExecutionStarted;
53      Content.ScriptExecutionFinished += Content_ScriptExecutionFinished;
54      Content.ConsoleOutputChanged += Content_ConsoleOutputChanged;
55    }
56
57    protected override void DeregisterContentEvents() {
58      Content.CodeChanged -= Content_CodeChanged;
59      Content.ScriptExecutionStarted -= Content_ScriptExecutionStarted;
60      Content.ScriptExecutionFinished -= Content_ScriptExecutionFinished;
61      Content.ConsoleOutputChanged -= Content_ConsoleOutputChanged;
62      base.DeregisterContentEvents();
63    }
64
65    #region Content event handlers
66    private void Content_CodeChanged(object sender, EventArgs e) {
67      codeEditor.UserCode = Content.Code;
68    }
69    private void Content_ScriptExecutionStarted(object sender, EventArgs e) {
70      Locked = true;
71      ReadOnly = true;
72      startStopButton.Image = VSImageLibrary.Stop;
73    }
74    private void Content_ScriptExecutionFinished(object sender, EventArgs e) {
75      Locked = false;
76      ReadOnly = false;
77      startStopButton.Image = VSImageLibrary.Play;
78      running = false;
79    }
80    private void Content_ConsoleOutputChanged(object sender, EventArgs<string> e) {
81      if (InvokeRequired) Invoke((Action<object, EventArgs<string>>)Content_ConsoleOutputChanged, sender, e);
82      else {
83        outputTextBox.AppendText(e.Value);
84      }
85    }
86    #endregion
87
88    protected override void OnContentChanged() {
89      base.OnContentChanged();
90      if (Content == null) {
91        codeEditor.UserCode = string.Empty;
92        variableStoreView.Content = null;
93      } else {
94        codeEditor.UserCode = Content.Code;
95        foreach (var asm in Content.GetAssemblies())
96          codeEditor.AddAssembly(asm);
97        variableStoreView.Content = Content.VariableStore;
98        if (Content.CompileErrors == null) {
99          compilationLabel.ForeColor = SystemColors.ControlDarkDark;
100          compilationLabel.Text = "Not compiled";
101        } else if (Content.CompileErrors.HasErrors) {
102          compilationLabel.ForeColor = Color.DarkRed;
103          compilationLabel.Text = "Compilation failed";
104        } else {
105          compilationLabel.ForeColor = Color.DarkGreen;
106          compilationLabel.Text = "Compilation successful";
107        }
108      }
109    }
110
111    protected override void SetEnabledStateOfControls() {
112      base.SetEnabledStateOfControls();
113      startStopButton.Enabled = Content != null && (!Locked || running);
114      codeEditor.Enabled = Content != null && !Locked && !ReadOnly;
115    }
116
117    #region Child Control event handlers
118    private void startStopButton_Click(object sender, EventArgs e) {
119      if (running) {
120        Content.Kill();
121      } else
122        if (Compile()) {
123          outputTextBox.Clear();
124          Content.Execute();
125          running = true;
126        }
127    }
128
129    private void codeEditor_TextEditorTextChanged(object sender, EventArgs e) {
130      Content.Code = codeEditor.UserCode;
131    }
132    #endregion
133
134    #region global HotKeys
135    protected override bool ProcessCmdKey(ref Message msg, Keys keyData) {
136      if (keyData == Keys.F5) {
137        if (Content == null || Locked)
138          return base.ProcessCmdKey(ref msg, keyData);
139        outputTextBox.Clear();
140        bool result = Compile();
141        if (result) {
142          outputTextBox.Clear();
143          Content.Execute();
144          running = true;
145        }
146        return true;
147      } else if (keyData == (Keys.F5 | Keys.Shift)) {
148        Content.Kill();
149      }
150      return base.ProcessCmdKey(ref msg, keyData);
151    }
152    #endregion
153
154    #region Auxiliary functions
155    private bool Compile() {
156      ReadOnly = true;
157      Locked = true;
158      errorListView.Items.Clear();
159      outputTextBox.Clear();
160      outputTextBox.AppendText("Compiling ... ");
161      try {
162        Content.Compile();
163        outputTextBox.AppendText("Compilation succeeded.");
164        return true;
165      } catch {
166        outputTextBox.AppendText("Compilation failed.");
167        ShowCompilationErrors();
168        return false;
169      } finally {
170        OnContentChanged();
171        ReadOnly = false;
172        Locked = false;
173      }
174    }
175    #endregion
176
177    private void ShowCompilationErrors() {
178      if (Content.CompileErrors.Count == 0) return;
179      var warnings = new List<CompilerError>();
180      var errors = new List<CompilerError>();
181      foreach (CompilerError ce in Content.CompileErrors) {
182        if (ce.IsWarning) warnings.Add(ce);
183        else errors.Add(ce);
184      }
185      var msgs = warnings.OrderBy(x => x.Line)
186                         .ThenBy(x => x.Column)
187                   .Concat(errors.OrderBy(x => x.Line)
188                                 .ThenBy(x => x.Column));
189      outputTextBox.AppendText(Environment.NewLine);
190      outputTextBox.AppendText("---");
191      outputTextBox.AppendText(Environment.NewLine);
192      foreach (var m in msgs) {
193        var item = new ListViewItem(new[] {
194          m.IsWarning ? "Warning" : "Error",
195          m.ErrorNumber,
196          m.Line.ToString(),
197          m.Column.ToString(),
198          m.ErrorText
199        });
200        errorListView.Items.Add(item);
201        outputTextBox.AppendText(string.Format("{0} {1} ({2}:{3}): {4}",
202                                 item.SubItems[0].Text,
203                                 item.SubItems[1].Text,
204                                 item.SubItems[2].Text,
205                                 item.SubItems[3].Text,
206                                 item.SubItems[4].Text));
207        outputTextBox.AppendText(Environment.NewLine);
208      }
209    }
210  }
211}
Note: See TracBrowser for help on using the repository browser.