Free cookie consent management tool by TermsFeed Policy Generator

Changeset 13080 for trunk


Ignore:
Timestamp:
10/29/15 12:50:06 (8 years ago)
Author:
jkarder
Message:

#2298: added ExecutableScript and ExecutableScriptView

Location:
trunk/sources
Files:
3 added
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Scripting.Views/3.3/CSharpScriptView.Designer.cs

    r13024 r13080  
    2020#endregion
    2121
    22 using System.Windows.Forms;
    2322
    2423namespace HeuristicLab.Scripting.Views {
     
    4746    /// </summary>
    4847    private void InitializeComponent() {
    49       this.components = new System.ComponentModel.Container();
    50       this.startStopButton = new System.Windows.Forms.Button();
    5148      this.splitContainer2 = new System.Windows.Forms.SplitContainer();
    5249      this.variableStoreView = new HeuristicLab.Scripting.Views.VariableStoreView();
    5350      this.viewHost = new HeuristicLab.MainForm.WindowsForms.ViewHost();
    54       this.executionTimeTextBox = new System.Windows.Forms.TextBox();
    55       this.executionTimeLabel = new System.Windows.Forms.Label();
     51      this.infoTabControl.SuspendLayout();
     52      this.outputTabPage.SuspendLayout();
     53      this.errorListTabPage.SuspendLayout();
    5654      ((System.ComponentModel.ISupportInitialize)(this.splitContainer1)).BeginInit();
    5755      this.splitContainer1.Panel1.SuspendLayout();
     56      this.splitContainer1.Panel2.SuspendLayout();
    5857      this.splitContainer1.SuspendLayout();
    5958      ((System.ComponentModel.ISupportInitialize)(this.errorProvider)).BeginInit();
     
    6463      this.SuspendLayout();
    6564      //
    66       // splitContainer1.Panel1
     65      // executionTimeLabel
    6766      //
    68       this.splitContainer1.Panel1.Controls.Add(this.executionTimeTextBox);
    69       this.splitContainer1.Panel1.Controls.Add(this.executionTimeLabel);
    70       this.splitContainer1.Panel1.Controls.SetChildIndex(this.executionTimeTextBox, 0);
    71       this.splitContainer1.Panel1.Controls.SetChildIndex(this.executionTimeLabel, 0);
     67      this.executionTimeLabel.Location = new System.Drawing.Point(394, 393);
     68      //
     69      // executionTimeTextBox
     70      //
     71      this.executionTimeTextBox.Location = new System.Drawing.Point(483, 390);
     72      //
     73      // splitContainer1
     74      //
     75      this.splitContainer1.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)));
     76      this.splitContainer1.Dock = System.Windows.Forms.DockStyle.Fill;
     77      this.splitContainer1.Location = new System.Drawing.Point(0, 0);
    7278      this.splitContainer1.Size = new System.Drawing.Size(637, 543);
    73       //
    74       // nameTextBox
    75       //
    76       this.errorProvider.SetIconAlignment(this.nameTextBox, System.Windows.Forms.ErrorIconAlignment.MiddleLeft);
    77       this.errorProvider.SetIconPadding(this.nameTextBox, 2);
    78       this.nameTextBox.Location = new System.Drawing.Point(60, 0);
    79       this.nameTextBox.Size = new System.Drawing.Size(750, 20);
    80       //
    81       // infoLabel
    82       //
    83       this.infoLabel.Location = new System.Drawing.Point(816, 4);
    84       //
    85       // startStopButton
    86       //
    87       this.startStopButton.Image = HeuristicLab.Common.Resources.VSImageLibrary.Play;
    88       this.startStopButton.Location = new System.Drawing.Point(36, 26);
    89       this.startStopButton.Name = "startStopButton";
    90       this.startStopButton.Size = new System.Drawing.Size(24, 24);
    91       this.startStopButton.TabIndex = 1;
    92       this.startStopButton.TextImageRelation = System.Windows.Forms.TextImageRelation.ImageBeforeText;
    93       this.toolTip.SetToolTip(this.startStopButton, "Run (F5)");
    94       this.startStopButton.UseVisualStyleBackColor = true;
    95       this.startStopButton.Click += new System.EventHandler(this.StartStopButtonOnClick);
     79      this.splitContainer1.SplitterDistance = 427;
    9680      //
    9781      // splitContainer2
     
    11498      this.splitContainer2.SplitterDistance = 637;
    11599      this.splitContainer2.TabIndex = 7;
    116       //
    117       // splitContainer1
    118       //
    119       this.splitContainer1.Location = new System.Drawing.Point(0, 0);
    120       this.splitContainer1.Dock = DockStyle.Fill;
    121100      //
    122101      // variableStoreView
     
    145124      this.viewHost.ViewType = null;
    146125      //
    147       // executionTimeTextBox
    148       //
    149       this.executionTimeTextBox.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
    150       this.executionTimeTextBox.Location = new System.Drawing.Point(483, 390);
    151       this.executionTimeTextBox.Name = "executionTimeTextBox";
    152       this.executionTimeTextBox.ReadOnly = true;
    153       this.executionTimeTextBox.Size = new System.Drawing.Size(137, 20);
    154       this.executionTimeTextBox.TabIndex = 17;
    155       //
    156       // executionTimeLabel
    157       //
    158       this.executionTimeLabel.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
    159       this.executionTimeLabel.AutoSize = true;
    160       this.executionTimeLabel.Location = new System.Drawing.Point(394, 393);
    161       this.executionTimeLabel.Name = "executionTimeLabel";
    162       this.executionTimeLabel.Size = new System.Drawing.Size(83, 13);
    163       this.executionTimeLabel.TabIndex = 16;
    164       this.executionTimeLabel.Text = "&Execution Time:";
    165       //
    166       // ScriptView
     126      // CSharpScriptView
    167127      //
    168128      this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Inherit;
    169129      this.Controls.Add(this.splitContainer2);
    170       this.Controls.Add(this.startStopButton);
    171       this.Name = "ScriptView";
    172       this.Size = new System.Drawing.Size(835, 602);
     130      this.Name = "CSharpScriptView";
     131      this.Controls.SetChildIndex(this.infoTextLabel, 0);
     132      this.Controls.SetChildIndex(this.compileButton, 0);
    173133      this.Controls.SetChildIndex(this.startStopButton, 0);
    174134      this.Controls.SetChildIndex(this.splitContainer2, 0);
     
    176136      this.Controls.SetChildIndex(this.nameTextBox, 0);
    177137      this.Controls.SetChildIndex(this.infoLabel, 0);
     138      this.infoTabControl.ResumeLayout(false);
     139      this.outputTabPage.ResumeLayout(false);
     140      this.outputTabPage.PerformLayout();
     141      this.errorListTabPage.ResumeLayout(false);
    178142      this.splitContainer1.Panel1.ResumeLayout(false);
    179143      this.splitContainer1.Panel1.PerformLayout();
     144      this.splitContainer1.Panel2.ResumeLayout(false);
    180145      ((System.ComponentModel.ISupportInitialize)(this.splitContainer1)).EndInit();
    181146      this.splitContainer1.ResumeLayout(false);
     147      ((System.ComponentModel.ISupportInitialize)(this.errorProvider)).EndInit();
    182148      this.splitContainer2.Panel1.ResumeLayout(false);
    183149      this.splitContainer2.Panel2.ResumeLayout(false);
     
    191157    #endregion
    192158
    193     protected System.Windows.Forms.Button startStopButton;
    194159    protected System.Windows.Forms.SplitContainer splitContainer2;
    195160    protected MainForm.WindowsForms.ViewHost viewHost;
    196161    protected VariableStoreView variableStoreView;
    197     protected TextBox executionTimeTextBox;
    198     protected Label executionTimeLabel;
    199162  }
    200163}
  • trunk/sources/HeuristicLab.Scripting.Views/3.3/CSharpScriptView.cs

    r13024 r13080  
    2121
    2222using System;
    23 using System.Drawing;
    24 using System.Threading;
    2523using System.Windows.Forms;
    2624using HeuristicLab.Common;
    27 using HeuristicLab.Common.Resources;
    2825using HeuristicLab.MainForm;
    2926
     
    3229  [View("C# Script View")]
    3330  [Content(typeof(CSharpScript), true)]
    34   public partial class CSharpScriptView : ScriptView {
    35     private const string ScriptExecutionStartedMessage = "Script execution started";
    36     private const string ScriptExecutionCanceledMessage = "Script execution canceled";
    37     private const string ScriptExecutionSuccessfulMessage = "Script execution successful";
    38     private const string ScriptExecutionFailedMessage = "Script execution failed";
    39 
    40     protected bool Running { get; set; }
     31  public partial class CSharpScriptView : ExecutableScriptView {
    4132
    4233    public new CSharpScript Content {
     
    5142    protected override void RegisterContentEvents() {
    5243      base.RegisterContentEvents();
    53       Content.ScriptExecutionStarted += ContentOnScriptExecutionStarted;
    54       Content.ScriptExecutionFinished += ContentOnScriptExecutionFinished;
    5544      Content.ConsoleOutputChanged += ContentOnConsoleOutputChanged;
    56       Content.ExecutionTimeChanged += ContentOnExecutionTimeChanged;
    5745    }
    5846
    5947    protected override void DeregisterContentEvents() {
    60       Content.ScriptExecutionStarted -= ContentOnScriptExecutionStarted;
    61       Content.ScriptExecutionFinished -= ContentOnScriptExecutionFinished;
    6248      Content.ConsoleOutputChanged -= ContentOnConsoleOutputChanged;
    63       Content.ExecutionTimeChanged -= ContentOnExecutionTimeChanged;
    6449      base.DeregisterContentEvents();
    6550    }
    6651
    6752    #region Content event handlers
    68     protected virtual void ContentOnScriptExecutionStarted(object sender, EventArgs e) {
    69       if (InvokeRequired)
    70         Invoke((Action<object, EventArgs>)ContentOnScriptExecutionStarted, sender, e);
    71       else {
    72         Locked = true;
    73         ReadOnly = true;
    74         startStopButton.Image = VSImageLibrary.Stop;
    75         toolTip.SetToolTip(startStopButton, "Stop (Shift+F5)");
    76         UpdateInfoTextLabel(ScriptExecutionStartedMessage, SystemColors.ControlText);
    77         infoTabControl.SelectedTab = outputTabPage;
    78       }
    79     }
    80     protected virtual void ContentOnScriptExecutionFinished(object sender, EventArgs<Exception> e) {
    81       if (InvokeRequired)
    82         Invoke((Action<object, EventArgs<Exception>>)ContentOnScriptExecutionFinished, sender, e);
    83       else {
    84         Locked = false;
    85         ReadOnly = false;
    86         startStopButton.Image = VSImageLibrary.Play;
    87         toolTip.SetToolTip(startStopButton, "Run (F5)");
    88 
    89         var ex = e.Value;
    90         if (ex == null) {
    91           UpdateInfoTextLabel(ScriptExecutionSuccessfulMessage, Color.DarkGreen);
    92         } else if (ex is ThreadAbortException) {
    93           // the execution was canceled by the user
    94           UpdateInfoTextLabel(ScriptExecutionCanceledMessage, Color.DarkOrange);
    95         } else {
    96           UpdateInfoTextLabel(ScriptExecutionFailedMessage, Color.DarkRed);
    97           PluginInfrastructure.ErrorHandling.ShowErrorDialog(this, ex);
    98         }
    99 
    100         Running = false;
    101       }
    102     }
    10353    protected virtual void ContentOnConsoleOutputChanged(object sender, EventArgs<string> e) {
    10454      if (InvokeRequired)
     
    10656      else {
    10757        outputTextBox.AppendText(e.Value);
    108       }
    109     }
    110     protected virtual void ContentOnExecutionTimeChanged(object sender, EventArgs eventArgs) {
    111       if (InvokeRequired)
    112         Invoke((Action<object, EventArgs>)ContentOnExecutionTimeChanged, sender, eventArgs);
    113       else {
    114         executionTimeTextBox.Text = Content == null ? "-" : Content.ExecutionTime.ToString();
    11558      }
    11659    }
     
    12164      if (Content == null) {
    12265        variableStoreView.Content = null;
    123         executionTimeTextBox.Text = "-";
    12466      } else {
    12567        variableStoreView.Content = Content.VariableStore;
    126         executionTimeTextBox.Text = Content.ExecutionTime.ToString();
    12768      }
    12869    }
    129 
    130     protected override void SetEnabledStateOfControls() {
    131       base.SetEnabledStateOfControls();
    132       startStopButton.Enabled = Content != null && (!Locked || Running);
    133     }
    134 
    135     protected virtual void StartStopButtonOnClick(object sender, EventArgs e) {
    136       if (Running) {
    137         Content.Kill();
    138       } else
    139         if (Compile()) {
    140         outputTextBox.Clear();
    141         Running = true;
    142         Content.ExecuteAsync();
    143       }
    144     }
    145 
    146     #region global HotKeys
    147     protected override bool ProcessCmdKey(ref Message msg, Keys keyData) {
    148       switch (keyData) {
    149         case Keys.F5:
    150           if (Content != null && !Locked && !Running) {
    151             if (Compile()) {
    152               outputTextBox.Clear();
    153               Content.ExecuteAsync();
    154               Running = true;
    155             }
    156           }
    157           return true;
    158         case Keys.F5 | Keys.Shift:
    159           if (Running) Content.Kill();
    160           return true;
    161         case Keys.F6:
    162           if (!Running) base.ProcessCmdKey(ref msg, keyData);
    163           return true;
    164       }
    165       return base.ProcessCmdKey(ref msg, keyData);
    166     }
    167     #endregion
    16870  }
    16971}
  • trunk/sources/HeuristicLab.Scripting.Views/3.3/HeuristicLab.Scripting.Views-3.3.csproj

    r11807 r13080  
    9797    <Compile Include="CSharpScriptView.Designer.cs">
    9898      <DependentUpon>CSharpScriptView.cs</DependentUpon>
     99    </Compile>
     100    <Compile Include="ExecutableScriptView.cs">
     101      <SubType>UserControl</SubType>
     102    </Compile>
     103    <Compile Include="ExecutableScriptView.Designer.cs">
     104      <DependentUpon>ExecutableScriptView.cs</DependentUpon>
    99105    </Compile>
    100106    <Compile Include="ScriptView.cs">
  • trunk/sources/HeuristicLab.Scripting/3.3/HeuristicLab.Scripting-3.3.csproj

    r11882 r13080  
    9292  <ItemGroup>
    9393    <Compile Include="CompilationException.cs" />
     94    <Compile Include="ExecutableScript.cs" />
    9495    <Compile Include="Scripts\CSharp\CSharpScript.cs" />
    9596    <Compile Include="Scripts\Templates\CSharpScriptTemplate.cs" />
  • trunk/sources/HeuristicLab.Scripting/3.3/Scripts/CSharp/CSharpScript.cs

    r13024 r13080  
    2323using System.Linq;
    2424using System.Reflection;
    25 using System.Threading;
    2625using HeuristicLab.Common;
    2726using HeuristicLab.Core;
     
    3231  [Creatable(CreatableAttribute.Categories.Scripts, Priority = 100)]
    3332  [StorableClass]
    34   public class CSharpScript : Script, IStorableContent {
     33  public class CSharpScript : ExecutableScript, IStorableContent {
    3534    #region Constants
    36     protected const string ExecuteMethodName = "Execute";
    3735    protected override string CodeTemplate { get { return ScriptTemplates.CSharpScriptTemplate; } }
    3836    #endregion
     
    4038    #region Fields & Properties
    4139    private CSharpScriptBase compiledScript;
    42     private Thread scriptThread;
    43     private DateTime lastUpdateTime;
    4440
    4541    public string Filename { get; set; }
     
    5046      get { return variableStore; }
    5147    }
    52 
    53     [Storable]
    54     private TimeSpan executionTime;
    55     public TimeSpan ExecutionTime {
    56       get { return executionTime; }
    57       protected set {
    58         executionTime = value;
    59         OnExecutionTimeChanged();
    60       }
    61     }
    6248    #endregion
    6349
    64     #region Construction & Initialization
     50    #region Construction & Cloning
    6551    [StorableConstructor]
    6652    protected CSharpScript(bool deserializing) : base(deserializing) { }
     
    6854      : base(original, cloner) {
    6955      variableStore = cloner.Clone(original.variableStore);
    70       executionTime = original.executionTime;
    7156    }
    72     public CSharpScript() {
     57    public CSharpScript()
     58      : base() {
    7359      variableStore = new VariableStore();
    74       executionTime = TimeSpan.Zero;
    75       Code = CodeTemplate;
    7660    }
    7761    public CSharpScript(string code)
    7862      : base(code) {
    7963      variableStore = new VariableStore();
    80       executionTime = TimeSpan.Zero;
    8164    }
    8265
     
    9780
    9881    #region Compilation
    99 
    10082    public override Assembly Compile() {
    10183      DeregisterScriptEvents();
     
    10991    #endregion
    11092
    111     public virtual void ExecuteAsync() {
     93    protected override void ExecuteCode() {
    11294      if (compiledScript == null) return;
    113       executionTime = TimeSpan.Zero;
    114       scriptThread = new Thread(() => {
    115         Exception ex = null;
    116         var timer = new System.Timers.Timer(250) { AutoReset = true };
    117         timer.Elapsed += timer_Elapsed;
    118         try {
    119           OnScriptExecutionStarted();
    120           lastUpdateTime = DateTime.UtcNow;
    121           timer.Start();
    122           compiledScript.Execute(VariableStore);
    123         } catch (Exception e) {
    124           ex = e;
    125         } finally {
    126           scriptThread = null;
    127           timer.Elapsed -= timer_Elapsed;
    128           timer.Stop();
    129           ExecutionTime += DateTime.UtcNow - lastUpdateTime;
    130           OnScriptExecutionFinished(ex);
    131         }
    132       });
    133       scriptThread.SetApartmentState(ApartmentState.STA);
    134       scriptThread.Start();
    135     }
    13695
    137     public virtual void Kill() {
    138       if (scriptThread == null) return;
    139       scriptThread.Abort();
    140     }
    141 
    142     private void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e) {
    143       var timer = (System.Timers.Timer)sender;
    144       timer.Enabled = false;
    145       DateTime now = DateTime.UtcNow;
    146       ExecutionTime += now - lastUpdateTime;
    147       lastUpdateTime = now;
    148       timer.Enabled = true;
     96      compiledScript.Execute(VariableStore);
    14997    }
    15098
    15199    protected virtual void CompiledScriptOnConsoleOutputChanged(object sender, EventArgs<string> e) {
    152100      OnConsoleOutputChanged(e.Value);
    153     }
    154 
    155     public event EventHandler ScriptExecutionStarted;
    156     protected virtual void OnScriptExecutionStarted() {
    157       var handler = ScriptExecutionStarted;
    158       if (handler != null) handler(this, EventArgs.Empty);
    159     }
    160 
    161     public event EventHandler<EventArgs<Exception>> ScriptExecutionFinished;
    162     protected virtual void OnScriptExecutionFinished(Exception e) {
    163       var handler = ScriptExecutionFinished;
    164       if (handler != null) handler(this, new EventArgs<Exception>(e));
    165101    }
    166102
     
    170106      if (handler != null) handler(this, new EventArgs<string>(args));
    171107    }
    172 
    173     public event EventHandler ExecutionTimeChanged;
    174     protected virtual void OnExecutionTimeChanged() {
    175       var handler = ExecutionTimeChanged;
    176       if (handler != null) handler(this, EventArgs.Empty);
    177     }
    178108  }
    179109}
Note: See TracChangeset for help on using the changeset viewer.