Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2136: fixed compilation error output in console

File size: 5.4 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.Resources;
29using HeuristicLab.Core.Views;
30using HeuristicLab.MainForm;
31
32namespace HeuristicLab.Scripting.Views {
33
34  [View("Script View")]
35  [Content(typeof(Script), true)]
36  public partial class ScriptView : NamedItemView {
37    public new Script Content {
38      get { return (Script)base.Content; }
39      set { base.Content = value; }
40    }
41
42    public ScriptView() {
43      InitializeComponent();
44      errorListView.SmallImageList.Images.AddRange(new Image[] { VSImageLibrary.Warning, VSImageLibrary.Error });
45    }
46
47    protected override void RegisterContentEvents() {
48      base.RegisterContentEvents();
49      Content.CodeChanged += ContentOnCodeChanged;
50    }
51
52    protected override void DeregisterContentEvents() {
53      Content.CodeChanged -= ContentOnCodeChanged;
54      base.DeregisterContentEvents();
55    }
56
57    protected virtual void ContentOnCodeChanged(object sender, EventArgs e) {
58      codeEditor.UserCode = Content.Code;
59    }
60
61    protected override void OnContentChanged() {
62      base.OnContentChanged();
63      if (Content == null) {
64        codeEditor.UserCode = string.Empty;
65      } else {
66        codeEditor.UserCode = Content.Code;
67        foreach (var asm in Content.GetAssemblies())
68          codeEditor.AddAssembly(asm);
69        if (Content.CompileErrors == null) {
70          compilationLabel.ForeColor = SystemColors.ControlDarkDark;
71          compilationLabel.Text = "Not compiled";
72        } else if (Content.CompileErrors.HasErrors) {
73          compilationLabel.ForeColor = Color.DarkRed;
74          compilationLabel.Text = "Compilation failed";
75        } else {
76          compilationLabel.ForeColor = Color.DarkGreen;
77          compilationLabel.Text = "Compilation successful";
78        }
79      }
80    }
81
82    protected override void SetEnabledStateOfControls() {
83      base.SetEnabledStateOfControls();
84      compileButton.Enabled = Content != null && !Locked && !ReadOnly;
85      codeEditor.Enabled = Content != null && !Locked && !ReadOnly;
86    }
87
88    protected virtual void CompileButtonOnClick(object sender, EventArgs e) {
89      Compile();
90    }
91
92    protected virtual void CodeEditorOnTextEditorTextChanged(object sender, EventArgs e) {
93      if (Content == null) return;
94      Content.Code = codeEditor.UserCode;
95    }
96    protected override bool ProcessCmdKey(ref Message msg, Keys keyData) {
97      switch (keyData) {
98        case Keys.F6:
99          if (Content != null && !Locked)
100            Compile();
101          return true;
102      }
103      return base.ProcessCmdKey(ref msg, keyData);
104    }
105
106    public virtual bool Compile() {
107      ReadOnly = true;
108      Locked = true;
109      errorListView.Items.Clear();
110      outputTextBox.Text = "Compiling ... ";
111      try {
112        Content.Compile();
113        outputTextBox.AppendText("Compilation succeeded.");
114        return true;
115      } catch {
116        if (Content.CompileErrors.HasErrors) {
117          outputTextBox.AppendText("Compilation failed.");
118          return false;
119        } else {
120          outputTextBox.AppendText("Compilation succeeded.");
121          return true;
122        }
123      } finally {
124        ShowCompilationResults();
125        if (Content.CompileErrors.Count > 0)
126          infoTabControl.SelectedTab = errorListTabPage;
127        ReadOnly = false;
128        Locked = false;
129        OnContentChanged();
130      }
131    }
132
133    protected virtual void ShowCompilationResults() {
134      if (Content.CompileErrors.Count == 0) return;
135      var msgs = Content.CompileErrors.OfType<CompilerError>()
136                                      .OrderBy(x => x.IsWarning)
137                                      .ThenBy(x => x.Line)
138                                      .ThenBy(x => x.Column);
139      foreach (var m in msgs) {
140        var item = new ListViewItem();
141        item.SubItems.AddRange(new[] {
142          m.IsWarning ? "Warning" : "Error",
143          m.ErrorNumber,
144          m.Line.ToString(CultureInfo.InvariantCulture),
145          m.Column.ToString(CultureInfo.InvariantCulture),
146          m.ErrorText
147        });
148        item.ImageIndex = m.IsWarning ? 0 : 1;
149        errorListView.Items.Add(item);
150      }
151      AdjustErrorListViewColumnSizes();
152    }
153
154    protected virtual void AdjustErrorListViewColumnSizes() {
155      foreach (ColumnHeader ch in errorListView.Columns)
156        // adjusts the column width to the width of the column header
157        ch.Width = -2;
158    }
159  }
160}
Note: See TracBrowser for help on using the repository browser.