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 |
|
---|
31 | namespace 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 | protected IProblemInstanceConsumer<T> GenericConsumer { get { return Consumer as IProblemInstanceConsumer<T>; } }
|
---|
42 | protected IProblemInstanceConsumer consumer;
|
---|
43 | public override IProblemInstanceConsumer Consumer {
|
---|
44 | get { return consumer; }
|
---|
45 | set {
|
---|
46 | consumer = value;
|
---|
47 | SetEnabledStateOfControls();
|
---|
48 | }
|
---|
49 | }
|
---|
50 |
|
---|
51 | protected IProblemInstanceExporter<T> GenericExporter { get { return Exporter as IProblemInstanceExporter<T>; } }
|
---|
52 | protected IProblemInstanceExporter exporter;
|
---|
53 | public override IProblemInstanceExporter Exporter {
|
---|
54 | get { return exporter; }
|
---|
55 | set {
|
---|
56 | exporter = value;
|
---|
57 | SetEnabledStateOfControls();
|
---|
58 | }
|
---|
59 | }
|
---|
60 |
|
---|
61 | public ProblemInstanceProviderViewGeneric() {
|
---|
62 | InitializeComponent();
|
---|
63 | importButton.Text = String.Empty;
|
---|
64 | importButton.Image = VSImageLibrary.Open;
|
---|
65 | toolTip.SetToolTip(importButton, "Import a " + GetProblemType() + " problem from file.");
|
---|
66 | exportButton.Text = String.Empty;
|
---|
67 | exportButton.Image = VSImageLibrary.Save;
|
---|
68 | toolTip.SetToolTip(exportButton, "Export currently loaded " + GetProblemType() + " problem to a file.");
|
---|
69 | loadButton.Text = String.Empty;
|
---|
70 | loadButton.Image = VSImageLibrary.RefreshDocument;
|
---|
71 | toolTip.SetToolTip(loadButton, "Load the selected problem.");
|
---|
72 | }
|
---|
73 |
|
---|
74 | protected override void OnContentChanged() {
|
---|
75 | base.OnContentChanged();
|
---|
76 | if (Content == null) {
|
---|
77 | instancesComboBox.DataSource = null;
|
---|
78 | } else {
|
---|
79 | instancesComboBox.DisplayMember = "Name";
|
---|
80 | instancesComboBox.DataSource = Content.GetDataDescriptors().ToList();
|
---|
81 | }
|
---|
82 | }
|
---|
83 |
|
---|
84 | protected override void SetEnabledStateOfControls() {
|
---|
85 | base.SetEnabledStateOfControls();
|
---|
86 | instancesComboBox.Enabled = !ReadOnly && !Locked && Content != null && GenericConsumer != null;
|
---|
87 | loadButton.Enabled = !ReadOnly && !Locked && Content != null && GenericConsumer != null;
|
---|
88 | importButton.Enabled = !ReadOnly && !Locked && Content != null && GenericConsumer != null;
|
---|
89 | exportButton.Enabled = !ReadOnly && !Locked && Content != null && GenericExporter != null;
|
---|
90 | problemInstanceProviderSplitContainer.Panel2Collapsed = !exportButton.Enabled;
|
---|
91 | }
|
---|
92 |
|
---|
93 | protected virtual void loadButton_Click(object sender, EventArgs e) {
|
---|
94 | var descriptor = (IDataDescriptor)instancesComboBox.SelectedItem;
|
---|
95 | var instance = Content.LoadData(descriptor);
|
---|
96 | try {
|
---|
97 | GenericConsumer.Load(instance);
|
---|
98 | }
|
---|
99 | catch (Exception ex) {
|
---|
100 | MessageBox.Show(String.Format("This problem does not support loading the instance {0}: {1}", descriptor.Name, Environment.NewLine + ex.Message), "Cannot load instance");
|
---|
101 | }
|
---|
102 | }
|
---|
103 |
|
---|
104 | protected virtual void importButton_Click(object sender, EventArgs e) {
|
---|
105 | openFileDialog.FileName = GetProblemType() + " instance";
|
---|
106 | if (openFileDialog.ShowDialog() == DialogResult.OK) {
|
---|
107 | T instance = default(T);
|
---|
108 | try {
|
---|
109 | instance = Content.LoadData(openFileDialog.FileName);
|
---|
110 | }
|
---|
111 | catch (Exception ex) {
|
---|
112 | MessageBox.Show(String.Format("There was an error parsing the file: {0}", Environment.NewLine + ex.Message), "Error while parsing", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
---|
113 | return;
|
---|
114 | }
|
---|
115 | try {
|
---|
116 | GenericConsumer.Load(instance);
|
---|
117 | }
|
---|
118 | catch (Exception ex) {
|
---|
119 | 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");
|
---|
120 | }
|
---|
121 | }
|
---|
122 | }
|
---|
123 |
|
---|
124 | protected virtual void exportButton_Click(object sender, EventArgs e) {
|
---|
125 | if (saveFileDialog.ShowDialog(this) == DialogResult.OK) {
|
---|
126 | try {
|
---|
127 | Content.SaveData(GenericExporter.Export(), saveFileDialog.FileName);
|
---|
128 | }
|
---|
129 | catch (Exception ex) {
|
---|
130 | ErrorHandling.ShowErrorDialog(this, ex);
|
---|
131 | }
|
---|
132 | }
|
---|
133 | }
|
---|
134 |
|
---|
135 | private void comboBox_DataSourceChanged(object sender, EventArgs e) {
|
---|
136 | var comboBox = (ComboBox)sender;
|
---|
137 | if (comboBox.DataSource == null)
|
---|
138 | comboBox.Items.Clear();
|
---|
139 | }
|
---|
140 |
|
---|
141 | protected virtual string GetProblemType() {
|
---|
142 | string dataTypeName = typeof(T).Name.Split(new char[] { '.' }, StringSplitOptions.RemoveEmptyEntries).Last();
|
---|
143 | if (dataTypeName.EndsWith("Data"))
|
---|
144 | return dataTypeName.Substring(0, dataTypeName.Length - "Data".Length);
|
---|
145 | else return dataTypeName;
|
---|
146 | }
|
---|
147 | }
|
---|
148 | }
|
---|