[2851] | 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;
|
---|
[2916] | 23 | using System.Collections.Generic;
|
---|
[2851] | 24 | using System.Windows.Forms;
|
---|
| 25 | using HeuristicLab.Common;
|
---|
[3262] | 26 | using HeuristicLab.Core;
|
---|
[2851] | 27 | using HeuristicLab.Core.Views;
|
---|
| 28 | using HeuristicLab.MainForm;
|
---|
[3758] | 29 | using HeuristicLab.PluginInfrastructure;
|
---|
[2851] | 30 |
|
---|
[2852] | 31 | namespace HeuristicLab.Optimization.Views {
|
---|
[2851] | 32 | /// <summary>
|
---|
| 33 | /// The base class for visual representations of items.
|
---|
| 34 | /// </summary>
|
---|
[2917] | 35 | [View("Algorithm View")]
|
---|
[2851] | 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 |
|
---|
[5237] | 53 | protected override void Dispose(bool disposing) {
|
---|
| 54 | if (disposing) {
|
---|
| 55 | if (problemTypeSelectorDialog != null) problemTypeSelectorDialog.Dispose();
|
---|
| 56 | if (components != null) components.Dispose();
|
---|
| 57 | }
|
---|
| 58 | base.Dispose(disposing);
|
---|
| 59 | }
|
---|
| 60 |
|
---|
[2916] | 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 |
|
---|
[2851] | 75 | protected override void DeregisterContentEvents() {
|
---|
| 76 | Content.ExceptionOccurred -= new EventHandler<EventArgs<Exception>>(Content_ExceptionOccurred);
|
---|
[3262] | 77 | Content.ExecutionStateChanged -= new EventHandler(Content_ExecutionStateChanged);
|
---|
[2851] | 78 | Content.ExecutionTimeChanged -= new EventHandler(Content_ExecutionTimeChanged);
|
---|
| 79 | Content.Prepared -= new EventHandler(Content_Prepared);
|
---|
[4070] | 80 | Content.Started -= new EventHandler(Content_Started);
|
---|
| 81 | Content.Paused -= new EventHandler(Content_Paused);
|
---|
| 82 | Content.Stopped -= new EventHandler(Content_Stopped);
|
---|
[2851] | 83 | Content.ProblemChanged -= new EventHandler(Content_ProblemChanged);
|
---|
[4102] | 84 | Content.StoreAlgorithmInEachRunChanged -= new EventHandler(Content_StoreAlgorithmInEachRunChanged);
|
---|
[2851] | 85 | base.DeregisterContentEvents();
|
---|
| 86 | }
|
---|
| 87 | protected override void RegisterContentEvents() {
|
---|
| 88 | base.RegisterContentEvents();
|
---|
| 89 | Content.ExceptionOccurred += new EventHandler<EventArgs<Exception>>(Content_ExceptionOccurred);
|
---|
[3262] | 90 | Content.ExecutionStateChanged += new EventHandler(Content_ExecutionStateChanged);
|
---|
[2851] | 91 | Content.ExecutionTimeChanged += new EventHandler(Content_ExecutionTimeChanged);
|
---|
| 92 | Content.Prepared += new EventHandler(Content_Prepared);
|
---|
[4070] | 93 | Content.Started += new EventHandler(Content_Started);
|
---|
| 94 | Content.Paused += new EventHandler(Content_Paused);
|
---|
| 95 | Content.Stopped += new EventHandler(Content_Stopped);
|
---|
[2851] | 96 | Content.ProblemChanged += new EventHandler(Content_ProblemChanged);
|
---|
[4102] | 97 | Content.StoreAlgorithmInEachRunChanged += new EventHandler(Content_StoreAlgorithmInEachRunChanged);
|
---|
[2851] | 98 | }
|
---|
| 99 |
|
---|
| 100 | protected override void OnContentChanged() {
|
---|
| 101 | base.OnContentChanged();
|
---|
| 102 | if (Content == null) {
|
---|
| 103 | parameterCollectionView.Content = null;
|
---|
| 104 | problemViewHost.Content = null;
|
---|
[2882] | 105 | resultsView.Content = null;
|
---|
[3275] | 106 | runsView.Content = null;
|
---|
[4102] | 107 | storeAlgorithmInEachRunCheckBox.Checked = true;
|
---|
[2851] | 108 | executionTimeTextBox.Text = "-";
|
---|
| 109 | } else {
|
---|
[4540] | 110 | Locked = ReadOnly = Content.ExecutionState == ExecutionState.Started;
|
---|
[2851] | 111 | parameterCollectionView.Content = Content.Parameters;
|
---|
[2949] | 112 | problemViewHost.ViewType = null;
|
---|
[2851] | 113 | problemViewHost.Content = Content.Problem;
|
---|
[3226] | 114 | resultsView.Content = Content.Results.AsReadOnly();
|
---|
[3275] | 115 | runsView.Content = Content.Runs;
|
---|
[4102] | 116 | storeAlgorithmInEachRunCheckBox.Checked = Content.StoreAlgorithmInEachRun;
|
---|
[3262] | 117 | executionTimeTextBox.Text = Content.ExecutionTime.ToString();
|
---|
[2851] | 118 | }
|
---|
| 119 | }
|
---|
| 120 |
|
---|
[3904] | 121 | protected override void SetEnabledStateOfControls() {
|
---|
| 122 | base.SetEnabledStateOfControls();
|
---|
[3454] | 123 | parameterCollectionView.Enabled = Content != null;
|
---|
| 124 | newProblemButton.Enabled = Content != null && !ReadOnly;
|
---|
| 125 | openProblemButton.Enabled = Content != null && !ReadOnly;
|
---|
| 126 | problemViewHost.Enabled = Content != null;
|
---|
| 127 | resultsView.Enabled = Content != null;
|
---|
| 128 | runsView.Enabled = Content != null;
|
---|
[4102] | 129 | storeAlgorithmInEachRunCheckBox.Enabled = Content != null && !ReadOnly;
|
---|
[3454] | 130 | executionTimeTextBox.Enabled = Content != null;
|
---|
| 131 | SetEnabledStateOfExecutableButtons();
|
---|
[3367] | 132 | }
|
---|
| 133 |
|
---|
[2955] | 134 | protected override void OnClosed(FormClosedEventArgs e) {
|
---|
[3262] | 135 | if ((Content != null) && (Content.ExecutionState == ExecutionState.Started)) Content.Stop();
|
---|
[2955] | 136 | base.OnClosed(e);
|
---|
| 137 | }
|
---|
| 138 |
|
---|
[2851] | 139 | #region Content Events
|
---|
[2998] | 140 | protected virtual void Content_ProblemChanged(object sender, EventArgs e) {
|
---|
[2851] | 141 | if (InvokeRequired)
|
---|
| 142 | Invoke(new EventHandler(Content_ProblemChanged), sender, e);
|
---|
| 143 | else {
|
---|
[2949] | 144 | problemViewHost.ViewType = null;
|
---|
[2851] | 145 | problemViewHost.Content = Content.Problem;
|
---|
| 146 | }
|
---|
| 147 | }
|
---|
[4070] | 148 | protected virtual void Content_ExecutionStateChanged(object sender, EventArgs e) {
|
---|
| 149 | if (InvokeRequired)
|
---|
| 150 | Invoke(new EventHandler(Content_ExecutionStateChanged), sender, e);
|
---|
| 151 | else
|
---|
| 152 | startButton.Enabled = pauseButton.Enabled = stopButton.Enabled = resetButton.Enabled = false;
|
---|
| 153 | }
|
---|
[4102] | 154 | protected virtual void Content_ExecutionTimeChanged(object sender, EventArgs e) {
|
---|
| 155 | if (InvokeRequired)
|
---|
| 156 | Invoke(new EventHandler(Content_ExecutionTimeChanged), sender, e);
|
---|
| 157 | else
|
---|
[4216] | 158 | executionTimeTextBox.Text = Content == null ? "-" : Content.ExecutionTime.ToString();
|
---|
[4102] | 159 | }
|
---|
| 160 | protected virtual void Content_StoreAlgorithmInEachRunChanged(object sender, EventArgs e) {
|
---|
| 161 | if (InvokeRequired)
|
---|
| 162 | Invoke(new EventHandler(Content_StoreAlgorithmInEachRunChanged), sender, e);
|
---|
| 163 | else
|
---|
| 164 | storeAlgorithmInEachRunCheckBox.Checked = Content.StoreAlgorithmInEachRun;
|
---|
| 165 | }
|
---|
[3262] | 166 | protected virtual void Content_Prepared(object sender, EventArgs e) {
|
---|
[2851] | 167 | if (InvokeRequired)
|
---|
[3262] | 168 | Invoke(new EventHandler(Content_Prepared), sender, e);
|
---|
[4070] | 169 | else {
|
---|
[3262] | 170 | resultsView.Content = Content.Results.AsReadOnly();
|
---|
[4070] | 171 | ReadOnly = Locked = false;
|
---|
| 172 | SetEnabledStateOfExecutableButtons();
|
---|
| 173 | }
|
---|
[3262] | 174 | }
|
---|
[4070] | 175 | protected virtual void Content_Started(object sender, EventArgs e) {
|
---|
[3262] | 176 | if (InvokeRequired)
|
---|
[4070] | 177 | Invoke(new EventHandler(Content_Started), sender, e);
|
---|
[2851] | 178 | else {
|
---|
[4070] | 179 | ReadOnly = Locked = true;
|
---|
[3454] | 180 | SetEnabledStateOfExecutableButtons();
|
---|
[2851] | 181 | }
|
---|
| 182 | }
|
---|
[4070] | 183 | protected virtual void Content_Paused(object sender, EventArgs e) {
|
---|
| 184 | if (InvokeRequired)
|
---|
| 185 | Invoke(new EventHandler(Content_Paused), sender, e);
|
---|
| 186 | else {
|
---|
| 187 | ReadOnly = Locked = false;
|
---|
| 188 | SetEnabledStateOfExecutableButtons();
|
---|
| 189 | }
|
---|
| 190 | }
|
---|
| 191 | protected virtual void Content_Stopped(object sender, EventArgs e) {
|
---|
| 192 | if (InvokeRequired)
|
---|
| 193 | Invoke(new EventHandler(Content_Stopped), sender, e);
|
---|
| 194 | else {
|
---|
| 195 | ReadOnly = Locked = false;
|
---|
| 196 | SetEnabledStateOfExecutableButtons();
|
---|
| 197 | }
|
---|
| 198 | }
|
---|
[2851] | 199 | protected virtual void Content_ExceptionOccurred(object sender, EventArgs<Exception> e) {
|
---|
| 200 | if (InvokeRequired)
|
---|
| 201 | Invoke(new EventHandler<EventArgs<Exception>>(Content_ExceptionOccurred), sender, e);
|
---|
| 202 | else
|
---|
[3758] | 203 | ErrorHandling.ShowErrorDialog(this, e.Value);
|
---|
[2851] | 204 | }
|
---|
| 205 | #endregion
|
---|
| 206 |
|
---|
[3299] | 207 | #region Control Events
|
---|
[2998] | 208 | protected virtual void newProblemButton_Click(object sender, EventArgs e) {
|
---|
[2851] | 209 | if (problemTypeSelectorDialog == null) {
|
---|
| 210 | problemTypeSelectorDialog = new TypeSelectorDialog();
|
---|
| 211 | problemTypeSelectorDialog.Caption = "Select Problem";
|
---|
[3407] | 212 | problemTypeSelectorDialog.TypeSelector.Caption = "Available Problems";
|
---|
[3588] | 213 | problemTypeSelectorDialog.TypeSelector.Configure(Content.ProblemType, false, true);
|
---|
[2851] | 214 | }
|
---|
| 215 | if (problemTypeSelectorDialog.ShowDialog(this) == DialogResult.OK) {
|
---|
[3407] | 216 | try {
|
---|
| 217 | Content.Problem = (IProblem)problemTypeSelectorDialog.TypeSelector.CreateInstanceOfSelectedType();
|
---|
| 218 | }
|
---|
| 219 | catch (Exception ex) {
|
---|
[3758] | 220 | ErrorHandling.ShowErrorDialog(this, ex);
|
---|
[3407] | 221 | }
|
---|
[2851] | 222 | }
|
---|
| 223 | }
|
---|
[2998] | 224 | protected virtual void openProblemButton_Click(object sender, EventArgs e) {
|
---|
[2851] | 225 | openFileDialog.Title = "Open Problem";
|
---|
| 226 | if (openFileDialog.ShowDialog(this) == DialogResult.OK) {
|
---|
[3454] | 227 | newProblemButton.Enabled = openProblemButton.Enabled = false;
|
---|
[3177] | 228 | problemViewHost.Enabled = false;
|
---|
[2954] | 229 |
|
---|
[3500] | 230 | ContentManager.LoadAsync(openFileDialog.FileName, delegate(IStorableContent content, Exception error) {
|
---|
[2954] | 231 | try {
|
---|
[3500] | 232 | if (error != null) throw error;
|
---|
| 233 | IProblem problem = content as IProblem;
|
---|
[2954] | 234 | if (problem == null)
|
---|
[4068] | 235 | Invoke(new Action(() =>
|
---|
[3548] | 236 | MessageBox.Show(this, "The selected file does not contain a problem.", "Invalid File", MessageBoxButtons.OK, MessageBoxIcon.Error)));
|
---|
[2954] | 237 | else if (!Content.ProblemType.IsInstanceOfType(problem))
|
---|
[3548] | 238 | Invoke(new Action(() =>
|
---|
| 239 | MessageBox.Show(this, "The selected file contains a problem type which is not supported by this algorithm.", "Invalid Problem Type", MessageBoxButtons.OK, MessageBoxIcon.Error)));
|
---|
[2954] | 240 | else
|
---|
| 241 | Content.Problem = problem;
|
---|
[3500] | 242 | }
|
---|
| 243 | catch (Exception ex) {
|
---|
[3758] | 244 | Invoke(new Action(() => ErrorHandling.ShowErrorDialog(this, ex)));
|
---|
[3500] | 245 | }
|
---|
| 246 | finally {
|
---|
| 247 | Invoke(new Action(delegate() {
|
---|
| 248 | problemViewHost.Enabled = true;
|
---|
| 249 | newProblemButton.Enabled = openProblemButton.Enabled = true;
|
---|
| 250 | }));
|
---|
| 251 | }
|
---|
| 252 | });
|
---|
[2851] | 253 | }
|
---|
| 254 | }
|
---|
[4102] | 255 | protected virtual void storeAlgorithmInEachRunCheckBox_CheckedChanged(object sender, EventArgs e) {
|
---|
| 256 | if (Content != null) Content.StoreAlgorithmInEachRun = storeAlgorithmInEachRunCheckBox.Checked;
|
---|
| 257 | }
|
---|
[2851] | 258 | protected virtual void startButton_Click(object sender, EventArgs e) {
|
---|
| 259 | Content.Start();
|
---|
| 260 | }
|
---|
[3262] | 261 | protected virtual void pauseButton_Click(object sender, EventArgs e) {
|
---|
| 262 | Content.Pause();
|
---|
| 263 | }
|
---|
[2851] | 264 | protected virtual void stopButton_Click(object sender, EventArgs e) {
|
---|
| 265 | Content.Stop();
|
---|
| 266 | }
|
---|
| 267 | protected virtual void resetButton_Click(object sender, EventArgs e) {
|
---|
[3716] | 268 | Content.Prepare(false);
|
---|
[2851] | 269 | }
|
---|
[4522] | 270 | protected virtual void problemTabPage_DragEnterOver(object sender, DragEventArgs e) {
|
---|
[3299] | 271 | e.Effect = DragDropEffects.None;
|
---|
| 272 | Type type = e.Data.GetData("Type") as Type;
|
---|
| 273 | if ((type != null) && (Content.ProblemType.IsAssignableFrom(type))) {
|
---|
[3694] | 274 | if ((e.KeyState & 32) == 32) e.Effect = DragDropEffects.Link; // ALT key
|
---|
[3299] | 275 | else if ((e.KeyState & 4) == 4) e.Effect = DragDropEffects.Move; // SHIFT key
|
---|
| 276 | else if ((e.AllowedEffect & DragDropEffects.Copy) == DragDropEffects.Copy) e.Effect = DragDropEffects.Copy;
|
---|
| 277 | else if ((e.AllowedEffect & DragDropEffects.Move) == DragDropEffects.Move) e.Effect = DragDropEffects.Move;
|
---|
[3526] | 278 | else if ((e.AllowedEffect & DragDropEffects.Link) == DragDropEffects.Link) e.Effect = DragDropEffects.Link;
|
---|
[3299] | 279 | }
|
---|
| 280 | }
|
---|
[4522] | 281 | protected virtual void problemTabPage_DragDrop(object sender, DragEventArgs e) {
|
---|
[3299] | 282 | if (e.Effect != DragDropEffects.None) {
|
---|
| 283 | IProblem problem = e.Data.GetData("Value") as IProblem;
|
---|
| 284 | if ((e.Effect & DragDropEffects.Copy) == DragDropEffects.Copy) problem = (IProblem)problem.Clone();
|
---|
| 285 | Content.Problem = problem;
|
---|
| 286 | }
|
---|
| 287 | }
|
---|
[2851] | 288 | #endregion
|
---|
| 289 |
|
---|
| 290 | #region Helpers
|
---|
[3454] | 291 | private void SetEnabledStateOfExecutableButtons() {
|
---|
| 292 | if (Content == null) {
|
---|
| 293 | startButton.Enabled = pauseButton.Enabled = stopButton.Enabled = resetButton.Enabled = false;
|
---|
| 294 | } else {
|
---|
| 295 | startButton.Enabled = (Content.ExecutionState == ExecutionState.Prepared) || (Content.ExecutionState == ExecutionState.Paused);
|
---|
| 296 | pauseButton.Enabled = Content.ExecutionState == ExecutionState.Started;
|
---|
| 297 | stopButton.Enabled = (Content.ExecutionState == ExecutionState.Started) || (Content.ExecutionState == ExecutionState.Paused);
|
---|
| 298 | resetButton.Enabled = Content.ExecutionState != ExecutionState.Started;
|
---|
| 299 | }
|
---|
[2851] | 300 | }
|
---|
| 301 | #endregion
|
---|
| 302 | }
|
---|
| 303 | }
|
---|