Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2205:

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