[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;
|
---|
| 29 | using HeuristicLab.Persistence.Default.Xml;
|
---|
| 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 | /// <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 |
|
---|
[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);
|
---|
| 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);
|
---|
[3262] | 86 | Content.ExecutionStateChanged += new EventHandler(Content_ExecutionStateChanged);
|
---|
[2851] | 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;
|
---|
[2882] | 97 | resultsView.Content = null;
|
---|
[3275] | 98 | runsView.Content = null;
|
---|
[2851] | 99 | executionTimeTextBox.Text = "-";
|
---|
| 100 | } else {
|
---|
| 101 | parameterCollectionView.Content = Content.Parameters;
|
---|
[2949] | 102 | problemViewHost.ViewType = null;
|
---|
[2851] | 103 | problemViewHost.Content = Content.Problem;
|
---|
[3226] | 104 | resultsView.Content = Content.Results.AsReadOnly();
|
---|
[3275] | 105 | runsView.Content = Content.Runs;
|
---|
[3262] | 106 | executionTimeTextBox.Text = Content.ExecutionTime.ToString();
|
---|
[2851] | 107 | }
|
---|
[3367] | 108 | SetEnableStateOfControls();
|
---|
[2851] | 109 | }
|
---|
| 110 |
|
---|
[3367] | 111 | protected override void OnReadOnlyChanged() {
|
---|
| 112 | base.OnReadOnlyChanged();
|
---|
| 113 | SetEnableStateOfControls();
|
---|
| 114 | }
|
---|
| 115 | private void SetEnableStateOfControls() {
|
---|
[3454] | 116 | parameterCollectionView.Enabled = Content != null;
|
---|
| 117 | newProblemButton.Enabled = Content != null && !ReadOnly;
|
---|
| 118 | openProblemButton.Enabled = Content != null && !ReadOnly;
|
---|
| 119 | problemViewHost.Enabled = Content != null;
|
---|
| 120 | resultsView.Enabled = Content != null;
|
---|
| 121 | runsView.Enabled = Content != null;
|
---|
| 122 | executionTimeTextBox.Enabled = Content != null;
|
---|
| 123 | SetEnabledStateOfExecutableButtons();
|
---|
[3367] | 124 | }
|
---|
| 125 |
|
---|
[2955] | 126 | protected override void OnClosed(FormClosedEventArgs e) {
|
---|
[3262] | 127 | if ((Content != null) && (Content.ExecutionState == ExecutionState.Started)) Content.Stop();
|
---|
[2955] | 128 | base.OnClosed(e);
|
---|
| 129 | }
|
---|
| 130 |
|
---|
[2851] | 131 | #region Content Events
|
---|
[2998] | 132 | protected virtual void Content_ProblemChanged(object sender, EventArgs e) {
|
---|
[2851] | 133 | if (InvokeRequired)
|
---|
| 134 | Invoke(new EventHandler(Content_ProblemChanged), sender, e);
|
---|
| 135 | else {
|
---|
[2949] | 136 | problemViewHost.ViewType = null;
|
---|
[2851] | 137 | problemViewHost.Content = Content.Problem;
|
---|
| 138 | }
|
---|
| 139 | }
|
---|
[3262] | 140 | protected virtual void Content_Prepared(object sender, EventArgs e) {
|
---|
[2851] | 141 | if (InvokeRequired)
|
---|
[3262] | 142 | Invoke(new EventHandler(Content_Prepared), sender, e);
|
---|
| 143 | else
|
---|
| 144 | resultsView.Content = Content.Results.AsReadOnly();
|
---|
| 145 | }
|
---|
| 146 | protected virtual void Content_ExecutionStateChanged(object sender, EventArgs e) {
|
---|
| 147 | if (InvokeRequired)
|
---|
| 148 | Invoke(new EventHandler(Content_ExecutionStateChanged), sender, e);
|
---|
[2851] | 149 | else {
|
---|
[3454] | 150 | ReadOnly = Content.ExecutionState == ExecutionState.Started;
|
---|
[3416] | 151 | Locked = Content.ExecutionState == ExecutionState.Started;
|
---|
[3454] | 152 | SetEnabledStateOfExecutableButtons();
|
---|
[2851] | 153 | }
|
---|
| 154 | }
|
---|
| 155 | protected virtual void Content_ExecutionTimeChanged(object sender, EventArgs e) {
|
---|
[3262] | 156 | if (InvokeRequired)
|
---|
| 157 | Invoke(new EventHandler(Content_ExecutionTimeChanged), sender, e);
|
---|
| 158 | else
|
---|
| 159 | executionTimeTextBox.Text = Content.ExecutionTime.ToString();
|
---|
[2851] | 160 | }
|
---|
| 161 | protected virtual void Content_ExceptionOccurred(object sender, EventArgs<Exception> e) {
|
---|
| 162 | if (InvokeRequired)
|
---|
| 163 | Invoke(new EventHandler<EventArgs<Exception>>(Content_ExceptionOccurred), sender, e);
|
---|
| 164 | else
|
---|
| 165 | Auxiliary.ShowErrorMessageBox(e.Value);
|
---|
| 166 | }
|
---|
| 167 | #endregion
|
---|
| 168 |
|
---|
[3299] | 169 | #region Control Events
|
---|
[2998] | 170 | protected virtual void newProblemButton_Click(object sender, EventArgs e) {
|
---|
[2851] | 171 | if (problemTypeSelectorDialog == null) {
|
---|
| 172 | problemTypeSelectorDialog = new TypeSelectorDialog();
|
---|
| 173 | problemTypeSelectorDialog.Caption = "Select Problem";
|
---|
[3407] | 174 | problemTypeSelectorDialog.TypeSelector.Caption = "Available Problems";
|
---|
[2852] | 175 | problemTypeSelectorDialog.TypeSelector.Configure(Content.ProblemType, false, false);
|
---|
[2851] | 176 | }
|
---|
| 177 | if (problemTypeSelectorDialog.ShowDialog(this) == DialogResult.OK) {
|
---|
[3407] | 178 | try {
|
---|
| 179 | Content.Problem = (IProblem)problemTypeSelectorDialog.TypeSelector.CreateInstanceOfSelectedType();
|
---|
| 180 | }
|
---|
| 181 | catch (Exception ex) {
|
---|
| 182 | Auxiliary.ShowErrorMessageBox(ex);
|
---|
| 183 | }
|
---|
[2851] | 184 | }
|
---|
| 185 | }
|
---|
[2998] | 186 | protected virtual void openProblemButton_Click(object sender, EventArgs e) {
|
---|
[2851] | 187 | openFileDialog.Title = "Open Problem";
|
---|
| 188 | if (openFileDialog.ShowDialog(this) == DialogResult.OK) {
|
---|
[2954] | 189 | this.Cursor = Cursors.AppStarting;
|
---|
[3454] | 190 | newProblemButton.Enabled = openProblemButton.Enabled = false;
|
---|
[3177] | 191 | problemViewHost.Enabled = false;
|
---|
[2954] | 192 |
|
---|
| 193 | var call = new Func<string, object>(XmlParser.Deserialize);
|
---|
| 194 | call.BeginInvoke(openFileDialog.FileName, delegate(IAsyncResult a) {
|
---|
| 195 | IProblem problem = null;
|
---|
| 196 | try {
|
---|
| 197 | problem = call.EndInvoke(a) as IProblem;
|
---|
| 198 | } catch (Exception ex) {
|
---|
| 199 | Auxiliary.ShowErrorMessageBox(ex);
|
---|
| 200 | }
|
---|
| 201 | Invoke(new Action(delegate() {
|
---|
| 202 | if (problem == null)
|
---|
| 203 | MessageBox.Show(this, "The selected file does not contain a problem.", "Invalid File", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
---|
| 204 | else if (!Content.ProblemType.IsInstanceOfType(problem))
|
---|
| 205 | MessageBox.Show(this, "The selected file contains a problem type which is not supported by this algorithm.", "Invalid Problem Type", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
---|
| 206 | else
|
---|
| 207 | Content.Problem = problem;
|
---|
[3177] | 208 | problemViewHost.Enabled = true;
|
---|
[3454] | 209 | newProblemButton.Enabled = openProblemButton.Enabled = true;
|
---|
[2954] | 210 | this.Cursor = Cursors.Default;
|
---|
| 211 | }));
|
---|
| 212 | }, null);
|
---|
[2851] | 213 | }
|
---|
| 214 | }
|
---|
| 215 | protected virtual void startButton_Click(object sender, EventArgs e) {
|
---|
| 216 | Content.Start();
|
---|
| 217 | }
|
---|
[3262] | 218 | protected virtual void pauseButton_Click(object sender, EventArgs e) {
|
---|
| 219 | Content.Pause();
|
---|
| 220 | }
|
---|
[2851] | 221 | protected virtual void stopButton_Click(object sender, EventArgs e) {
|
---|
| 222 | Content.Stop();
|
---|
| 223 | }
|
---|
| 224 | protected virtual void resetButton_Click(object sender, EventArgs e) {
|
---|
[3275] | 225 | if (Content.Runs.Count > 0) {
|
---|
| 226 | if (MessageBox.Show(this, "Clear all runs executed so far?", "Clear All Runs?", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.Yes)
|
---|
[3274] | 227 | Content.Prepare(true);
|
---|
| 228 | else
|
---|
| 229 | Content.Prepare(false);
|
---|
| 230 | } else {
|
---|
| 231 | Content.Prepare();
|
---|
| 232 | }
|
---|
[2851] | 233 | }
|
---|
[3299] | 234 | protected virtual void problemPanel_DragEnterOver(object sender, DragEventArgs e) {
|
---|
| 235 | e.Effect = DragDropEffects.None;
|
---|
| 236 | Type type = e.Data.GetData("Type") as Type;
|
---|
| 237 | if ((type != null) && (Content.ProblemType.IsAssignableFrom(type))) {
|
---|
| 238 | if ((e.KeyState & 8) == 8) e.Effect = DragDropEffects.Copy; // CTRL key
|
---|
| 239 | else if ((e.KeyState & 4) == 4) e.Effect = DragDropEffects.Move; // SHIFT key
|
---|
| 240 | else if ((e.AllowedEffect & DragDropEffects.Link) == DragDropEffects.Link) e.Effect = DragDropEffects.Link;
|
---|
| 241 | else if ((e.AllowedEffect & DragDropEffects.Copy) == DragDropEffects.Copy) e.Effect = DragDropEffects.Copy;
|
---|
| 242 | else if ((e.AllowedEffect & DragDropEffects.Move) == DragDropEffects.Move) e.Effect = DragDropEffects.Move;
|
---|
| 243 | }
|
---|
| 244 | }
|
---|
| 245 | protected virtual void problemPanel_DragDrop(object sender, DragEventArgs e) {
|
---|
| 246 | if (e.Effect != DragDropEffects.None) {
|
---|
| 247 | IProblem problem = e.Data.GetData("Value") as IProblem;
|
---|
| 248 | if ((e.Effect & DragDropEffects.Copy) == DragDropEffects.Copy) problem = (IProblem)problem.Clone();
|
---|
| 249 | Content.Problem = problem;
|
---|
| 250 | }
|
---|
| 251 | }
|
---|
[2851] | 252 | #endregion
|
---|
| 253 |
|
---|
| 254 | #region Helpers
|
---|
[3454] | 255 | private void SetEnabledStateOfExecutableButtons() {
|
---|
| 256 | if (Content == null) {
|
---|
| 257 | startButton.Enabled = pauseButton.Enabled = stopButton.Enabled = resetButton.Enabled = false;
|
---|
| 258 | } else {
|
---|
| 259 | startButton.Enabled = (Content.ExecutionState == ExecutionState.Prepared) || (Content.ExecutionState == ExecutionState.Paused);
|
---|
| 260 | pauseButton.Enabled = Content.ExecutionState == ExecutionState.Started;
|
---|
| 261 | stopButton.Enabled = (Content.ExecutionState == ExecutionState.Started) || (Content.ExecutionState == ExecutionState.Paused);
|
---|
| 262 | resetButton.Enabled = Content.ExecutionState != ExecutionState.Started;
|
---|
| 263 | }
|
---|
[2851] | 264 | }
|
---|
| 265 | #endregion
|
---|
| 266 | }
|
---|
| 267 | }
|
---|