1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2010 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.Collections.Generic;
|
---|
24 | using System.ComponentModel;
|
---|
25 | using System.Drawing;
|
---|
26 | using System.Data;
|
---|
27 | using System.Text;
|
---|
28 | using System.Windows.Forms;
|
---|
29 | using HeuristicLab.Core;
|
---|
30 | using HeuristicLab.Common;
|
---|
31 | using HeuristicLab.MainForm;
|
---|
32 | using HeuristicLab.Persistence.Default.Xml;
|
---|
33 |
|
---|
34 | namespace HeuristicLab.Core.Views {
|
---|
35 | /// <summary>
|
---|
36 | /// Base class for editors of engines.
|
---|
37 | /// </summary>
|
---|
38 | [Content(typeof(Engine), true)]
|
---|
39 | [Content(typeof(IEngine), false)]
|
---|
40 | public partial class EngineView : ItemView {
|
---|
41 | protected TypeSelectorDialog typeSelectorDialog;
|
---|
42 | private int executionTimeCounter;
|
---|
43 |
|
---|
44 | /// <summary>
|
---|
45 | /// Gets or sets the current engine.
|
---|
46 | /// </summary>
|
---|
47 | /// <remarks>Uses property <see cref="ViewBase.Item"/> of base class <see cref="EditorBase"/>.</remarks>
|
---|
48 | public new IEngine Content {
|
---|
49 | get { return (IEngine)base.Content; }
|
---|
50 | set { base.Content = value; }
|
---|
51 | }
|
---|
52 |
|
---|
53 | /// <summary>
|
---|
54 | /// Initializes a new instance of <see cref="EngineBaseEditor"/>.
|
---|
55 | /// </summary>
|
---|
56 | public EngineView() {
|
---|
57 | InitializeComponent();
|
---|
58 | }
|
---|
59 | public EngineView(IEngine content)
|
---|
60 | : this() {
|
---|
61 | Content = content;
|
---|
62 | }
|
---|
63 |
|
---|
64 | /// <summary>
|
---|
65 | /// Removes the event handlers from the underlying <see cref="IEngine"/>.
|
---|
66 | /// </summary>
|
---|
67 | /// <remarks>Calls <see cref="ViewBase.RemoveItemEvents"/> of base class <see cref="ViewBase"/>.</remarks>
|
---|
68 | protected override void DeregisterContentEvents() {
|
---|
69 | Content.OperatorGraphChanged -= new EventHandler(Content_OperatorGraphChanged);
|
---|
70 | Content.ProblemChanged -= new EventHandler(Content_ProblemChanged);
|
---|
71 | Content.Prepared -= new EventHandler(Content_Prepared);
|
---|
72 | Content.Started -= new EventHandler(Content_Started);
|
---|
73 | Content.Stopped -= new EventHandler(Content_Stopped);
|
---|
74 | Content.ExecutionTimeChanged -= new EventHandler(Content_ExecutionTimeChanged);
|
---|
75 | Content.ExceptionOccurred -= new EventHandler<EventArgs<Exception>>(Content_ExceptionOccurred);
|
---|
76 | base.DeregisterContentEvents();
|
---|
77 | }
|
---|
78 |
|
---|
79 | /// <summary>
|
---|
80 | /// Adds event handlers to the underlying <see cref="IEngine"/>.
|
---|
81 | /// </summary>
|
---|
82 | /// <remarks>Calls <see cref="ViewBase.AddItemEvents"/> of base class <see cref="ViewBase"/>.</remarks>
|
---|
83 | protected override void RegisterContentEvents() {
|
---|
84 | base.RegisterContentEvents();
|
---|
85 | Content.OperatorGraphChanged += new EventHandler(Content_OperatorGraphChanged);
|
---|
86 | Content.ProblemChanged += new EventHandler(Content_ProblemChanged);
|
---|
87 | Content.Prepared += new EventHandler(Content_Prepared);
|
---|
88 | Content.Started += new EventHandler(Content_Started);
|
---|
89 | Content.Stopped += new EventHandler(Content_Stopped);
|
---|
90 | Content.ExecutionTimeChanged += new EventHandler(Content_ExecutionTimeChanged);
|
---|
91 | Content.ExceptionOccurred += new EventHandler<EventArgs<Exception>>(Content_ExceptionOccurred);
|
---|
92 | }
|
---|
93 |
|
---|
94 | /// <summary>
|
---|
95 | /// Updates all controls with the latest data of the model.
|
---|
96 | /// </summary>
|
---|
97 | /// <remarks>Calls <see cref="EditorBase.UpdateControls"/> of base class <see cref="EditorBase"/>.</remarks>
|
---|
98 | protected override void OnContentChanged() {
|
---|
99 | base.OnContentChanged();
|
---|
100 | stopButton.Enabled = false;
|
---|
101 | if (Content == null) {
|
---|
102 | newOperatorGraphButton.Enabled = openOperatorGraphButton.Enabled = saveOperatorGraphButton.Enabled = false;
|
---|
103 | operatorGraphView.Enabled = false;
|
---|
104 | scopeView.Enabled = false;
|
---|
105 | newProblemButton.Enabled = openProblemButton.Enabled = saveProblemButton.Enabled = false;
|
---|
106 | problemViewHost.Enabled = false;
|
---|
107 | startButton.Enabled = resetButton.Enabled = false;
|
---|
108 | executionTimeTextBox.Enabled = false;
|
---|
109 | } else {
|
---|
110 | newOperatorGraphButton.Enabled = openOperatorGraphButton.Enabled = saveOperatorGraphButton.Enabled = true;
|
---|
111 | operatorGraphView.Content = Content.OperatorGraph;
|
---|
112 | operatorGraphView.Enabled = true;
|
---|
113 | scopeView.Content = Content.GlobalScope;
|
---|
114 | scopeView.Enabled = true;
|
---|
115 | newProblemButton.Enabled = openProblemButton.Enabled = true;
|
---|
116 | saveProblemButton.Enabled = Content.Problem != null;
|
---|
117 | problemViewHost.Content = Content.Problem;
|
---|
118 | problemViewHost.Enabled = true;
|
---|
119 | startButton.Enabled = !Content.Finished;
|
---|
120 | resetButton.Enabled = true;
|
---|
121 | UpdateExecutionTimeTextBox();
|
---|
122 | executionTimeTextBox.Enabled = true;
|
---|
123 | }
|
---|
124 | }
|
---|
125 |
|
---|
126 | #region Content Events
|
---|
127 | protected virtual void Content_OperatorGraphChanged(object sender, EventArgs e) {
|
---|
128 | if (InvokeRequired)
|
---|
129 | Invoke(new EventHandler(Content_OperatorGraphChanged), sender, e);
|
---|
130 | else
|
---|
131 | operatorGraphView.Content = Content.OperatorGraph;
|
---|
132 | }
|
---|
133 | protected void Content_ProblemChanged(object sender, EventArgs e) {
|
---|
134 | if (InvokeRequired)
|
---|
135 | Invoke(new EventHandler(Content_ProblemChanged), sender, e);
|
---|
136 | else {
|
---|
137 | saveProblemButton.Enabled = Content.Problem != null;
|
---|
138 | problemViewHost.Content = Content.Problem;
|
---|
139 | }
|
---|
140 | }
|
---|
141 | protected virtual void Content_Prepared(object sender, EventArgs e) {
|
---|
142 | if (InvokeRequired)
|
---|
143 | Invoke(new EventHandler(Content_Prepared), sender, e);
|
---|
144 | else {
|
---|
145 | newOperatorGraphButton.Enabled = openOperatorGraphButton.Enabled = saveOperatorGraphButton.Enabled = true;
|
---|
146 | operatorGraphView.Enabled = true;
|
---|
147 | scopeView.Enabled = true;
|
---|
148 | newProblemButton.Enabled = openProblemButton.Enabled = true;
|
---|
149 | saveProblemButton.Enabled = Content.Problem != null;
|
---|
150 | problemViewHost.Enabled = true;
|
---|
151 | startButton.Enabled = !Content.Finished;
|
---|
152 | stopButton.Enabled = false;
|
---|
153 | resetButton.Enabled = true;
|
---|
154 | UpdateExecutionTimeTextBox();
|
---|
155 | }
|
---|
156 | }
|
---|
157 | protected virtual void Content_Started(object sender, EventArgs e) {
|
---|
158 | executionTimeCounter = 0;
|
---|
159 | if (InvokeRequired)
|
---|
160 | Invoke(new EventHandler(Content_Started), sender, e);
|
---|
161 | else {
|
---|
162 | newOperatorGraphButton.Enabled = openOperatorGraphButton.Enabled = saveOperatorGraphButton.Enabled = false;
|
---|
163 | operatorGraphView.Enabled = false;
|
---|
164 | scopeView.Enabled = false;
|
---|
165 | newProblemButton.Enabled = openProblemButton.Enabled = saveProblemButton.Enabled = false;
|
---|
166 | problemViewHost.Enabled = false;
|
---|
167 | startButton.Enabled = false;
|
---|
168 | stopButton.Enabled = true;
|
---|
169 | resetButton.Enabled = false;
|
---|
170 | UpdateExecutionTimeTextBox();
|
---|
171 | }
|
---|
172 | }
|
---|
173 | protected virtual void Content_Stopped(object sender, EventArgs e) {
|
---|
174 | if (InvokeRequired)
|
---|
175 | Invoke(new EventHandler(Content_Stopped), sender, e);
|
---|
176 | else {
|
---|
177 | newOperatorGraphButton.Enabled = openOperatorGraphButton.Enabled = saveOperatorGraphButton.Enabled = true;
|
---|
178 | operatorGraphView.Enabled = true;
|
---|
179 | scopeView.Enabled = true;
|
---|
180 | newProblemButton.Enabled = openProblemButton.Enabled = true;
|
---|
181 | saveProblemButton.Enabled = Content.Problem != null;
|
---|
182 | problemViewHost.Enabled = true;
|
---|
183 | startButton.Enabled = !Content.Finished;
|
---|
184 | stopButton.Enabled = false;
|
---|
185 | resetButton.Enabled = true;
|
---|
186 | UpdateExecutionTimeTextBox();
|
---|
187 | }
|
---|
188 | }
|
---|
189 | protected virtual void Content_ExecutionTimeChanged(object sender, EventArgs e) {
|
---|
190 | executionTimeCounter++;
|
---|
191 | if ((executionTimeCounter == 100) || !Content.Running) {
|
---|
192 | executionTimeCounter = 0;
|
---|
193 | UpdateExecutionTimeTextBox();
|
---|
194 | }
|
---|
195 | }
|
---|
196 | protected virtual void Content_ExceptionOccurred(object sender, EventArgs<Exception> e) {
|
---|
197 | if (InvokeRequired)
|
---|
198 | Invoke(new EventHandler<EventArgs<Exception>>(Content_ExceptionOccurred), sender, e);
|
---|
199 | else
|
---|
200 | Auxiliary.ShowErrorMessageBox(e.Value);
|
---|
201 | }
|
---|
202 | #endregion
|
---|
203 |
|
---|
204 | #region Button events
|
---|
205 | protected void newOperatorGraphButton_Click(object sender, EventArgs e) {
|
---|
206 | Content.OperatorGraph = new OperatorGraph();
|
---|
207 | }
|
---|
208 | protected void openOperatorGraphButton_Click(object sender, EventArgs e) {
|
---|
209 | openFileDialog.Title = "Open Operator Graph";
|
---|
210 | if (openFileDialog.ShowDialog(this) == DialogResult.OK) {
|
---|
211 | OperatorGraph operatorGraph = null;
|
---|
212 | try {
|
---|
213 | operatorGraph = XmlParser.Deserialize(openFileDialog.FileName) as OperatorGraph;
|
---|
214 | }
|
---|
215 | catch (Exception ex) {
|
---|
216 | Auxiliary.ShowErrorMessageBox(ex);
|
---|
217 | }
|
---|
218 | if (operatorGraph == null)
|
---|
219 | MessageBox.Show(this, "Selected file does not contain an operator graph.", "Invalid File", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
---|
220 | else
|
---|
221 | Content.OperatorGraph = operatorGraph;
|
---|
222 | }
|
---|
223 | }
|
---|
224 | protected void saveOperatorGraphButton_Click(object sender, EventArgs e) {
|
---|
225 | saveFileDialog.Title = "Save Operator Graph";
|
---|
226 | if (saveFileDialog.ShowDialog(this) == DialogResult.OK) {
|
---|
227 | try {
|
---|
228 | if (saveFileDialog.FilterIndex == 1)
|
---|
229 | XmlGenerator.Serialize(Content.OperatorGraph, saveFileDialog.FileName, 0);
|
---|
230 | else
|
---|
231 | XmlGenerator.Serialize(Content.OperatorGraph, saveFileDialog.FileName, 9);
|
---|
232 | }
|
---|
233 | catch (Exception ex) {
|
---|
234 | Auxiliary.ShowErrorMessageBox(ex);
|
---|
235 | }
|
---|
236 | }
|
---|
237 | }
|
---|
238 | protected void newProblemButton_Click(object sender, EventArgs e) {
|
---|
239 | if (typeSelectorDialog == null) {
|
---|
240 | typeSelectorDialog = new TypeSelectorDialog();
|
---|
241 | }
|
---|
242 | typeSelectorDialog.Caption = "Select Problem";
|
---|
243 | typeSelectorDialog.TypeSelector.Configure(typeof(IProblem), false, false);
|
---|
244 |
|
---|
245 | if (typeSelectorDialog.ShowDialog(this) == DialogResult.OK) {
|
---|
246 | Content.Problem = (IProblem)typeSelectorDialog.TypeSelector.CreateInstanceOfSelectedType();
|
---|
247 | }
|
---|
248 | }
|
---|
249 | protected void openProblemButton_Click(object sender, EventArgs e) {
|
---|
250 | openFileDialog.Title = "Open Problem";
|
---|
251 | if (openFileDialog.ShowDialog(this) == DialogResult.OK) {
|
---|
252 | IProblem problem = null;
|
---|
253 | try {
|
---|
254 | problem = XmlParser.Deserialize(openFileDialog.FileName) as IProblem;
|
---|
255 | }
|
---|
256 | catch (Exception ex) {
|
---|
257 | Auxiliary.ShowErrorMessageBox(ex);
|
---|
258 | }
|
---|
259 | if (problem == null)
|
---|
260 | MessageBox.Show(this, "Selected file does not contain a problem.", "Invalid File", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
---|
261 | else
|
---|
262 | Content.Problem = problem;
|
---|
263 | }
|
---|
264 | }
|
---|
265 | protected void saveProblemButton_Click(object sender, EventArgs e) {
|
---|
266 | saveFileDialog.Title = "Save Problem";
|
---|
267 | if (saveFileDialog.ShowDialog(this) == DialogResult.OK) {
|
---|
268 | try {
|
---|
269 | if (saveFileDialog.FilterIndex == 1)
|
---|
270 | XmlGenerator.Serialize(Content.Problem, saveFileDialog.FileName, 0);
|
---|
271 | else
|
---|
272 | XmlGenerator.Serialize(Content.Problem, saveFileDialog.FileName, 9);
|
---|
273 | }
|
---|
274 | catch (Exception ex) {
|
---|
275 | Auxiliary.ShowErrorMessageBox(ex);
|
---|
276 | }
|
---|
277 | }
|
---|
278 | }
|
---|
279 | protected virtual void startButton_Click(object sender, EventArgs e) {
|
---|
280 | Content.Start();
|
---|
281 | }
|
---|
282 | protected virtual void stopButton_Click(object sender, EventArgs e) {
|
---|
283 | Content.Stop();
|
---|
284 | }
|
---|
285 | protected virtual void resetButton_Click(object sender, EventArgs e) {
|
---|
286 | Content.Prepare();
|
---|
287 | }
|
---|
288 | #endregion
|
---|
289 |
|
---|
290 | #region Helpers
|
---|
291 | protected virtual void UpdateExecutionTimeTextBox() {
|
---|
292 | if (InvokeRequired)
|
---|
293 | Invoke(new Action(UpdateExecutionTimeTextBox));
|
---|
294 | else
|
---|
295 | executionTimeTextBox.Text = Content.ExecutionTime.ToString();
|
---|
296 | }
|
---|
297 | #endregion
|
---|
298 | }
|
---|
299 | }
|
---|