Free cookie consent management tool by TermsFeed Policy Generator

source: branches/GeneralizedQAP/HeuristicLab.Problems.GeneralizedQuadraticAssignment.Views/3.3/ProblemInstanceProviderView.cs @ 7538

Last change on this file since 7538 was 7538, checked in by abeham, 12 years ago

#1614

  • Fixed plugin dependencies
  • Updated GQAP view
  • Changed instances infrastructure
    • Changed interface types into classes
    • Removed the library specific instance classes
File size: 3.6 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 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.Resources;
26using HeuristicLab.MainForm;
27using HeuristicLab.MainForm.WindowsForms;
28using HeuristicLab.Problems.Instances;
29
30namespace HeuristicLab.Problems.GeneralizedQuadraticAssignment.Views {
31  [View("ProblemInstanceProviderView")]
32  [Content(typeof(IProblemInstanceProvider<>), IsDefaultView = true)]
33  public partial class ProblemInstanceProviderView<T> : AsynchronousContentView {
34
35    public new IProblemInstanceProvider<T> Content {
36      get { return (IProblemInstanceProvider<T>)base.Content; }
37      set { base.Content = value; }
38    }
39
40    public ProblemInstanceProviderView() {
41      InitializeComponent();
42      importButton.Image = VSImageLibrary.Open;
43    }
44
45    protected override void OnContentChanged() {
46      base.OnContentChanged();
47      if (Content == null) {
48        instancesComboBox.DataSource = null;
49      } else {
50        instancesComboBox.DisplayMember = "Name";
51        instancesComboBox.DataSource = Content.GetInstanceDescriptors().ToList();
52      }
53    }
54
55    protected override void SetEnabledStateOfControls() {
56      base.SetEnabledStateOfControls();
57      instancesComboBox.Enabled = !ReadOnly && !Locked && Content != null && Content.Consumer != null;
58      loadButton.Enabled = !ReadOnly && !Locked && Content != null && Content.Consumer != null;
59    }
60
61    private void loadButton_Click(object sender, EventArgs e) {
62      var descriptor = (IInstanceDescriptor)instancesComboBox.SelectedItem;
63      var instance = Content.LoadInstance(descriptor);
64      if (!Content.Consumer.LoadFrom(instance)) {
65        MessageBox.Show("This problem does not support loading the instance " + descriptor.Name + ".", "Cannot load instance");
66      }
67    }
68
69    private void importButton_Click(object sender, EventArgs e) {
70      if (openFileDialog.ShowDialog() == DialogResult.OK) {
71        T instance = default(T);
72        try {
73          instance = Content.LoadInstance(openFileDialog.FileName);
74        } catch {
75          MessageBox.Show("There was an error parsing the file.", "Error while parsing", MessageBoxButtons.OK, MessageBoxIcon.Error);
76          return;
77        }
78        try {
79          if (!Content.Consumer.LoadFrom(instance)) {
80            MessageBox.Show("This problem does not support loading the instance in the file.", "Cannot load instance", MessageBoxButtons.OK, MessageBoxIcon.Error);
81          }
82        } catch {
83          MessageBox.Show("There was an error while importing the file.");
84        }
85      }
86    }
87
88    private void comboBox_DataSourceChanged(object sender, EventArgs e) {
89      var comboBox = (ComboBox)sender;
90      if (comboBox.DataSource == null)
91        comboBox.Items.Clear();
92    }
93  }
94}
Note: See TracBrowser for help on using the repository browser.