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.Core;
|
---|
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.AlgorithmChanged -= new EventHandler(Content_AlgorithmChanged);
|
---|
61 | Content.ExceptionOccurred -= new EventHandler<EventArgs<Exception>>(Content_ExceptionOccurred);
|
---|
62 | Content.ExecutionStateChanged -= new EventHandler(Content_ExecutionStateChanged);
|
---|
63 | Content.ExecutionTimeChanged -= new EventHandler(Content_ExecutionTimeChanged);
|
---|
64 | Content.RepetitionsChanged -= new EventHandler(Content_RepetitionsChanged);
|
---|
65 | base.DeregisterContentEvents();
|
---|
66 | }
|
---|
67 | protected override void RegisterContentEvents() {
|
---|
68 | base.RegisterContentEvents();
|
---|
69 | Content.AlgorithmChanged += new EventHandler(Content_AlgorithmChanged);
|
---|
70 | Content.ExceptionOccurred += new EventHandler<EventArgs<Exception>>(Content_ExceptionOccurred);
|
---|
71 | Content.ExecutionStateChanged += new EventHandler(Content_ExecutionStateChanged);
|
---|
72 | Content.ExecutionTimeChanged += new EventHandler(Content_ExecutionTimeChanged);
|
---|
73 | Content.RepetitionsChanged += new EventHandler(Content_RepetitionsChanged);
|
---|
74 | }
|
---|
75 |
|
---|
76 | protected override void OnContentChanged() {
|
---|
77 | base.OnContentChanged();
|
---|
78 | if (Content == null) {
|
---|
79 | repetitionsNumericUpDown.Value = 1;
|
---|
80 | repetitionsNumericUpDown.Enabled = false;
|
---|
81 | algorithmViewHost.Content = null;
|
---|
82 | runsView.Content = null;
|
---|
83 | tabControl.Enabled = false;
|
---|
84 | startButton.Enabled = pauseButton.Enabled = stopButton.Enabled = resetButton.Enabled = false;
|
---|
85 | executionTimeTextBox.Text = "-";
|
---|
86 | executionTimeTextBox.Enabled = false;
|
---|
87 | } else {
|
---|
88 | repetitionsNumericUpDown.Value = Content.Repetitions;
|
---|
89 | repetitionsNumericUpDown.Enabled = true;
|
---|
90 | saveAlgorithmButton.Enabled = Content.Algorithm != null;
|
---|
91 | algorithmViewHost.ViewType = null;
|
---|
92 | algorithmViewHost.Content = Content.Algorithm;
|
---|
93 | runsView.Content = Content.Runs;
|
---|
94 | tabControl.Enabled = true;
|
---|
95 | EnableDisableButtons();
|
---|
96 | executionTimeTextBox.Text = Content.ExecutionTime.ToString();
|
---|
97 | executionTimeTextBox.Enabled = true;
|
---|
98 | }
|
---|
99 | SetEnableStateOfControls();
|
---|
100 | }
|
---|
101 | protected override void OnReadOnlyChanged() {
|
---|
102 | base.OnReadOnlyChanged();
|
---|
103 | SetEnableStateOfControls();
|
---|
104 | }
|
---|
105 | private void SetEnableStateOfControls() {
|
---|
106 | repetitionsNumericUpDown.ReadOnly = ReadOnly;
|
---|
107 | algorithmViewHost.ReadOnly = ReadOnly;
|
---|
108 | runsView.ReadOnly = ReadOnly;
|
---|
109 | }
|
---|
110 |
|
---|
111 | protected override void OnClosed(FormClosedEventArgs e) {
|
---|
112 | if ((Content != null) && (Content.ExecutionState == ExecutionState.Started)) Content.Stop();
|
---|
113 | base.OnClosed(e);
|
---|
114 | }
|
---|
115 |
|
---|
116 | #region Content Events
|
---|
117 | private void Content_ExecutionStateChanged(object sender, EventArgs e) {
|
---|
118 | if (InvokeRequired)
|
---|
119 | Invoke(new EventHandler(Content_ExecutionStateChanged), sender, e);
|
---|
120 | else {
|
---|
121 | this.ReadOnly = Content.ExecutionState == ExecutionState.Started;
|
---|
122 | SaveEnabled = Content.ExecutionState != ExecutionState.Started;
|
---|
123 | newAlgorithmButton.Enabled = openAlgorithmButton.Enabled = saveAlgorithmButton.Enabled = Content.ExecutionState != ExecutionState.Started;
|
---|
124 | EnableDisableButtons();
|
---|
125 | }
|
---|
126 | }
|
---|
127 | private void Content_ExecutionTimeChanged(object sender, EventArgs e) {
|
---|
128 | if (InvokeRequired)
|
---|
129 | Invoke(new EventHandler(Content_ExecutionTimeChanged), sender, e);
|
---|
130 | else
|
---|
131 | executionTimeTextBox.Text = Content.ExecutionTime.ToString();
|
---|
132 | }
|
---|
133 | private void Content_ExceptionOccurred(object sender, EventArgs<Exception> e) {
|
---|
134 | if (InvokeRequired)
|
---|
135 | Invoke(new EventHandler<EventArgs<Exception>>(Content_ExceptionOccurred), sender, e);
|
---|
136 | else
|
---|
137 | Auxiliary.ShowErrorMessageBox(e.Value);
|
---|
138 | }
|
---|
139 | private void Content_AlgorithmChanged(object sender, EventArgs e) {
|
---|
140 | if (InvokeRequired)
|
---|
141 | Invoke(new EventHandler(Content_AlgorithmChanged), sender, e);
|
---|
142 | else {
|
---|
143 | algorithmViewHost.ViewType = null;
|
---|
144 | algorithmViewHost.Content = Content.Algorithm;
|
---|
145 | saveAlgorithmButton.Enabled = Content.Algorithm != null;
|
---|
146 | }
|
---|
147 | }
|
---|
148 | private void Content_RepetitionsChanged(object sender, EventArgs e) {
|
---|
149 | if (InvokeRequired)
|
---|
150 | Invoke(new EventHandler(Content_RepetitionsChanged), sender, e);
|
---|
151 | else
|
---|
152 | repetitionsNumericUpDown.Value = Content.Repetitions;
|
---|
153 | }
|
---|
154 | #endregion
|
---|
155 |
|
---|
156 | #region Control events
|
---|
157 | private void repetitionsNumericUpDown_Validated(object sender, System.EventArgs e) {
|
---|
158 | if (repetitionsNumericUpDown.Text == string.Empty)
|
---|
159 | repetitionsNumericUpDown.Text = repetitionsNumericUpDown.Value.ToString();
|
---|
160 | }
|
---|
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.Caption = "Available Algorithms";
|
---|
169 | algorithmTypeSelectorDialog.TypeSelector.Configure(typeof(IAlgorithm), false, false);
|
---|
170 | }
|
---|
171 | if (algorithmTypeSelectorDialog.ShowDialog(this) == DialogResult.OK) {
|
---|
172 | try {
|
---|
173 | Content.Algorithm = (IAlgorithm)algorithmTypeSelectorDialog.TypeSelector.CreateInstanceOfSelectedType();
|
---|
174 | }
|
---|
175 | catch (Exception ex) {
|
---|
176 | Auxiliary.ShowErrorMessageBox(ex);
|
---|
177 | }
|
---|
178 | }
|
---|
179 | }
|
---|
180 | private void openAlgorithmButton_Click(object sender, EventArgs e) {
|
---|
181 | openFileDialog.Title = "Open Algorithm";
|
---|
182 | if (openFileDialog.ShowDialog(this) == DialogResult.OK) {
|
---|
183 | this.Cursor = Cursors.AppStarting;
|
---|
184 | newAlgorithmButton.Enabled = openAlgorithmButton.Enabled = saveAlgorithmButton.Enabled = false;
|
---|
185 | algorithmViewHost.Enabled = false;
|
---|
186 |
|
---|
187 | var call = new Func<string, object>(XmlParser.Deserialize);
|
---|
188 | call.BeginInvoke(openFileDialog.FileName, delegate(IAsyncResult a) {
|
---|
189 | IAlgorithm algorithm = null;
|
---|
190 | try {
|
---|
191 | algorithm = call.EndInvoke(a) as IAlgorithm;
|
---|
192 | } catch (Exception ex) {
|
---|
193 | Auxiliary.ShowErrorMessageBox(ex);
|
---|
194 | }
|
---|
195 | Invoke(new Action(delegate() {
|
---|
196 | if (algorithm == null)
|
---|
197 | MessageBox.Show(this, "The selected file does not contain an algorithm.", "Invalid File", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
---|
198 | else
|
---|
199 | Content.Algorithm = algorithm;
|
---|
200 | algorithmViewHost.Enabled = true;
|
---|
201 | newAlgorithmButton.Enabled = openAlgorithmButton.Enabled = saveAlgorithmButton.Enabled = true;
|
---|
202 | this.Cursor = Cursors.Default;
|
---|
203 | }));
|
---|
204 | }, null);
|
---|
205 | }
|
---|
206 | }
|
---|
207 | private void saveAlgorithmButton_Click(object sender, EventArgs e) {
|
---|
208 | saveFileDialog.Title = "Save Algorithm";
|
---|
209 | if (saveFileDialog.ShowDialog(this) == DialogResult.OK) {
|
---|
210 | this.Cursor = Cursors.AppStarting;
|
---|
211 | newAlgorithmButton.Enabled = openAlgorithmButton.Enabled = saveAlgorithmButton.Enabled = false;
|
---|
212 | algorithmViewHost.Enabled = false;
|
---|
213 |
|
---|
214 | var call = new Action<IAlgorithm, string, int>(XmlGenerator.Serialize);
|
---|
215 | int compression = 9;
|
---|
216 | if (saveFileDialog.FilterIndex == 1) compression = 0;
|
---|
217 | call.BeginInvoke(Content.Algorithm, saveFileDialog.FileName, compression, delegate(IAsyncResult a) {
|
---|
218 | try {
|
---|
219 | call.EndInvoke(a);
|
---|
220 | }
|
---|
221 | catch (Exception ex) {
|
---|
222 | Auxiliary.ShowErrorMessageBox(ex);
|
---|
223 | }
|
---|
224 | Invoke(new Action(delegate() {
|
---|
225 | algorithmViewHost.Enabled = true;
|
---|
226 | newAlgorithmButton.Enabled = openAlgorithmButton.Enabled = saveAlgorithmButton.Enabled = true;
|
---|
227 | this.Cursor = Cursors.Default;
|
---|
228 | }));
|
---|
229 | }, null);
|
---|
230 | }
|
---|
231 | }
|
---|
232 | private void startButton_Click(object sender, EventArgs e) {
|
---|
233 | Content.Start();
|
---|
234 | }
|
---|
235 | private void pauseButton_Click(object sender, EventArgs e) {
|
---|
236 | Content.Pause();
|
---|
237 | }
|
---|
238 | private void stopButton_Click(object sender, EventArgs e) {
|
---|
239 | Content.Stop();
|
---|
240 | }
|
---|
241 | private void resetButton_Click(object sender, EventArgs e) {
|
---|
242 | if (Content.Runs.Count > 0) {
|
---|
243 | if (MessageBox.Show(this, "Clear all runs executed so far?", "Clear All Runs?", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.Yes)
|
---|
244 | Content.Prepare(true);
|
---|
245 | else
|
---|
246 | Content.Prepare(false);
|
---|
247 | } else {
|
---|
248 | Content.Prepare();
|
---|
249 | }
|
---|
250 | }
|
---|
251 | private void algorithmPanel_DragEnterOver(object sender, DragEventArgs e) {
|
---|
252 | e.Effect = DragDropEffects.None;
|
---|
253 | if (ReadOnly)
|
---|
254 | return;
|
---|
255 | Type type = e.Data.GetData("Type") as Type;
|
---|
256 | if ((type != null) && (typeof(IAlgorithm).IsAssignableFrom(type))) {
|
---|
257 | if ((e.KeyState & 8) == 8) e.Effect = DragDropEffects.Copy; // CTRL key
|
---|
258 | else if ((e.KeyState & 4) == 4) e.Effect = DragDropEffects.Move; // SHIFT key
|
---|
259 | else if ((e.AllowedEffect & DragDropEffects.Link) == DragDropEffects.Link) e.Effect = DragDropEffects.Link;
|
---|
260 | else if ((e.AllowedEffect & DragDropEffects.Copy) == DragDropEffects.Copy) e.Effect = DragDropEffects.Copy;
|
---|
261 | else if ((e.AllowedEffect & DragDropEffects.Move) == DragDropEffects.Move) e.Effect = DragDropEffects.Move;
|
---|
262 | }
|
---|
263 | }
|
---|
264 | private void algorithmPanel_DragDrop(object sender, DragEventArgs e) {
|
---|
265 | if (e.Effect != DragDropEffects.None) {
|
---|
266 | IAlgorithm algorithm = e.Data.GetData("Value") as IAlgorithm;
|
---|
267 | if ((e.Effect & DragDropEffects.Copy) == DragDropEffects.Copy) algorithm = (IAlgorithm)algorithm.Clone();
|
---|
268 | Content.Algorithm = algorithm;
|
---|
269 | }
|
---|
270 | }
|
---|
271 | #endregion
|
---|
272 |
|
---|
273 | #region Helpers
|
---|
274 | private void EnableDisableButtons() {
|
---|
275 | startButton.Enabled = (Content.ExecutionState == ExecutionState.Prepared) || (Content.ExecutionState == ExecutionState.Paused);
|
---|
276 | pauseButton.Enabled = Content.ExecutionState == ExecutionState.Started;
|
---|
277 | stopButton.Enabled = (Content.ExecutionState == ExecutionState.Started) || (Content.ExecutionState == ExecutionState.Paused);
|
---|
278 | resetButton.Enabled = Content.ExecutionState != ExecutionState.Started;
|
---|
279 | }
|
---|
280 | #endregion
|
---|
281 | }
|
---|
282 | }
|
---|