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.Windows.Forms;
|
---|
25 | using HeuristicLab.Common;
|
---|
26 | using HeuristicLab.Core.Views;
|
---|
27 | using HeuristicLab.MainForm;
|
---|
28 | using HeuristicLab.Persistence.Default.Xml;
|
---|
29 |
|
---|
30 | namespace HeuristicLab.Optimization.Views {
|
---|
31 | /// <summary>
|
---|
32 | /// The base class for visual representations of items.
|
---|
33 | /// </summary>
|
---|
34 | [View("BatchRun View")]
|
---|
35 | [Content(typeof(BatchRun), true)]
|
---|
36 | public sealed partial class BatchRunView : NamedItemView {
|
---|
37 | private TypeSelectorDialog algorithmTypeSelectorDialog;
|
---|
38 |
|
---|
39 | public new BatchRun Content {
|
---|
40 | get { return (BatchRun)base.Content; }
|
---|
41 | set { base.Content = value; }
|
---|
42 | }
|
---|
43 |
|
---|
44 | /// <summary>
|
---|
45 | /// Initializes a new instance of <see cref="ItemBaseView"/>.
|
---|
46 | /// </summary>
|
---|
47 | public BatchRunView() {
|
---|
48 | InitializeComponent();
|
---|
49 | }
|
---|
50 | /// <summary>
|
---|
51 | /// Intializes a new instance of <see cref="ItemBaseView"/> with the given <paramref name="item"/>.
|
---|
52 | /// </summary>
|
---|
53 | /// <param name="item">The item that should be displayed.</param>
|
---|
54 | public BatchRunView(BatchRun content)
|
---|
55 | : this() {
|
---|
56 | Content = content;
|
---|
57 | }
|
---|
58 |
|
---|
59 | protected override void DeregisterContentEvents() {
|
---|
60 | Content.ExceptionOccurred -= new EventHandler<EventArgs<Exception>>(Content_ExceptionOccurred);
|
---|
61 | Content.ExecutionTimeChanged -= new EventHandler(Content_ExecutionTimeChanged);
|
---|
62 | Content.Prepared -= new EventHandler(Content_Prepared);
|
---|
63 | Content.AlgorithmChanged -= new EventHandler(Content_AlgorithmChanged);
|
---|
64 | Content.RepetitionsChanged -= new EventHandler(Content_RepetitionsChanged);
|
---|
65 | Content.RunningChanged -= new EventHandler(Content_RunningChanged);
|
---|
66 | base.DeregisterContentEvents();
|
---|
67 | }
|
---|
68 | protected override void RegisterContentEvents() {
|
---|
69 | base.RegisterContentEvents();
|
---|
70 | Content.ExceptionOccurred += new EventHandler<EventArgs<Exception>>(Content_ExceptionOccurred);
|
---|
71 | Content.ExecutionTimeChanged += new EventHandler(Content_ExecutionTimeChanged);
|
---|
72 | Content.Prepared += new EventHandler(Content_Prepared);
|
---|
73 | Content.AlgorithmChanged += new EventHandler(Content_AlgorithmChanged);
|
---|
74 | Content.RepetitionsChanged += new EventHandler(Content_RepetitionsChanged);
|
---|
75 | Content.RunningChanged += new EventHandler(Content_RunningChanged);
|
---|
76 | }
|
---|
77 |
|
---|
78 | protected override void OnContentChanged() {
|
---|
79 | base.OnContentChanged();
|
---|
80 | stopButton.Enabled = false;
|
---|
81 | if (Content == null) {
|
---|
82 | repetitionsNumericUpDown.Value = 1;
|
---|
83 | repetitionsNumericUpDown.Enabled = false;
|
---|
84 | algorithmViewHost.Content = null;
|
---|
85 | runsView.Content = null;
|
---|
86 | tabControl.Enabled = false;
|
---|
87 | startButton.Enabled = resetButton.Enabled = false;
|
---|
88 | executionTimeTextBox.Text = "-";
|
---|
89 | executionTimeTextBox.Enabled = false;
|
---|
90 | } else {
|
---|
91 | repetitionsNumericUpDown.Value = Content.Repetitions;
|
---|
92 | repetitionsNumericUpDown.Enabled = true;
|
---|
93 | saveAlgorithmButton.Enabled = Content.Algorithm != null;
|
---|
94 | algorithmViewHost.ViewType = null;
|
---|
95 | algorithmViewHost.Content = Content.Algorithm;
|
---|
96 | runsView.Content = Content.Runs;
|
---|
97 | tabControl.Enabled = true;
|
---|
98 | startButton.Enabled = !Content.Finished;
|
---|
99 | resetButton.Enabled = true;
|
---|
100 | UpdateExecutionTimeTextBox();
|
---|
101 | executionTimeTextBox.Enabled = true;
|
---|
102 | }
|
---|
103 | }
|
---|
104 |
|
---|
105 | protected override void OnClosed(FormClosedEventArgs e) {
|
---|
106 | if (Content != null) Content.Stop();
|
---|
107 | base.OnClosed(e);
|
---|
108 | }
|
---|
109 |
|
---|
110 | #region Content Events
|
---|
111 | private void Content_AlgorithmChanged(object sender, EventArgs e) {
|
---|
112 | if (InvokeRequired)
|
---|
113 | Invoke(new EventHandler(Content_AlgorithmChanged), sender, e);
|
---|
114 | else {
|
---|
115 | algorithmViewHost.ViewType = null;
|
---|
116 | algorithmViewHost.Content = Content.Algorithm;
|
---|
117 | saveAlgorithmButton.Enabled = Content.Algorithm != null;
|
---|
118 | }
|
---|
119 | }
|
---|
120 | private void Content_Prepared(object sender, EventArgs e) {
|
---|
121 | if (InvokeRequired)
|
---|
122 | Invoke(new EventHandler(Content_Prepared), sender, e);
|
---|
123 | else {
|
---|
124 | startButton.Enabled = !Content.Finished;
|
---|
125 | UpdateExecutionTimeTextBox();
|
---|
126 | }
|
---|
127 | }
|
---|
128 | private void Content_RunningChanged(object sender, EventArgs e) {
|
---|
129 | if (InvokeRequired)
|
---|
130 | Invoke(new EventHandler(Content_RunningChanged), sender, e);
|
---|
131 | else {
|
---|
132 | SaveEnabled = !Content.Running;
|
---|
133 | repetitionsNumericUpDown.Enabled = !Content.Running;
|
---|
134 | newAlgorithmButton.Enabled = openAlgorithmButton.Enabled = saveAlgorithmButton.Enabled = !Content.Running;
|
---|
135 | startButton.Enabled = !Content.Running && !Content.Finished;
|
---|
136 | stopButton.Enabled = Content.Running;
|
---|
137 | resetButton.Enabled = !Content.Running;
|
---|
138 | UpdateExecutionTimeTextBox();
|
---|
139 | }
|
---|
140 | }
|
---|
141 | private void Content_ExecutionTimeChanged(object sender, EventArgs e) {
|
---|
142 | UpdateExecutionTimeTextBox();
|
---|
143 | }
|
---|
144 | private void Content_ExceptionOccurred(object sender, EventArgs<Exception> e) {
|
---|
145 | if (InvokeRequired)
|
---|
146 | Invoke(new EventHandler<EventArgs<Exception>>(Content_ExceptionOccurred), sender, e);
|
---|
147 | else
|
---|
148 | Auxiliary.ShowErrorMessageBox(e.Value);
|
---|
149 | }
|
---|
150 | private void Content_RepetitionsChanged(object sender, EventArgs e) {
|
---|
151 | if (InvokeRequired)
|
---|
152 | Invoke(new EventHandler(Content_RepetitionsChanged), sender, e);
|
---|
153 | else {
|
---|
154 | repetitionsNumericUpDown.Value = Content.Repetitions;
|
---|
155 | startButton.Enabled = !Content.Finished;
|
---|
156 | }
|
---|
157 | }
|
---|
158 | #endregion
|
---|
159 |
|
---|
160 | #region Control events
|
---|
161 | private void repetitionsNumericUpDown_ValueChanged(object sender, EventArgs e) {
|
---|
162 | Content.Repetitions = (int)repetitionsNumericUpDown.Value;
|
---|
163 | }
|
---|
164 | private void newAlgorithmButton_Click(object sender, EventArgs e) {
|
---|
165 | if (algorithmTypeSelectorDialog == null) {
|
---|
166 | algorithmTypeSelectorDialog = new TypeSelectorDialog();
|
---|
167 | algorithmTypeSelectorDialog.Caption = "Select Algorithm";
|
---|
168 | algorithmTypeSelectorDialog.TypeSelector.Configure(typeof(IAlgorithm), false, false);
|
---|
169 | }
|
---|
170 | if (algorithmTypeSelectorDialog.ShowDialog(this) == DialogResult.OK) {
|
---|
171 | Content.Algorithm = (IAlgorithm)algorithmTypeSelectorDialog.TypeSelector.CreateInstanceOfSelectedType();
|
---|
172 | }
|
---|
173 | }
|
---|
174 | private void openAlgorithmButton_Click(object sender, EventArgs e) {
|
---|
175 | openFileDialog.Title = "Open Algorithm";
|
---|
176 | if (openFileDialog.ShowDialog(this) == DialogResult.OK) {
|
---|
177 | this.Cursor = Cursors.AppStarting;
|
---|
178 | newAlgorithmButton.Enabled = openAlgorithmButton.Enabled = saveAlgorithmButton.Enabled = false;
|
---|
179 | algorithmViewHost.Enabled = false;
|
---|
180 |
|
---|
181 | var call = new Func<string, object>(XmlParser.Deserialize);
|
---|
182 | call.BeginInvoke(openFileDialog.FileName, delegate(IAsyncResult a) {
|
---|
183 | IAlgorithm algorithm = null;
|
---|
184 | try {
|
---|
185 | algorithm = call.EndInvoke(a) as IAlgorithm;
|
---|
186 | } catch (Exception ex) {
|
---|
187 | Auxiliary.ShowErrorMessageBox(ex);
|
---|
188 | }
|
---|
189 | Invoke(new Action(delegate() {
|
---|
190 | if (algorithm == null)
|
---|
191 | MessageBox.Show(this, "The selected file does not contain an algorithm.", "Invalid File", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
---|
192 | else
|
---|
193 | Content.Algorithm = algorithm;
|
---|
194 | algorithmViewHost.Enabled = true;
|
---|
195 | newAlgorithmButton.Enabled = openAlgorithmButton.Enabled = saveAlgorithmButton.Enabled = true;
|
---|
196 | this.Cursor = Cursors.Default;
|
---|
197 | }));
|
---|
198 | }, null);
|
---|
199 | }
|
---|
200 | }
|
---|
201 | private void saveAlgorithmButton_Click(object sender, EventArgs e) {
|
---|
202 | saveFileDialog.Title = "Save Algorithm";
|
---|
203 | if (saveFileDialog.ShowDialog(this) == DialogResult.OK) {
|
---|
204 | this.Cursor = Cursors.AppStarting;
|
---|
205 | newAlgorithmButton.Enabled = openAlgorithmButton.Enabled = saveAlgorithmButton.Enabled = false;
|
---|
206 | algorithmViewHost.Enabled = false;
|
---|
207 |
|
---|
208 | var call = new Action<IAlgorithm, string, int>(XmlGenerator.Serialize);
|
---|
209 | int compression = 9;
|
---|
210 | if (saveFileDialog.FilterIndex == 1) compression = 0;
|
---|
211 | call.BeginInvoke(Content.Algorithm, saveFileDialog.FileName, compression, delegate(IAsyncResult a) {
|
---|
212 | try {
|
---|
213 | call.EndInvoke(a);
|
---|
214 | }
|
---|
215 | catch (Exception ex) {
|
---|
216 | Auxiliary.ShowErrorMessageBox(ex);
|
---|
217 | }
|
---|
218 | Invoke(new Action(delegate() {
|
---|
219 | algorithmViewHost.Enabled = true;
|
---|
220 | newAlgorithmButton.Enabled = openAlgorithmButton.Enabled = saveAlgorithmButton.Enabled = true;
|
---|
221 | this.Cursor = Cursors.Default;
|
---|
222 | }));
|
---|
223 | }, null);
|
---|
224 | }
|
---|
225 | }
|
---|
226 | private void startButton_Click(object sender, EventArgs e) {
|
---|
227 | Content.Start();
|
---|
228 | }
|
---|
229 | private void stopButton_Click(object sender, EventArgs e) {
|
---|
230 | Content.Stop();
|
---|
231 | }
|
---|
232 | private void resetButton_Click(object sender, EventArgs e) {
|
---|
233 | Content.Prepare();
|
---|
234 | }
|
---|
235 | #endregion
|
---|
236 |
|
---|
237 | #region Helpers
|
---|
238 | private void UpdateExecutionTimeTextBox() {
|
---|
239 | if (InvokeRequired)
|
---|
240 | Invoke(new Action(UpdateExecutionTimeTextBox));
|
---|
241 | else
|
---|
242 | executionTimeTextBox.Text = Content.ExecutionTime.ToString();
|
---|
243 | }
|
---|
244 | #endregion
|
---|
245 | }
|
---|
246 | }
|
---|