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