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;
|
---|
27 | using HeuristicLab.Core.Views;
|
---|
28 | using HeuristicLab.MainForm;
|
---|
29 | using HeuristicLab.Persistence.Default.Xml;
|
---|
30 |
|
---|
31 | namespace HeuristicLab.Optimization.Views {
|
---|
32 | /// <summary>
|
---|
33 | /// The base class for visual representations of items.
|
---|
34 | /// </summary>
|
---|
35 | [View("Algorithm View")]
|
---|
36 | [Content(typeof(Algorithm), true)]
|
---|
37 | [Content(typeof(IAlgorithm), false)]
|
---|
38 | public partial class AlgorithmView : NamedItemView {
|
---|
39 | private TypeSelectorDialog problemTypeSelectorDialog;
|
---|
40 |
|
---|
41 | public new IAlgorithm Content {
|
---|
42 | get { return (IAlgorithm)base.Content; }
|
---|
43 | set { base.Content = value; }
|
---|
44 | }
|
---|
45 |
|
---|
46 | /// <summary>
|
---|
47 | /// Initializes a new instance of <see cref="ItemBaseView"/>.
|
---|
48 | /// </summary>
|
---|
49 | public AlgorithmView() {
|
---|
50 | InitializeComponent();
|
---|
51 | }
|
---|
52 | /// <summary>
|
---|
53 | /// Intializes a new instance of <see cref="ItemBaseView"/> with the given <paramref name="item"/>.
|
---|
54 | /// </summary>
|
---|
55 | /// <param name="item">The item that should be displayed.</param>
|
---|
56 | public AlgorithmView(IAlgorithm content)
|
---|
57 | : this() {
|
---|
58 | Content = content;
|
---|
59 | }
|
---|
60 |
|
---|
61 | protected override void OnInitialized(EventArgs e) {
|
---|
62 | // Set order of tab pages according to z order.
|
---|
63 | // NOTE: This is required due to a bug in the VS designer.
|
---|
64 | List<Control> tabPages = new List<Control>();
|
---|
65 | for (int i = 0; i < tabControl.Controls.Count; i++) {
|
---|
66 | tabPages.Add(tabControl.Controls[i]);
|
---|
67 | }
|
---|
68 | tabControl.Controls.Clear();
|
---|
69 | foreach (Control control in tabPages)
|
---|
70 | tabControl.Controls.Add(control);
|
---|
71 |
|
---|
72 | base.OnInitialized(e);
|
---|
73 | }
|
---|
74 |
|
---|
75 | protected override void DeregisterContentEvents() {
|
---|
76 | Content.ExceptionOccurred -= new EventHandler<EventArgs<Exception>>(Content_ExceptionOccurred);
|
---|
77 | Content.ExecutionStateChanged -= new EventHandler(Content_ExecutionStateChanged);
|
---|
78 | Content.ExecutionTimeChanged -= new EventHandler(Content_ExecutionTimeChanged);
|
---|
79 | Content.Prepared -= new EventHandler(Content_Prepared);
|
---|
80 | Content.ProblemChanged -= new EventHandler(Content_ProblemChanged);
|
---|
81 | base.DeregisterContentEvents();
|
---|
82 | }
|
---|
83 | protected override void RegisterContentEvents() {
|
---|
84 | base.RegisterContentEvents();
|
---|
85 | Content.ExceptionOccurred += new EventHandler<EventArgs<Exception>>(Content_ExceptionOccurred);
|
---|
86 | Content.ExecutionStateChanged += new EventHandler(Content_ExecutionStateChanged);
|
---|
87 | Content.ExecutionTimeChanged += new EventHandler(Content_ExecutionTimeChanged);
|
---|
88 | Content.Prepared += new EventHandler(Content_Prepared);
|
---|
89 | Content.ProblemChanged += new EventHandler(Content_ProblemChanged);
|
---|
90 | }
|
---|
91 |
|
---|
92 | protected override void OnContentChanged() {
|
---|
93 | base.OnContentChanged();
|
---|
94 | if (Content == null) {
|
---|
95 | parameterCollectionView.Content = null;
|
---|
96 | problemViewHost.Content = null;
|
---|
97 | resultsView.Content = null;
|
---|
98 | runsView.Content = null;
|
---|
99 | tabControl.Enabled = false;
|
---|
100 | startButton.Enabled = pauseButton.Enabled = stopButton.Enabled = resetButton.Enabled = false;
|
---|
101 | executionTimeTextBox.Text = "-";
|
---|
102 | executionTimeTextBox.Enabled = false;
|
---|
103 | } else {
|
---|
104 | parameterCollectionView.Content = Content.Parameters;
|
---|
105 | saveProblemButton.Enabled = Content.Problem != null;
|
---|
106 | problemViewHost.ViewType = null;
|
---|
107 | problemViewHost.Content = Content.Problem;
|
---|
108 | resultsView.Content = Content.Results.AsReadOnly();
|
---|
109 | runsView.Content = Content.Runs;
|
---|
110 | tabControl.Enabled = true;
|
---|
111 | EnableDisableButtons();
|
---|
112 | executionTimeTextBox.Text = Content.ExecutionTime.ToString();
|
---|
113 | executionTimeTextBox.Enabled = true;
|
---|
114 | }
|
---|
115 | }
|
---|
116 |
|
---|
117 | protected override void OnClosed(FormClosedEventArgs e) {
|
---|
118 | if ((Content != null) && (Content.ExecutionState == ExecutionState.Started)) Content.Stop();
|
---|
119 | base.OnClosed(e);
|
---|
120 | }
|
---|
121 |
|
---|
122 | #region Content Events
|
---|
123 | protected virtual void Content_ProblemChanged(object sender, EventArgs e) {
|
---|
124 | if (InvokeRequired)
|
---|
125 | Invoke(new EventHandler(Content_ProblemChanged), sender, e);
|
---|
126 | else {
|
---|
127 | problemViewHost.ViewType = null;
|
---|
128 | problemViewHost.Content = Content.Problem;
|
---|
129 | saveProblemButton.Enabled = Content.Problem != null;
|
---|
130 | }
|
---|
131 | }
|
---|
132 | protected virtual void Content_Prepared(object sender, EventArgs e) {
|
---|
133 | if (InvokeRequired)
|
---|
134 | Invoke(new EventHandler(Content_Prepared), sender, e);
|
---|
135 | else
|
---|
136 | resultsView.Content = Content.Results.AsReadOnly();
|
---|
137 | }
|
---|
138 | protected virtual void Content_ExecutionStateChanged(object sender, EventArgs e) {
|
---|
139 | if (InvokeRequired)
|
---|
140 | Invoke(new EventHandler(Content_ExecutionStateChanged), sender, e);
|
---|
141 | else {
|
---|
142 | nameTextBox.Enabled = Content.ExecutionState != ExecutionState.Started;
|
---|
143 | descriptionTextBox.Enabled = Content.ExecutionState != ExecutionState.Started;
|
---|
144 | SaveEnabled = Content.ExecutionState != ExecutionState.Started;
|
---|
145 | parameterCollectionView.Enabled = Content.ExecutionState != ExecutionState.Started;
|
---|
146 | newProblemButton.Enabled = openProblemButton.Enabled = saveProblemButton.Enabled = Content.ExecutionState != ExecutionState.Started;
|
---|
147 | problemViewHost.Enabled = Content.ExecutionState != ExecutionState.Started;
|
---|
148 | EnableDisableButtons();
|
---|
149 | }
|
---|
150 | }
|
---|
151 | protected virtual void Content_ExecutionTimeChanged(object sender, EventArgs e) {
|
---|
152 | if (InvokeRequired)
|
---|
153 | Invoke(new EventHandler(Content_ExecutionTimeChanged), sender, e);
|
---|
154 | else
|
---|
155 | executionTimeTextBox.Text = Content.ExecutionTime.ToString();
|
---|
156 | }
|
---|
157 | protected virtual void Content_ExceptionOccurred(object sender, EventArgs<Exception> e) {
|
---|
158 | if (InvokeRequired)
|
---|
159 | Invoke(new EventHandler<EventArgs<Exception>>(Content_ExceptionOccurred), sender, e);
|
---|
160 | else
|
---|
161 | Auxiliary.ShowErrorMessageBox(e.Value);
|
---|
162 | }
|
---|
163 | #endregion
|
---|
164 |
|
---|
165 | #region Control Events
|
---|
166 | protected virtual void newProblemButton_Click(object sender, EventArgs e) {
|
---|
167 | if (problemTypeSelectorDialog == null) {
|
---|
168 | problemTypeSelectorDialog = new TypeSelectorDialog();
|
---|
169 | problemTypeSelectorDialog.Caption = "Select Problem";
|
---|
170 | problemTypeSelectorDialog.TypeSelector.Configure(Content.ProblemType, false, false);
|
---|
171 | }
|
---|
172 | if (problemTypeSelectorDialog.ShowDialog(this) == DialogResult.OK) {
|
---|
173 | Content.Problem = (IProblem)problemTypeSelectorDialog.TypeSelector.CreateInstanceOfSelectedType();
|
---|
174 | }
|
---|
175 | }
|
---|
176 | protected virtual void openProblemButton_Click(object sender, EventArgs e) {
|
---|
177 | openFileDialog.Title = "Open Problem";
|
---|
178 | if (openFileDialog.ShowDialog(this) == DialogResult.OK) {
|
---|
179 | this.Cursor = Cursors.AppStarting;
|
---|
180 | newProblemButton.Enabled = openProblemButton.Enabled = saveProblemButton.Enabled = false;
|
---|
181 | problemViewHost.Enabled = false;
|
---|
182 |
|
---|
183 | var call = new Func<string, object>(XmlParser.Deserialize);
|
---|
184 | call.BeginInvoke(openFileDialog.FileName, delegate(IAsyncResult a) {
|
---|
185 | IProblem problem = null;
|
---|
186 | try {
|
---|
187 | problem = call.EndInvoke(a) as IProblem;
|
---|
188 | } catch (Exception ex) {
|
---|
189 | Auxiliary.ShowErrorMessageBox(ex);
|
---|
190 | }
|
---|
191 | Invoke(new Action(delegate() {
|
---|
192 | if (problem == null)
|
---|
193 | MessageBox.Show(this, "The selected file does not contain a problem.", "Invalid File", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
---|
194 | else if (!Content.ProblemType.IsInstanceOfType(problem))
|
---|
195 | MessageBox.Show(this, "The selected file contains a problem type which is not supported by this algorithm.", "Invalid Problem Type", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
---|
196 | else
|
---|
197 | Content.Problem = problem;
|
---|
198 | problemViewHost.Enabled = true;
|
---|
199 | newProblemButton.Enabled = openProblemButton.Enabled = saveProblemButton.Enabled = true;
|
---|
200 | this.Cursor = Cursors.Default;
|
---|
201 | }));
|
---|
202 | }, null);
|
---|
203 | }
|
---|
204 | }
|
---|
205 | protected virtual void saveProblemButton_Click(object sender, EventArgs e) {
|
---|
206 | saveFileDialog.Title = "Save Problem";
|
---|
207 | if (saveFileDialog.ShowDialog(this) == DialogResult.OK) {
|
---|
208 | this.Cursor = Cursors.AppStarting;
|
---|
209 | newProblemButton.Enabled = openProblemButton.Enabled = saveProblemButton.Enabled = false;
|
---|
210 | problemViewHost.Enabled = false;
|
---|
211 |
|
---|
212 | var call = new Action<IProblem, string, int>(XmlGenerator.Serialize);
|
---|
213 | int compression = 9;
|
---|
214 | if (saveFileDialog.FilterIndex == 1) compression = 0;
|
---|
215 | call.BeginInvoke(Content.Problem, saveFileDialog.FileName, compression, delegate(IAsyncResult a) {
|
---|
216 | try {
|
---|
217 | call.EndInvoke(a);
|
---|
218 | }
|
---|
219 | catch (Exception ex) {
|
---|
220 | Auxiliary.ShowErrorMessageBox(ex);
|
---|
221 | }
|
---|
222 | Invoke(new Action(delegate() {
|
---|
223 | problemViewHost.Enabled = true;
|
---|
224 | newProblemButton.Enabled = openProblemButton.Enabled = saveProblemButton.Enabled = true;
|
---|
225 | this.Cursor = Cursors.Default;
|
---|
226 | }));
|
---|
227 | }, null);
|
---|
228 | }
|
---|
229 | }
|
---|
230 | protected virtual void startButton_Click(object sender, EventArgs e) {
|
---|
231 | Content.Start();
|
---|
232 | }
|
---|
233 | protected virtual void pauseButton_Click(object sender, EventArgs e) {
|
---|
234 | Content.Pause();
|
---|
235 | }
|
---|
236 | protected virtual void stopButton_Click(object sender, EventArgs e) {
|
---|
237 | Content.Stop();
|
---|
238 | }
|
---|
239 | protected virtual void resetButton_Click(object sender, EventArgs e) {
|
---|
240 | if (Content.Runs.Count > 0) {
|
---|
241 | if (MessageBox.Show(this, "Clear all runs executed so far?", "Clear All Runs?", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.Yes)
|
---|
242 | Content.Prepare(true);
|
---|
243 | else
|
---|
244 | Content.Prepare(false);
|
---|
245 | } else {
|
---|
246 | Content.Prepare();
|
---|
247 | }
|
---|
248 | }
|
---|
249 | protected virtual void problemPanel_DragEnterOver(object sender, DragEventArgs e) {
|
---|
250 | e.Effect = DragDropEffects.None;
|
---|
251 | Type type = e.Data.GetData("Type") as Type;
|
---|
252 | if ((type != null) && (Content.ProblemType.IsAssignableFrom(type))) {
|
---|
253 | if ((e.KeyState & 8) == 8) e.Effect = DragDropEffects.Copy; // CTRL key
|
---|
254 | else if ((e.KeyState & 4) == 4) e.Effect = DragDropEffects.Move; // SHIFT key
|
---|
255 | else if ((e.AllowedEffect & DragDropEffects.Link) == DragDropEffects.Link) e.Effect = DragDropEffects.Link;
|
---|
256 | else if ((e.AllowedEffect & DragDropEffects.Copy) == DragDropEffects.Copy) e.Effect = DragDropEffects.Copy;
|
---|
257 | else if ((e.AllowedEffect & DragDropEffects.Move) == DragDropEffects.Move) e.Effect = DragDropEffects.Move;
|
---|
258 | }
|
---|
259 | }
|
---|
260 | protected virtual void problemPanel_DragDrop(object sender, DragEventArgs e) {
|
---|
261 | if (e.Effect != DragDropEffects.None) {
|
---|
262 | IProblem problem = e.Data.GetData("Value") as IProblem;
|
---|
263 | if ((e.Effect & DragDropEffects.Copy) == DragDropEffects.Copy) problem = (IProblem)problem.Clone();
|
---|
264 | Content.Problem = problem;
|
---|
265 | }
|
---|
266 | }
|
---|
267 | #endregion
|
---|
268 |
|
---|
269 | #region Helpers
|
---|
270 | private void EnableDisableButtons() {
|
---|
271 | startButton.Enabled = (Content.ExecutionState == ExecutionState.Prepared) || (Content.ExecutionState == ExecutionState.Paused);
|
---|
272 | pauseButton.Enabled = Content.ExecutionState == ExecutionState.Started;
|
---|
273 | stopButton.Enabled = (Content.ExecutionState == ExecutionState.Started) || (Content.ExecutionState == ExecutionState.Paused);
|
---|
274 | resetButton.Enabled = Content.ExecutionState != ExecutionState.Started;
|
---|
275 | }
|
---|
276 | #endregion
|
---|
277 | }
|
---|
278 | }
|
---|