Free cookie consent management tool by TermsFeed Policy Generator

source: branches/OptimizationNetworks/HeuristicLab.Optimization.Networks.Views/3.3/Core.Views/ProgrammableItemView.cs @ 11564

Last change on this file since 11564 was 11564, checked in by swagner, 9 years ago

#2205: Continued working on programmable network items

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 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.Core.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      } else {
63        codeEditor.UserCode = Content.Code;
64        foreach (var asm in Content.GetAssemblies())
65          codeEditor.AddAssembly(asm);
66      }
67      ShowCompilationResults();
68      UpdateCompilationLabel();
69    }
70
71    protected override void SetEnabledStateOfControls() {
72      base.SetEnabledStateOfControls();
73      compileButton.Enabled = Content != null && !Locked && !ReadOnly;
74      codeEditor.Enabled = Content != null;
75    }
76
77    protected override bool ProcessCmdKey(ref System.Windows.Forms.Message msg, Keys keyData) {
78      switch (keyData) {
79        case Keys.F6:
80          if (Content != null && !Locked)
81            Compile();
82          return true;
83      }
84      return base.ProcessCmdKey(ref msg, keyData);
85    }
86
87    protected virtual void Content_CodeChanged(object sender, EventArgs e) {
88      if (InvokeRequired)
89        Invoke(new EventHandler(Content_CodeChanged), sender, e);
90      else {
91        codeEditor.UserCode = Content.Code;
92      }
93    }
94
95    private void compileButton_Click(object sender, EventArgs e) {
96      Compile();
97    }
98    private void codeEditor_Validated(object sender, EventArgs e) {
99      if (Content != null) Content.Code = codeEditor.UserCode;
100    }
101    private void errorsListView_DoubleClick(object sender, EventArgs e) {
102      if ((Content != null) && (Content.CompileErrors != null)) {
103        codeEditor.ShowCompileErrors(Content.CompileErrors, ".cs");
104      }
105    }
106
107    #region Helpers
108    protected virtual void Compile() {
109      ReadOnly = true;
110      Locked = true;
111      errorsListView.Items.Clear();
112      try {
113        Content.Compile();
114      }
115      catch (Exception ex) {
116        if (!(ex is InvalidOperationException) || (Content.CompileErrors == null))
117          ErrorHandling.ShowErrorDialog(this, ex);
118      }
119      ShowCompilationResults();
120      UpdateCompilationLabel();
121      ReadOnly = false;
122      Locked = false;
123    }
124
125    protected virtual void ShowCompilationResults() {
126      if ((Content == null) || (Content.CompileErrors == null) || (Content.CompileErrors.Count == 0)) return;
127      var errors = Content.CompileErrors.OfType<CompilerError>()
128                                      .OrderBy(x => x.IsWarning)
129                                      .ThenBy(x => x.Line)
130                                      .ThenBy(x => x.Column);
131      foreach (var e in errors) {
132        var item = new ListViewItem();
133        item.SubItems.AddRange(new[] {
134          e.IsWarning ? "Warning" : "Error",
135          e.ErrorNumber,
136          e.Line.ToString(CultureInfo.InvariantCulture),
137          e.Column.ToString(CultureInfo.InvariantCulture),
138          e.ErrorText
139        });
140        item.ImageIndex = e.IsWarning ? 0 : 1;
141        errorsListView.Items.Add(item);
142      }
143
144      foreach (ColumnHeader ch in errorsListView.Columns) {
145        // adjusts column width to width of column header
146        ch.Width = -2;
147      }
148
149      codeEditor.ShowCompileErrors(Content.CompileErrors, ".cs");
150    }
151
152    protected virtual void UpdateCompilationLabel() {
153      if ((Content == null) || (Content.CompileErrors == null)) {
154        compilationLabel.ForeColor = SystemColors.ControlDarkDark;
155        compilationLabel.Text = "Not compiled";
156      } else if (Content.CompileErrors.HasErrors) {
157        compilationLabel.ForeColor = Color.DarkRed;
158        compilationLabel.Text = "Compilation failed";
159      } else {
160        compilationLabel.ForeColor = Color.DarkGreen;
161        compilationLabel.Text = "Compilation successful";
162      }
163    }
164    #endregion
165  }
166}
Note: See TracBrowser for help on using the repository browser.