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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Linq;
|
---|
25 | using System.Windows.Forms;
|
---|
26 | using HeuristicLab.Common;
|
---|
27 | using HeuristicLab.Core;
|
---|
28 | using HeuristicLab.Core.Views;
|
---|
29 | using HeuristicLab.MainForm;
|
---|
30 | using HeuristicLab.PluginInfrastructure;
|
---|
31 |
|
---|
32 | namespace HeuristicLab.Optimization.Views {
|
---|
33 | [View("Algorithm View")]
|
---|
34 | [Content(typeof(Algorithm), true)]
|
---|
35 | [Content(typeof(IAlgorithm), false)]
|
---|
36 | public partial class AlgorithmView : IOptimizerView {
|
---|
37 | private TypeSelectorDialog problemTypeSelectorDialog;
|
---|
38 | public AlgorithmView() {
|
---|
39 | InitializeComponent();
|
---|
40 | }
|
---|
41 |
|
---|
42 | public new IAlgorithm Content {
|
---|
43 | get { return (IAlgorithm)base.Content; }
|
---|
44 | set { base.Content = value; }
|
---|
45 | }
|
---|
46 |
|
---|
47 | protected override void Dispose(bool disposing) {
|
---|
48 | if (disposing) {
|
---|
49 | if (problemTypeSelectorDialog != null) problemTypeSelectorDialog.Dispose();
|
---|
50 | if (components != null) components.Dispose();
|
---|
51 | }
|
---|
52 | base.Dispose(disposing);
|
---|
53 | }
|
---|
54 |
|
---|
55 | protected override void OnInitialized(EventArgs e) {
|
---|
56 | // Set order of tab pages according to z order.
|
---|
57 | // NOTE: This is required due to a bug in the VS designer.
|
---|
58 | List<Control> tabPages = new List<Control>();
|
---|
59 | for (int i = 0; i < tabControl.Controls.Count; i++) {
|
---|
60 | tabPages.Add(tabControl.Controls[i]);
|
---|
61 | }
|
---|
62 | tabControl.Controls.Clear();
|
---|
63 | foreach (Control control in tabPages)
|
---|
64 | tabControl.Controls.Add(control);
|
---|
65 |
|
---|
66 | base.OnInitialized(e);
|
---|
67 | }
|
---|
68 |
|
---|
69 | protected override void DeregisterContentEvents() {
|
---|
70 | Content.ProblemChanged -= new EventHandler(Content_ProblemChanged);
|
---|
71 | Content.StoreAlgorithmInEachRunChanged -= new EventHandler(Content_StoreAlgorithmInEachRunChanged);
|
---|
72 | base.DeregisterContentEvents();
|
---|
73 | }
|
---|
74 | protected override void RegisterContentEvents() {
|
---|
75 | base.RegisterContentEvents();
|
---|
76 | Content.ProblemChanged += new EventHandler(Content_ProblemChanged);
|
---|
77 | Content.StoreAlgorithmInEachRunChanged += new EventHandler(Content_StoreAlgorithmInEachRunChanged);
|
---|
78 | }
|
---|
79 |
|
---|
80 | protected override void OnContentChanged() {
|
---|
81 | base.OnContentChanged();
|
---|
82 | if (Content == null) {
|
---|
83 | parameterCollectionView.Content = null;
|
---|
84 | problemViewHost.Content = null;
|
---|
85 | resultsView.Content = null;
|
---|
86 | runsView.Content = null;
|
---|
87 | storeAlgorithmInEachRunCheckBox.Checked = true;
|
---|
88 | } else {
|
---|
89 | parameterCollectionView.Content = Content.Parameters;
|
---|
90 | var view = parameterCollectionView.ActiveView as ParameterCollectionView;
|
---|
91 | if (view != null) view.AllowEditingOfHiddenParameters = true;
|
---|
92 | problemViewHost.ViewType = null;
|
---|
93 | problemViewHost.Content = Content.Problem;
|
---|
94 | resultsView.Content = Content.Results.AsReadOnly();
|
---|
95 | runsView.Content = Content.Runs;
|
---|
96 | storeAlgorithmInEachRunCheckBox.Checked = Content.StoreAlgorithmInEachRun;
|
---|
97 | }
|
---|
98 | }
|
---|
99 |
|
---|
100 | protected override void SetEnabledStateOfControls() {
|
---|
101 | base.SetEnabledStateOfControls();
|
---|
102 | parameterCollectionView.Enabled = Content != null;
|
---|
103 | newProblemButton.Enabled = Content != null && !ReadOnly;
|
---|
104 | openProblemButton.Enabled = Content != null && !ReadOnly;
|
---|
105 | problemViewHost.Enabled = Content != null;
|
---|
106 | resultsView.Enabled = Content != null;
|
---|
107 | runsView.Enabled = Content != null;
|
---|
108 | storeAlgorithmInEachRunCheckBox.Enabled = Content != null && !ReadOnly;
|
---|
109 | }
|
---|
110 |
|
---|
111 | protected override void OnClosed(FormClosedEventArgs e) {
|
---|
112 | if ((Content != null) && (Content.ExecutionState == ExecutionState.Started)) {
|
---|
113 | //The content must be stopped if no other view showing the content is available
|
---|
114 | var optimizers = MainFormManager.MainForm.Views.OfType<IContentView>().Where(v => v != this).Select(v => v.Content).OfType<IOptimizer>();
|
---|
115 | if (!optimizers.Contains(Content)) {
|
---|
116 | var nestedOptimizers = optimizers.SelectMany(opt => opt.NestedOptimizers);
|
---|
117 | if (!nestedOptimizers.Contains(Content)) Content.Stop();
|
---|
118 | }
|
---|
119 | }
|
---|
120 | base.OnClosed(e);
|
---|
121 | }
|
---|
122 |
|
---|
123 | #region Content Events
|
---|
124 | protected override void Content_Prepared(object sender, EventArgs e) {
|
---|
125 | if (InvokeRequired)
|
---|
126 | Invoke(new EventHandler(Content_Prepared), sender, e);
|
---|
127 | else {
|
---|
128 | base.Content_Prepared(sender, e);
|
---|
129 | resultsView.Content = Content.Results.AsReadOnly();
|
---|
130 | }
|
---|
131 | }
|
---|
132 |
|
---|
133 | protected virtual void Content_ProblemChanged(object sender, EventArgs e) {
|
---|
134 | if (InvokeRequired)
|
---|
135 | Invoke(new EventHandler(Content_ProblemChanged), sender, e);
|
---|
136 | else {
|
---|
137 | problemViewHost.ViewType = null;
|
---|
138 | problemViewHost.Content = Content.Problem;
|
---|
139 | }
|
---|
140 | }
|
---|
141 | protected virtual void Content_StoreAlgorithmInEachRunChanged(object sender, EventArgs e) {
|
---|
142 | if (InvokeRequired)
|
---|
143 | Invoke(new EventHandler(Content_StoreAlgorithmInEachRunChanged), sender, e);
|
---|
144 | else
|
---|
145 | storeAlgorithmInEachRunCheckBox.Checked = Content.StoreAlgorithmInEachRun;
|
---|
146 | }
|
---|
147 | #endregion
|
---|
148 |
|
---|
149 | #region Control Events
|
---|
150 | protected virtual void newProblemButton_Click(object sender, EventArgs e) {
|
---|
151 | if (problemTypeSelectorDialog == null) {
|
---|
152 | problemTypeSelectorDialog = new TypeSelectorDialog();
|
---|
153 | problemTypeSelectorDialog.Caption = "Select Problem";
|
---|
154 | problemTypeSelectorDialog.TypeSelector.Caption = "Available Problems";
|
---|
155 | problemTypeSelectorDialog.TypeSelector.Configure(Content.ProblemType, false, true);
|
---|
156 | }
|
---|
157 | if (problemTypeSelectorDialog.ShowDialog(this) == DialogResult.OK) {
|
---|
158 | try {
|
---|
159 | Content.Problem = (IProblem)problemTypeSelectorDialog.TypeSelector.CreateInstanceOfSelectedType();
|
---|
160 | }
|
---|
161 | catch (Exception ex) {
|
---|
162 | ErrorHandling.ShowErrorDialog(this, ex);
|
---|
163 | }
|
---|
164 | }
|
---|
165 | }
|
---|
166 | protected virtual void openProblemButton_Click(object sender, EventArgs e) {
|
---|
167 | openFileDialog.Title = "Open Problem";
|
---|
168 | if (openFileDialog.ShowDialog(this) == DialogResult.OK) {
|
---|
169 | newProblemButton.Enabled = openProblemButton.Enabled = false;
|
---|
170 | problemViewHost.Enabled = false;
|
---|
171 |
|
---|
172 | ContentManager.LoadAsync(openFileDialog.FileName, delegate(IStorableContent content, Exception error) {
|
---|
173 | try {
|
---|
174 | if (error != null) throw error;
|
---|
175 | IProblem problem = content as IProblem;
|
---|
176 | if (problem == null)
|
---|
177 | Invoke(new Action(() =>
|
---|
178 | MessageBox.Show(this, "The selected file does not contain a problem.", "Invalid File", MessageBoxButtons.OK, MessageBoxIcon.Error)));
|
---|
179 | else if (!Content.ProblemType.IsInstanceOfType(problem))
|
---|
180 | Invoke(new Action(() =>
|
---|
181 | MessageBox.Show(this, "The selected file contains a problem type which is not supported by this algorithm.", "Invalid Problem Type", MessageBoxButtons.OK, MessageBoxIcon.Error)));
|
---|
182 | else
|
---|
183 | Content.Problem = problem;
|
---|
184 | }
|
---|
185 | catch (Exception ex) {
|
---|
186 | Invoke(new Action(() => ErrorHandling.ShowErrorDialog(this, ex)));
|
---|
187 | }
|
---|
188 | finally {
|
---|
189 | Invoke(new Action(delegate() {
|
---|
190 | problemViewHost.Enabled = true;
|
---|
191 | newProblemButton.Enabled = openProblemButton.Enabled = true;
|
---|
192 | }));
|
---|
193 | }
|
---|
194 | });
|
---|
195 | }
|
---|
196 | }
|
---|
197 | protected virtual void storeAlgorithmInEachRunCheckBox_CheckedChanged(object sender, EventArgs e) {
|
---|
198 | if (Content != null) Content.StoreAlgorithmInEachRun = storeAlgorithmInEachRunCheckBox.Checked;
|
---|
199 | }
|
---|
200 | protected virtual void problemTabPage_DragEnterOver(object sender, DragEventArgs e) {
|
---|
201 | e.Effect = DragDropEffects.None;
|
---|
202 | if (!ReadOnly && (e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat) != null) && Content.ProblemType.IsAssignableFrom(e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat).GetType())) {
|
---|
203 | if ((e.KeyState & 32) == 32) e.Effect = DragDropEffects.Link; // ALT key
|
---|
204 | else if ((e.KeyState & 4) == 4) e.Effect = DragDropEffects.Move; // SHIFT key
|
---|
205 | else if (e.AllowedEffect.HasFlag(DragDropEffects.Copy)) e.Effect = DragDropEffects.Copy;
|
---|
206 | else if (e.AllowedEffect.HasFlag(DragDropEffects.Move)) e.Effect = DragDropEffects.Move;
|
---|
207 | else if (e.AllowedEffect.HasFlag(DragDropEffects.Link)) e.Effect = DragDropEffects.Link;
|
---|
208 | }
|
---|
209 | }
|
---|
210 | protected virtual void problemTabPage_DragDrop(object sender, DragEventArgs e) {
|
---|
211 | if (e.Effect != DragDropEffects.None) {
|
---|
212 | IProblem problem = e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat) as IProblem;
|
---|
213 | if (e.Effect.HasFlag(DragDropEffects.Copy)) problem = (IProblem)problem.Clone();
|
---|
214 | Content.Problem = problem;
|
---|
215 | }
|
---|
216 | }
|
---|
217 | #endregion
|
---|
218 | }
|
---|
219 | }
|
---|