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

Last change on this file since 10761 was 10761, checked in by abeham, 6 years ago

#2136: fixed a bug when switching views

File size: 5.3 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        outputTextBox.AppendText("Compilation failed.");
117        return false;
118      } finally {
119        ShowCompilationResults();
120        if (Content.CompileErrors.Count > 0)
121          infoTabControl.SelectedTab = errorListTabPage;
122        ReadOnly = false;
123        Locked = false;
124        OnContentChanged();
125      }
126    }
127
128    protected virtual void ShowCompilationResults() {
129      if (Content.CompileErrors.Count == 0) return;
130      var msgs = Content.CompileErrors.OfType<CompilerError>()
131                                      .OrderBy(x => x.IsWarning)
132                                      .ThenBy(x => x.Line)
133                                      .ThenBy(x => x.Column);
134      foreach (var m in msgs) {
135        var item = new ListViewItem();
136        item.SubItems.AddRange(new[] {
137          m.IsWarning ? "Warning" : "Error",
138          m.ErrorNumber,
139          m.Line.ToString(CultureInfo.InvariantCulture),
140          m.Column.ToString(CultureInfo.InvariantCulture),
141          m.ErrorText
142        });
143        item.ImageIndex = m.IsWarning ? 0 : 1;
144        errorListView.Items.Add(item);
145      }
146      AdjustErrorListViewColumnSizes();
147    }
148
149    protected virtual void AdjustErrorListViewColumnSizes() {
150      foreach (ColumnHeader ch in errorListView.Columns)
151        // adjusts the column width to the width of the column
152        // header or the column content, whichever is greater
153        ch.Width = -2;
154    }
155  }
156}
Note: See TracBrowser for help on using the repository browser.