Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Scripting.Views/3.3/ScriptView.cs @ 10642

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

#2136: fixed automatic selection of tabs (output window and error list)

File size: 7.8 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.Drawing;
25using System.Globalization;
26using System.Linq;
27using System.Windows.Forms;
28using HeuristicLab.Common;
29using HeuristicLab.Common.Resources;
30using HeuristicLab.Core.Views;
31using HeuristicLab.MainForm;
32
33namespace HeuristicLab.Scripting.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      errorListView.SmallImageList.Images.AddRange(new Image[] { VSImageLibrary.Warning, VSImageLibrary.Error });
48      AdjustErrorListViewColumnSizes();
49    }
50
51    protected override void RegisterContentEvents() {
52      base.RegisterContentEvents();
53      Content.CodeChanged += Content_CodeChanged;
54      Content.ScriptExecutionStarted += Content_ScriptExecutionStarted;
55      Content.ScriptExecutionFinished += Content_ScriptExecutionFinished;
56      Content.ConsoleOutputChanged += Content_ConsoleOutputChanged;
57    }
58
59    protected override void DeregisterContentEvents() {
60      Content.CodeChanged -= Content_CodeChanged;
61      Content.ScriptExecutionStarted -= Content_ScriptExecutionStarted;
62      Content.ScriptExecutionFinished -= Content_ScriptExecutionFinished;
63      Content.ConsoleOutputChanged -= Content_ConsoleOutputChanged;
64      base.DeregisterContentEvents();
65    }
66
67    #region Content event handlers
68    private void Content_CodeChanged(object sender, EventArgs e) {
69      codeEditor.UserCode = Content.Code;
70    }
71    private void Content_ScriptExecutionStarted(object sender, EventArgs e) {
72      if (InvokeRequired)
73        Invoke((Action<object, EventArgs>)Content_ScriptExecutionStarted, sender, e);
74      else {
75        Locked = true;
76        ReadOnly = true;
77        startStopButton.Image = VSImageLibrary.Stop;
78        infoTabControl.SelectedTab = outputTabPage;
79      }
80    }
81    private void Content_ScriptExecutionFinished(object sender, EventArgs<Exception> e) {
82      if (InvokeRequired)
83        Invoke((Action<object, EventArgs<Exception>>)Content_ScriptExecutionFinished, sender, e);
84      else {
85        Locked = false;
86        ReadOnly = false;
87        startStopButton.Image = VSImageLibrary.Play;
88        running = false;
89        var ex = e.Value;
90        if (ex != null)
91          PluginInfrastructure.ErrorHandling.ShowErrorDialog(this, ex);
92      }
93    }
94    private void Content_ConsoleOutputChanged(object sender, EventArgs<string> e) {
95      if (InvokeRequired)
96        Invoke((Action<object, EventArgs<string>>)Content_ConsoleOutputChanged, sender, e);
97      else {
98        outputTextBox.AppendText(e.Value);
99      }
100    }
101    #endregion
102
103    protected override void OnContentChanged() {
104      base.OnContentChanged();
105      if (Content == null) {
106        codeEditor.UserCode = string.Empty;
107        variableStoreView.Content = null;
108      } else {
109        codeEditor.UserCode = Content.Code;
110        foreach (var asm in Content.GetAssemblies())
111          codeEditor.AddAssembly(asm);
112        variableStoreView.Content = Content.VariableStore;
113        if (Content.CompileErrors == null) {
114          compilationLabel.ForeColor = SystemColors.ControlDarkDark;
115          compilationLabel.Text = "Not compiled";
116        } else if (Content.CompileErrors.HasErrors) {
117          compilationLabel.ForeColor = Color.DarkRed;
118          compilationLabel.Text = "Compilation failed";
119        } else {
120          compilationLabel.ForeColor = Color.DarkGreen;
121          compilationLabel.Text = "Compilation successful";
122        }
123      }
124    }
125
126    protected override void SetEnabledStateOfControls() {
127      base.SetEnabledStateOfControls();
128      compileButton.Enabled = Content != null && !Locked && !ReadOnly;
129      startStopButton.Enabled = Content != null && (!Locked || running);
130      codeEditor.Enabled = Content != null && !Locked && !ReadOnly;
131    }
132
133    #region Child Control event handlers
134    private void compileButton_Click(object sender, EventArgs e) {
135      Compile();
136    }
137
138    private void startStopButton_Click(object sender, EventArgs e) {
139      if (running) {
140        Content.Kill();
141      } else
142        if (Compile()) {
143          outputTextBox.Clear();
144          Content.Execute();
145          running = true;
146        }
147    }
148
149    private void codeEditor_TextEditorTextChanged(object sender, EventArgs e) {
150      Content.Code = codeEditor.UserCode;
151    }
152    #endregion
153
154    #region global HotKeys
155    protected override bool ProcessCmdKey(ref Message msg, Keys keyData) {
156      switch (keyData) {
157        case Keys.F5:
158          if (Content != null && !Locked) {
159            if (Compile()) {
160              outputTextBox.Clear();
161              Content.Execute();
162              running = true;
163            }
164          }
165          break;
166        case Keys.F5 | Keys.Shift:
167          if (running) Content.Kill();
168          break;
169        case Keys.F6:
170          Compile();
171          break;
172      }
173      return base.ProcessCmdKey(ref msg, keyData);
174    }
175    #endregion
176
177    #region Auxiliary functions
178    private bool Compile() {
179      ReadOnly = true;
180      Locked = true;
181      errorListView.Items.Clear();
182      outputTextBox.Text = "Compiling ... ";
183      try {
184        Content.Compile();
185        outputTextBox.AppendText("Compilation succeeded.");
186        return true;
187      } catch {
188        outputTextBox.AppendText("Compilation failed.");
189        return false;
190      } finally {
191        ShowCompilationResults();
192        if (Content.CompileErrors.Count > 0)
193          infoTabControl.SelectedTab = errorListTabPage;
194        ReadOnly = false;
195        Locked = false;
196        OnContentChanged();
197      }
198    }
199    #endregion
200
201    private void ShowCompilationResults() {
202      if (Content.CompileErrors.Count == 0) return;
203      var msgs = Content.CompileErrors.OfType<CompilerError>()
204                                      .OrderBy(x => x.IsWarning)
205                                      .ThenBy(x => x.Line)
206                                      .ThenBy(x => x.Column);
207      foreach (var m in msgs) {
208        var item = new ListViewItem();
209        item.SubItems.AddRange(new[] {
210          m.IsWarning ? "Warning" : "Error",
211          m.ErrorNumber,
212          m.Line.ToString(CultureInfo.InvariantCulture),
213          m.Column.ToString(CultureInfo.InvariantCulture),
214          m.ErrorText
215        });
216        item.ImageIndex = m.IsWarning ? 0 : 1;
217        errorListView.Items.Add(item);
218      }
219      AdjustErrorListViewColumnSizes();
220    }
221
222    private void AdjustErrorListViewColumnSizes() {
223      foreach (ColumnHeader ch in errorListView.Columns)
224        // adjusts the column width to the width of the column
225        // header or the column content, whichever is greater
226        ch.Width = -2;
227    }
228  }
229}
Note: See TracBrowser for help on using the repository browser.