[10332] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[11170] | 3 | * Copyright (C) 2002-2014 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[10332] | 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 |
|
---|
| 22 | using System;
|
---|
| 23 | using System.CodeDom.Compiler;
|
---|
| 24 | using System.Drawing;
|
---|
[10506] | 25 | using System.Globalization;
|
---|
[10332] | 26 | using System.Linq;
|
---|
| 27 | using System.Windows.Forms;
|
---|
| 28 | using HeuristicLab.Common.Resources;
|
---|
| 29 | using HeuristicLab.Core.Views;
|
---|
| 30 | using HeuristicLab.MainForm;
|
---|
| 31 |
|
---|
[10506] | 32 | namespace HeuristicLab.Scripting.Views {
|
---|
[10332] | 33 |
|
---|
[10401] | 34 | [View("Script View")]
|
---|
| 35 | [Content(typeof(Script), true)]
|
---|
| 36 | public partial class ScriptView : NamedItemView {
|
---|
| 37 | public new Script Content {
|
---|
| 38 | get { return (Script)base.Content; }
|
---|
[10892] | 39 | set { base.Content = value; }
|
---|
[10332] | 40 | }
|
---|
| 41 |
|
---|
[10401] | 42 | public ScriptView() {
|
---|
[10332] | 43 | InitializeComponent();
|
---|
[10506] | 44 | errorListView.SmallImageList.Images.AddRange(new Image[] { VSImageLibrary.Warning, VSImageLibrary.Error });
|
---|
[10332] | 45 | }
|
---|
| 46 |
|
---|
| 47 | protected override void RegisterContentEvents() {
|
---|
| 48 | base.RegisterContentEvents();
|
---|
[10892] | 49 | Content.CodeChanged += ContentOnCodeChanged;
|
---|
[10332] | 50 | }
|
---|
| 51 |
|
---|
| 52 | protected override void DeregisterContentEvents() {
|
---|
[10892] | 53 | Content.CodeChanged -= ContentOnCodeChanged;
|
---|
[10332] | 54 | base.DeregisterContentEvents();
|
---|
| 55 | }
|
---|
| 56 |
|
---|
[10892] | 57 | protected virtual void ContentOnCodeChanged(object sender, EventArgs e) {
|
---|
[10332] | 58 | codeEditor.UserCode = Content.Code;
|
---|
| 59 | }
|
---|
| 60 |
|
---|
| 61 | protected override void OnContentChanged() {
|
---|
| 62 | base.OnContentChanged();
|
---|
| 63 | if (Content == null) {
|
---|
| 64 | codeEditor.UserCode = string.Empty;
|
---|
| 65 | } else {
|
---|
| 66 | codeEditor.UserCode = Content.Code;
|
---|
[10358] | 67 | foreach (var asm in Content.GetAssemblies())
|
---|
| 68 | codeEditor.AddAssembly(asm);
|
---|
[10332] | 69 | if (Content.CompileErrors == null) {
|
---|
| 70 | compilationLabel.ForeColor = SystemColors.ControlDarkDark;
|
---|
| 71 | compilationLabel.Text = "Not compiled";
|
---|
| 72 | } else if (Content.CompileErrors.HasErrors) {
|
---|
| 73 | compilationLabel.ForeColor = Color.DarkRed;
|
---|
| 74 | compilationLabel.Text = "Compilation failed";
|
---|
| 75 | } else {
|
---|
| 76 | compilationLabel.ForeColor = Color.DarkGreen;
|
---|
| 77 | compilationLabel.Text = "Compilation successful";
|
---|
| 78 | }
|
---|
| 79 | }
|
---|
| 80 | }
|
---|
| 81 |
|
---|
| 82 | protected override void SetEnabledStateOfControls() {
|
---|
| 83 | base.SetEnabledStateOfControls();
|
---|
[10506] | 84 | compileButton.Enabled = Content != null && !Locked && !ReadOnly;
|
---|
[10358] | 85 | codeEditor.Enabled = Content != null && !Locked && !ReadOnly;
|
---|
[10332] | 86 | }
|
---|
| 87 |
|
---|
[10892] | 88 | protected virtual void CompileButtonOnClick(object sender, EventArgs e) {
|
---|
[10506] | 89 | Compile();
|
---|
| 90 | }
|
---|
| 91 |
|
---|
[10892] | 92 | protected virtual void CodeEditorOnTextEditorTextChanged(object sender, EventArgs e) {
|
---|
| 93 | if (Content == null) return;
|
---|
[10332] | 94 | Content.Code = codeEditor.UserCode;
|
---|
| 95 | }
|
---|
| 96 | protected override bool ProcessCmdKey(ref Message msg, Keys keyData) {
|
---|
[10506] | 97 | switch (keyData) {
|
---|
| 98 | case Keys.F6:
|
---|
[10892] | 99 | if (Content != null && !Locked)
|
---|
| 100 | Compile();
|
---|
| 101 | return true;
|
---|
[10332] | 102 | }
|
---|
| 103 | return base.ProcessCmdKey(ref msg, keyData);
|
---|
| 104 | }
|
---|
| 105 |
|
---|
[10892] | 106 | public virtual bool Compile() {
|
---|
[10332] | 107 | ReadOnly = true;
|
---|
[10358] | 108 | Locked = true;
|
---|
[10332] | 109 | errorListView.Items.Clear();
|
---|
[10506] | 110 | outputTextBox.Text = "Compiling ... ";
|
---|
[10332] | 111 | try {
|
---|
| 112 | Content.Compile();
|
---|
| 113 | outputTextBox.AppendText("Compilation succeeded.");
|
---|
| 114 | return true;
|
---|
| 115 | } catch {
|
---|
[10892] | 116 | if (Content.CompileErrors.HasErrors) {
|
---|
| 117 | outputTextBox.AppendText("Compilation failed.");
|
---|
| 118 | return false;
|
---|
| 119 | } else {
|
---|
| 120 | outputTextBox.AppendText("Compilation succeeded.");
|
---|
| 121 | return true;
|
---|
| 122 | }
|
---|
[10332] | 123 | } finally {
|
---|
[10506] | 124 | ShowCompilationResults();
|
---|
[10892] | 125 | if (Content.CompileErrors.Count > 0)
|
---|
| 126 | infoTabControl.SelectedTab = errorListTabPage;
|
---|
[10332] | 127 | ReadOnly = false;
|
---|
[10358] | 128 | Locked = false;
|
---|
[10506] | 129 | OnContentChanged();
|
---|
[10332] | 130 | }
|
---|
| 131 | }
|
---|
| 132 |
|
---|
[10892] | 133 | protected virtual void ShowCompilationResults() {
|
---|
[10332] | 134 | if (Content.CompileErrors.Count == 0) return;
|
---|
[10506] | 135 | var msgs = Content.CompileErrors.OfType<CompilerError>()
|
---|
| 136 | .OrderBy(x => x.IsWarning)
|
---|
| 137 | .ThenBy(x => x.Line)
|
---|
| 138 | .ThenBy(x => x.Column);
|
---|
[10332] | 139 | foreach (var m in msgs) {
|
---|
[10506] | 140 | var item = new ListViewItem();
|
---|
| 141 | item.SubItems.AddRange(new[] {
|
---|
[10332] | 142 | m.IsWarning ? "Warning" : "Error",
|
---|
| 143 | m.ErrorNumber,
|
---|
[10506] | 144 | m.Line.ToString(CultureInfo.InvariantCulture),
|
---|
| 145 | m.Column.ToString(CultureInfo.InvariantCulture),
|
---|
[10332] | 146 | m.ErrorText
|
---|
| 147 | });
|
---|
[10506] | 148 | item.ImageIndex = m.IsWarning ? 0 : 1;
|
---|
[10332] | 149 | errorListView.Items.Add(item);
|
---|
| 150 | }
|
---|
[10506] | 151 | AdjustErrorListViewColumnSizes();
|
---|
[10332] | 152 | }
|
---|
[10506] | 153 |
|
---|
[10892] | 154 | protected virtual void AdjustErrorListViewColumnSizes() {
|
---|
[10506] | 155 | foreach (ColumnHeader ch in errorListView.Columns)
|
---|
[10892] | 156 | // adjusts the column width to the width of the column header
|
---|
[10506] | 157 | ch.Width = -2;
|
---|
| 158 | }
|
---|
[10332] | 159 | }
|
---|
| 160 | } |
---|