#region License Information /* HeuristicLab * Copyright (C) 2002-2014 Heuristic and Evolutionary Algorithms Laboratory (HEAL) * * This file is part of HeuristicLab. * * HeuristicLab is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * HeuristicLab is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with HeuristicLab. If not, see . */ #endregion using HeuristicLab.Common.Resources; using HeuristicLab.Core.Views; using HeuristicLab.MainForm; using HeuristicLab.PluginInfrastructure; using System; using System.CodeDom.Compiler; using System.Drawing; using System.Globalization; using System.Linq; using System.Windows.Forms; namespace HeuristicLab.Scripting.Views { [View("ProgrammableItem View")] [Content(typeof(IProgrammableItem), false)] public partial class ProgrammableItemView : ItemView { public new IProgrammableItem Content { get { return (IProgrammableItem)base.Content; } set { base.Content = value; } } public ProgrammableItemView() { InitializeComponent(); errorsListView.SmallImageList.Images.AddRange(new Image[] { VSImageLibrary.Warning, VSImageLibrary.Error }); } protected override void DeregisterContentEvents() { Content.CodeChanged -= Content_CodeChanged; base.DeregisterContentEvents(); } protected override void RegisterContentEvents() { base.RegisterContentEvents(); Content.CodeChanged += Content_CodeChanged; } protected override void OnContentChanged() { base.OnContentChanged(); errorsListView.Items.Clear(); if (Content == null) { codeEditor.UserCode = string.Empty; } else { codeEditor.UserCode = Content.Code; foreach (var asm in Content.GetAssemblies()) codeEditor.AddAssembly(asm); } ShowCompilationResults(); UpdateCompilationLabel(); } protected override void SetEnabledStateOfControls() { base.SetEnabledStateOfControls(); compileButton.Enabled = Content != null && Content.CanChangeCode && !Locked && !ReadOnly; codeEditor.Enabled = Content != null && !Locked; codeEditor.ReadOnly = ReadOnly || Locked || ((Content != null) && !Content.CanChangeCode); } protected override bool ProcessCmdKey(ref System.Windows.Forms.Message msg, Keys keyData) { switch (keyData) { case Keys.F6: if (Content != null && !Locked) Compile(); return true; } return base.ProcessCmdKey(ref msg, keyData); } protected virtual void Content_CodeChanged(object sender, EventArgs e) { if (InvokeRequired) Invoke(new EventHandler(Content_CodeChanged), sender, e); else { codeEditor.UserCode = Content.Code; } } private void compileButton_Click(object sender, EventArgs e) { Compile(); } private void codeEditor_Validated(object sender, EventArgs e) { if ((Content != null) && Content.CanChangeCode) Content.Code = codeEditor.UserCode; } private void errorsListView_DoubleClick(object sender, EventArgs e) { if ((Content != null) && (Content.CompileErrors != null)) { codeEditor.ShowCompileErrors(Content.CompileErrors, ".cs"); } } #region Helpers protected virtual void Compile() { ReadOnly = true; Locked = true; errorsListView.Items.Clear(); try { Content.Compile(); } catch (Exception ex) { if (!(ex is InvalidOperationException) || (Content.CompileErrors == null)) ErrorHandling.ShowErrorDialog(this, ex); } ShowCompilationResults(); UpdateCompilationLabel(); ReadOnly = false; Locked = false; } protected virtual void ShowCompilationResults() { if ((Content == null) || (Content.CompileErrors == null) || (Content.CompileErrors.Count == 0)) return; var errors = Content.CompileErrors.OfType() .OrderBy(x => x.IsWarning) .ThenBy(x => x.Line) .ThenBy(x => x.Column); foreach (var e in errors) { var item = new ListViewItem(); item.SubItems.AddRange(new[] { e.IsWarning ? "Warning" : "Error", e.ErrorNumber, e.Line.ToString(CultureInfo.InvariantCulture), e.Column.ToString(CultureInfo.InvariantCulture), e.ErrorText }); item.ImageIndex = e.IsWarning ? 0 : 1; errorsListView.Items.Add(item); } foreach (ColumnHeader ch in errorsListView.Columns) { // adjusts column width to width of column header ch.Width = -2; } codeEditor.ShowCompileErrors(Content.CompileErrors, ".cs"); } protected virtual void UpdateCompilationLabel() { if ((Content == null) || (Content.CompileErrors == null)) { compilationLabel.ForeColor = SystemColors.ControlDarkDark; compilationLabel.Text = "Not compiled"; } else if (Content.CompileErrors.HasErrors) { compilationLabel.ForeColor = Color.DarkRed; compilationLabel.Text = "Compilation failed"; } else { compilationLabel.ForeColor = Color.DarkGreen; compilationLabel.Text = "Compilation successful"; } } #endregion } }