Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Scripting.Views/3.3/ScriptView.cs @ 17035

Last change on this file since 17035 was 16832, checked in by gkronber, 5 years ago

#2967: merged r16528 from trunk to stable

File size: 8.9 KB
RevLine 
[10332]1#region License Information
2/* HeuristicLab
[15584]3 * Copyright (C) 2002-2018 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;
[11937]24using System.Collections.Generic;
[10332]25using System.Drawing;
[10506]26using System.Globalization;
[10332]27using System.Linq;
[11937]28using System.Reflection;
[10332]29using System.Windows.Forms;
[11937]30using HeuristicLab.Common;
[10332]31using HeuristicLab.Common.Resources;
32using HeuristicLab.Core.Views;
33using HeuristicLab.MainForm;
34
[10506]35namespace HeuristicLab.Scripting.Views {
[10332]36
[10401]37  [View("Script View")]
38  [Content(typeof(Script), true)]
39  public partial class ScriptView : NamedItemView {
[11937]40    private const string NotCompiledMessage = "Not compiled";
41    private const string CompilationSucceededMessage = "Compilation succeeded";
42    private const string CompilationFailedMessage = "Compilation failed";
43    private const string AssembliesLoadingMessage = "Loading Assemblies";
44    private const string AssembliesUnloadingMessage = "Unloading Assemblies";
45    private const int SilentAssemblyLoadingOperationLimit = 10;
46
[11907]47    #region Properties
[10401]48    public new Script Content {
49      get { return (Script)base.Content; }
[10892]50      set { base.Content = value; }
[10332]51    }
52
[11907]53    public override bool ReadOnly {
54      get { return codeEditor.ReadOnly || base.ReadOnly; }
55      set { base.ReadOnly = codeEditor.ReadOnly = value; }
56    }
57
58    public override bool Locked {
59      get { return codeEditor.ReadOnly || base.Locked; }
60      set { base.Locked = codeEditor.ReadOnly = value; }
61    }
62    #endregion
63
[10401]64    public ScriptView() {
[10332]65      InitializeComponent();
[10506]66      errorListView.SmallImageList.Images.AddRange(new Image[] { VSImageLibrary.Warning, VSImageLibrary.Error });
[10332]67    }
68
69    protected override void RegisterContentEvents() {
70      base.RegisterContentEvents();
[11907]71      Content.CodeChanged += Content_CodeChanged;
[10332]72    }
73
74    protected override void DeregisterContentEvents() {
[11907]75      Content.CodeChanged -= Content_CodeChanged;
[10332]76      base.DeregisterContentEvents();
77    }
78
[11907]79    #region Overrides
[10332]80    protected override void OnContentChanged() {
81      base.OnContentChanged();
82      if (Content == null) {
83        codeEditor.UserCode = string.Empty;
[16832]84        codeEditor.ClearEditHistory();
[10332]85      } else {
[11937]86        codeEditor.UserCode = Content.Code;
[16832]87        codeEditor.ClearEditHistory();
[11937]88        codeEditor.AddAssembliesAsync(Content.GetAssemblies());
[10332]89        if (Content.CompileErrors == null) {
[11937]90          UpdateInfoTextLabel(NotCompiledMessage, SystemColors.ControlText);
[10332]91        }
92      }
93    }
94
95    protected override void SetEnabledStateOfControls() {
96      base.SetEnabledStateOfControls();
[10506]97      compileButton.Enabled = Content != null && !Locked && !ReadOnly;
[11907]98      codeEditor.Enabled = Content != null;
[10332]99    }
100
101    protected override bool ProcessCmdKey(ref Message msg, Keys keyData) {
[10506]102      switch (keyData) {
103        case Keys.F6:
[10892]104          if (Content != null && !Locked)
105            Compile();
106          return true;
[10332]107      }
108      return base.ProcessCmdKey(ref msg, keyData);
109    }
[11907]110    #endregion
[10332]111
[10892]112    public virtual bool Compile() {
[10332]113      ReadOnly = true;
[10358]114      Locked = true;
[10332]115      errorListView.Items.Clear();
[10506]116      outputTextBox.Text = "Compiling ... ";
[10332]117      try {
118        Content.Compile();
[11937]119        outputTextBox.AppendText(CompilationSucceededMessage);
120        UpdateInfoTextLabel(CompilationSucceededMessage, Color.DarkGreen);
[10332]121        return true;
[11908]122      } catch (CompilationException) {
[10892]123        if (Content.CompileErrors.HasErrors) {
[11937]124          outputTextBox.AppendText(CompilationFailedMessage);
125          UpdateInfoTextLabel(CompilationFailedMessage, Color.DarkRed);
[10892]126          return false;
127        } else {
[11937]128          outputTextBox.AppendText(CompilationSucceededMessage);
129          UpdateInfoTextLabel(CompilationSucceededMessage, Color.DarkGreen);
[10892]130          return true;
131        }
[10332]132      } finally {
[10506]133        ShowCompilationResults();
[10892]134        if (Content.CompileErrors.Count > 0)
135          infoTabControl.SelectedTab = errorListTabPage;
[10332]136        ReadOnly = false;
[10358]137        Locked = false;
[11907]138        codeEditor.Focus();
[10332]139      }
140    }
141
[11907]142    #region Helpers
[10892]143    protected virtual void ShowCompilationResults() {
[11907]144      var messages = Content.CompileErrors.OfType<CompilerError>()
[10506]145                                      .OrderBy(x => x.IsWarning)
146                                      .ThenBy(x => x.Line)
147                                      .ThenBy(x => x.Column);
[11907]148
149      foreach (var m in messages) {
150        var item = new ListViewItem { Tag = m };
[10506]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      }
[11907]161
[11937]162      codeEditor.ShowCompileErrors(Content.CompileErrors);
[11907]163
[10506]164      AdjustErrorListViewColumnSizes();
[10332]165    }
[10506]166
[11937]167    protected virtual void UpdateInfoTextLabel(string message, Color color) {
168      infoTextLabel.Text = message;
169      infoTextLabel.ForeColor = color;
170    }
171
[10892]172    protected virtual void AdjustErrorListViewColumnSizes() {
[10506]173      foreach (ColumnHeader ch in errorListView.Columns)
[10892]174        // adjusts the column width to the width of the column header
[10506]175        ch.Width = -2;
176    }
[11937]177
178    #region ProgressView
179    private bool progressViewCreated;
180
181    private void AddProgressView(string progressMessage) {
182      var mainForm = MainFormManager.GetMainForm<MainForm.WindowsForms.MainForm>();
183      mainForm.AddOperationProgressToView(this, progressMessage);
184      progressViewCreated = true;
185    }
186
187    private void RemoveProgressView() {
188      if (!progressViewCreated) return;
189      var mainForm = MainFormManager.GetMainForm<MainForm.WindowsForms.MainForm>();
190      mainForm.RemoveOperationProgressFromView(this);
191      progressViewCreated = false;
192    }
[11907]193    #endregion
[11937]194    #endregion
[11907]195
196    #region Event Handlers
[11953]197    protected virtual void Content_CodeChanged(object sender, EventArgs e) {
[11937]198      if (InvokeRequired) Invoke((Action<object, EventArgs>)Content_CodeChanged, sender, e);
[11907]199      else {
200        codeEditor.UserCode = Content.Code;
201      }
202    }
203
204    private void compileButton_Click(object sender, EventArgs e) {
205      Compile();
206    }
207
208    private void codeEditor_TextEditorTextChanged(object sender, EventArgs e) {
209      if (Content == null) return;
210      Content.Code = codeEditor.UserCode;
211    }
212
213    private void errorListView_MouseDoubleClick(object sender, MouseEventArgs e) {
214      if (e.Button == MouseButtons.Left) {
215        var item = errorListView.SelectedItems[0];
216        var message = (CompilerError)item.Tag;
217        codeEditor.ScrollToPosition(message.Line, message.Column);
218        codeEditor.Focus();
219      }
220    }
221    #endregion
[11937]222
223    private void codeEditor_AssembliesLoading(object sender, EventArgs<IEnumerable<Assembly>> e) {
224      if (InvokeRequired) Invoke((Action<object, EventArgs<IEnumerable<Assembly>>>)codeEditor_AssembliesLoading, sender, e);
225      else {
226        int nrOfAssemblies = e.Value.Count();
227        if (nrOfAssemblies > SilentAssemblyLoadingOperationLimit)
228          AddProgressView(AssembliesLoadingMessage);
229      }
230    }
231
232    private void codeEditor_AssembliesLoaded(object sender, EventArgs<IEnumerable<Assembly>> e) {
233      if (InvokeRequired) Invoke((Action<object, EventArgs<IEnumerable<Assembly>>>)codeEditor_AssembliesLoaded, sender, e);
234      else {
235        RemoveProgressView();
236      }
237    }
238
239    private void codeEditor_AssembliesUnloading(object sender, EventArgs<IEnumerable<Assembly>> e) {
240      if (InvokeRequired) Invoke((Action<object, EventArgs<IEnumerable<Assembly>>>)codeEditor_AssembliesUnloading, sender, e);
241      else {
242        int nrOfAssemblies = e.Value.Count();
243        if (nrOfAssemblies > SilentAssemblyLoadingOperationLimit)
244          AddProgressView(AssembliesUnloadingMessage);
245      }
246    }
247
248    private void codeEditor_AssembliesUnloaded(object sender, EventArgs<IEnumerable<Assembly>> e) {
249      if (InvokeRequired) Invoke((Action<object, EventArgs<IEnumerable<Assembly>>>)codeEditor_AssembliesUnloaded, sender, e);
250      else {
251        RemoveProgressView();
252      }
253    }
[10332]254  }
255}
Note: See TracBrowser for help on using the repository browser.