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 |
|
---|
22 | using System;
|
---|
23 | using System.CodeDom.Compiler;
|
---|
24 | using System.Drawing;
|
---|
25 | using System.Globalization;
|
---|
26 | using System.Linq;
|
---|
27 | using System.Windows.Forms;
|
---|
28 | using HeuristicLab.Common;
|
---|
29 | using HeuristicLab.Common.Resources;
|
---|
30 | using HeuristicLab.Core.Views;
|
---|
31 | using HeuristicLab.MainForm;
|
---|
32 |
|
---|
33 | namespace HeuristicLab.Scripting.Views {
|
---|
34 |
|
---|
35 | [View("Script View")]
|
---|
36 | [Content(typeof(Script), true)]
|
---|
37 | public partial class ScriptView : NamedItemView {
|
---|
38 | private bool running;
|
---|
39 |
|
---|
40 | public new Script Content {
|
---|
41 | get { return (Script)base.Content; }
|
---|
42 | set { base.Content = (Script)value; }
|
---|
43 | }
|
---|
44 |
|
---|
45 | public ScriptView() {
|
---|
46 | InitializeComponent();
|
---|
47 | errorListView.SmallImageList.Images.AddRange(new Image[] { VSImageLibrary.Warning, VSImageLibrary.Error });
|
---|
48 | AdjustErrorListViewColumnSizes();
|
---|
49 | }
|
---|
50 |
|
---|
51 | protected override void RegisterContentEvents() {
|
---|
52 | base.RegisterContentEvents();
|
---|
53 | Content.CodeChanged += Content_CodeChanged;
|
---|
54 | Content.ScriptExecutionStarted += Content_ScriptExecutionStarted;
|
---|
55 | Content.ScriptExecutionFinished += Content_ScriptExecutionFinished;
|
---|
56 | Content.ConsoleOutputChanged += Content_ConsoleOutputChanged;
|
---|
57 | }
|
---|
58 |
|
---|
59 | protected override void DeregisterContentEvents() {
|
---|
60 | Content.CodeChanged -= Content_CodeChanged;
|
---|
61 | Content.ScriptExecutionStarted -= Content_ScriptExecutionStarted;
|
---|
62 | Content.ScriptExecutionFinished -= Content_ScriptExecutionFinished;
|
---|
63 | Content.ConsoleOutputChanged -= Content_ConsoleOutputChanged;
|
---|
64 | base.DeregisterContentEvents();
|
---|
65 | }
|
---|
66 |
|
---|
67 | #region Content event handlers
|
---|
68 | private void Content_CodeChanged(object sender, EventArgs e) {
|
---|
69 | codeEditor.UserCode = Content.Code;
|
---|
70 | }
|
---|
71 | private void Content_ScriptExecutionStarted(object sender, EventArgs e) {
|
---|
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 | }
|
---|
93 | }
|
---|
94 | private void Content_ConsoleOutputChanged(object sender, EventArgs<string> e) {
|
---|
95 | if (InvokeRequired)
|
---|
96 | Invoke((Action<object, EventArgs<string>>)Content_ConsoleOutputChanged, sender, e);
|
---|
97 | else {
|
---|
98 | outputTextBox.AppendText(e.Value);
|
---|
99 | }
|
---|
100 | }
|
---|
101 | #endregion
|
---|
102 |
|
---|
103 | protected override void OnContentChanged() {
|
---|
104 | base.OnContentChanged();
|
---|
105 | if (Content == null) {
|
---|
106 | codeEditor.UserCode = string.Empty;
|
---|
107 | variableStoreView.Content = null;
|
---|
108 | } else {
|
---|
109 | codeEditor.UserCode = Content.Code;
|
---|
110 | foreach (var asm in Content.GetAssemblies())
|
---|
111 | codeEditor.AddAssembly(asm);
|
---|
112 | variableStoreView.Content = Content.VariableStore;
|
---|
113 | if (Content.CompileErrors == null) {
|
---|
114 | compilationLabel.ForeColor = SystemColors.ControlDarkDark;
|
---|
115 | compilationLabel.Text = "Not compiled";
|
---|
116 | } else if (Content.CompileErrors.HasErrors) {
|
---|
117 | compilationLabel.ForeColor = Color.DarkRed;
|
---|
118 | compilationLabel.Text = "Compilation failed";
|
---|
119 | } else {
|
---|
120 | compilationLabel.ForeColor = Color.DarkGreen;
|
---|
121 | compilationLabel.Text = "Compilation successful";
|
---|
122 | }
|
---|
123 | }
|
---|
124 | }
|
---|
125 |
|
---|
126 | protected override void SetEnabledStateOfControls() {
|
---|
127 | base.SetEnabledStateOfControls();
|
---|
128 | compileButton.Enabled = Content != null && !Locked && !ReadOnly;
|
---|
129 | startStopButton.Enabled = Content != null && (!Locked || running);
|
---|
130 | codeEditor.Enabled = Content != null && !Locked && !ReadOnly;
|
---|
131 | }
|
---|
132 |
|
---|
133 | #region Child Control event handlers
|
---|
134 | private void compileButton_Click(object sender, EventArgs e) {
|
---|
135 | Compile();
|
---|
136 | }
|
---|
137 |
|
---|
138 | private void startStopButton_Click(object sender, EventArgs e) {
|
---|
139 | if (running) {
|
---|
140 | Content.Kill();
|
---|
141 | } else
|
---|
142 | if (Compile()) {
|
---|
143 | outputTextBox.Clear();
|
---|
144 | Content.Execute();
|
---|
145 | running = true;
|
---|
146 | }
|
---|
147 | }
|
---|
148 |
|
---|
149 | private void codeEditor_TextEditorTextChanged(object sender, EventArgs e) {
|
---|
150 | Content.Code = codeEditor.UserCode;
|
---|
151 | }
|
---|
152 | #endregion
|
---|
153 |
|
---|
154 | #region global HotKeys
|
---|
155 | protected override bool ProcessCmdKey(ref Message msg, Keys keyData) {
|
---|
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;
|
---|
174 | }
|
---|
175 | return base.ProcessCmdKey(ref msg, keyData);
|
---|
176 | }
|
---|
177 | #endregion
|
---|
178 |
|
---|
179 | #region Auxiliary functions
|
---|
180 | private bool Compile() {
|
---|
181 | ReadOnly = true;
|
---|
182 | Locked = true;
|
---|
183 | errorListView.Items.Clear();
|
---|
184 | outputTextBox.Text = "Compiling ... ";
|
---|
185 | try {
|
---|
186 | Content.Compile();
|
---|
187 | outputTextBox.AppendText("Compilation succeeded.");
|
---|
188 | return true;
|
---|
189 | } catch {
|
---|
190 | outputTextBox.AppendText("Compilation failed.");
|
---|
191 | return false;
|
---|
192 | } finally {
|
---|
193 | ShowCompilationResults();
|
---|
194 | ReadOnly = false;
|
---|
195 | Locked = false;
|
---|
196 | OnContentChanged();
|
---|
197 | }
|
---|
198 | }
|
---|
199 | #endregion
|
---|
200 |
|
---|
201 | private void ShowCompilationResults() {
|
---|
202 | if (Content.CompileErrors.Count == 0) return;
|
---|
203 | var msgs = Content.CompileErrors.OfType<CompilerError>()
|
---|
204 | .OrderBy(x => x.IsWarning)
|
---|
205 | .ThenBy(x => x.Line)
|
---|
206 | .ThenBy(x => x.Column);
|
---|
207 | foreach (var m in msgs) {
|
---|
208 | var item = new ListViewItem();
|
---|
209 | item.SubItems.AddRange(new[] {
|
---|
210 | m.IsWarning ? "Warning" : "Error",
|
---|
211 | m.ErrorNumber,
|
---|
212 | m.Line.ToString(CultureInfo.InvariantCulture),
|
---|
213 | m.Column.ToString(CultureInfo.InvariantCulture),
|
---|
214 | m.ErrorText
|
---|
215 | });
|
---|
216 | item.ImageIndex = m.IsWarning ? 0 : 1;
|
---|
217 | errorListView.Items.Add(item);
|
---|
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;
|
---|
227 | }
|
---|
228 | }
|
---|
229 | } |
---|