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