Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Optimization.Views/3.3/BatchRunView.cs @ 6868

Last change on this file since 6868 was 6425, checked in by mkommend, 13 years ago

#1555: Refactored optimizer views to derive from common base class IOptimizerView.

File size: 7.9 KB
RevLine 
[3226]1#region License Information
2/* HeuristicLab
[5445]3 * Copyright (C) 2002-2011 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[3226]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
22using System;
[5419]23using System.Linq;
[3226]24using System.Windows.Forms;
25using HeuristicLab.Common;
[3265]26using HeuristicLab.Core;
[3226]27using HeuristicLab.Core.Views;
28using HeuristicLab.MainForm;
[3758]29using HeuristicLab.PluginInfrastructure;
[3226]30
31namespace HeuristicLab.Optimization.Views {
32  /// <summary>
33  /// The base class for visual representations of items.
34  /// </summary>
35  [View("BatchRun View")]
36  [Content(typeof(BatchRun), true)]
[6425]37  public sealed partial class BatchRunView : IOptimizerView {
[5300]38    private TypeSelectorDialog optimizerTypeSelectorDialog;
[3226]39
40    public new BatchRun Content {
41      get { return (BatchRun)base.Content; }
42      set { base.Content = value; }
43    }
44
45    /// <summary>
46    /// Initializes a new instance of <see cref="ItemBaseView"/>.
47    /// </summary>
48    public BatchRunView() {
49      InitializeComponent();
50    }
51
[5237]52    protected override void Dispose(bool disposing) {
53      if (disposing) {
[5300]54        if (optimizerTypeSelectorDialog != null) optimizerTypeSelectorDialog.Dispose();
[5237]55        if (components != null) components.Dispose();
56      }
57      base.Dispose(disposing);
58    }
59
[3226]60    protected override void DeregisterContentEvents() {
[5300]61      Content.OptimizerChanged -= new EventHandler(Content_OptimizerChanged);
[3226]62      Content.RepetitionsChanged -= new EventHandler(Content_RepetitionsChanged);
63      base.DeregisterContentEvents();
64    }
65    protected override void RegisterContentEvents() {
66      base.RegisterContentEvents();
[5300]67      Content.OptimizerChanged += new EventHandler(Content_OptimizerChanged);
[3226]68      Content.RepetitionsChanged += new EventHandler(Content_RepetitionsChanged);
69    }
70
71    protected override void OnContentChanged() {
72      base.OnContentChanged();
73      if (Content == null) {
74        repetitionsNumericUpDown.Value = 1;
[5300]75        optimizerViewHost.Content = null;
[3260]76        runsView.Content = null;
[3226]77      } else {
78        repetitionsNumericUpDown.Value = Content.Repetitions;
[5300]79        optimizerViewHost.Content = Content.Optimizer;
[3260]80        runsView.Content = Content.Runs;
[3226]81      }
82    }
[3904]83    protected override void SetEnabledStateOfControls() {
84      base.SetEnabledStateOfControls();
[3454]85      repetitionsNumericUpDown.Enabled = Content != null && !ReadOnly;
[5300]86      newOptimizerButton.Enabled = Content != null && !ReadOnly;
87      openOptimizerButton.Enabled = Content != null && !ReadOnly;
88      optimizerViewHost.Enabled = Content != null;
[3454]89      runsView.Enabled = Content != null;
[3367]90    }
[3226]91
92    protected override void OnClosed(FormClosedEventArgs e) {
[5419]93      if ((Content != null) && (Content.ExecutionState == ExecutionState.Started)) {
94        //The content must be stopped if no other view showing the content is available
95        var optimizers = MainFormManager.MainForm.Views.OfType<IContentView>().Where(v => v != this).Select(v => v.Content).OfType<IOptimizer>();
[5420]96        if (!optimizers.Contains(Content)) {
97          var nestedOptimizers = optimizers.SelectMany(opt => opt.NestedOptimizers);
98          if (!nestedOptimizers.Contains(Content)) Content.Stop();
99        }
[5419]100      }
[3226]101      base.OnClosed(e);
102    }
103
104    #region Content Events
[5300]105    private void Content_OptimizerChanged(object sender, EventArgs e) {
[3265]106      if (InvokeRequired)
[5300]107        Invoke(new EventHandler(Content_OptimizerChanged), sender, e);
[3265]108      else {
[5300]109        optimizerViewHost.Content = Content.Optimizer;
[3265]110      }
111    }
[3226]112    private void Content_RepetitionsChanged(object sender, EventArgs e) {
113      if (InvokeRequired)
114        Invoke(new EventHandler(Content_RepetitionsChanged), sender, e);
[3265]115      else
[3226]116        repetitionsNumericUpDown.Value = Content.Repetitions;
117    }
118    #endregion
119
120    #region Control events
[3265]121    private void repetitionsNumericUpDown_Validated(object sender, System.EventArgs e) {
122      if (repetitionsNumericUpDown.Text == string.Empty)
123        repetitionsNumericUpDown.Text = repetitionsNumericUpDown.Value.ToString();
124    }
[3226]125    private void repetitionsNumericUpDown_ValueChanged(object sender, EventArgs e) {
[4011]126      if (Content != null)
127        Content.Repetitions = (int)repetitionsNumericUpDown.Value;
[3226]128    }
[5300]129    private void newOptimizerButton_Click(object sender, EventArgs e) {
130      if (optimizerTypeSelectorDialog == null) {
131        optimizerTypeSelectorDialog = new TypeSelectorDialog();
132        optimizerTypeSelectorDialog.Caption = "Select Optimizer";
133        optimizerTypeSelectorDialog.TypeSelector.Caption = "Available Optimizers";
134        optimizerTypeSelectorDialog.TypeSelector.Configure(typeof(IOptimizer), false, true);
[3226]135      }
[5300]136      if (optimizerTypeSelectorDialog.ShowDialog(this) == DialogResult.OK) {
[3407]137        try {
[5300]138          Content.Optimizer = (IOptimizer)optimizerTypeSelectorDialog.TypeSelector.CreateInstanceOfSelectedType();
[3407]139        }
140        catch (Exception ex) {
[3758]141          ErrorHandling.ShowErrorDialog(this, ex);
[3407]142        }
[3226]143      }
144    }
[5300]145    private void openOptimizerButton_Click(object sender, EventArgs e) {
146      openFileDialog.Title = "Open Optimizer";
[3226]147      if (openFileDialog.ShowDialog(this) == DialogResult.OK) {
[5300]148        newOptimizerButton.Enabled = openOptimizerButton.Enabled = false;
149        optimizerViewHost.Enabled = false;
[3226]150
[3500]151        ContentManager.LoadAsync(openFileDialog.FileName, delegate(IStorableContent content, Exception error) {
[3226]152          try {
[3500]153            if (error != null) throw error;
[5300]154            IOptimizer optimizer = content as IOptimizer;
155            if (optimizer == null)
156              MessageBox.Show(this, "The selected file does not contain an optimizer.", "Invalid File", MessageBoxButtons.OK, MessageBoxIcon.Error);
[3226]157            else
[5300]158              Content.Optimizer = optimizer;
[3500]159          }
160          catch (Exception ex) {
[3758]161            ErrorHandling.ShowErrorDialog(this, ex);
[3500]162          }
163          finally {
164            Invoke(new Action(delegate() {
[5300]165              optimizerViewHost.Enabled = true;
166              newOptimizerButton.Enabled = openOptimizerButton.Enabled = true;
[3500]167            }));
168          }
169        });
[3226]170      }
171    }
[6425]172
[5300]173    private void optimizerTabPage_DragEnterOver(object sender, DragEventArgs e) {
[3299]174      e.Effect = DragDropEffects.None;
[5837]175      if (!ReadOnly && (e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat) is IOptimizer)) {
[3694]176        if ((e.KeyState & 32) == 32) e.Effect = DragDropEffects.Link;  // ALT key
[3299]177        else if ((e.KeyState & 4) == 4) e.Effect = DragDropEffects.Move;  // SHIFT key
[5744]178        else if (e.AllowedEffect.HasFlag(DragDropEffects.Copy)) e.Effect = DragDropEffects.Copy;
179        else if (e.AllowedEffect.HasFlag(DragDropEffects.Move)) e.Effect = DragDropEffects.Move;
180        else if (e.AllowedEffect.HasFlag(DragDropEffects.Link)) e.Effect = DragDropEffects.Link;
[3299]181      }
182    }
[5300]183    private void optimizerTabPage_DragDrop(object sender, DragEventArgs e) {
[3299]184      if (e.Effect != DragDropEffects.None) {
[5837]185        IOptimizer optimizer = e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat) as IOptimizer;
[5744]186        if (e.Effect.HasFlag(DragDropEffects.Copy)) optimizer = (IOptimizer)optimizer.Clone();
[5300]187        Content.Optimizer = optimizer;
[3299]188      }
189    }
[3226]190    #endregion
191
192  }
193}
Note: See TracBrowser for help on using the repository browser.