Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2262:

  • worked on variable management of VariableStoreView
  • fixed ReadOnly and Locked state changes in ScriptView
  • minor code improvements
File size: 5.7 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2014 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    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
57    protected override void RegisterContentEvents() {
58      base.RegisterContentEvents();
59      Content.CodeChanged += ContentOnCodeChanged;
60    }
61
62    protected override void DeregisterContentEvents() {
63      Content.CodeChanged -= ContentOnCodeChanged;
64      base.DeregisterContentEvents();
65    }
66
67    protected virtual void ContentOnCodeChanged(object sender, EventArgs e) {
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;
77        foreach (var asm in Content.GetAssemblies())
78          codeEditor.AddAssembly(asm);
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();
94      compileButton.Enabled = Content != null && !Locked && !ReadOnly;
95      codeEditor.Enabled = Content != null;
96    }
97
98    protected virtual void CompileButtonOnClick(object sender, EventArgs e) {
99      Compile();
100    }
101
102    protected virtual void CodeEditorOnTextEditorTextChanged(object sender, EventArgs e) {
103      if (Content == null) return;
104      Content.Code = codeEditor.UserCode;
105    }
106    protected override bool ProcessCmdKey(ref Message msg, Keys keyData) {
107      switch (keyData) {
108        case Keys.F6:
109          if (Content != null && !Locked)
110            Compile();
111          return true;
112      }
113      return base.ProcessCmdKey(ref msg, keyData);
114    }
115
116    public virtual bool Compile() {
117      ReadOnly = true;
118      Locked = true;
119      errorListView.Items.Clear();
120      outputTextBox.Text = "Compiling ... ";
121      try {
122        Content.Compile();
123        outputTextBox.AppendText("Compilation succeeded.");
124        return true;
125      } catch {
126        if (Content.CompileErrors.HasErrors) {
127          outputTextBox.AppendText("Compilation failed.");
128          return false;
129        } else {
130          outputTextBox.AppendText("Compilation succeeded.");
131          return true;
132        }
133      } finally {
134        ShowCompilationResults();
135        if (Content.CompileErrors.Count > 0)
136          infoTabControl.SelectedTab = errorListTabPage;
137        ReadOnly = false;
138        Locked = false;
139        OnContentChanged();
140      }
141    }
142
143    protected virtual void ShowCompilationResults() {
144      if (Content.CompileErrors.Count == 0) return;
145      var msgs = Content.CompileErrors.OfType<CompilerError>()
146                                      .OrderBy(x => x.IsWarning)
147                                      .ThenBy(x => x.Line)
148                                      .ThenBy(x => x.Column);
149      foreach (var m in msgs) {
150        var item = new ListViewItem();
151        item.SubItems.AddRange(new[] {
152          m.IsWarning ? "Warning" : "Error",
153          m.ErrorNumber,
154          m.Line.ToString(CultureInfo.InvariantCulture),
155          m.Column.ToString(CultureInfo.InvariantCulture),
156          m.ErrorText
157        });
158        item.ImageIndex = m.IsWarning ? 0 : 1;
159        errorListView.Items.Add(item);
160      }
161      AdjustErrorListViewColumnSizes();
162    }
163
164    protected virtual void AdjustErrorListViewColumnSizes() {
165      foreach (ColumnHeader ch in errorListView.Columns)
166        // adjusts the column width to the width of the column header
167        ch.Width = -2;
168    }
169  }
170}
Note: See TracBrowser for help on using the repository browser.