[7956] | 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.Diagnostics;
|
---|
| 24 | using System.IO;
|
---|
| 25 | using System.Linq;
|
---|
| 26 | using System.Windows.Forms;
|
---|
| 27 | using HeuristicLab.Common.Resources;
|
---|
[8211] | 28 | using HeuristicLab.Core;
|
---|
[7956] | 29 | using HeuristicLab.MainForm;
|
---|
| 30 | using HeuristicLab.MainForm.WindowsForms;
|
---|
| 31 | using HeuristicLab.PluginInfrastructure;
|
---|
| 32 |
|
---|
| 33 | namespace HeuristicLab.Problems.Instances.Views {
|
---|
| 34 | [View("ProblemInstanceConsumerView")]
|
---|
| 35 | [Content(typeof(IProblemInstanceConsumer<>), IsDefaultView = true)]
|
---|
| 36 | public partial class ProblemInstanceConsumerViewGeneric<T> : ProblemInstanceConsumerView {
|
---|
| 37 |
|
---|
| 38 | public new IProblemInstanceConsumer<T> Content {
|
---|
| 39 | get { return (IProblemInstanceConsumer<T>)base.Content; }
|
---|
| 40 | set { base.Content = value; }
|
---|
| 41 | }
|
---|
| 42 |
|
---|
| 43 | protected IProblemInstanceProvider<T> GenericSelectedProvider { get { return SelectedProvider as IProblemInstanceProvider<T>; } }
|
---|
| 44 | public IProblemInstanceProvider SelectedProvider { get; protected set; }
|
---|
| 45 |
|
---|
| 46 | #region Importer & Exporter
|
---|
| 47 | protected IProblemInstanceConsumer<T> GenericConsumer { get { return Consumer as IProblemInstanceConsumer<T>; } }
|
---|
| 48 | protected IProblemInstanceConsumer consumer;
|
---|
| 49 | public IProblemInstanceConsumer Consumer {
|
---|
| 50 | get { return consumer; }
|
---|
| 51 | set {
|
---|
| 52 | consumer = value;
|
---|
| 53 | SetEnabledStateOfControls();
|
---|
| 54 | }
|
---|
| 55 | }
|
---|
| 56 |
|
---|
| 57 | protected IProblemInstanceExporter<T> GenericExporter { get { return Exporter as IProblemInstanceExporter<T>; } }
|
---|
| 58 | protected IProblemInstanceExporter exporter;
|
---|
| 59 | public IProblemInstanceExporter Exporter {
|
---|
| 60 | get { return exporter; }
|
---|
| 61 | set {
|
---|
| 62 | exporter = value;
|
---|
| 63 | SetEnabledStateOfControls();
|
---|
| 64 | }
|
---|
| 65 | }
|
---|
| 66 | #endregion
|
---|
| 67 |
|
---|
| 68 | public ProblemInstanceConsumerViewGeneric() {
|
---|
| 69 | InitializeComponent();
|
---|
| 70 | importButton.Text = String.Empty;
|
---|
| 71 | importButton.Image = VSImageLibrary.Open;
|
---|
| 72 | exportButton.Text = String.Empty;
|
---|
| 73 | exportButton.Image = VSImageLibrary.SaveAs;
|
---|
| 74 | libraryInfoButton.Text = String.Empty;
|
---|
| 75 | libraryInfoButton.Image = VSImageLibrary.Help;
|
---|
| 76 | }
|
---|
| 77 |
|
---|
| 78 | protected override void OnContentChanged() {
|
---|
| 79 | base.OnContentChanged();
|
---|
| 80 | if (Content == null) {
|
---|
| 81 | problemInstanceProviders = null;
|
---|
| 82 | problemInstanceProviderComboBox.DataSource = null;
|
---|
| 83 | } else {
|
---|
| 84 | problemInstanceProviderComboBox.DisplayMember = "Name";
|
---|
| 85 | problemInstanceProviders = ProblemInstanceManager.GetProviders(Content);
|
---|
| 86 | problemInstanceProviderComboBox.DataSource = ProblemInstanceProviders.OrderBy(x => x.Name).ToList();
|
---|
| 87 | }
|
---|
| 88 | SetEnabledStateOfControls();
|
---|
| 89 | }
|
---|
| 90 |
|
---|
| 91 | protected override void SetEnabledStateOfControls() {
|
---|
| 92 | base.SetEnabledStateOfControls();
|
---|
| 93 | problemInstanceProviderComboBox.Enabled = !ReadOnly && !Locked && Content != null && problemInstanceProviderComboBox.Items.Count > 0;
|
---|
| 94 | libraryInfoButton.Enabled = SelectedProvider != null && SelectedProvider.WebLink != null;
|
---|
[8192] | 95 | importButton.Enabled = !ReadOnly && !Locked && Content != null && Consumer != null &&
|
---|
| 96 | GenericSelectedProvider != null && GenericSelectedProvider.CanImportData;
|
---|
| 97 | ProviderImportSplitContainer.Panel2Collapsed = !importButton.Enabled;
|
---|
[8180] | 98 | exportButton.Enabled = !ReadOnly && !Locked && Content != null && Exporter != null &&
|
---|
[8192] | 99 | GenericSelectedProvider != null && GenericSelectedProvider.CanExportData;
|
---|
| 100 | ProviderExportSplitContainer.Panel2Collapsed = !exportButton.Enabled;
|
---|
[7956] | 101 | }
|
---|
| 102 |
|
---|
| 103 | protected virtual void problemInstanceProviderComboBox_SelectedIndexChanged(object sender, System.EventArgs e) {
|
---|
| 104 | if (problemInstanceProviderComboBox.SelectedIndex >= 0) {
|
---|
| 105 | SelectedProvider = (IProblemInstanceProvider)problemInstanceProviderComboBox.SelectedItem;
|
---|
| 106 | problemInstanceProviderViewHost.Content = SelectedProvider;
|
---|
| 107 | ProblemInstanceProviderView view = (ProblemInstanceProviderView)problemInstanceProviderViewHost.ActiveView;
|
---|
| 108 | consumer = Content;
|
---|
| 109 | view.Consumer = Content;
|
---|
| 110 | if (CheckForIProblemInstanceExporter(Content)) {
|
---|
| 111 | exporter = (IProblemInstanceExporter)Content;
|
---|
| 112 | }
|
---|
| 113 | SetTooltip();
|
---|
| 114 | } else {
|
---|
| 115 | SelectedProvider = null;
|
---|
| 116 | }
|
---|
| 117 | SetEnabledStateOfControls();
|
---|
| 118 | }
|
---|
| 119 |
|
---|
| 120 | protected bool CheckForIProblemInstanceExporter(IProblemInstanceConsumer content) {
|
---|
| 121 | return Content.GetType().GetInterfaces()
|
---|
| 122 | .Any(x => x.Equals(typeof(IProblemInstanceExporter)));
|
---|
| 123 | }
|
---|
| 124 |
|
---|
| 125 | private void libraryInfoButton_Click(object sender, EventArgs e) {
|
---|
| 126 | if (problemInstanceProviderComboBox.SelectedIndex >= 0) {
|
---|
| 127 | if (SelectedProvider != null && SelectedProvider.WebLink != null)
|
---|
| 128 | Process.Start(SelectedProvider.WebLink.ToString());
|
---|
| 129 | }
|
---|
| 130 | }
|
---|
| 131 |
|
---|
| 132 | protected virtual void importButton_Click(object sender, EventArgs e) {
|
---|
| 133 | openFileDialog.FileName = GetProblemType() + " instance";
|
---|
| 134 | if (openFileDialog.ShowDialog() == DialogResult.OK) {
|
---|
| 135 | T instance = default(T);
|
---|
| 136 | try {
|
---|
[8192] | 137 | instance = GenericSelectedProvider.ImportData(openFileDialog.FileName);
|
---|
[7956] | 138 | }
|
---|
| 139 | catch (Exception ex) {
|
---|
| 140 | MessageBox.Show(String.Format("There was an error parsing the file: {0}", Environment.NewLine + ex.Message), "Error while parsing", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
---|
| 141 | return;
|
---|
| 142 | }
|
---|
| 143 | try {
|
---|
| 144 | GenericConsumer.Load(instance);
|
---|
| 145 | }
|
---|
| 146 | catch (Exception ex) {
|
---|
| 147 | 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");
|
---|
| 148 | }
|
---|
| 149 | }
|
---|
| 150 | }
|
---|
| 151 |
|
---|
[8192] | 152 | protected virtual void exportButton_Click(object sender, EventArgs e) {
|
---|
[7956] | 153 | if (saveFileDialog.ShowDialog(this) == DialogResult.OK) {
|
---|
| 154 | try {
|
---|
[8192] | 155 | GenericSelectedProvider.ExportData(GenericExporter.Export(), saveFileDialog.FileName);
|
---|
[7956] | 156 | }
|
---|
| 157 | catch (Exception ex) {
|
---|
| 158 | ErrorHandling.ShowErrorDialog(this, ex);
|
---|
| 159 | }
|
---|
| 160 | }
|
---|
| 161 | }
|
---|
| 162 |
|
---|
| 163 | protected string GetProblemType() {
|
---|
[8211] | 164 | var item = Content as IItem;
|
---|
| 165 | if (item != null)
|
---|
| 166 | return item.ItemName;
|
---|
| 167 | return "problem";
|
---|
| 168 | }
|
---|
| 169 | protected string GetProviderFormatInfo() {
|
---|
[8031] | 170 | return SelectedProvider.Name;
|
---|
[7956] | 171 | }
|
---|
| 172 |
|
---|
| 173 | #region ToolTip
|
---|
| 174 | protected void SetTooltip() {
|
---|
| 175 | toolTip.SetToolTip(problemInstanceProviderComboBox, GetProviderToolTip());
|
---|
[8211] | 176 | toolTip.SetToolTip(importButton, "Import a " + GetProblemType() + " from a file in the " + GetProviderFormatInfo() + " format.");
|
---|
| 177 | toolTip.SetToolTip(exportButton, "Export currently loaded " + GetProblemType() + " to a file in the " + GetProviderFormatInfo() + " format.");
|
---|
[7956] | 178 | if (SelectedProvider.WebLink != null)
|
---|
| 179 | toolTip.SetToolTip(libraryInfoButton, "Browse to " + SelectedProvider.WebLink.ToString());
|
---|
| 180 | else toolTip.SetToolTip(libraryInfoButton, "Library does not have a web reference.");
|
---|
| 181 | }
|
---|
| 182 |
|
---|
| 183 | private string GetProviderToolTip() {
|
---|
| 184 | var provider = SelectedProvider;
|
---|
| 185 | string toolTip = provider.Name;
|
---|
| 186 |
|
---|
| 187 | if (!String.IsNullOrEmpty(provider.ReferencePublication)) {
|
---|
| 188 | toolTip = toolTip
|
---|
| 189 | + Environment.NewLine + Environment.NewLine
|
---|
| 190 | + provider.ReferencePublication;
|
---|
| 191 | }
|
---|
| 192 | if (provider.WebLink != null) {
|
---|
| 193 | toolTip = toolTip
|
---|
| 194 | + Environment.NewLine
|
---|
| 195 | + provider.WebLink.ToString();
|
---|
| 196 | }
|
---|
| 197 |
|
---|
| 198 | return toolTip;
|
---|
| 199 | }
|
---|
| 200 | #endregion
|
---|
| 201 | }
|
---|
| 202 | }
|
---|