Free cookie consent management tool by TermsFeed Policy Generator

source: branches/Breadcrumbs/HeuristicLab.Scripting.Views/3.3/ScriptView.cs @ 12231

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

#2116: merged r10041-r11593 from trunk into branch

File size: 5.7 KB
RevLine 
[10332]1#region License Information
2/* HeuristicLab
[11594]3 * Copyright (C) 2002-2014 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[10332]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
[11594]47    public override bool ReadOnly {
48      get { return codeEditor.ReadOnly || base.ReadOnly; }
49      set { base.ReadOnly = codeEditor.ReadOnly = value; }
50    }
51
52    public override bool Locked {
53      get { return codeEditor.ReadOnly || base.Locked; }
54      set { base.Locked = codeEditor.ReadOnly = value; }
55    }
56
[10332]57    protected override void RegisterContentEvents() {
58      base.RegisterContentEvents();
[10731]59      Content.CodeChanged += ContentOnCodeChanged;
[10332]60    }
61
62    protected override void DeregisterContentEvents() {
[10731]63      Content.CodeChanged -= ContentOnCodeChanged;
[10332]64      base.DeregisterContentEvents();
65    }
66
[10731]67    protected virtual void ContentOnCodeChanged(object sender, EventArgs e) {
[10332]68      codeEditor.UserCode = Content.Code;
69    }
70
71    protected override void OnContentChanged() {
72      base.OnContentChanged();
73      if (Content == null) {
74        codeEditor.UserCode = string.Empty;
75      } else {
76        codeEditor.UserCode = Content.Code;
[10358]77        foreach (var asm in Content.GetAssemblies())
78          codeEditor.AddAssembly(asm);
[10332]79        if (Content.CompileErrors == null) {
80          compilationLabel.ForeColor = SystemColors.ControlDarkDark;
81          compilationLabel.Text = "Not compiled";
82        } else if (Content.CompileErrors.HasErrors) {
83          compilationLabel.ForeColor = Color.DarkRed;
84          compilationLabel.Text = "Compilation failed";
85        } else {
86          compilationLabel.ForeColor = Color.DarkGreen;
87          compilationLabel.Text = "Compilation successful";
88        }
89      }
90    }
91
92    protected override void SetEnabledStateOfControls() {
93      base.SetEnabledStateOfControls();
[10506]94      compileButton.Enabled = Content != null && !Locked && !ReadOnly;
[11594]95      codeEditor.Enabled = Content != null;
[10332]96    }
97
[10731]98    protected virtual void CompileButtonOnClick(object sender, EventArgs e) {
[10506]99      Compile();
100    }
101
[10731]102    protected virtual void CodeEditorOnTextEditorTextChanged(object sender, EventArgs e) {
[10761]103      if (Content == null) return;
[10332]104      Content.Code = codeEditor.UserCode;
105    }
106    protected override bool ProcessCmdKey(ref Message msg, Keys keyData) {
[10506]107      switch (keyData) {
108        case Keys.F6:
[10731]109          if (Content != null && !Locked)
110            Compile();
111          return true;
[10332]112      }
113      return base.ProcessCmdKey(ref msg, keyData);
114    }
115
[10731]116    public virtual bool Compile() {
[10332]117      ReadOnly = true;
[10358]118      Locked = true;
[10332]119      errorListView.Items.Clear();
[10506]120      outputTextBox.Text = "Compiling ... ";
[10332]121      try {
122        Content.Compile();
123        outputTextBox.AppendText("Compilation succeeded.");
124        return true;
125      } catch {
[10865]126        if (Content.CompileErrors.HasErrors) {
127          outputTextBox.AppendText("Compilation failed.");
128          return false;
129        } else {
130          outputTextBox.AppendText("Compilation succeeded.");
131          return true;
132        }
[10332]133      } finally {
[10506]134        ShowCompilationResults();
[10642]135        if (Content.CompileErrors.Count > 0)
136          infoTabControl.SelectedTab = errorListTabPage;
[10332]137        ReadOnly = false;
[10358]138        Locked = false;
[10506]139        OnContentChanged();
[10332]140      }
141    }
142
[10731]143    protected virtual void ShowCompilationResults() {
[10332]144      if (Content.CompileErrors.Count == 0) return;
[10506]145      var msgs = Content.CompileErrors.OfType<CompilerError>()
146                                      .OrderBy(x => x.IsWarning)
147                                      .ThenBy(x => x.Line)
148                                      .ThenBy(x => x.Column);
[10332]149      foreach (var m in msgs) {
[10506]150        var item = new ListViewItem();
151        item.SubItems.AddRange(new[] {
[10332]152          m.IsWarning ? "Warning" : "Error",
153          m.ErrorNumber,
[10506]154          m.Line.ToString(CultureInfo.InvariantCulture),
155          m.Column.ToString(CultureInfo.InvariantCulture),
[10332]156          m.ErrorText
157        });
[10506]158        item.ImageIndex = m.IsWarning ? 0 : 1;
[10332]159        errorListView.Items.Add(item);
160      }
[10506]161      AdjustErrorListViewColumnSizes();
[10332]162    }
[10506]163
[10731]164    protected virtual void AdjustErrorListViewColumnSizes() {
[10506]165      foreach (ColumnHeader ch in errorListView.Columns)
[10865]166        // adjusts the column width to the width of the column header
[10506]167        ch.Width = -2;
168    }
[10332]169  }
170}
Note: See TracBrowser for help on using the repository browser.