[3267] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[5445] | 3 | * Copyright (C) 2002-2011 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[3267] | 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;
|
---|
[5419] | 23 | using System.Linq;
|
---|
[3267] | 24 | using System.Windows.Forms;
|
---|
| 25 | using HeuristicLab.Common;
|
---|
| 26 | using HeuristicLab.Core;
|
---|
| 27 | using HeuristicLab.Core.Views;
|
---|
| 28 | using HeuristicLab.MainForm;
|
---|
[3758] | 29 | using HeuristicLab.PluginInfrastructure;
|
---|
[3267] | 30 |
|
---|
| 31 | namespace HeuristicLab.Optimization.Views {
|
---|
| 32 | /// <summary>
|
---|
| 33 | /// The base class for visual representations of items.
|
---|
| 34 | /// </summary>
|
---|
| 35 | [View("Experiment View")]
|
---|
| 36 | [Content(typeof(Experiment), true)]
|
---|
| 37 | public sealed partial class ExperimentView : NamedItemView {
|
---|
| 38 | public new Experiment Content {
|
---|
| 39 | get { return (Experiment)base.Content; }
|
---|
| 40 | set { base.Content = value; }
|
---|
| 41 | }
|
---|
| 42 |
|
---|
| 43 | /// <summary>
|
---|
| 44 | /// Initializes a new instance of <see cref="ItemBaseView"/>.
|
---|
| 45 | /// </summary>
|
---|
| 46 | public ExperimentView() {
|
---|
| 47 | InitializeComponent();
|
---|
| 48 | }
|
---|
| 49 |
|
---|
| 50 | protected override void DeregisterContentEvents() {
|
---|
| 51 | Content.ExceptionOccurred -= new EventHandler<EventArgs<Exception>>(Content_ExceptionOccurred);
|
---|
| 52 | Content.ExecutionStateChanged -= new EventHandler(Content_ExecutionStateChanged);
|
---|
| 53 | Content.ExecutionTimeChanged -= new EventHandler(Content_ExecutionTimeChanged);
|
---|
[4070] | 54 | Content.Prepared -= new EventHandler(Content_Prepared);
|
---|
| 55 | Content.Started -= new EventHandler(Content_Started);
|
---|
| 56 | Content.Paused -= new EventHandler(Content_Paused);
|
---|
| 57 | Content.Stopped -= new EventHandler(Content_Stopped);
|
---|
[3267] | 58 | base.DeregisterContentEvents();
|
---|
| 59 | }
|
---|
| 60 | protected override void RegisterContentEvents() {
|
---|
| 61 | base.RegisterContentEvents();
|
---|
| 62 | Content.ExceptionOccurred += new EventHandler<EventArgs<Exception>>(Content_ExceptionOccurred);
|
---|
| 63 | Content.ExecutionStateChanged += new EventHandler(Content_ExecutionStateChanged);
|
---|
| 64 | Content.ExecutionTimeChanged += new EventHandler(Content_ExecutionTimeChanged);
|
---|
[4070] | 65 | Content.Prepared += new EventHandler(Content_Prepared);
|
---|
| 66 | Content.Started += new EventHandler(Content_Started);
|
---|
| 67 | Content.Paused += new EventHandler(Content_Paused);
|
---|
| 68 | Content.Stopped += new EventHandler(Content_Stopped);
|
---|
[3267] | 69 | }
|
---|
| 70 |
|
---|
| 71 | protected override void OnContentChanged() {
|
---|
| 72 | base.OnContentChanged();
|
---|
| 73 | if (Content == null) {
|
---|
[3274] | 74 | optimizerListView.Content = null;
|
---|
[3329] | 75 | runsViewHost.Content = null;
|
---|
[3267] | 76 | executionTimeTextBox.Text = "-";
|
---|
| 77 | } else {
|
---|
[4540] | 78 | Locked = ReadOnly = Content.ExecutionState == ExecutionState.Started;
|
---|
[3274] | 79 | optimizerListView.Content = Content.Optimizers;
|
---|
[3329] | 80 | runsViewHost.Content = Content.Runs;
|
---|
[3267] | 81 | executionTimeTextBox.Text = Content.ExecutionTime.ToString();
|
---|
| 82 | }
|
---|
| 83 | }
|
---|
| 84 |
|
---|
[3904] | 85 | protected override void SetEnabledStateOfControls() {
|
---|
| 86 | base.SetEnabledStateOfControls();
|
---|
[3454] | 87 | optimizerListView.Enabled = Content != null;
|
---|
| 88 | runsViewHost.Enabled = Content != null;
|
---|
| 89 | executionTimeTextBox.Enabled = Content != null;
|
---|
| 90 | SetEnabledStateOfExecutableButtons();
|
---|
[3367] | 91 | }
|
---|
| 92 |
|
---|
[3267] | 93 | protected override void OnClosed(FormClosedEventArgs e) {
|
---|
[5419] | 94 | if ((Content != null) && (Content.ExecutionState == ExecutionState.Started)) {
|
---|
| 95 | //The content must be stopped if no other view showing the content is available
|
---|
| 96 | var optimizers = MainFormManager.MainForm.Views.OfType<IContentView>().Where(v => v != this).Select(v => v.Content).OfType<IOptimizer>();
|
---|
[5420] | 97 | if (!optimizers.Contains(Content)) {
|
---|
| 98 | var nestedOptimizers = optimizers.SelectMany(opt => opt.NestedOptimizers);
|
---|
| 99 | if (!nestedOptimizers.Contains(Content)) Content.Stop();
|
---|
| 100 | }
|
---|
[5419] | 101 | }
|
---|
[3267] | 102 | base.OnClosed(e);
|
---|
| 103 | }
|
---|
| 104 |
|
---|
| 105 | #region Content Events
|
---|
| 106 | private void Content_ExecutionStateChanged(object sender, EventArgs e) {
|
---|
| 107 | if (InvokeRequired)
|
---|
| 108 | Invoke(new EventHandler(Content_ExecutionStateChanged), sender, e);
|
---|
[4070] | 109 | else
|
---|
| 110 | startButton.Enabled = pauseButton.Enabled = stopButton.Enabled = resetButton.Enabled = false;
|
---|
| 111 | }
|
---|
| 112 | private void Content_Prepared(object sender, EventArgs e) {
|
---|
| 113 | if (InvokeRequired)
|
---|
| 114 | Invoke(new EventHandler(Content_Prepared), sender, e);
|
---|
[3267] | 115 | else {
|
---|
[4070] | 116 | nameTextBox.Enabled = descriptionTextBox.Enabled = true;
|
---|
[4526] | 117 | ReadOnly = Locked = false;
|
---|
[3454] | 118 | SetEnabledStateOfExecutableButtons();
|
---|
[3267] | 119 | }
|
---|
| 120 | }
|
---|
[4070] | 121 | private void Content_Started(object sender, EventArgs e) {
|
---|
| 122 | if (InvokeRequired)
|
---|
| 123 | Invoke(new EventHandler(Content_Started), sender, e);
|
---|
| 124 | else {
|
---|
| 125 | nameTextBox.Enabled = descriptionTextBox.Enabled = false;
|
---|
[4526] | 126 | ReadOnly = Locked = true;
|
---|
[4070] | 127 | SetEnabledStateOfExecutableButtons();
|
---|
| 128 | }
|
---|
| 129 | }
|
---|
| 130 | private void Content_Paused(object sender, EventArgs e) {
|
---|
| 131 | if (InvokeRequired)
|
---|
| 132 | Invoke(new EventHandler(Content_Paused), sender, e);
|
---|
| 133 | else {
|
---|
| 134 | nameTextBox.Enabled = descriptionTextBox.Enabled = true;
|
---|
[4526] | 135 | ReadOnly = Locked = false;
|
---|
[4070] | 136 | SetEnabledStateOfExecutableButtons();
|
---|
| 137 | }
|
---|
| 138 | }
|
---|
| 139 | private void Content_Stopped(object sender, EventArgs e) {
|
---|
| 140 | if (InvokeRequired)
|
---|
| 141 | Invoke(new EventHandler(Content_Stopped), sender, e);
|
---|
| 142 | else {
|
---|
| 143 | nameTextBox.Enabled = descriptionTextBox.Enabled = true;
|
---|
[4526] | 144 | ReadOnly = Locked = false;
|
---|
[4070] | 145 | SetEnabledStateOfExecutableButtons();
|
---|
| 146 | }
|
---|
| 147 | }
|
---|
[3267] | 148 | private void Content_ExecutionTimeChanged(object sender, EventArgs e) {
|
---|
| 149 | if (InvokeRequired)
|
---|
| 150 | Invoke(new EventHandler(Content_ExecutionTimeChanged), sender, e);
|
---|
| 151 | else
|
---|
[4216] | 152 | executionTimeTextBox.Text = Content == null ? "-" : Content.ExecutionTime.ToString();
|
---|
[3267] | 153 | }
|
---|
| 154 | private void Content_ExceptionOccurred(object sender, EventArgs<Exception> e) {
|
---|
| 155 | if (InvokeRequired)
|
---|
| 156 | Invoke(new EventHandler<EventArgs<Exception>>(Content_ExceptionOccurred), sender, e);
|
---|
| 157 | else
|
---|
[3758] | 158 | ErrorHandling.ShowErrorDialog(this, e.Value);
|
---|
[3267] | 159 | }
|
---|
| 160 | #endregion
|
---|
| 161 |
|
---|
| 162 | #region Control events
|
---|
| 163 | private void startButton_Click(object sender, EventArgs e) {
|
---|
| 164 | Content.Start();
|
---|
| 165 | }
|
---|
| 166 | private void pauseButton_Click(object sender, EventArgs e) {
|
---|
| 167 | Content.Pause();
|
---|
| 168 | }
|
---|
| 169 | private void stopButton_Click(object sender, EventArgs e) {
|
---|
| 170 | Content.Stop();
|
---|
| 171 | }
|
---|
| 172 | private void resetButton_Click(object sender, EventArgs e) {
|
---|
[3716] | 173 | Content.Prepare(false);
|
---|
[3267] | 174 | }
|
---|
| 175 | #endregion
|
---|
| 176 |
|
---|
| 177 | #region Helpers
|
---|
[3454] | 178 | private void SetEnabledStateOfExecutableButtons() {
|
---|
| 179 | if (Content == null) {
|
---|
| 180 | startButton.Enabled = pauseButton.Enabled = stopButton.Enabled = resetButton.Enabled = false;
|
---|
| 181 | } else {
|
---|
| 182 | startButton.Enabled = (Content.ExecutionState == ExecutionState.Prepared) || (Content.ExecutionState == ExecutionState.Paused);
|
---|
| 183 | pauseButton.Enabled = Content.ExecutionState == ExecutionState.Started;
|
---|
| 184 | stopButton.Enabled = (Content.ExecutionState == ExecutionState.Started) || (Content.ExecutionState == ExecutionState.Paused);
|
---|
| 185 | resetButton.Enabled = Content.ExecutionState != ExecutionState.Started;
|
---|
| 186 | }
|
---|
[3267] | 187 | }
|
---|
| 188 | #endregion
|
---|
| 189 | }
|
---|
| 190 | }
|
---|