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 |
|
---|
53 | protected override void OnInitialized(EventArgs e) {
|
---|
54 | // Set order of tab pages according to z order.
|
---|
55 | // NOTE: This is required due to a bug in the VS designer.
|
---|
56 | List<Control> tabPages = new List<Control>();
|
---|
57 | for (int i = 0; i < tabControl.Controls.Count; i++) {
|
---|
58 | tabPages.Add(tabControl.Controls[i]);
|
---|
59 | }
|
---|
60 | tabControl.Controls.Clear();
|
---|
61 | foreach (Control control in tabPages)
|
---|
62 | tabControl.Controls.Add(control);
|
---|
63 |
|
---|
64 | base.OnInitialized(e);
|
---|
65 | }
|
---|
66 |
|
---|
67 | protected override void DeregisterContentEvents() {
|
---|
68 | Content.ExceptionOccurred -= new EventHandler<EventArgs<Exception>>(Content_ExceptionOccurred);
|
---|
69 | Content.ExecutionStateChanged -= new EventHandler(Content_ExecutionStateChanged);
|
---|
70 | Content.ExecutionTimeChanged -= new EventHandler(Content_ExecutionTimeChanged);
|
---|
71 | Content.Prepared -= new EventHandler(Content_Prepared);
|
---|
72 | Content.ProblemChanged -= new EventHandler(Content_ProblemChanged);
|
---|
73 | base.DeregisterContentEvents();
|
---|
74 | }
|
---|
75 | protected override void RegisterContentEvents() {
|
---|
76 | base.RegisterContentEvents();
|
---|
77 | Content.ExceptionOccurred += new EventHandler<EventArgs<Exception>>(Content_ExceptionOccurred);
|
---|
78 | Content.ExecutionStateChanged += new EventHandler(Content_ExecutionStateChanged);
|
---|
79 | Content.ExecutionTimeChanged += new EventHandler(Content_ExecutionTimeChanged);
|
---|
80 | Content.Prepared += new EventHandler(Content_Prepared);
|
---|
81 | Content.ProblemChanged += new EventHandler(Content_ProblemChanged);
|
---|
82 | }
|
---|
83 |
|
---|
84 | protected override void OnContentChanged() {
|
---|
85 | base.OnContentChanged();
|
---|
86 | if (Content == null) {
|
---|
87 | parameterCollectionView.Content = null;
|
---|
88 | problemViewHost.Content = null;
|
---|
89 | resultsView.Content = null;
|
---|
90 | runsView.Content = null;
|
---|
91 | executionTimeTextBox.Text = "-";
|
---|
92 | } else {
|
---|
93 | parameterCollectionView.Content = Content.Parameters;
|
---|
94 | problemViewHost.ViewType = null;
|
---|
95 | problemViewHost.Content = Content.Problem;
|
---|
96 | resultsView.Content = Content.Results.AsReadOnly();
|
---|
97 | runsView.Content = Content.Runs;
|
---|
98 | executionTimeTextBox.Text = Content.ExecutionTime.ToString();
|
---|
99 | }
|
---|
100 | SetEnableStateOfControls();
|
---|
101 | }
|
---|
102 |
|
---|
103 | protected override void OnReadOnlyChanged() {
|
---|
104 | base.OnReadOnlyChanged();
|
---|
105 | SetEnableStateOfControls();
|
---|
106 | }
|
---|
107 | private void SetEnableStateOfControls() {
|
---|
108 | parameterCollectionView.Enabled = Content != null;
|
---|
109 | newProblemButton.Enabled = Content != null && !ReadOnly;
|
---|
110 | openProblemButton.Enabled = Content != null && !ReadOnly;
|
---|
111 | problemViewHost.Enabled = Content != null;
|
---|
112 | resultsView.Enabled = Content != null;
|
---|
113 | runsView.Enabled = Content != null;
|
---|
114 | executionTimeTextBox.Enabled = Content != null;
|
---|
115 | SetEnabledStateOfExecutableButtons();
|
---|
116 | }
|
---|
117 |
|
---|
118 | protected override void OnClosed(FormClosedEventArgs e) {
|
---|
119 | if ((Content != null) && (Content.ExecutionState == ExecutionState.Started)) Content.Stop();
|
---|
120 | base.OnClosed(e);
|
---|
121 | }
|
---|
122 |
|
---|
123 | #region Content Events
|
---|
124 | protected virtual void Content_ProblemChanged(object sender, EventArgs e) {
|
---|
125 | if (InvokeRequired)
|
---|
126 | Invoke(new EventHandler(Content_ProblemChanged), sender, e);
|
---|
127 | else {
|
---|
128 | problemViewHost.ViewType = null;
|
---|
129 | problemViewHost.Content = Content.Problem;
|
---|
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 | ReadOnly = Content.ExecutionState == ExecutionState.Started;
|
---|
143 | Locked = Content.ExecutionState == ExecutionState.Started;
|
---|
144 | SetEnabledStateOfExecutableButtons();
|
---|
145 | }
|
---|
146 | }
|
---|
147 | protected virtual void Content_ExecutionTimeChanged(object sender, EventArgs e) {
|
---|
148 | if (InvokeRequired)
|
---|
149 | Invoke(new EventHandler(Content_ExecutionTimeChanged), sender, e);
|
---|
150 | else
|
---|
151 | executionTimeTextBox.Text = Content.ExecutionTime.ToString();
|
---|
152 | }
|
---|
153 | protected virtual void Content_ExceptionOccurred(object sender, EventArgs<Exception> e) {
|
---|
154 | if (InvokeRequired)
|
---|
155 | Invoke(new EventHandler<EventArgs<Exception>>(Content_ExceptionOccurred), sender, e);
|
---|
156 | else
|
---|
157 | Auxiliary.ShowErrorMessageBox(e.Value);
|
---|
158 | }
|
---|
159 | #endregion
|
---|
160 |
|
---|
161 | #region Control Events
|
---|
162 | protected virtual void newProblemButton_Click(object sender, EventArgs e) {
|
---|
163 | if (problemTypeSelectorDialog == null) {
|
---|
164 | problemTypeSelectorDialog = new TypeSelectorDialog();
|
---|
165 | problemTypeSelectorDialog.Caption = "Select Problem";
|
---|
166 | problemTypeSelectorDialog.TypeSelector.Caption = "Available Problems";
|
---|
167 | problemTypeSelectorDialog.TypeSelector.Configure(Content.ProblemType, false, true);
|
---|
168 | }
|
---|
169 | if (problemTypeSelectorDialog.ShowDialog(this) == DialogResult.OK) {
|
---|
170 | try {
|
---|
171 | Content.Problem = (IProblem)problemTypeSelectorDialog.TypeSelector.CreateInstanceOfSelectedType();
|
---|
172 | }
|
---|
173 | catch (Exception ex) {
|
---|
174 | Auxiliary.ShowErrorMessageBox(ex);
|
---|
175 | }
|
---|
176 | }
|
---|
177 | }
|
---|
178 | protected virtual void openProblemButton_Click(object sender, EventArgs e) {
|
---|
179 | openFileDialog.Title = "Open Problem";
|
---|
180 | if (openFileDialog.ShowDialog(this) == DialogResult.OK) {
|
---|
181 | newProblemButton.Enabled = openProblemButton.Enabled = false;
|
---|
182 | problemViewHost.Enabled = false;
|
---|
183 |
|
---|
184 | ContentManager.LoadAsync(openFileDialog.FileName, delegate(IStorableContent content, Exception error) {
|
---|
185 | try {
|
---|
186 | if (error != null) throw error;
|
---|
187 | IProblem problem = content as IProblem;
|
---|
188 | if (problem == null)
|
---|
189 | Invoke(new Action(() =>
|
---|
190 | MessageBox.Show(this, "The selected file does not contain a problem.", "Invalid File", MessageBoxButtons.OK, MessageBoxIcon.Error)));
|
---|
191 | else if (!Content.ProblemType.IsInstanceOfType(problem))
|
---|
192 | Invoke(new Action(() =>
|
---|
193 | MessageBox.Show(this, "The selected file contains a problem type which is not supported by this algorithm.", "Invalid Problem Type", MessageBoxButtons.OK, MessageBoxIcon.Error)));
|
---|
194 | else
|
---|
195 | Content.Problem = problem;
|
---|
196 | }
|
---|
197 | catch (Exception ex) {
|
---|
198 | Invoke(new Action(() => Auxiliary.ShowErrorMessageBox(ex)));
|
---|
199 | }
|
---|
200 | finally {
|
---|
201 | Invoke(new Action(delegate() {
|
---|
202 | problemViewHost.Enabled = true;
|
---|
203 | newProblemButton.Enabled = openProblemButton.Enabled = true;
|
---|
204 | }));
|
---|
205 | }
|
---|
206 | });
|
---|
207 | }
|
---|
208 | }
|
---|
209 | protected virtual void startButton_Click(object sender, EventArgs e) {
|
---|
210 | Content.Start();
|
---|
211 | }
|
---|
212 | protected virtual void pauseButton_Click(object sender, EventArgs e) {
|
---|
213 | Content.Pause();
|
---|
214 | }
|
---|
215 | protected virtual void stopButton_Click(object sender, EventArgs e) {
|
---|
216 | Content.Stop();
|
---|
217 | }
|
---|
218 | protected virtual void resetButton_Click(object sender, EventArgs e) {
|
---|
219 | Content.Prepare(false);
|
---|
220 | }
|
---|
221 | protected virtual void problemPanel_DragEnterOver(object sender, DragEventArgs e) {
|
---|
222 | e.Effect = DragDropEffects.None;
|
---|
223 | Type type = e.Data.GetData("Type") as Type;
|
---|
224 | if ((type != null) && (Content.ProblemType.IsAssignableFrom(type))) {
|
---|
225 | if ((e.KeyState & 32) == 32) e.Effect = DragDropEffects.Link; // ALT key
|
---|
226 | else if ((e.KeyState & 4) == 4) e.Effect = DragDropEffects.Move; // SHIFT key
|
---|
227 | else if ((e.AllowedEffect & DragDropEffects.Copy) == DragDropEffects.Copy) e.Effect = DragDropEffects.Copy;
|
---|
228 | else if ((e.AllowedEffect & DragDropEffects.Move) == DragDropEffects.Move) e.Effect = DragDropEffects.Move;
|
---|
229 | else if ((e.AllowedEffect & DragDropEffects.Link) == DragDropEffects.Link) e.Effect = DragDropEffects.Link;
|
---|
230 | }
|
---|
231 | }
|
---|
232 | protected virtual void problemPanel_DragDrop(object sender, DragEventArgs e) {
|
---|
233 | if (e.Effect != DragDropEffects.None) {
|
---|
234 | IProblem problem = e.Data.GetData("Value") as IProblem;
|
---|
235 | if ((e.Effect & DragDropEffects.Copy) == DragDropEffects.Copy) problem = (IProblem)problem.Clone();
|
---|
236 | Content.Problem = problem;
|
---|
237 | }
|
---|
238 | }
|
---|
239 | #endregion
|
---|
240 |
|
---|
241 | #region Helpers
|
---|
242 | private void SetEnabledStateOfExecutableButtons() {
|
---|
243 | if (Content == null) {
|
---|
244 | startButton.Enabled = pauseButton.Enabled = stopButton.Enabled = resetButton.Enabled = false;
|
---|
245 | } else {
|
---|
246 | startButton.Enabled = (Content.ExecutionState == ExecutionState.Prepared) || (Content.ExecutionState == ExecutionState.Paused);
|
---|
247 | pauseButton.Enabled = Content.ExecutionState == ExecutionState.Started;
|
---|
248 | stopButton.Enabled = (Content.ExecutionState == ExecutionState.Started) || (Content.ExecutionState == ExecutionState.Paused);
|
---|
249 | resetButton.Enabled = Content.ExecutionState != ExecutionState.Started;
|
---|
250 | }
|
---|
251 | }
|
---|
252 | #endregion
|
---|
253 | }
|
---|
254 | }
|
---|