[10332] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[16140] | 3 | * Copyright (C) 2002-2018 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;
|
---|
[11807] | 24 | using System.Collections.Generic;
|
---|
[10332] | 25 | using System.Drawing;
|
---|
[10506] | 26 | using System.Globalization;
|
---|
[10332] | 27 | using System.Linq;
|
---|
[11807] | 28 | using System.Reflection;
|
---|
[10332] | 29 | using System.Windows.Forms;
|
---|
[11807] | 30 | using HeuristicLab.Common;
|
---|
[10332] | 31 | using HeuristicLab.Common.Resources;
|
---|
| 32 | using HeuristicLab.Core.Views;
|
---|
| 33 | using HeuristicLab.MainForm;
|
---|
| 34 |
|
---|
[10506] | 35 | namespace HeuristicLab.Scripting.Views {
|
---|
[10332] | 36 |
|
---|
[10401] | 37 | [View("Script View")]
|
---|
| 38 | [Content(typeof(Script), true)]
|
---|
| 39 | public partial class ScriptView : NamedItemView {
|
---|
[11819] | 40 | private const string NotCompiledMessage = "Not compiled";
|
---|
| 41 | private const string CompilationSucceededMessage = "Compilation succeeded";
|
---|
| 42 | private const string CompilationFailedMessage = "Compilation failed";
|
---|
[11807] | 43 | private const string AssembliesLoadingMessage = "Loading Assemblies";
|
---|
| 44 | private const string AssembliesUnloadingMessage = "Unloading Assemblies";
|
---|
| 45 | private const int SilentAssemblyLoadingOperationLimit = 10;
|
---|
| 46 |
|
---|
[11657] | 47 | #region Properties
|
---|
[10401] | 48 | public new Script Content {
|
---|
| 49 | get { return (Script)base.Content; }
|
---|
[10731] | 50 | set { base.Content = value; }
|
---|
[10332] | 51 | }
|
---|
| 52 |
|
---|
[11436] | 53 | public override bool ReadOnly {
|
---|
[11480] | 54 | get { return codeEditor.ReadOnly || base.ReadOnly; }
|
---|
| 55 | set { base.ReadOnly = codeEditor.ReadOnly = value; }
|
---|
[11436] | 56 | }
|
---|
| 57 |
|
---|
| 58 | public override bool Locked {
|
---|
[11480] | 59 | get { return codeEditor.ReadOnly || base.Locked; }
|
---|
| 60 | set { base.Locked = codeEditor.ReadOnly = value; }
|
---|
[11436] | 61 | }
|
---|
[11657] | 62 | #endregion
|
---|
[11436] | 63 |
|
---|
[11657] | 64 | public ScriptView() {
|
---|
| 65 | InitializeComponent();
|
---|
| 66 | errorListView.SmallImageList.Images.AddRange(new Image[] { VSImageLibrary.Warning, VSImageLibrary.Error });
|
---|
| 67 | }
|
---|
| 68 |
|
---|
[10332] | 69 | protected override void RegisterContentEvents() {
|
---|
| 70 | base.RegisterContentEvents();
|
---|
[11657] | 71 | Content.CodeChanged += Content_CodeChanged;
|
---|
[10332] | 72 | }
|
---|
| 73 |
|
---|
| 74 | protected override void DeregisterContentEvents() {
|
---|
[11657] | 75 | Content.CodeChanged -= Content_CodeChanged;
|
---|
[10332] | 76 | base.DeregisterContentEvents();
|
---|
| 77 | }
|
---|
| 78 |
|
---|
[11657] | 79 | #region Overrides
|
---|
[10332] | 80 | protected override void OnContentChanged() {
|
---|
| 81 | base.OnContentChanged();
|
---|
| 82 | if (Content == null) {
|
---|
| 83 | codeEditor.UserCode = string.Empty;
|
---|
| 84 | } else {
|
---|
[11807] | 85 | codeEditor.UserCode = Content.Code;
|
---|
| 86 | codeEditor.AddAssembliesAsync(Content.GetAssemblies());
|
---|
[10332] | 87 | if (Content.CompileErrors == null) {
|
---|
[11836] | 88 | UpdateInfoTextLabel(NotCompiledMessage, SystemColors.ControlText);
|
---|
[10332] | 89 | }
|
---|
| 90 | }
|
---|
| 91 | }
|
---|
| 92 |
|
---|
| 93 | protected override void SetEnabledStateOfControls() {
|
---|
| 94 | base.SetEnabledStateOfControls();
|
---|
[10506] | 95 | compileButton.Enabled = Content != null && !Locked && !ReadOnly;
|
---|
[11436] | 96 | codeEditor.Enabled = Content != null;
|
---|
[10332] | 97 | }
|
---|
| 98 |
|
---|
| 99 | protected override bool ProcessCmdKey(ref Message msg, Keys keyData) {
|
---|
[10506] | 100 | switch (keyData) {
|
---|
| 101 | case Keys.F6:
|
---|
[10731] | 102 | if (Content != null && !Locked)
|
---|
| 103 | Compile();
|
---|
| 104 | return true;
|
---|
[10332] | 105 | }
|
---|
| 106 | return base.ProcessCmdKey(ref msg, keyData);
|
---|
| 107 | }
|
---|
[11657] | 108 | #endregion
|
---|
[10332] | 109 |
|
---|
[10731] | 110 | public virtual bool Compile() {
|
---|
[10332] | 111 | ReadOnly = true;
|
---|
[10358] | 112 | Locked = true;
|
---|
[10332] | 113 | errorListView.Items.Clear();
|
---|
[10506] | 114 | outputTextBox.Text = "Compiling ... ";
|
---|
[10332] | 115 | try {
|
---|
| 116 | Content.Compile();
|
---|
[11819] | 117 | outputTextBox.AppendText(CompilationSucceededMessage);
|
---|
[11836] | 118 | UpdateInfoTextLabel(CompilationSucceededMessage, Color.DarkGreen);
|
---|
[10332] | 119 | return true;
|
---|
[11882] | 120 | } catch (CompilationException) {
|
---|
[10865] | 121 | if (Content.CompileErrors.HasErrors) {
|
---|
[11819] | 122 | outputTextBox.AppendText(CompilationFailedMessage);
|
---|
[11836] | 123 | UpdateInfoTextLabel(CompilationFailedMessage, Color.DarkRed);
|
---|
[10865] | 124 | return false;
|
---|
| 125 | } else {
|
---|
[11819] | 126 | outputTextBox.AppendText(CompilationSucceededMessage);
|
---|
[11836] | 127 | UpdateInfoTextLabel(CompilationSucceededMessage, Color.DarkGreen);
|
---|
[10865] | 128 | return true;
|
---|
| 129 | }
|
---|
[10332] | 130 | } finally {
|
---|
[10506] | 131 | ShowCompilationResults();
|
---|
[10642] | 132 | if (Content.CompileErrors.Count > 0)
|
---|
| 133 | infoTabControl.SelectedTab = errorListTabPage;
|
---|
[10332] | 134 | ReadOnly = false;
|
---|
[10358] | 135 | Locked = false;
|
---|
[11657] | 136 | codeEditor.Focus();
|
---|
[10332] | 137 | }
|
---|
| 138 | }
|
---|
| 139 |
|
---|
[11657] | 140 | #region Helpers
|
---|
[10731] | 141 | protected virtual void ShowCompilationResults() {
|
---|
[11657] | 142 | var messages = Content.CompileErrors.OfType<CompilerError>()
|
---|
[10506] | 143 | .OrderBy(x => x.IsWarning)
|
---|
| 144 | .ThenBy(x => x.Line)
|
---|
| 145 | .ThenBy(x => x.Column);
|
---|
[11657] | 146 |
|
---|
| 147 | foreach (var m in messages) {
|
---|
| 148 | var item = new ListViewItem { Tag = m };
|
---|
[10506] | 149 | item.SubItems.AddRange(new[] {
|
---|
[10332] | 150 | m.IsWarning ? "Warning" : "Error",
|
---|
| 151 | m.ErrorNumber,
|
---|
[10506] | 152 | m.Line.ToString(CultureInfo.InvariantCulture),
|
---|
| 153 | m.Column.ToString(CultureInfo.InvariantCulture),
|
---|
[10332] | 154 | m.ErrorText
|
---|
| 155 | });
|
---|
[10506] | 156 | item.ImageIndex = m.IsWarning ? 0 : 1;
|
---|
[10332] | 157 | errorListView.Items.Add(item);
|
---|
| 158 | }
|
---|
[11657] | 159 |
|
---|
[11807] | 160 | codeEditor.ShowCompileErrors(Content.CompileErrors);
|
---|
[11657] | 161 |
|
---|
[10506] | 162 | AdjustErrorListViewColumnSizes();
|
---|
[10332] | 163 | }
|
---|
[10506] | 164 |
|
---|
[11836] | 165 | protected virtual void UpdateInfoTextLabel(string message, Color color) {
|
---|
| 166 | infoTextLabel.Text = message;
|
---|
| 167 | infoTextLabel.ForeColor = color;
|
---|
| 168 | }
|
---|
| 169 |
|
---|
[10731] | 170 | protected virtual void AdjustErrorListViewColumnSizes() {
|
---|
[10506] | 171 | foreach (ColumnHeader ch in errorListView.Columns)
|
---|
[10865] | 172 | // adjusts the column width to the width of the column header
|
---|
[10506] | 173 | ch.Width = -2;
|
---|
| 174 | }
|
---|
[11807] | 175 |
|
---|
| 176 | #region ProgressView
|
---|
| 177 | private bool progressViewCreated;
|
---|
| 178 |
|
---|
| 179 | private void AddProgressView(string progressMessage) {
|
---|
| 180 | var mainForm = MainFormManager.GetMainForm<MainForm.WindowsForms.MainForm>();
|
---|
| 181 | mainForm.AddOperationProgressToView(this, progressMessage);
|
---|
| 182 | progressViewCreated = true;
|
---|
| 183 | }
|
---|
| 184 |
|
---|
| 185 | private void RemoveProgressView() {
|
---|
| 186 | if (!progressViewCreated) return;
|
---|
| 187 | var mainForm = MainFormManager.GetMainForm<MainForm.WindowsForms.MainForm>();
|
---|
| 188 | mainForm.RemoveOperationProgressFromView(this);
|
---|
| 189 | progressViewCreated = false;
|
---|
| 190 | }
|
---|
[11657] | 191 | #endregion
|
---|
[11807] | 192 | #endregion
|
---|
[11657] | 193 |
|
---|
| 194 | #region Event Handlers
|
---|
[11942] | 195 | protected virtual void Content_CodeChanged(object sender, EventArgs e) {
|
---|
[11807] | 196 | if (InvokeRequired) Invoke((Action<object, EventArgs>)Content_CodeChanged, sender, e);
|
---|
[11657] | 197 | else {
|
---|
| 198 | codeEditor.UserCode = Content.Code;
|
---|
| 199 | }
|
---|
| 200 | }
|
---|
| 201 |
|
---|
| 202 | private void compileButton_Click(object sender, EventArgs e) {
|
---|
| 203 | Compile();
|
---|
| 204 | }
|
---|
| 205 |
|
---|
| 206 | private void codeEditor_TextEditorTextChanged(object sender, EventArgs e) {
|
---|
| 207 | if (Content == null) return;
|
---|
| 208 | Content.Code = codeEditor.UserCode;
|
---|
| 209 | }
|
---|
| 210 |
|
---|
| 211 | private void errorListView_MouseDoubleClick(object sender, MouseEventArgs e) {
|
---|
| 212 | if (e.Button == MouseButtons.Left) {
|
---|
| 213 | var item = errorListView.SelectedItems[0];
|
---|
| 214 | var message = (CompilerError)item.Tag;
|
---|
| 215 | codeEditor.ScrollToPosition(message.Line, message.Column);
|
---|
| 216 | codeEditor.Focus();
|
---|
| 217 | }
|
---|
| 218 | }
|
---|
| 219 | #endregion
|
---|
[11807] | 220 |
|
---|
| 221 | private void codeEditor_AssembliesLoading(object sender, EventArgs<IEnumerable<Assembly>> e) {
|
---|
| 222 | if (InvokeRequired) Invoke((Action<object, EventArgs<IEnumerable<Assembly>>>)codeEditor_AssembliesLoading, sender, e);
|
---|
| 223 | else {
|
---|
| 224 | int nrOfAssemblies = e.Value.Count();
|
---|
| 225 | if (nrOfAssemblies > SilentAssemblyLoadingOperationLimit)
|
---|
| 226 | AddProgressView(AssembliesLoadingMessage);
|
---|
| 227 | }
|
---|
| 228 | }
|
---|
| 229 |
|
---|
| 230 | private void codeEditor_AssembliesLoaded(object sender, EventArgs<IEnumerable<Assembly>> e) {
|
---|
| 231 | if (InvokeRequired) Invoke((Action<object, EventArgs<IEnumerable<Assembly>>>)codeEditor_AssembliesLoaded, sender, e);
|
---|
| 232 | else {
|
---|
| 233 | RemoveProgressView();
|
---|
| 234 | }
|
---|
| 235 | }
|
---|
| 236 |
|
---|
| 237 | private void codeEditor_AssembliesUnloading(object sender, EventArgs<IEnumerable<Assembly>> e) {
|
---|
| 238 | if (InvokeRequired) Invoke((Action<object, EventArgs<IEnumerable<Assembly>>>)codeEditor_AssembliesUnloading, sender, e);
|
---|
| 239 | else {
|
---|
| 240 | int nrOfAssemblies = e.Value.Count();
|
---|
| 241 | if (nrOfAssemblies > SilentAssemblyLoadingOperationLimit)
|
---|
| 242 | AddProgressView(AssembliesUnloadingMessage);
|
---|
| 243 | }
|
---|
| 244 | }
|
---|
| 245 |
|
---|
| 246 | private void codeEditor_AssembliesUnloaded(object sender, EventArgs<IEnumerable<Assembly>> e) {
|
---|
| 247 | if (InvokeRequired) Invoke((Action<object, EventArgs<IEnumerable<Assembly>>>)codeEditor_AssembliesUnloaded, sender, e);
|
---|
| 248 | else {
|
---|
| 249 | RemoveProgressView();
|
---|
| 250 | }
|
---|
| 251 | }
|
---|
[10332] | 252 | }
|
---|
| 253 | } |
---|