Free cookie consent management tool by TermsFeed Policy Generator

source: branches/OptimizationNetworks/HeuristicLab.Networks.Views/3.3/Scripting/ProgrammableItemView.cs @ 15866

Last change on this file since 15866 was 13467, checked in by ascheibe, 9 years ago

#2205 fixed a bug in ProgrammableItemView introduced in r13459

File size: 6.2 KB
RevLine 
[11564]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;
[13459]27using System.Threading.Tasks;
[11564]28using System.Windows.Forms;
[13459]29using HeuristicLab.Common.Resources;
30using HeuristicLab.Core.Views;
31using HeuristicLab.MainForm;
32using HeuristicLab.PluginInfrastructure;
[11564]33
[11577]34namespace HeuristicLab.Scripting.Views {
[11564]35  [View("ProgrammableItem View")]
36  [Content(typeof(IProgrammableItem), false)]
37  public partial class ProgrammableItemView : ItemView {
38    public new IProgrammableItem Content {
39      get { return (IProgrammableItem)base.Content; }
40      set { base.Content = value; }
41    }
42
43    public ProgrammableItemView() {
44      InitializeComponent();
45      errorsListView.SmallImageList.Images.AddRange(new Image[] { VSImageLibrary.Warning, VSImageLibrary.Error });
46    }
47
48    protected override void DeregisterContentEvents() {
49      Content.CodeChanged -= Content_CodeChanged;
50      base.DeregisterContentEvents();
51    }
52
53    protected override void RegisterContentEvents() {
54      base.RegisterContentEvents();
55      Content.CodeChanged += Content_CodeChanged;
56    }
57
[13459]58    protected async override void OnContentChanged() {
[11564]59      base.OnContentChanged();
60      errorsListView.Items.Clear();
61      if (Content == null) {
62        codeEditor.UserCode = string.Empty;
[11602]63        variableStoreView.Content = null;
[11564]64      } else {
65        codeEditor.UserCode = Content.Code;
[13467]66        variableStoreView.Content = Content.VariableStore;
[13459]67        Locked = true;
68        Task t = Task.Factory.StartNew(() => {
69          foreach (var asm in Content.GetAssemblies())
70            codeEditor.AddAssembly(asm);
71        });
72        await t;
73        Locked = false;
[11564]74      }
75      ShowCompilationResults();
76      UpdateCompilationLabel();
77    }
78
79    protected override void SetEnabledStateOfControls() {
80      base.SetEnabledStateOfControls();
[11565]81      compileButton.Enabled = Content != null && Content.CanChangeCode && !Locked && !ReadOnly;
82      codeEditor.Enabled = Content != null && !Locked;
83      codeEditor.ReadOnly = ReadOnly || Locked || ((Content != null) && !Content.CanChangeCode);
[11602]84      variableStoreView.ReadOnly = (Content != null) && !Content.CanChangeCode;
[11564]85    }
86
87    protected override bool ProcessCmdKey(ref System.Windows.Forms.Message msg, Keys keyData) {
88      switch (keyData) {
89        case Keys.F6:
90          if (Content != null && !Locked)
91            Compile();
92          return true;
93      }
94      return base.ProcessCmdKey(ref msg, keyData);
95    }
96
97    protected virtual void Content_CodeChanged(object sender, EventArgs e) {
98      if (InvokeRequired)
99        Invoke(new EventHandler(Content_CodeChanged), sender, e);
100      else {
101        codeEditor.UserCode = Content.Code;
102      }
103    }
104
105    private void compileButton_Click(object sender, EventArgs e) {
106      Compile();
107    }
108    private void codeEditor_Validated(object sender, EventArgs e) {
[11565]109      if ((Content != null) && Content.CanChangeCode) Content.Code = codeEditor.UserCode;
[11564]110    }
111    private void errorsListView_DoubleClick(object sender, EventArgs e) {
112      if ((Content != null) && (Content.CompileErrors != null)) {
[12232]113        codeEditor.ShowCompileErrors(Content.CompileErrors);
[11564]114      }
115    }
116
117    #region Helpers
118    protected virtual void Compile() {
119      ReadOnly = true;
120      Locked = true;
121      errorsListView.Items.Clear();
122      try {
123        Content.Compile();
[13459]124      }
125      catch (Exception ex) {
[11564]126        if (!(ex is InvalidOperationException) || (Content.CompileErrors == null))
127          ErrorHandling.ShowErrorDialog(this, ex);
128      }
129      ShowCompilationResults();
130      UpdateCompilationLabel();
131      ReadOnly = false;
132      Locked = false;
133    }
134
135    protected virtual void ShowCompilationResults() {
136      if ((Content == null) || (Content.CompileErrors == null) || (Content.CompileErrors.Count == 0)) return;
137      var errors = Content.CompileErrors.OfType<CompilerError>()
138                                      .OrderBy(x => x.IsWarning)
139                                      .ThenBy(x => x.Line)
140                                      .ThenBy(x => x.Column);
141      foreach (var e in errors) {
142        var item = new ListViewItem();
143        item.SubItems.AddRange(new[] {
144          e.IsWarning ? "Warning" : "Error",
145          e.ErrorNumber,
146          e.Line.ToString(CultureInfo.InvariantCulture),
147          e.Column.ToString(CultureInfo.InvariantCulture),
148          e.ErrorText
149        });
150        item.ImageIndex = e.IsWarning ? 0 : 1;
151        errorsListView.Items.Add(item);
152      }
153
154      foreach (ColumnHeader ch in errorsListView.Columns) {
155        // adjusts column width to width of column header
156        ch.Width = -2;
157      }
158
[12232]159      codeEditor.ShowCompileErrors(Content.CompileErrors);
[11564]160    }
161
162    protected virtual void UpdateCompilationLabel() {
163      if ((Content == null) || (Content.CompileErrors == null)) {
164        compilationLabel.ForeColor = SystemColors.ControlDarkDark;
165        compilationLabel.Text = "Not compiled";
166      } else if (Content.CompileErrors.HasErrors) {
167        compilationLabel.ForeColor = Color.DarkRed;
168        compilationLabel.Text = "Compilation failed";
169      } else {
170        compilationLabel.ForeColor = Color.DarkGreen;
171        compilationLabel.Text = "Compilation successful";
172      }
173    }
174    #endregion
175  }
176}
Note: See TracBrowser for help on using the repository browser.