Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PythonScript/HeuristicLab.Scripting.Python.Views/3.3/PytonScriptView.cs @ 11826

Last change on this file since 11826 was 10567, checked in by abeham, 11 years ago

#2165: Imported branch of adding python scripting support through IronPython

File size: 7.2 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2013 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
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
22using System;
23using System.Drawing;
24using System.Windows.Forms;
25using HeuristicLab.Common;
26using HeuristicLab.Common.Resources;
27using HeuristicLab.Core.Views;
28using HeuristicLab.MainForm;
29
30namespace HeuristicLab.Scripting.Python.Views {
31
32  [View("Python-Script View")]
33  [Content(typeof(PythonScript), true)]
34  public partial class PythonScriptView : NamedItemView {
35    private bool running;
36    private bool suppressEvents;
37
38    public new PythonScript Content {
39      get { return (PythonScript)base.Content; }
40      set { base.Content = value; }
41    }
42
43    public PythonScriptView() {
44      InitializeComponent();
45      errorListView.SmallImageList.Images.AddRange(new Image[] { VSImageLibrary.Warning, VSImageLibrary.Error });
46      AdjustErrorListViewColumnSizes();
47    }
48
49    protected override void RegisterContentEvents() {
50      base.RegisterContentEvents();
51      Content.CodeChanged += Content_CodeChanged;
52      Content.ScriptExecutionStarted += Content_ScriptExecutionStarted;
53      Content.ScriptExecutionFinished += Content_ScriptExecutionFinished;
54      Content.ConsoleOutputChanged += Content_ConsoleOutputChanged;
55    }
56
57    protected override void DeregisterContentEvents() {
58      Content.CodeChanged -= Content_CodeChanged;
59      Content.ScriptExecutionStarted -= Content_ScriptExecutionStarted;
60      Content.ScriptExecutionFinished -= Content_ScriptExecutionFinished;
61      Content.ConsoleOutputChanged -= Content_ConsoleOutputChanged;
62      base.DeregisterContentEvents();
63    }
64
65    #region Content event handlers
66    private void Content_CodeChanged(object sender, EventArgs e) {
67      try {
68        suppressEvents = true;
69        codeTextBox.Text = Content.Code;
70      } finally { suppressEvents = false; }
71    }
72    private void Content_ScriptExecutionStarted(object sender, EventArgs e) {
73      if (InvokeRequired)
74        Invoke((Action<object, EventArgs>)Content_ScriptExecutionStarted, sender, e);
75      else {
76        Locked = true;
77        ReadOnly = true;
78        startStopButton.Image = VSImageLibrary.Stop;
79        infoTabControl.SelectedTab = outputTabPage;
80      }
81    }
82    private void Content_ScriptExecutionFinished(object sender, EventArgs<Exception> e) {
83      if (InvokeRequired)
84        Invoke((Action<object, EventArgs<Exception>>)Content_ScriptExecutionFinished, sender, e);
85      else {
86        Locked = false;
87        ReadOnly = false;
88        startStopButton.Image = VSImageLibrary.Play;
89        running = false;
90        var ex = e.Value;
91        if (ex != null)
92          PluginInfrastructure.ErrorHandling.ShowErrorDialog(this, ex);
93      }
94    }
95    private void Content_ConsoleOutputChanged(object sender, EventArgs<string> e) {
96      if (InvokeRequired)
97        Invoke((Action<object, EventArgs<string>>)Content_ConsoleOutputChanged, sender, e);
98      else {
99        outputTextBox.AppendText(e.Value);
100      }
101    }
102    #endregion
103
104    protected override void OnContentChanged() {
105      base.OnContentChanged();
106      if (Content == null) {
107        codeTextBox.Text = string.Empty;
108        variableStoreView.Content = null;
109      } else {
110        codeTextBox.Text = Content.Code;
111        variableStoreView.Content = Content.VariableStore;
112        /*if (Content.CompileErrors == null) {
113          compilationLabel.ForeColor = SystemColors.ControlDarkDark;
114          compilationLabel.Text = "Not compiled";
115        } else if (Content.CompileErrors.HasErrors) {
116          compilationLabel.ForeColor = Color.DarkRed;
117          compilationLabel.Text = "Compilation failed";
118        } else {
119          compilationLabel.ForeColor = Color.DarkGreen;
120          compilationLabel.Text = "Compilation successful";
121        }*/
122      }
123    }
124
125    protected override void SetEnabledStateOfControls() {
126      base.SetEnabledStateOfControls();
127      compileButton.Enabled = Content != null && !Locked && !ReadOnly;
128      startStopButton.Enabled = Content != null && (!Locked || running);
129      codeTextBox.Enabled = Content != null && !Locked && !ReadOnly;
130    }
131
132    #region Child Control event handlers
133    private void compileButton_Click(object sender, EventArgs e) {
134      //Compile();
135    }
136
137    private void startStopButton_Click(object sender, EventArgs e) {
138      if (running) {
139        Content.Kill();
140      } else {
141        outputTextBox.Clear();
142        Content.Execute();
143        running = true;
144      }
145    }
146
147    private void CodeTextBoxOnTextChanged(object sender, EventArgs e) {
148      if (suppressEvents || Content == null) return;
149      Content.Code = codeTextBox.Text;
150    }
151    #endregion
152
153    #region global HotKeys
154    protected override bool ProcessCmdKey(ref Message msg, Keys keyData) {
155      switch (keyData) {
156        case Keys.F5:
157          if (Content != null && !Locked) {
158            outputTextBox.Clear();
159            Content.Execute();
160            running = true;
161          }
162          break;
163        case Keys.F5 | Keys.Shift:
164          if (running) Content.Kill();
165          break;
166        /*case Keys.F6:
167          if (!Compile() || Content.CompileErrors.HasWarnings)
168            infoTabControl.SelectedTab = errorListTabPage;
169          break;*/
170      }
171      return base.ProcessCmdKey(ref msg, keyData);
172    }
173    #endregion
174
175    private void ShowCompilationResults() {
176      /*if (Content.CompileErrors.Count == 0) return;
177      var msgs = Content.CompileErrors.OfType<CompilerError>()
178                                      .OrderBy(x => x.IsWarning)
179                                      .ThenBy(x => x.Line)
180                                      .ThenBy(x => x.Column);
181      foreach (var m in msgs) {
182        var item = new ListViewItem();
183        item.SubItems.AddRange(new[] {
184          m.IsWarning ? "Warning" : "Error",
185          m.ErrorNumber,
186          m.Line.ToString(CultureInfo.InvariantCulture),
187          m.Column.ToString(CultureInfo.InvariantCulture),
188          m.ErrorText
189        });
190        item.ImageIndex = m.IsWarning ? 0 : 1;
191        errorListView.Items.Add(item);
192      }
193      AdjustErrorListViewColumnSizes();
194       * */
195    }
196
197    private void AdjustErrorListViewColumnSizes() {
198      foreach (ColumnHeader ch in errorListView.Columns)
199        // adjusts the column width to the width of the column
200        // header or the column content, whichever is greater
201        ch.Width = -2;
202    }
203  }
204}
Note: See TracBrowser for help on using the repository browser.