[3226] | 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;
|
---|
[3265] | 25 | using HeuristicLab.Core;
|
---|
[3226] | 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() {
|
---|
[3265] | 60 | Content.AlgorithmChanged -= new EventHandler(Content_AlgorithmChanged);
|
---|
[3226] | 61 | Content.ExceptionOccurred -= new EventHandler<EventArgs<Exception>>(Content_ExceptionOccurred);
|
---|
[3265] | 62 | Content.ExecutionStateChanged -= new EventHandler(Content_ExecutionStateChanged);
|
---|
[3226] | 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();
|
---|
[3265] | 69 | Content.AlgorithmChanged += new EventHandler(Content_AlgorithmChanged);
|
---|
[3226] | 70 | Content.ExceptionOccurred += new EventHandler<EventArgs<Exception>>(Content_ExceptionOccurred);
|
---|
[3265] | 71 | Content.ExecutionStateChanged += new EventHandler(Content_ExecutionStateChanged);
|
---|
[3226] | 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 | algorithmViewHost.Content = null;
|
---|
[3260] | 81 | runsView.Content = null;
|
---|
[3226] | 82 | executionTimeTextBox.Text = "-";
|
---|
| 83 | } else {
|
---|
| 84 | repetitionsNumericUpDown.Value = Content.Repetitions;
|
---|
| 85 | algorithmViewHost.ViewType = null;
|
---|
| 86 | algorithmViewHost.Content = Content.Algorithm;
|
---|
[3260] | 87 | runsView.Content = Content.Runs;
|
---|
[3265] | 88 | executionTimeTextBox.Text = Content.ExecutionTime.ToString();
|
---|
[3226] | 89 | }
|
---|
[3367] | 90 | SetEnableStateOfControls();
|
---|
[3226] | 91 | }
|
---|
[3367] | 92 | protected override void OnReadOnlyChanged() {
|
---|
| 93 | base.OnReadOnlyChanged();
|
---|
| 94 | SetEnableStateOfControls();
|
---|
| 95 | }
|
---|
| 96 | private void SetEnableStateOfControls() {
|
---|
[3454] | 97 | repetitionsNumericUpDown.Enabled = Content != null && !ReadOnly;
|
---|
| 98 | newAlgorithmButton.Enabled = Content != null && !ReadOnly;
|
---|
| 99 | openAlgorithmButton.Enabled = Content != null && !ReadOnly;
|
---|
| 100 | algorithmViewHost.Enabled = Content != null;
|
---|
| 101 | runsView.Enabled = Content != null;
|
---|
| 102 | executionTimeTextBox.Enabled = Content != null;
|
---|
| 103 | SetEnabledStateOfExecutableButtons();
|
---|
[3367] | 104 | }
|
---|
[3226] | 105 |
|
---|
| 106 | protected override void OnClosed(FormClosedEventArgs e) {
|
---|
[3265] | 107 | if ((Content != null) && (Content.ExecutionState == ExecutionState.Started)) Content.Stop();
|
---|
[3226] | 108 | base.OnClosed(e);
|
---|
| 109 | }
|
---|
| 110 |
|
---|
| 111 | #region Content Events
|
---|
[3265] | 112 | private void Content_ExecutionStateChanged(object sender, EventArgs e) {
|
---|
[3226] | 113 | if (InvokeRequired)
|
---|
[3265] | 114 | Invoke(new EventHandler(Content_ExecutionStateChanged), sender, e);
|
---|
[3226] | 115 | else {
|
---|
[3367] | 116 | this.ReadOnly = Content.ExecutionState == ExecutionState.Started;
|
---|
[3416] | 117 | Locked = Content.ExecutionState == ExecutionState.Started;
|
---|
[3454] | 118 | SetEnabledStateOfExecutableButtons();
|
---|
[3226] | 119 | }
|
---|
| 120 | }
|
---|
[3265] | 121 | private void Content_ExecutionTimeChanged(object sender, EventArgs e) {
|
---|
[3226] | 122 | if (InvokeRequired)
|
---|
[3265] | 123 | Invoke(new EventHandler(Content_ExecutionTimeChanged), sender, e);
|
---|
| 124 | else
|
---|
| 125 | executionTimeTextBox.Text = Content.ExecutionTime.ToString();
|
---|
[3226] | 126 | }
|
---|
| 127 | private void Content_ExceptionOccurred(object sender, EventArgs<Exception> e) {
|
---|
| 128 | if (InvokeRequired)
|
---|
| 129 | Invoke(new EventHandler<EventArgs<Exception>>(Content_ExceptionOccurred), sender, e);
|
---|
| 130 | else
|
---|
| 131 | Auxiliary.ShowErrorMessageBox(e.Value);
|
---|
| 132 | }
|
---|
[3265] | 133 | private void Content_AlgorithmChanged(object sender, EventArgs e) {
|
---|
| 134 | if (InvokeRequired)
|
---|
| 135 | Invoke(new EventHandler(Content_AlgorithmChanged), sender, e);
|
---|
| 136 | else {
|
---|
| 137 | algorithmViewHost.ViewType = null;
|
---|
| 138 | algorithmViewHost.Content = Content.Algorithm;
|
---|
| 139 | }
|
---|
| 140 | }
|
---|
[3226] | 141 | private void Content_RepetitionsChanged(object sender, EventArgs e) {
|
---|
| 142 | if (InvokeRequired)
|
---|
| 143 | Invoke(new EventHandler(Content_RepetitionsChanged), sender, e);
|
---|
[3265] | 144 | else
|
---|
[3226] | 145 | repetitionsNumericUpDown.Value = Content.Repetitions;
|
---|
| 146 | }
|
---|
| 147 | #endregion
|
---|
| 148 |
|
---|
| 149 | #region Control events
|
---|
[3265] | 150 | private void repetitionsNumericUpDown_Validated(object sender, System.EventArgs e) {
|
---|
| 151 | if (repetitionsNumericUpDown.Text == string.Empty)
|
---|
| 152 | repetitionsNumericUpDown.Text = repetitionsNumericUpDown.Value.ToString();
|
---|
| 153 | }
|
---|
[3226] | 154 | private void repetitionsNumericUpDown_ValueChanged(object sender, EventArgs e) {
|
---|
| 155 | Content.Repetitions = (int)repetitionsNumericUpDown.Value;
|
---|
| 156 | }
|
---|
| 157 | private void newAlgorithmButton_Click(object sender, EventArgs e) {
|
---|
| 158 | if (algorithmTypeSelectorDialog == null) {
|
---|
| 159 | algorithmTypeSelectorDialog = new TypeSelectorDialog();
|
---|
| 160 | algorithmTypeSelectorDialog.Caption = "Select Algorithm";
|
---|
[3407] | 161 | algorithmTypeSelectorDialog.TypeSelector.Caption = "Available Algorithms";
|
---|
[3226] | 162 | algorithmTypeSelectorDialog.TypeSelector.Configure(typeof(IAlgorithm), false, false);
|
---|
| 163 | }
|
---|
| 164 | if (algorithmTypeSelectorDialog.ShowDialog(this) == DialogResult.OK) {
|
---|
[3407] | 165 | try {
|
---|
| 166 | Content.Algorithm = (IAlgorithm)algorithmTypeSelectorDialog.TypeSelector.CreateInstanceOfSelectedType();
|
---|
| 167 | }
|
---|
| 168 | catch (Exception ex) {
|
---|
| 169 | Auxiliary.ShowErrorMessageBox(ex);
|
---|
| 170 | }
|
---|
[3226] | 171 | }
|
---|
| 172 | }
|
---|
| 173 | private void openAlgorithmButton_Click(object sender, EventArgs e) {
|
---|
| 174 | openFileDialog.Title = "Open Algorithm";
|
---|
| 175 | if (openFileDialog.ShowDialog(this) == DialogResult.OK) {
|
---|
[3454] | 176 | newAlgorithmButton.Enabled = openAlgorithmButton.Enabled = false;
|
---|
[3226] | 177 | algorithmViewHost.Enabled = false;
|
---|
| 178 |
|
---|
[3500] | 179 | ContentManager.LoadAsync(openFileDialog.FileName, delegate(IStorableContent content, Exception error) {
|
---|
[3226] | 180 | try {
|
---|
[3500] | 181 | if (error != null) throw error;
|
---|
| 182 | IAlgorithm algorithm = content as IAlgorithm;
|
---|
[3226] | 183 | if (algorithm == null)
|
---|
| 184 | MessageBox.Show(this, "The selected file does not contain an algorithm.", "Invalid File", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
---|
| 185 | else
|
---|
| 186 | Content.Algorithm = algorithm;
|
---|
[3500] | 187 | }
|
---|
| 188 | catch (Exception ex) {
|
---|
| 189 | Auxiliary.ShowErrorMessageBox(ex);
|
---|
| 190 | }
|
---|
| 191 | finally {
|
---|
| 192 | Invoke(new Action(delegate() {
|
---|
| 193 | algorithmViewHost.Enabled = true;
|
---|
| 194 | newAlgorithmButton.Enabled = openAlgorithmButton.Enabled = true;
|
---|
| 195 | }));
|
---|
| 196 | }
|
---|
| 197 | });
|
---|
[3226] | 198 | }
|
---|
| 199 | }
|
---|
| 200 | private void startButton_Click(object sender, EventArgs e) {
|
---|
| 201 | Content.Start();
|
---|
| 202 | }
|
---|
[3265] | 203 | private void pauseButton_Click(object sender, EventArgs e) {
|
---|
| 204 | Content.Pause();
|
---|
| 205 | }
|
---|
[3226] | 206 | private void stopButton_Click(object sender, EventArgs e) {
|
---|
| 207 | Content.Stop();
|
---|
| 208 | }
|
---|
| 209 | private void resetButton_Click(object sender, EventArgs e) {
|
---|
[3265] | 210 | if (Content.Runs.Count > 0) {
|
---|
[3274] | 211 | if (MessageBox.Show(this, "Clear all runs executed so far?", "Clear All Runs?", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.Yes)
|
---|
[3265] | 212 | Content.Prepare(true);
|
---|
| 213 | else
|
---|
| 214 | Content.Prepare(false);
|
---|
| 215 | } else {
|
---|
| 216 | Content.Prepare();
|
---|
| 217 | }
|
---|
[3226] | 218 | }
|
---|
[3299] | 219 | private void algorithmPanel_DragEnterOver(object sender, DragEventArgs e) {
|
---|
| 220 | e.Effect = DragDropEffects.None;
|
---|
[3367] | 221 | if (ReadOnly)
|
---|
| 222 | return;
|
---|
[3299] | 223 | Type type = e.Data.GetData("Type") as Type;
|
---|
| 224 | if ((type != null) && (typeof(IAlgorithm).IsAssignableFrom(type))) {
|
---|
[3526] | 225 | if ((e.KeyState & 8) == 8) e.Effect = DragDropEffects.Link; // CTRL key
|
---|
[3299] | 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;
|
---|
[3526] | 229 | else if ((e.AllowedEffect & DragDropEffects.Link) == DragDropEffects.Link) e.Effect = DragDropEffects.Link;
|
---|
[3299] | 230 | }
|
---|
| 231 | }
|
---|
| 232 | private void algorithmPanel_DragDrop(object sender, DragEventArgs e) {
|
---|
| 233 | if (e.Effect != DragDropEffects.None) {
|
---|
| 234 | IAlgorithm algorithm = e.Data.GetData("Value") as IAlgorithm;
|
---|
| 235 | if ((e.Effect & DragDropEffects.Copy) == DragDropEffects.Copy) algorithm = (IAlgorithm)algorithm.Clone();
|
---|
| 236 | Content.Algorithm = algorithm;
|
---|
| 237 | }
|
---|
| 238 | }
|
---|
[3226] | 239 | #endregion
|
---|
| 240 |
|
---|
| 241 | #region Helpers
|
---|
[3454] | 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 | }
|
---|
[3226] | 251 | }
|
---|
| 252 | #endregion
|
---|
| 253 | }
|
---|
| 254 | }
|
---|