1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2016 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.Collections.Generic;
|
---|
25 | using System.Drawing;
|
---|
26 | using System.Globalization;
|
---|
27 | using System.Linq;
|
---|
28 | using System.Reflection;
|
---|
29 | using System.Windows.Forms;
|
---|
30 | using HeuristicLab.Common;
|
---|
31 | using HeuristicLab.Common.Resources;
|
---|
32 | using HeuristicLab.Core.Views;
|
---|
33 | using HeuristicLab.MainForm;
|
---|
34 |
|
---|
35 | namespace HeuristicLab.Scripting.Views {
|
---|
36 |
|
---|
37 | [View("Script View")]
|
---|
38 | [Content(typeof(Script), true)]
|
---|
39 | public partial class ScriptView : NamedItemView {
|
---|
40 | private const string NotCompiledMessage = "Not compiled";
|
---|
41 | private const string CompilationSucceededMessage = "Compilation succeeded";
|
---|
42 | private const string CompilationFailedMessage = "Compilation failed";
|
---|
43 | private const string AssembliesLoadingMessage = "Loading Assemblies";
|
---|
44 | private const string AssembliesUnloadingMessage = "Unloading Assemblies";
|
---|
45 | private const int SilentAssemblyLoadingOperationLimit = 10;
|
---|
46 |
|
---|
47 | #region Properties
|
---|
48 | public new Script Content {
|
---|
49 | get { return (Script)base.Content; }
|
---|
50 | set { base.Content = value; }
|
---|
51 | }
|
---|
52 |
|
---|
53 | public override bool ReadOnly {
|
---|
54 | get { return codeEditor.ReadOnly || base.ReadOnly; }
|
---|
55 | set { base.ReadOnly = codeEditor.ReadOnly = value; }
|
---|
56 | }
|
---|
57 |
|
---|
58 | public override bool Locked {
|
---|
59 | get { return codeEditor.ReadOnly || base.Locked; }
|
---|
60 | set { base.Locked = codeEditor.ReadOnly = value; }
|
---|
61 | }
|
---|
62 | #endregion
|
---|
63 |
|
---|
64 | public ScriptView() {
|
---|
65 | InitializeComponent();
|
---|
66 | errorListView.SmallImageList.Images.AddRange(new Image[] { VSImageLibrary.Warning, VSImageLibrary.Error });
|
---|
67 | }
|
---|
68 |
|
---|
69 | protected override void RegisterContentEvents() {
|
---|
70 | base.RegisterContentEvents();
|
---|
71 | Content.CodeChanged += Content_CodeChanged;
|
---|
72 | }
|
---|
73 |
|
---|
74 | protected override void DeregisterContentEvents() {
|
---|
75 | Content.CodeChanged -= Content_CodeChanged;
|
---|
76 | base.DeregisterContentEvents();
|
---|
77 | }
|
---|
78 |
|
---|
79 | #region Overrides
|
---|
80 | protected override void OnContentChanged() {
|
---|
81 | base.OnContentChanged();
|
---|
82 | if (Content == null) {
|
---|
83 | codeEditor.UserCode = string.Empty;
|
---|
84 | } else {
|
---|
85 | codeEditor.UserCode = Content.Code;
|
---|
86 | codeEditor.AddAssembliesAsync(Content.GetAssemblies());
|
---|
87 | if (Content.CompileErrors == null) {
|
---|
88 | UpdateInfoTextLabel(NotCompiledMessage, SystemColors.ControlText);
|
---|
89 | }
|
---|
90 | }
|
---|
91 | }
|
---|
92 |
|
---|
93 | protected override void SetEnabledStateOfControls() {
|
---|
94 | base.SetEnabledStateOfControls();
|
---|
95 | compileButton.Enabled = Content != null && !Locked && !ReadOnly;
|
---|
96 | codeEditor.Enabled = Content != null;
|
---|
97 | }
|
---|
98 |
|
---|
99 | protected override bool ProcessCmdKey(ref Message msg, Keys keyData) {
|
---|
100 | switch (keyData) {
|
---|
101 | case Keys.F6:
|
---|
102 | if (Content != null && !Locked)
|
---|
103 | Compile();
|
---|
104 | return true;
|
---|
105 | }
|
---|
106 | return base.ProcessCmdKey(ref msg, keyData);
|
---|
107 | }
|
---|
108 | #endregion
|
---|
109 |
|
---|
110 | public virtual bool Compile() {
|
---|
111 | ReadOnly = true;
|
---|
112 | Locked = true;
|
---|
113 | errorListView.Items.Clear();
|
---|
114 | outputTextBox.Text = "Compiling ... ";
|
---|
115 | try {
|
---|
116 | Content.Compile();
|
---|
117 | outputTextBox.AppendText(CompilationSucceededMessage);
|
---|
118 | UpdateInfoTextLabel(CompilationSucceededMessage, Color.DarkGreen);
|
---|
119 | return true;
|
---|
120 | } catch (CompilationException) {
|
---|
121 | if (Content.CompileErrors.HasErrors) {
|
---|
122 | outputTextBox.AppendText(CompilationFailedMessage);
|
---|
123 | UpdateInfoTextLabel(CompilationFailedMessage, Color.DarkRed);
|
---|
124 | return false;
|
---|
125 | } else {
|
---|
126 | outputTextBox.AppendText(CompilationSucceededMessage);
|
---|
127 | UpdateInfoTextLabel(CompilationSucceededMessage, Color.DarkGreen);
|
---|
128 | return true;
|
---|
129 | }
|
---|
130 | } finally {
|
---|
131 | ShowCompilationResults();
|
---|
132 | if (Content.CompileErrors.Count > 0)
|
---|
133 | infoTabControl.SelectedTab = errorListTabPage;
|
---|
134 | ReadOnly = false;
|
---|
135 | Locked = false;
|
---|
136 | codeEditor.Focus();
|
---|
137 | }
|
---|
138 | }
|
---|
139 |
|
---|
140 | #region Helpers
|
---|
141 | protected virtual void ShowCompilationResults() {
|
---|
142 | var messages = Content.CompileErrors.OfType<CompilerError>()
|
---|
143 | .OrderBy(x => x.IsWarning)
|
---|
144 | .ThenBy(x => x.Line)
|
---|
145 | .ThenBy(x => x.Column);
|
---|
146 |
|
---|
147 | foreach (var m in messages) {
|
---|
148 | var item = new ListViewItem { Tag = m };
|
---|
149 | item.SubItems.AddRange(new[] {
|
---|
150 | m.IsWarning ? "Warning" : "Error",
|
---|
151 | m.ErrorNumber,
|
---|
152 | m.Line.ToString(CultureInfo.InvariantCulture),
|
---|
153 | m.Column.ToString(CultureInfo.InvariantCulture),
|
---|
154 | m.ErrorText
|
---|
155 | });
|
---|
156 | item.ImageIndex = m.IsWarning ? 0 : 1;
|
---|
157 | errorListView.Items.Add(item);
|
---|
158 | }
|
---|
159 |
|
---|
160 | codeEditor.ShowCompileErrors(Content.CompileErrors);
|
---|
161 |
|
---|
162 | AdjustErrorListViewColumnSizes();
|
---|
163 | }
|
---|
164 |
|
---|
165 | protected virtual void UpdateInfoTextLabel(string message, Color color) {
|
---|
166 | infoTextLabel.Text = message;
|
---|
167 | infoTextLabel.ForeColor = color;
|
---|
168 | }
|
---|
169 |
|
---|
170 | protected virtual void AdjustErrorListViewColumnSizes() {
|
---|
171 | foreach (ColumnHeader ch in errorListView.Columns)
|
---|
172 | // adjusts the column width to the width of the column header
|
---|
173 | ch.Width = -2;
|
---|
174 | }
|
---|
175 |
|
---|
176 | #region ProgressView
|
---|
177 | private bool progressViewCreated;
|
---|
178 |
|
---|
179 | private void AddProgressView(string progressMessage) {
|
---|
180 | Progress.ShowMarquee(this, progressMessage);
|
---|
181 | progressViewCreated = true;
|
---|
182 | }
|
---|
183 |
|
---|
184 | private void RemoveProgressView() {
|
---|
185 | if (!progressViewCreated) return;
|
---|
186 | Progress.Hide(this);
|
---|
187 | progressViewCreated = false;
|
---|
188 | }
|
---|
189 | #endregion
|
---|
190 | #endregion
|
---|
191 |
|
---|
192 | #region Event Handlers
|
---|
193 | protected virtual void Content_CodeChanged(object sender, EventArgs e) {
|
---|
194 | if (InvokeRequired) Invoke((Action<object, EventArgs>)Content_CodeChanged, sender, e);
|
---|
195 | else {
|
---|
196 | codeEditor.UserCode = Content.Code;
|
---|
197 | }
|
---|
198 | }
|
---|
199 |
|
---|
200 | private void compileButton_Click(object sender, EventArgs e) {
|
---|
201 | Compile();
|
---|
202 | }
|
---|
203 |
|
---|
204 | private void codeEditor_TextEditorTextChanged(object sender, EventArgs e) {
|
---|
205 | if (Content == null) return;
|
---|
206 | Content.Code = codeEditor.UserCode;
|
---|
207 | }
|
---|
208 |
|
---|
209 | private void errorListView_MouseDoubleClick(object sender, MouseEventArgs e) {
|
---|
210 | if (e.Button == MouseButtons.Left) {
|
---|
211 | var item = errorListView.SelectedItems[0];
|
---|
212 | var message = (CompilerError)item.Tag;
|
---|
213 | codeEditor.ScrollToPosition(message.Line, message.Column);
|
---|
214 | codeEditor.Focus();
|
---|
215 | }
|
---|
216 | }
|
---|
217 | #endregion
|
---|
218 |
|
---|
219 | private void codeEditor_AssembliesLoading(object sender, EventArgs<IEnumerable<Assembly>> e) {
|
---|
220 | if (InvokeRequired) Invoke((Action<object, EventArgs<IEnumerable<Assembly>>>)codeEditor_AssembliesLoading, sender, e);
|
---|
221 | else {
|
---|
222 | int nrOfAssemblies = e.Value.Count();
|
---|
223 | if (nrOfAssemblies > SilentAssemblyLoadingOperationLimit)
|
---|
224 | AddProgressView(AssembliesLoadingMessage);
|
---|
225 | }
|
---|
226 | }
|
---|
227 |
|
---|
228 | private void codeEditor_AssembliesLoaded(object sender, EventArgs<IEnumerable<Assembly>> e) {
|
---|
229 | if (InvokeRequired) Invoke((Action<object, EventArgs<IEnumerable<Assembly>>>)codeEditor_AssembliesLoaded, sender, e);
|
---|
230 | else {
|
---|
231 | RemoveProgressView();
|
---|
232 | }
|
---|
233 | }
|
---|
234 |
|
---|
235 | private void codeEditor_AssembliesUnloading(object sender, EventArgs<IEnumerable<Assembly>> e) {
|
---|
236 | if (InvokeRequired) Invoke((Action<object, EventArgs<IEnumerable<Assembly>>>)codeEditor_AssembliesUnloading, sender, e);
|
---|
237 | else {
|
---|
238 | int nrOfAssemblies = e.Value.Count();
|
---|
239 | if (nrOfAssemblies > SilentAssemblyLoadingOperationLimit)
|
---|
240 | AddProgressView(AssembliesUnloadingMessage);
|
---|
241 | }
|
---|
242 | }
|
---|
243 |
|
---|
244 | private void codeEditor_AssembliesUnloaded(object sender, EventArgs<IEnumerable<Assembly>> e) {
|
---|
245 | if (InvokeRequired) Invoke((Action<object, EventArgs<IEnumerable<Assembly>>>)codeEditor_AssembliesUnloaded, sender, e);
|
---|
246 | else {
|
---|
247 | RemoveProgressView();
|
---|
248 | }
|
---|
249 | }
|
---|
250 | }
|
---|
251 | } |
---|