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