Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Clients.OKB.Views/3.3/RunCreation/Views/OKBProblemView.cs @ 13806

Last change on this file since 13806 was 13746, checked in by abeham, 9 years ago

#2588: added check for invokerequired in event handler

File size: 12.8 KB
RevLine 
[8055]1#region License Information
2/* HeuristicLab
[12012]3 * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[8055]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
[13534]22using HeuristicLab.Common.Resources;
[13666]23using HeuristicLab.Core;
[8055]24using HeuristicLab.Core.Views;
[13534]25using HeuristicLab.Data;
[8055]26using HeuristicLab.MainForm;
[13540]27using HeuristicLab.Optimization;
28using HeuristicLab.PluginInfrastructure;
[13593]29using System;
30using System.Collections.Generic;
[13666]31using System.Drawing;
[13593]32using System.Linq;
33using System.Windows.Forms;
[8055]34
35namespace HeuristicLab.Clients.OKB.RunCreation {
36  [View("OKBProblem View")]
37  [Content(typeof(SingleObjectiveOKBProblem), true)]
38  [Content(typeof(MultiObjectiveOKBProblem), true)]
39  public sealed partial class OKBProblemView : NamedItemView {
[13666]40    private readonly CheckedItemList<ICharacteristicCalculator> calculatorList;
41
[8055]42    public new OKBProblem Content {
43      get { return (OKBProblem)base.Content; }
44      set { base.Content = value; }
45    }
46
47    public OKBProblemView() {
48      InitializeComponent();
[13666]49      var calculatorListView = new CheckedItemListView<ICharacteristicCalculator>() {
50        Anchor = AnchorStyles.Left | AnchorStyles.Bottom | AnchorStyles.Right | AnchorStyles.Top,
51        Location = new Point(flaSplitContainer.Padding.Left, calculateButton.Location.Y + calculateButton.Height + calculateButton.Padding.Bottom + 3),
52      };
53      calculatorListView.Size = new Size(flaSplitContainer.Panel1.Size.Width - flaSplitContainer.Panel1.Padding.Horizontal,
54          flaSplitContainer.Panel1.Height - calculatorListView.Location.Y - flaSplitContainer.Panel1.Padding.Bottom);
55      calculatorList = new CheckedItemList<ICharacteristicCalculator>();
56      calculatorList.ItemsAdded += CalculatorListOnChanged;
57      calculatorList.ItemsRemoved += CalculatorListOnChanged;
58      calculatorList.ItemsReplaced += CalculatorListOnChanged;
59      calculatorList.CollectionReset += CalculatorListOnChanged;
60      calculatorList.CheckedItemsChanged += CalculatorListOnChanged;
61
62      calculatorListView.Content = calculatorList.AsReadOnly();
63
64      flaSplitContainer.Panel1.Controls.Add(calculatorListView);
65      calculateButton.Text = string.Empty;
66      calculateButton.Image = VSImageLibrary.Play;
[13540]67      refreshButton.Text = string.Empty;
68      refreshButton.Image = VSImageLibrary.Refresh;
69      cloneProblemButton.Text = string.Empty;
70      cloneProblemButton.Image = VSImageLibrary.Clone;
[13534]71      downloadCharacteristicsButton.Text = string.Empty;
72      downloadCharacteristicsButton.Image = VSImageLibrary.Refresh;
73      uploadCharacteristicsButton.Text = string.Empty;
74      uploadCharacteristicsButton.Image = VSImageLibrary.PublishToWeb;
[13691]75      refreshSolutionsButton.Text = string.Empty;
76      refreshSolutionsButton.Image = VSImageLibrary.Refresh;
77      uploadSolutionsButton.Text = string.Empty;
78      uploadSolutionsButton.Image = VSImageLibrary.PublishToWeb;
[8055]79    }
80
[13666]81    private void CalculatorListOnChanged(object sender, EventArgs e) {
82      SetEnabledStateOfControls();
83    }
84
[8055]85    protected override void OnInitialized(System.EventArgs e) {
86      base.OnInitialized(e);
87      RunCreationClient.Instance.Refreshing += new EventHandler(RunCreationClient_Refreshing);
88      RunCreationClient.Instance.Refreshed += new EventHandler(RunCreationClient_Refreshed);
89      PopulateComboBox();
90    }
91
92    protected override void DeregisterContentEvents() {
93      Content.ProblemChanged -= new EventHandler(Content_ProblemChanged);
94      base.DeregisterContentEvents();
95    }
96    protected override void RegisterContentEvents() {
97      base.RegisterContentEvents();
98      Content.ProblemChanged += new EventHandler(Content_ProblemChanged);
[13691]99      Content.Solutions.ItemsAdded += SolutionsOnChanged;
100      Content.Solutions.ItemsReplaced += SolutionsOnChanged;
101      Content.Solutions.ItemsRemoved += SolutionsOnChanged;
102      Content.Solutions.CollectionReset += SolutionsOnChanged;
[8055]103    }
104
[13691]105    private void SolutionsOnChanged(object sender, EventArgs e) {
[13746]106      if (InvokeRequired) { Invoke((Action<object, EventArgs>)SolutionsOnChanged, sender, e); return; }
[13691]107      SetEnabledStateOfControls();
108    }
109
[8055]110    protected override void OnContentChanged() {
111      base.OnContentChanged();
112      if (Content == null) {
113        problemComboBox.SelectedIndex = -1;
114        parameterCollectionView.Content = null;
[13691]115        solutionsViewHost.Content = null;
[8055]116      } else {
117        problemComboBox.SelectedItem = RunCreationClient.Instance.Problems.FirstOrDefault(x => x.Id == Content.ProblemId);
118        parameterCollectionView.Content = Content.Parameters;
[13691]119        solutionsViewHost.Content = Content.Solutions;
[8055]120      }
[13540]121      UpdateCharacteristicCalculators();
[8055]122    }
123
124    protected override void SetEnabledStateOfControls() {
125      base.SetEnabledStateOfControls();
126      problemComboBox.Enabled = (Content != null) && !ReadOnly && !Locked && (problemComboBox.Items.Count > 0);
127      cloneProblemButton.Enabled = (Content != null) && (Content.ProblemId != -1) && !ReadOnly && !Locked;
128      refreshButton.Enabled = (Content != null) && !ReadOnly && !Locked;
129      parameterCollectionView.Enabled = Content != null;
[13534]130      characteristicsMatrixView.Enabled = Content != null;
131      downloadCharacteristicsButton.Enabled = Content != null && Content.ProblemId != -1 && !Locked;
132      uploadCharacteristicsButton.Enabled = Content != null && Content.ProblemId != -1 && !Locked && !ReadOnly
133        && characteristicsMatrixView.Content != null && characteristicsMatrixView.Content.Rows > 0;
[13666]134      calculateButton.Enabled = Content != null && Content.ProblemId != -1 && !Locked && !ReadOnly && calculatorList.CheckedItems.Any();
[13691]135      refreshSolutionsButton.Enabled = Content != null && !ReadOnly && !Locked && Content.ProblemId != -1;
136      uploadSolutionsButton.Enabled = Content != null && !ReadOnly && !Locked && Content.ProblemId != -1 && Content.Solutions.Any(x => x.SolutionId == -1);
[8055]137    }
138
139    protected override void OnClosed(FormClosedEventArgs e) {
140      RunCreationClient.Instance.Refreshing -= new EventHandler(RunCreationClient_Refreshing);
141      RunCreationClient.Instance.Refreshed -= new EventHandler(RunCreationClient_Refreshed);
142      base.OnClosed(e);
143    }
144
[13540]145    private void UpdateCharacteristicCalculators() {
[13666]146      calculatorList.Clear();
[13540]147      if (Content == null || Content.ProblemId == -1) return;
148      var problem = Content.CloneProblem();
[13593]149      var calculators = ApplicationManager.Manager.GetInstances<ICharacteristicCalculator>().ToList();
[13666]150      foreach (var calc in calculators) {
151        calc.Problem = problem;
152        if (!calc.CanCalculate()) continue;
153        calculatorList.Add(calc, true);
154      }
[13540]155    }
156
[8055]157    private void RunCreationClient_Refreshing(object sender, EventArgs e) {
158      if (InvokeRequired) {
159        Invoke(new EventHandler(RunCreationClient_Refreshing), sender, e);
160      } else {
161        Cursor = Cursors.AppStarting;
162        problemComboBox.Enabled = cloneProblemButton.Enabled = refreshButton.Enabled = parameterCollectionView.Enabled = false;
163      }
164    }
165    private void RunCreationClient_Refreshed(object sender, EventArgs e) {
166      if (InvokeRequired) {
167        Invoke(new EventHandler(RunCreationClient_Refreshed), sender, e);
168      } else {
169        PopulateComboBox();
170        SetEnabledStateOfControls();
171        Cursor = Cursors.Default;
172      }
173    }
174
175    #region Content Events
176    private void Content_ProblemChanged(object sender, EventArgs e) {
177      if (InvokeRequired)
178        Invoke(new EventHandler(Content_ProblemChanged), sender, e);
[13534]179      else {
[8055]180        OnContentChanged();
[13534]181        SetEnabledStateOfControls();
182      }
[8055]183    }
184    #endregion
185
186    #region Control Events
187    private void cloneProblemButton_Click(object sender, EventArgs e) {
188      MainFormManager.MainForm.ShowContent(Content.CloneProblem());
189    }
190    private void refreshButton_Click(object sender, System.EventArgs e) {
191      RunCreationClient.Instance.Refresh();
192    }
193    private void problemComboBox_SelectedValueChanged(object sender, System.EventArgs e) {
194      Problem problem = problemComboBox.SelectedValue as Problem;
195      if ((problem != null) && (Content != null)) {
196        Content.Load(problem.Id);
197        if (Content.ProblemId != problem.Id)  // reset selected item if load was not successful
198          problemComboBox.SelectedItem = RunCreationClient.Instance.Problems.FirstOrDefault(x => x.Id == Content.ProblemId);
199      }
200    }
[13534]201    private void downloadCharacteristicsButton_Click(object sender, EventArgs e) {
[13684]202      var values = RunCreationClient.Instance.GetCharacteristicValues(Content.ProblemId).ToList();
[13534]203      var content = new StringMatrix(values.Count, 3);
204      for (var i = 0; i < values.Count; i++) {
205        content[i, 0] = values[i].Name;
[13552]206        content[i, 1] = values[i].GetValue();
[13534]207        content[i, 2] = values[i].GetType().Name;
208      }
209      characteristicsMatrixView.Content = content;
210      SetEnabledStateOfControls();
211    }
212    private void uploadCharacteristicsButton_Click(object sender, EventArgs e) {
213      var matrix = characteristicsMatrixView.Content as StringMatrix;
214      if (matrix == null) return;
215      var values = new List<Value>(matrix.Rows);
216      for (var i = 0; i < matrix.Rows; i++) {
217        var name = matrix[i, 0];
218        var strValue = matrix[i, 1];
219        var type = matrix[i, 2];
220        values.Add(Value.Create(name, strValue, type));
221      }
222      try {
[13684]223        RunCreationClient.Instance.SetCharacteristicValues(Content.ProblemId, values);
[13550]224      } catch (Exception ex) { ErrorHandling.ShowErrorDialog(ex); }
[13534]225    }
[13540]226    private void calculateButton_Click(object sender, EventArgs e) {
[13666]227      var calculators = calculatorList.CheckedItems.Select(x => x.Value).Where(x => x.CanCalculate()).ToList();
[13593]228      if (calculators.Count == 0) return;
229
[13550]230      var results = new Dictionary<string, Value>();
[13593]231      foreach (var calc in calculators) {
232        foreach (var result in calc.Calculate())
233          results[result.Name] = RunCreationClient.Instance.ConvertToValue(result.Value, result.Name);
[13540]234      }
[13550]235      var matrix = (characteristicsMatrixView.Content as StringMatrix) ?? (new StringMatrix(results.Count, 3));
[13691]236      try {
237        for (var i = 0; i < matrix.Rows; i++) {
238          Value r;
239          if (results.TryGetValue(matrix[i, 0], out r)) {
240            matrix[i, 1] = r.GetValue();
241            matrix[i, 2] = r.GetType().Name;
242            results.Remove(matrix[i, 0]);
243          }
[13540]244        }
[13691]245        if (results.Count == 0) return;
246        var resultsList = results.ToList();
247        var counter = resultsList.Count - 1;
248        for (var i = 0; i < matrix.Rows; i++) {
249          if (string.IsNullOrEmpty(matrix[i, 0])) {
250            matrix[i, 0] = resultsList[counter].Key;
251            matrix[i, 1] = resultsList[counter].Value.GetValue();
252            matrix[i, 2] = resultsList[counter].Value.GetType().Name;
253            resultsList.RemoveAt(counter);
254            counter--;
255            if (counter < 0) return;
256          }
[13540]257        }
[13691]258        if (counter >= 0) {
259          ((IStringConvertibleMatrix)matrix).Rows += counter + 1;
260          for (var i = matrix.Rows - 1; counter >= 0; i--) {
261            matrix[i, 0] = resultsList[0].Key;
262            matrix[i, 1] = resultsList[0].Value.GetValue();
263            matrix[i, 2] = resultsList[0].Value.GetType().Name;
264            resultsList.RemoveAt(0);
265            counter--;
266          }
[13540]267        }
[13691]268      } finally {
269        characteristicsMatrixView.Content = matrix;
270        SetEnabledStateOfControls();
[13540]271      }
272    }
[8055]273    #endregion
274
275    #region Helpers
276    private void PopulateComboBox() {
277      problemComboBox.DataSource = null;
278      problemComboBox.DataSource = RunCreationClient.Instance.Problems.ToList();
279      problemComboBox.DisplayMember = "Name";
280    }
281    #endregion
[13550]282
[13691]283    private void refreshSolutionsButton_Click(object sender, EventArgs e) {
284      Content.RefreshSolutions();
285    }
286
287    private void uploadSolutionsButton_Click(object sender, EventArgs e) {
288      foreach (var solution in Content.Solutions.Where(x => x.SolutionId == -1))
289        solution.Upload();
[13692]290      SetEnabledStateOfControls();
[13691]291    }
292
[8055]293  }
294}
Note: See TracBrowser for help on using the repository browser.