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