Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 11714 was 11602, checked in by swagner, 10 years ago

#2205: Continued working on programmable network items

  • added VariableStore to all programmable network items
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, ".cs");
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      }
119      catch (Exception ex) {
120        if (!(ex is InvalidOperationException) || (Content.CompileErrors == null))
121          ErrorHandling.ShowErrorDialog(this, ex);
122      }
123      ShowCompilationResults();
124      UpdateCompilationLabel();
125      ReadOnly = false;
126      Locked = false;
127    }
128
129    protected virtual void ShowCompilationResults() {
130      if ((Content == null) || (Content.CompileErrors == null) || (Content.CompileErrors.Count == 0)) return;
131      var errors = Content.CompileErrors.OfType<CompilerError>()
132                                      .OrderBy(x => x.IsWarning)
133                                      .ThenBy(x => x.Line)
134                                      .ThenBy(x => x.Column);
135      foreach (var e in errors) {
136        var item = new ListViewItem();
137        item.SubItems.AddRange(new[] {
138          e.IsWarning ? "Warning" : "Error",
139          e.ErrorNumber,
140          e.Line.ToString(CultureInfo.InvariantCulture),
141          e.Column.ToString(CultureInfo.InvariantCulture),
142          e.ErrorText
143        });
144        item.ImageIndex = e.IsWarning ? 0 : 1;
145        errorsListView.Items.Add(item);
146      }
147
148      foreach (ColumnHeader ch in errorsListView.Columns) {
149        // adjusts column width to width of column header
150        ch.Width = -2;
151      }
152
153      codeEditor.ShowCompileErrors(Content.CompileErrors, ".cs");
154    }
155
156    protected virtual void UpdateCompilationLabel() {
157      if ((Content == null) || (Content.CompileErrors == null)) {
158        compilationLabel.ForeColor = SystemColors.ControlDarkDark;
159        compilationLabel.Text = "Not compiled";
160      } else if (Content.CompileErrors.HasErrors) {
161        compilationLabel.ForeColor = Color.DarkRed;
162        compilationLabel.Text = "Compilation failed";
163      } else {
164        compilationLabel.ForeColor = Color.DarkGreen;
165        compilationLabel.Text = "Compilation successful";
166      }
167    }
168    #endregion
169  }
170}
Note: See TracBrowser for help on using the repository browser.