Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2136: fixed a bug when switching views

File size: 5.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.Drawing;
[10506]25using System.Globalization;
[10332]26using System.Linq;
27using System.Windows.Forms;
28using HeuristicLab.Common.Resources;
29using HeuristicLab.Core.Views;
30using HeuristicLab.MainForm;
31
[10506]32namespace HeuristicLab.Scripting.Views {
[10332]33
[10401]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; }
[10731]39      set { base.Content = value; }
[10332]40    }
41
[10401]42    public ScriptView() {
[10332]43      InitializeComponent();
[10506]44      errorListView.SmallImageList.Images.AddRange(new Image[] { VSImageLibrary.Warning, VSImageLibrary.Error });
[10332]45    }
46
47    protected override void RegisterContentEvents() {
48      base.RegisterContentEvents();
[10731]49      Content.CodeChanged += ContentOnCodeChanged;
[10332]50    }
51
52    protected override void DeregisterContentEvents() {
[10731]53      Content.CodeChanged -= ContentOnCodeChanged;
[10332]54      base.DeregisterContentEvents();
55    }
56
[10731]57    protected virtual void ContentOnCodeChanged(object sender, EventArgs e) {
[10332]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;
[10358]67        foreach (var asm in Content.GetAssemblies())
68          codeEditor.AddAssembly(asm);
[10332]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();
[10506]84      compileButton.Enabled = Content != null && !Locked && !ReadOnly;
[10358]85      codeEditor.Enabled = Content != null && !Locked && !ReadOnly;
[10332]86    }
87
[10731]88    protected virtual void CompileButtonOnClick(object sender, EventArgs e) {
[10506]89      Compile();
90    }
91
[10731]92    protected virtual void CodeEditorOnTextEditorTextChanged(object sender, EventArgs e) {
[10761]93      if (Content == null) return;
[10332]94      Content.Code = codeEditor.UserCode;
95    }
96    protected override bool ProcessCmdKey(ref Message msg, Keys keyData) {
[10506]97      switch (keyData) {
98        case Keys.F6:
[10731]99          if (Content != null && !Locked)
100            Compile();
101          return true;
[10332]102      }
103      return base.ProcessCmdKey(ref msg, keyData);
104    }
105
[10731]106    public virtual bool Compile() {
[10332]107      ReadOnly = true;
[10358]108      Locked = true;
[10332]109      errorListView.Items.Clear();
[10506]110      outputTextBox.Text = "Compiling ... ";
[10332]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 {
[10506]119        ShowCompilationResults();
[10642]120        if (Content.CompileErrors.Count > 0)
121          infoTabControl.SelectedTab = errorListTabPage;
[10332]122        ReadOnly = false;
[10358]123        Locked = false;
[10506]124        OnContentChanged();
[10332]125      }
126    }
127
[10731]128    protected virtual void ShowCompilationResults() {
[10332]129      if (Content.CompileErrors.Count == 0) return;
[10506]130      var msgs = Content.CompileErrors.OfType<CompilerError>()
131                                      .OrderBy(x => x.IsWarning)
132                                      .ThenBy(x => x.Line)
133                                      .ThenBy(x => x.Column);
[10332]134      foreach (var m in msgs) {
[10506]135        var item = new ListViewItem();
136        item.SubItems.AddRange(new[] {
[10332]137          m.IsWarning ? "Warning" : "Error",
138          m.ErrorNumber,
[10506]139          m.Line.ToString(CultureInfo.InvariantCulture),
140          m.Column.ToString(CultureInfo.InvariantCulture),
[10332]141          m.ErrorText
142        });
[10506]143        item.ImageIndex = m.IsWarning ? 0 : 1;
[10332]144        errorListView.Items.Add(item);
145      }
[10506]146      AdjustErrorListViewColumnSizes();
[10332]147    }
[10506]148
[10731]149    protected virtual void AdjustErrorListViewColumnSizes() {
[10506]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    }
[10332]155  }
156}
Note: See TracBrowser for help on using the repository browser.