Free cookie consent management tool by TermsFeed Policy Generator

source: branches/TerminationCriteria/HeuristicLab.Optimization.Views/3.3/BatchRunView.cs @ 12833

Last change on this file since 12833 was 12012, checked in by ascheibe, 10 years ago

#2212 merged r12008, r12009, r12010 back into trunk

File size: 7.9 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 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
22using System;
23using System.Linq;
24using System.Windows.Forms;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Core.Views;
28using HeuristicLab.MainForm;
29using HeuristicLab.PluginInfrastructure;
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)]
37  public sealed partial class BatchRunView : IOptimizerView {
38    private TypeSelectorDialog optimizerTypeSelectorDialog;
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
52    protected override void Dispose(bool disposing) {
53      if (disposing) {
54        if (optimizerTypeSelectorDialog != null) optimizerTypeSelectorDialog.Dispose();
55        if (components != null) components.Dispose();
56      }
57      base.Dispose(disposing);
58    }
59
60    protected override void DeregisterContentEvents() {
61      Content.OptimizerChanged -= new EventHandler(Content_OptimizerChanged);
62      Content.RepetitionsChanged -= new EventHandler(Content_RepetitionsChanged);
63      base.DeregisterContentEvents();
64    }
65    protected override void RegisterContentEvents() {
66      base.RegisterContentEvents();
67      Content.OptimizerChanged += new EventHandler(Content_OptimizerChanged);
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;
75        optimizerViewHost.Content = null;
76        runsView.Content = null;
77      } else {
78        repetitionsNumericUpDown.Value = Content.Repetitions;
79        optimizerViewHost.Content = Content.Optimizer;
80        runsView.Content = Content.Runs;
81      }
82    }
83    protected override void SetEnabledStateOfControls() {
84      base.SetEnabledStateOfControls();
85      repetitionsNumericUpDown.Enabled = Content != null && !ReadOnly;
86      newOptimizerButton.Enabled = Content != null && !ReadOnly;
87      openOptimizerButton.Enabled = Content != null && !ReadOnly;
88      optimizerViewHost.Enabled = Content != null;
89      runsView.Enabled = Content != null;
90    }
91
92    protected override void OnClosed(FormClosedEventArgs e) {
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>();
96        if (!optimizers.Contains(Content)) {
97          var nestedOptimizers = optimizers.SelectMany(opt => opt.NestedOptimizers);
98          if (!nestedOptimizers.Contains(Content)) Content.Stop();
99        }
100      }
101      base.OnClosed(e);
102    }
103
104    #region Content Events
105    private void Content_OptimizerChanged(object sender, EventArgs e) {
106      if (InvokeRequired)
107        Invoke(new EventHandler(Content_OptimizerChanged), sender, e);
108      else {
109        optimizerViewHost.Content = Content.Optimizer;
110      }
111    }
112    private void Content_RepetitionsChanged(object sender, EventArgs e) {
113      if (InvokeRequired)
114        Invoke(new EventHandler(Content_RepetitionsChanged), sender, e);
115      else
116        repetitionsNumericUpDown.Value = Content.Repetitions;
117    }
118    #endregion
119
120    #region Control events
121    private void repetitionsNumericUpDown_Validated(object sender, System.EventArgs e) {
122      if (repetitionsNumericUpDown.Text == string.Empty)
123        repetitionsNumericUpDown.Text = repetitionsNumericUpDown.Value.ToString();
124    }
125    private void repetitionsNumericUpDown_ValueChanged(object sender, EventArgs e) {
126      if (Content != null)
127        Content.Repetitions = (int)repetitionsNumericUpDown.Value;
128    }
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);
135      }
136      if (optimizerTypeSelectorDialog.ShowDialog(this) == DialogResult.OK) {
137        try {
138          Content.Optimizer = (IOptimizer)optimizerTypeSelectorDialog.TypeSelector.CreateInstanceOfSelectedType();
139        }
140        catch (Exception ex) {
141          ErrorHandling.ShowErrorDialog(this, ex);
142        }
143      }
144    }
145    private void openOptimizerButton_Click(object sender, EventArgs e) {
146      openFileDialog.Title = "Open Optimizer";
147      if (openFileDialog.ShowDialog(this) == DialogResult.OK) {
148        newOptimizerButton.Enabled = openOptimizerButton.Enabled = false;
149        optimizerViewHost.Enabled = false;
150
151        ContentManager.LoadAsync(openFileDialog.FileName, delegate(IStorableContent content, Exception error) {
152          try {
153            if (error != null) throw error;
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);
157            else
158              Content.Optimizer = optimizer;
159          }
160          catch (Exception ex) {
161            ErrorHandling.ShowErrorDialog(this, ex);
162          }
163          finally {
164            Invoke(new Action(delegate() {
165              optimizerViewHost.Enabled = true;
166              newOptimizerButton.Enabled = openOptimizerButton.Enabled = true;
167            }));
168          }
169        });
170      }
171    }
172
173    private void optimizerTabPage_DragEnterOver(object sender, DragEventArgs e) {
174      e.Effect = DragDropEffects.None;
175      if (!ReadOnly && (e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat) is IOptimizer)) {
176        if ((e.KeyState & 32) == 32) e.Effect = DragDropEffects.Link;  // ALT key
177        else if ((e.KeyState & 4) == 4) e.Effect = DragDropEffects.Move;  // SHIFT key
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;
181      }
182    }
183    private void optimizerTabPage_DragDrop(object sender, DragEventArgs e) {
184      if (e.Effect != DragDropEffects.None) {
185        IOptimizer optimizer = e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat) as IOptimizer;
186        if (e.Effect.HasFlag(DragDropEffects.Copy)) optimizer = (IOptimizer)optimizer.Clone();
187        Content.Optimizer = optimizer;
188      }
189    }
190    #endregion
191
192  }
193}
Note: See TracBrowser for help on using the repository browser.