Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 10332 was 10332, checked in by jkarder, 10 years ago

#2136: added prototype of a scripting environment

File size: 7.1 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.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)]
38  public partial class HLScriptView : ItemView {
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) {
71      ReadOnly = true;
72      startStopButton.Image = VSImageLibrary.Stop;
73    }
74    private void Content_ScriptExecutionFinished(object sender, EventArgs e) {
75      ReadOnly = false;
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;
94        variableStoreView.Content = Content.VariableStore;
95        if (Content.CompileErrors == null) {
96          compilationLabel.ForeColor = SystemColors.ControlDarkDark;
97          compilationLabel.Text = "Not compiled";
98        } else if (Content.CompileErrors.HasErrors) {
99          compilationLabel.ForeColor = Color.DarkRed;
100          compilationLabel.Text = "Compilation failed";
101        } else {
102          compilationLabel.ForeColor = Color.DarkGreen;
103          compilationLabel.Text = "Compilation successful";
104        }
105      }
106    }
107
108    protected override void SetEnabledStateOfControls() {
109      base.SetEnabledStateOfControls();
110      startStopButton.Enabled = Content != null;
111      showCodeButton.Enabled = Content != null && !string.IsNullOrEmpty(Content.CompilationUnitCode);
112      codeEditor.Enabled = Content != null && !ReadOnly;
113    }
114
115    #region Child Control event handlers
116    private void startStopButton_Click(object sender, EventArgs e) {
117      if (running) {
118        Content.Kill();
119      } else
120        if (Compile()) {
121          outputTextBox.Clear();
122          Content.Execute();
123          running = true;
124        }
125    }
126
127    private void showCodeButton_Click(object sender, EventArgs e) {
128      new CodeViewer(Content.CompilationUnitCode).ShowDialog(this);
129    }
130
131    private void codeEditor_TextEditorTextChanged(object sender, EventArgs e) {
132      Content.Code = codeEditor.UserCode;
133    }
134    #endregion
135
136    #region global HotKeys
137    protected override bool ProcessCmdKey(ref Message msg, Keys keyData) {
138      if (keyData == Keys.F5) {
139        if (Content == null || ReadOnly)
140          return base.ProcessCmdKey(ref msg, keyData);
141        outputTextBox.Clear();
142        bool result = Compile();
143        if (result) {
144          outputTextBox.Clear();
145          Content.Execute();
146        }
147        return true;
148      } else if (keyData == (Keys.F5 | Keys.Shift)) {
149        Content.Kill();
150      }
151      return base.ProcessCmdKey(ref msg, keyData);
152    }
153    #endregion
154
155    #region Auxiliary functions
156    private bool Compile() {
157      ReadOnly = 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      }
173    }
174    #endregion
175
176    private void ShowCompilationErrors() {
177      if (Content.CompileErrors.Count == 0) return;
178      var warnings = new List<CompilerError>();
179      var errors = new List<CompilerError>();
180      foreach (CompilerError ce in Content.CompileErrors) {
181        if (ce.IsWarning) warnings.Add(ce);
182        else errors.Add(ce);
183      }
184      var msgs = warnings.OrderBy(x => x.Line)
185                         .ThenBy(x => x.Column)
186                   .Concat(errors.OrderBy(x => x.Line)
187                                 .ThenBy(x => x.Column));
188      outputTextBox.AppendText(Environment.NewLine);
189      outputTextBox.AppendText("---");
190      outputTextBox.AppendText(Environment.NewLine);
191      foreach (var m in msgs) {
192        var item = new ListViewItem(new[] {
193          m.IsWarning ? "Warning" : "Error",
194          m.ErrorNumber,
195          m.Line.ToString(),
196          m.Column.ToString(),
197          m.ErrorText
198        });
199        errorListView.Items.Add(item);
200        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));
201        outputTextBox.AppendText(Environment.NewLine);
202      }
203    }
204  }
205}
Note: See TracBrowser for help on using the repository browser.