Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
02/25/14 13:55:01 (10 years ago)
Author:
jkarder
Message:

#2136: applied some of the changes suggested in comment:11:ticket:2136

  • change namespaces to HeuristicLab.Scripting.*
  • added extra compile button and registered F6 as a shortcut
  • changed the order of the output window and the error list
  • tabs (output window and error list) are selected automatically
  • thrown exceptions are shown using the PluginInfrastructure
  • removed namespace declaration in the script
  • names in the VariableStore are now conform to C# property names
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.HLScript.Views/3.3/ScriptView.cs

    r10401 r10506  
    2222using System;
    2323using System.CodeDom.Compiler;
    24 using System.Collections.Generic;
    2524using System.Drawing;
     25using System.Globalization;
    2626using System.Linq;
    2727using System.Windows.Forms;
     
    3131using HeuristicLab.MainForm;
    3232
    33 namespace HeuristicLab.HLScript.Views {
     33namespace HeuristicLab.Scripting.Views {
    3434
    3535  [View("Script View")]
     
    4545    public ScriptView() {
    4646      InitializeComponent();
     47      errorListView.SmallImageList.Images.AddRange(new Image[] { VSImageLibrary.Warning, VSImageLibrary.Error });
     48      AdjustErrorListViewColumnSizes();
    4749    }
    4850
     
    6870    }
    6971    private void Content_ScriptExecutionStarted(object sender, EventArgs e) {
    70       Locked = true;
    71       ReadOnly = true;
    72       startStopButton.Image = VSImageLibrary.Stop;
    73     }
    74     private void Content_ScriptExecutionFinished(object sender, EventArgs e) {
    75       Locked = false;
    76       ReadOnly = false;
    77       startStopButton.Image = VSImageLibrary.Play;
    78       running = false;
     72      if (InvokeRequired)
     73        Invoke((Action<object, EventArgs>)Content_ScriptExecutionStarted, sender, e);
     74      else {
     75        Locked = true;
     76        ReadOnly = true;
     77        startStopButton.Image = VSImageLibrary.Stop;
     78        infoTabControl.SelectedTab = outputTabPage;
     79      }
     80    }
     81    private void Content_ScriptExecutionFinished(object sender, EventArgs<Exception> e) {
     82      if (InvokeRequired)
     83        Invoke((Action<object, EventArgs<Exception>>)Content_ScriptExecutionFinished, sender, e);
     84      else {
     85        Locked = false;
     86        ReadOnly = false;
     87        startStopButton.Image = VSImageLibrary.Play;
     88        running = false;
     89        var ex = e.Value;
     90        if (ex != null)
     91          PluginInfrastructure.ErrorHandling.ShowErrorDialog(this, ex);
     92      }
    7993    }
    8094    private void Content_ConsoleOutputChanged(object sender, EventArgs<string> e) {
    81       if (InvokeRequired) Invoke((Action<object, EventArgs<string>>)Content_ConsoleOutputChanged, sender, e);
     95      if (InvokeRequired)
     96        Invoke((Action<object, EventArgs<string>>)Content_ConsoleOutputChanged, sender, e);
    8297      else {
    8398        outputTextBox.AppendText(e.Value);
     
    111126    protected override void SetEnabledStateOfControls() {
    112127      base.SetEnabledStateOfControls();
     128      compileButton.Enabled = Content != null && !Locked && !ReadOnly;
    113129      startStopButton.Enabled = Content != null && (!Locked || running);
    114130      codeEditor.Enabled = Content != null && !Locked && !ReadOnly;
     
    116132
    117133    #region Child Control event handlers
     134    private void compileButton_Click(object sender, EventArgs e) {
     135      Compile();
     136    }
     137
    118138    private void startStopButton_Click(object sender, EventArgs e) {
    119139      if (running) {
     
    134154    #region global HotKeys
    135155    protected override bool ProcessCmdKey(ref Message msg, Keys keyData) {
    136       if (keyData == Keys.F5) {
    137         if (Content == null || Locked)
    138           return base.ProcessCmdKey(ref msg, keyData);
    139         outputTextBox.Clear();
    140         bool result = Compile();
    141         if (result) {
    142           outputTextBox.Clear();
    143           Content.Execute();
    144           running = true;
    145         }
    146         return true;
    147       } else if (keyData == (Keys.F5 | Keys.Shift)) {
    148         Content.Kill();
     156      switch (keyData) {
     157        case Keys.F5:
     158          if (Content != null && !Locked) {
     159            if (Compile()) {
     160              outputTextBox.Clear();
     161              Content.Execute();
     162              running = true;
     163            } else
     164              infoTabControl.SelectedTab = errorListTabPage;
     165          }
     166          break;
     167        case Keys.F5 | Keys.Shift:
     168          if (running) Content.Kill();
     169          break;
     170        case Keys.F6:
     171          if (!Compile() || Content.CompileErrors.HasWarnings)
     172            infoTabControl.SelectedTab = errorListTabPage;
     173          break;
    149174      }
    150175      return base.ProcessCmdKey(ref msg, keyData);
     
    157182      Locked = true;
    158183      errorListView.Items.Clear();
    159       outputTextBox.Clear();
    160       outputTextBox.AppendText("Compiling ... ");
     184      outputTextBox.Text = "Compiling ... ";
    161185      try {
    162186        Content.Compile();
     
    165189      } catch {
    166190        outputTextBox.AppendText("Compilation failed.");
    167         ShowCompilationErrors();
    168191        return false;
    169192      } finally {
    170         OnContentChanged();
     193        ShowCompilationResults();
    171194        ReadOnly = false;
    172195        Locked = false;
    173       }
    174     }
    175     #endregion
    176 
    177     private void ShowCompilationErrors() {
     196        OnContentChanged();
     197      }
     198    }
     199    #endregion
     200
     201    private void ShowCompilationResults() {
    178202      if (Content.CompileErrors.Count == 0) return;
    179       var warnings = new List<CompilerError>();
    180       var errors = new List<CompilerError>();
    181       foreach (CompilerError ce in Content.CompileErrors) {
    182         if (ce.IsWarning) warnings.Add(ce);
    183         else errors.Add(ce);
    184       }
    185       var msgs = warnings.OrderBy(x => x.Line)
    186                          .ThenBy(x => x.Column)
    187                    .Concat(errors.OrderBy(x => x.Line)
    188                                  .ThenBy(x => x.Column));
    189       outputTextBox.AppendText(Environment.NewLine);
    190       outputTextBox.AppendText("---");
    191       outputTextBox.AppendText(Environment.NewLine);
     203      var msgs = Content.CompileErrors.OfType<CompilerError>()
     204                                      .OrderBy(x => x.IsWarning)
     205                                      .ThenBy(x => x.Line)
     206                                      .ThenBy(x => x.Column);
    192207      foreach (var m in msgs) {
    193         var item = new ListViewItem(new[] {
     208        var item = new ListViewItem();
     209        item.SubItems.AddRange(new[] {
    194210          m.IsWarning ? "Warning" : "Error",
    195211          m.ErrorNumber,
    196           m.Line.ToString(),
    197           m.Column.ToString(),
     212          m.Line.ToString(CultureInfo.InvariantCulture),
     213          m.Column.ToString(CultureInfo.InvariantCulture),
    198214          m.ErrorText
    199215        });
     216        item.ImageIndex = m.IsWarning ? 0 : 1;
    200217        errorListView.Items.Add(item);
    201         outputTextBox.AppendText(string.Format("{0} {1} ({2}:{3}): {4}",
    202                                  item.SubItems[0].Text,
    203                                  item.SubItems[1].Text,
    204                                  item.SubItems[2].Text,
    205                                  item.SubItems[3].Text,
    206                                  item.SubItems[4].Text));
    207         outputTextBox.AppendText(Environment.NewLine);
    208       }
     218      }
     219      AdjustErrorListViewColumnSizes();
     220    }
     221
     222    private void AdjustErrorListViewColumnSizes() {
     223      foreach (ColumnHeader ch in errorListView.Columns)
     224        // adjusts the column width to the width of the column
     225        // header or the column content, whichever is greater
     226        ch.Width = -2;
    209227    }
    210228  }
Note: See TracChangeset for help on using the changeset viewer.