Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Problems.Instances.Views/3.3/ProblemInstanceProviderViewGeneric.cs @ 10020

Last change on this file since 10020 was 10020, checked in by gkronber, 11 years ago

#1508: merged r9804:9805,r9808:9809,r9811:9812,r9822,r9824:9825,r9897,r9928,r9938:9941,r9964:9965,r9989,r9991:9992,r9995,r9997,r10004:10015 from trunk into stable branch.

File size: 5.4 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2013 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.ComponentModel;
24using System.Linq;
25using System.Threading.Tasks;
26using System.Windows.Forms;
27using HeuristicLab.MainForm;
28using HeuristicLab.MainForm.WindowsForms;
29using HeuristicLab.PluginInfrastructure;
30
31namespace HeuristicLab.Problems.Instances.Views {
32  [View("ProblemInstanceProviderViewGeneric")]
33  [Content(typeof(IProblemInstanceProvider<>), IsDefaultView = true)]
34  public partial class ProblemInstanceProviderViewGeneric<T> : ProblemInstanceProviderView {
35
36    public new IProblemInstanceProvider<T> Content {
37      get { return (IProblemInstanceProvider<T>)base.Content; }
38      set { base.Content = value; }
39    }
40
41    private IProblemInstanceConsumer<T> GenericConsumer { get { return Consumer as IProblemInstanceConsumer<T>; } }
42
43    private IProblemInstanceConsumer consumer;
44    public override IProblemInstanceConsumer Consumer {
45      get { return consumer; }
46      set {
47        consumer = value;
48        SetEnabledStateOfControls();
49      }
50    }
51
52    public ProblemInstanceProviderViewGeneric() {
53      InitializeComponent();
54    }
55
56    protected override void OnContentChanged() {
57      base.OnContentChanged();
58      if (Content == null) {
59        instancesComboBox.DataSource = null;
60      } else {
61        instancesComboBox.DisplayMember = "Name";
62        var dataDescriptors = Content.GetDataDescriptors().ToList();
63        ShowInstanceLoad(dataDescriptors.Any());
64        instancesComboBox.DataSource = dataDescriptors;
65        instancesComboBox.SelectedIndex = -1;
66      }
67    }
68
69    protected void ShowInstanceLoad(bool show) {
70      if (show) {
71        instanceLabel.Show();
72        instancesComboBox.Show();
73      } else {
74        instanceLabel.Hide();
75        instancesComboBox.Hide();
76      }
77    }
78
79    protected override void SetEnabledStateOfControls() {
80      base.SetEnabledStateOfControls();
81      instancesComboBox.Enabled = !ReadOnly && !Locked && Content != null && GenericConsumer != null;
82    }
83
84    private void instancesComboBox_DataSourceChanged(object sender, EventArgs e) {
85      var comboBox = (ComboBox)sender;
86      if (comboBox.DataSource == null)
87        comboBox.Items.Clear();
88      toolTip.SetToolTip(comboBox, String.Empty);
89    }
90
91    private void instancesComboBox_SelectionChangeCommitted(object sender, System.EventArgs e) {
92      toolTip.SetToolTip(instancesComboBox, String.Empty);
93      if (instancesComboBox.SelectedIndex >= 0) {
94        var descriptor = (IDataDescriptor)instancesComboBox.SelectedItem;
95
96        IContentView activeView = (IContentView)MainFormManager.MainForm.ActiveView;
97        var mainForm = (MainForm.WindowsForms.MainForm)MainFormManager.MainForm;
98        // lock active view and show progress bar
99        mainForm.AddOperationProgressToContent(activeView.Content, "Loading problem instance.");
100        // continuation for removing the progess bar from the active view
101        Action<Task> removeProgressFromContent = (_) => mainForm.RemoveOperationProgressFromContent(activeView.Content);
102
103        // task structure:
104        // loadFromProvider
105        // |
106        // +-> on fault -> show error dialog -> remove progress bar
107        // |
108        // `-> success  -> loadToProblem
109        //                 |
110        //                 +-> on fault -> show error dialog -> remove progress bar
111        //                 |
112        //                 `-> success -> set tool tip -> remove progress bar
113        var loadFromProvider = new Task<T>(() => Content.LoadData(descriptor));
114
115        // success
116        var loadToProblem = loadFromProvider
117          .ContinueWith(task => GenericConsumer.Load(task.Result), TaskContinuationOptions.OnlyOnRanToCompletion);
118        // on error
119        loadFromProvider
120          .ContinueWith(task => { ErrorHandling.ShowErrorDialog(String.Format("Could not load the problem instance {0}", descriptor.Name), task.Exception); }, TaskContinuationOptions.OnlyOnFaulted)
121          .ContinueWith(removeProgressFromContent);
122
123        // success
124        loadToProblem
125          .ContinueWith(task => toolTip.SetToolTip(instancesComboBox, descriptor.Description), TaskContinuationOptions.OnlyOnRanToCompletion)
126          .ContinueWith(removeProgressFromContent);
127        // on error
128        loadToProblem.ContinueWith(task => { ErrorHandling.ShowErrorDialog(String.Format("This problem does not support loading the instance {0}", descriptor.Name), task.Exception); }, TaskContinuationOptions.OnlyOnFaulted)
129        .ContinueWith(removeProgressFromContent);
130
131        // start async loading task
132        loadFromProvider.Start();
133      }
134    }
135  }
136}
Note: See TracBrowser for help on using the repository browser.