Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2205: Restructured solution and projects and switched all projects to .NET 4.5

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