Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HLScript/HeuristicLab.HLScript.Views/3.3/HLScriptView.cs @ 10879

Last change on this file since 10879 was 10358, checked in by jkarder, 11 years ago

#2136:

  • refactored HLScriptGeneration to separate outputs from different running HL scripts.
  • added persistence support for HLScripts and fixed cloning
  • added code completion for all types in the currently loaded assemblies
  • merged trunk changes
File size: 7.3 KB
RevLine 
[10332]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.CodeEditor;
29using HeuristicLab.Common;
30using HeuristicLab.Common.Resources;
31using HeuristicLab.Core.Views;
32using HeuristicLab.MainForm;
33
34namespace HeuristicLab.HLScript.Views {
35
36  [View("HLScript View")]
37  [Content(typeof(HLScript), true)]
[10358]38  public partial class HLScriptView : NamedItemView {
[10332]39    private bool running;
40
41    public new HLScript Content {
42      get { return (HLScript)base.Content; }
43      set { base.Content = (HLScript)value; }
44    }
45
46    public HLScriptView() {
47      InitializeComponent();
48    }
49
50    protected override void RegisterContentEvents() {
51      base.RegisterContentEvents();
52      Content.CodeChanged += Content_CodeChanged;
53      Content.ScriptExecutionStarted += Content_ScriptExecutionStarted;
54      Content.ScriptExecutionFinished += Content_ScriptExecutionFinished;
55      Content.ConsoleOutputChanged += Content_ConsoleOutputChanged;
56    }
57
58    protected override void DeregisterContentEvents() {
59      Content.CodeChanged -= Content_CodeChanged;
60      Content.ScriptExecutionStarted -= Content_ScriptExecutionStarted;
61      Content.ScriptExecutionFinished -= Content_ScriptExecutionFinished;
62      Content.ConsoleOutputChanged -= Content_ConsoleOutputChanged;
63      base.DeregisterContentEvents();
64    }
65
66    #region Content event handlers
67    private void Content_CodeChanged(object sender, EventArgs e) {
68      codeEditor.UserCode = Content.Code;
69    }
70    private void Content_ScriptExecutionStarted(object sender, EventArgs e) {
[10358]71      Locked = true;
[10332]72      startStopButton.Image = VSImageLibrary.Stop;
73    }
74    private void Content_ScriptExecutionFinished(object sender, EventArgs e) {
[10358]75      Locked = false;
[10332]76      startStopButton.Image = VSImageLibrary.Play;
77      running = false;
78    }
79    private void Content_ConsoleOutputChanged(object sender, EventArgs<string> e) {
80      if (InvokeRequired) Invoke((Action<object, EventArgs<string>>)Content_ConsoleOutputChanged, sender, e);
81      else {
82        outputTextBox.AppendText(e.Value);
83      }
84    }
85    #endregion
86
87    protected override void OnContentChanged() {
88      base.OnContentChanged();
89      if (Content == null) {
90        codeEditor.UserCode = string.Empty;
91        variableStoreView.Content = null;
92      } else {
93        codeEditor.UserCode = Content.Code;
[10358]94        foreach (var asm in Content.GetAssemblies())
95          codeEditor.AddAssembly(asm);
[10332]96        variableStoreView.Content = Content.VariableStore;
97        if (Content.CompileErrors == null) {
98          compilationLabel.ForeColor = SystemColors.ControlDarkDark;
99          compilationLabel.Text = "Not compiled";
100        } else if (Content.CompileErrors.HasErrors) {
101          compilationLabel.ForeColor = Color.DarkRed;
102          compilationLabel.Text = "Compilation failed";
103        } else {
104          compilationLabel.ForeColor = Color.DarkGreen;
105          compilationLabel.Text = "Compilation successful";
106        }
107      }
108    }
109
110    protected override void SetEnabledStateOfControls() {
111      base.SetEnabledStateOfControls();
[10358]112      startStopButton.Enabled = Content != null && !Locked;
[10332]113      showCodeButton.Enabled = Content != null && !string.IsNullOrEmpty(Content.CompilationUnitCode);
[10358]114      codeEditor.Enabled = Content != null && !Locked && !ReadOnly;
[10332]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 showCodeButton_Click(object sender, EventArgs e) {
130      new CodeViewer(Content.CompilationUnitCode).ShowDialog(this);
131    }
132
133    private void codeEditor_TextEditorTextChanged(object sender, EventArgs e) {
134      Content.Code = codeEditor.UserCode;
135    }
136    #endregion
137
138    #region global HotKeys
139    protected override bool ProcessCmdKey(ref Message msg, Keys keyData) {
140      if (keyData == Keys.F5) {
[10358]141        if (Content == null || Locked)
[10332]142          return base.ProcessCmdKey(ref msg, keyData);
143        outputTextBox.Clear();
144        bool result = Compile();
145        if (result) {
146          outputTextBox.Clear();
147          Content.Execute();
[10358]148          running = true;
[10332]149        }
150        return true;
151      } else if (keyData == (Keys.F5 | Keys.Shift)) {
152        Content.Kill();
153      }
154      return base.ProcessCmdKey(ref msg, keyData);
155    }
156    #endregion
157
158    #region Auxiliary functions
159    private bool Compile() {
160      ReadOnly = true;
[10358]161      Locked = true;
[10332]162      errorListView.Items.Clear();
163      outputTextBox.Clear();
164      outputTextBox.AppendText("Compiling ... ");
165      try {
166        Content.Compile();
167        outputTextBox.AppendText("Compilation succeeded.");
168        return true;
169      } catch {
170        outputTextBox.AppendText("Compilation failed.");
171        ShowCompilationErrors();
172        return false;
173      } finally {
174        OnContentChanged();
175        ReadOnly = false;
[10358]176        Locked = false;
[10332]177      }
178    }
179    #endregion
180
181    private void ShowCompilationErrors() {
182      if (Content.CompileErrors.Count == 0) return;
183      var warnings = new List<CompilerError>();
184      var errors = new List<CompilerError>();
185      foreach (CompilerError ce in Content.CompileErrors) {
186        if (ce.IsWarning) warnings.Add(ce);
187        else errors.Add(ce);
188      }
189      var msgs = warnings.OrderBy(x => x.Line)
190                         .ThenBy(x => x.Column)
191                   .Concat(errors.OrderBy(x => x.Line)
192                                 .ThenBy(x => x.Column));
193      outputTextBox.AppendText(Environment.NewLine);
194      outputTextBox.AppendText("---");
195      outputTextBox.AppendText(Environment.NewLine);
196      foreach (var m in msgs) {
197        var item = new ListViewItem(new[] {
198          m.IsWarning ? "Warning" : "Error",
199          m.ErrorNumber,
200          m.Line.ToString(),
201          m.Column.ToString(),
202          m.ErrorText
203        });
204        errorListView.Items.Add(item);
205        outputTextBox.AppendText(string.Format("{0} {1} ({2}:{3}): {4}", item.SubItems[0].Text, item.SubItems[1].Text, item.SubItems[2].Text, item.SubItems[3].Text, item.SubItems[4].Text));
206        outputTextBox.AppendText(Environment.NewLine);
207      }
208    }
209  }
210}
Note: See TracBrowser for help on using the repository browser.