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 |
|
---|
22 | using System;
|
---|
23 | using System.IO;
|
---|
24 | using System.Linq;
|
---|
25 | using System.Windows.Forms;
|
---|
26 | using HeuristicLab.Common.Resources;
|
---|
27 | using HeuristicLab.MainForm;
|
---|
28 | using HeuristicLab.MainForm.WindowsForms;
|
---|
29 | using HeuristicLab.PluginInfrastructure;
|
---|
30 | using HeuristicLab.Problems.Instances;
|
---|
31 |
|
---|
32 | namespace HeuristicLab.Optimization.Views {
|
---|
33 | [View("ProblemInstanceProviderView")]
|
---|
34 | [Content(typeof(IProblemInstanceProvider<>), IsDefaultView = false)]
|
---|
35 | public partial class ProblemInstanceProviderView<T> : AsynchronousContentView {
|
---|
36 |
|
---|
37 | public new IProblemInstanceProvider<T> Content {
|
---|
38 | get { return (IProblemInstanceProvider<T>)base.Content; }
|
---|
39 | set { base.Content = value; }
|
---|
40 | }
|
---|
41 |
|
---|
42 | public ProblemInstanceProviderView() {
|
---|
43 | InitializeComponent();
|
---|
44 | importButton.Text = String.Empty;
|
---|
45 | importButton.Image = VSImageLibrary.Open;
|
---|
46 | toolTip.SetToolTip(importButton, "Import a " + GetProblemType() + " instance from file.");
|
---|
47 | exportButton.Text = String.Empty;
|
---|
48 | exportButton.Image = VSImageLibrary.Save;
|
---|
49 | toolTip.SetToolTip(exportButton, "Export a " + GetProblemType() + " instance to a file.");
|
---|
50 | loadButton.Text = String.Empty;
|
---|
51 | loadButton.Image = VSImageLibrary.RefreshDocument;
|
---|
52 | toolTip.SetToolTip(loadButton, "Load the selected instance.");
|
---|
53 | }
|
---|
54 |
|
---|
55 | protected override void OnContentChanged() {
|
---|
56 | base.OnContentChanged();
|
---|
57 | if (Content == null) {
|
---|
58 | instancesComboBox.DataSource = null;
|
---|
59 | } else {
|
---|
60 | instancesComboBox.DisplayMember = "Name";
|
---|
61 | instancesComboBox.DataSource = Content.GetDataDescriptors().ToList();
|
---|
62 | }
|
---|
63 | }
|
---|
64 |
|
---|
65 | protected override void SetEnabledStateOfControls() {
|
---|
66 | base.SetEnabledStateOfControls();
|
---|
67 | instancesComboBox.Enabled = !ReadOnly && !Locked && Content != null && Content.Consumer != null;
|
---|
68 | loadButton.Enabled = !ReadOnly && !Locked && Content != null && Content.Consumer != null;
|
---|
69 | }
|
---|
70 |
|
---|
71 | protected virtual void loadButton_Click(object sender, EventArgs e) {
|
---|
72 | var descriptor = (IDataDescriptor)instancesComboBox.SelectedItem;
|
---|
73 | var instance = Content.LoadData(descriptor);
|
---|
74 | try {
|
---|
75 | Content.Consumer.Load(instance);
|
---|
76 | }
|
---|
77 | catch (Exception ex) {
|
---|
78 | MessageBox.Show(String.Format("This problem does not support loading the instance {0}: {1}", descriptor.Name, Environment.NewLine + ex.Message), "Cannot load instance");
|
---|
79 | }
|
---|
80 | }
|
---|
81 |
|
---|
82 | protected virtual void importButton_Click(object sender, EventArgs e) {
|
---|
83 | openFileDialog.FileName = GetProblemType() + " instance";
|
---|
84 | if (openFileDialog.ShowDialog() == DialogResult.OK) {
|
---|
85 | T instance = default(T);
|
---|
86 | try {
|
---|
87 | instance = Content.LoadData(openFileDialog.FileName);
|
---|
88 | }
|
---|
89 | catch (Exception ex) {
|
---|
90 | MessageBox.Show(String.Format("There was an error parsing the file: {0}", Environment.NewLine + ex.Message), "Error while parsing", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
---|
91 | return;
|
---|
92 | }
|
---|
93 | try {
|
---|
94 | Content.Consumer.Load(instance);
|
---|
95 | }
|
---|
96 | catch (Exception ex) {
|
---|
97 | MessageBox.Show(String.Format("This problem does not support loading the instance {0}: {1}", Path.GetFileName(openFileDialog.FileName), Environment.NewLine + ex.Message), "Cannot load instance");
|
---|
98 | }
|
---|
99 | }
|
---|
100 | }
|
---|
101 |
|
---|
102 | protected virtual void exportButton_Click(object sender, EventArgs e) {
|
---|
103 | if (saveFileDialog.ShowDialog(this) == DialogResult.OK) {
|
---|
104 | try {
|
---|
105 | var descriptor = (IDataDescriptor)instancesComboBox.SelectedItem;
|
---|
106 | var instance = Content.LoadData(descriptor);
|
---|
107 | Content.SaveData(instance, saveFileDialog.FileName);
|
---|
108 | }
|
---|
109 | catch (Exception ex) {
|
---|
110 | ErrorHandling.ShowErrorDialog(this, ex);
|
---|
111 | }
|
---|
112 | }
|
---|
113 | }
|
---|
114 |
|
---|
115 | private void comboBox_DataSourceChanged(object sender, EventArgs e) {
|
---|
116 | var comboBox = (ComboBox)sender;
|
---|
117 | if (comboBox.DataSource == null)
|
---|
118 | comboBox.Items.Clear();
|
---|
119 | }
|
---|
120 |
|
---|
121 | protected virtual string GetProblemType() {
|
---|
122 | string dataTypeName = typeof(T).Name.Split(new char[] { '.' }, StringSplitOptions.RemoveEmptyEntries).Last();
|
---|
123 | if (dataTypeName.EndsWith("Data"))
|
---|
124 | return dataTypeName.Substring(0, dataTypeName.Length - "Data".Length);
|
---|
125 | else return dataTypeName;
|
---|
126 | }
|
---|
127 | }
|
---|
128 | }
|
---|