Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
09/24/13 11:49:28 (11 years ago)
Author:
jkarder
Message:

#2110: refactored ProblemInstanceProviderViews

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Problems.Instances.Views/3.3/ProblemInstanceProviderViewGeneric.cs

    r9991 r10000  
    2121
    2222using System;
    23 using System.ComponentModel;
     23using System.Diagnostics;
     24using System.IO;
    2425using System.Linq;
    2526using System.Threading.Tasks;
    2627using System.Windows.Forms;
     28using HeuristicLab.Common.Resources;
    2729using HeuristicLab.MainForm;
    2830using HeuristicLab.MainForm.WindowsForms;
     
    3941    }
    4042
    41     private IProblemInstanceConsumer<T> GenericConsumer { get { return Consumer as IProblemInstanceConsumer<T>; } }
    42 
    43     private IProblemInstanceConsumer consumer;
     43    #region Importer & Exporter
     44    protected IProblemInstanceConsumer<T> GenericConsumer { get { return Consumer as IProblemInstanceConsumer<T>; } }
     45    protected IProblemInstanceConsumer consumer;
    4446    public override IProblemInstanceConsumer Consumer {
    4547      get { return consumer; }
     
    4749        consumer = value;
    4850        SetEnabledStateOfControls();
     51        SetTooltip();
    4952      }
    5053    }
    5154
     55    protected IProblemInstanceExporter<T> GenericExporter { get { return Exporter as IProblemInstanceExporter<T>; } }
     56    protected IProblemInstanceExporter exporter;
     57    public override IProblemInstanceExporter Exporter {
     58      get { return exporter; }
     59      set {
     60        exporter = value;
     61        SetEnabledStateOfControls();
     62      }
     63    }
     64    #endregion
     65
    5266    public ProblemInstanceProviderViewGeneric() {
    5367      InitializeComponent();
     68      importButton.Text = String.Empty;
     69      importButton.Image = VSImageLibrary.Open;
     70      exportButton.Text = String.Empty;
     71      exportButton.Image = VSImageLibrary.SaveAs;
     72      libraryInfoButton.Text = String.Empty;
     73      libraryInfoButton.Image = VSImageLibrary.Help;
    5474    }
    5575
     
    6585        instancesComboBox.SelectedIndex = -1;
    6686      }
     87      SetTooltip();
    6788    }
    6889
     
    80101      base.SetEnabledStateOfControls();
    81102      instancesComboBox.Enabled = !ReadOnly && !Locked && Content != null && GenericConsumer != null;
     103      libraryInfoButton.Enabled = Content != null && Content.WebLink != null;
     104      importButton.Enabled = !ReadOnly && !Locked && Content != null && GenericConsumer != null && Content.CanImportData;
     105      splitContainer1.Panel1Collapsed = !importButton.Enabled;
     106      exportButton.Enabled = !ReadOnly && !Locked && Content != null && GenericExporter != null && Content.CanExportData;
     107      splitContainer2.Panel1Collapsed = !exportButton.Enabled;
    82108    }
    83109
     
    89115    }
    90116
    91     private void instancesComboBox_SelectionChangeCommitted(object sender, System.EventArgs e) {
     117    protected virtual void instancesComboBox_SelectionChangeCommitted(object sender, EventArgs e) {
    92118      toolTip.SetToolTip(instancesComboBox, String.Empty);
    93119      if (instancesComboBox.SelectedIndex >= 0) {
     
    98124        // lock active view and show progress bar
    99125        mainForm.AddOperationProgressToContent(activeView.Content, "Loading problem instance.");
    100         // continuation for removing the progess bar from the active view
    101         Action<Task> removeProgressFromContent = (_) => mainForm.RemoveOperationProgressFromContent(activeView.Content);
    102126
    103         // task structure:
    104         // loadFromProvider
    105         // |
    106         // +-> on fault -> show error dialog -> remove progress bar
    107         // |
    108         // `-> success  -> loadToProblem
    109         //                 |
    110         //                 +-> on fault -> show error dialog -> remove progress bar
    111         //                 |
    112         //                 `-> success -> set tool tip -> remove progress bar
    113         var loadFromProvider = new Task<T>(() => Content.LoadData(descriptor));
     127        Task.Factory.StartNew(() => {
     128          T data;
     129          try {
     130            data = Content.LoadData(descriptor);
     131          } catch (Exception ex) {
     132            ErrorHandling.ShowErrorDialog(String.Format("Could not load the problem instance {0}", descriptor.Name), ex);
     133            mainForm.RemoveOperationProgressFromContent(activeView.Content);
     134            return;
     135          }
     136          try {
     137            GenericConsumer.Load(data);
     138          } catch (Exception ex) {
     139            ErrorHandling.ShowErrorDialog(String.Format("This problem does not support loading the instance {0}", descriptor.Name), ex);
     140          } finally {
     141            mainForm.RemoveOperationProgressFromContent(activeView.Content);
     142          }
     143        });
     144      }
     145    }
    114146
    115         // success
    116         var loadToProblem = loadFromProvider
    117           .ContinueWith(task => GenericConsumer.Load(task.Result), TaskContinuationOptions.OnlyOnRanToCompletion);
    118         // on error
    119         loadFromProvider
    120           .ContinueWith(task => { ErrorHandling.ShowErrorDialog(String.Format("Could not load the problem instance {0}", descriptor.Name), task.Exception); }, TaskContinuationOptions.OnlyOnFaulted)
    121           .ContinueWith(removeProgressFromContent);
     147    private void libraryInfoButton_Click(object sender, EventArgs e) {
     148      Process.Start(Content.WebLink.ToString());
     149    }
    122150
    123         // success
    124         loadToProblem
    125           .ContinueWith(task => toolTip.SetToolTip(instancesComboBox, descriptor.Description), TaskContinuationOptions.OnlyOnRanToCompletion)
    126           .ContinueWith(removeProgressFromContent);
    127         // on error
    128         loadToProblem.ContinueWith(task => { ErrorHandling.ShowErrorDialog(String.Format("This problem does not support loading the instance {0}", descriptor.Name), task.Exception); }, TaskContinuationOptions.OnlyOnFaulted)
    129         .ContinueWith(removeProgressFromContent);
     151    protected virtual void importButton_Click(object sender, EventArgs e) {
     152      openFileDialog.FileName = Content.Name + " instance";
     153      if (openFileDialog.ShowDialog() == DialogResult.OK) {
     154        T instance = default(T);
     155        try {
     156          instance = Content.ImportData(openFileDialog.FileName);
     157        } catch (Exception ex) {
     158          MessageBox.Show(String.Format("There was an error parsing the file: {0}", Environment.NewLine + ex.Message), "Error while parsing", MessageBoxButtons.OK, MessageBoxIcon.Error);
     159          return;
     160        }
     161        try {
     162          GenericConsumer.Load(instance);
     163          instancesComboBox.SelectedIndex = -1;
     164        } catch (Exception ex) {
     165          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");
     166        }
     167      }
     168    }
    130169
    131         // start async loading task
    132         loadFromProvider.Start();
     170    protected virtual void exportButton_Click(object sender, EventArgs e) {
     171      if (saveFileDialog.ShowDialog(this) == DialogResult.OK) {
     172        try {
     173          Content.ExportData(GenericExporter.Export(), saveFileDialog.FileName);
     174        } catch (Exception ex) {
     175          ErrorHandling.ShowErrorDialog(this, ex);
     176        }
    133177      }
     178    }
     179
     180    protected override void SetTooltip() {
     181      toolTip.SetToolTip(importButton, "Import a " + GetProblemType() + " from a file in the " + GetProviderFormatInfo() + " format.");
     182      toolTip.SetToolTip(exportButton, "Export currently loaded " + GetProblemType() + " to a file in the " + GetProviderFormatInfo() + " format.");
     183      if (Content != null && Content.WebLink != null)
     184        toolTip.SetToolTip(libraryInfoButton, "Browse to " + Content.WebLink);
     185      else toolTip.SetToolTip(libraryInfoButton, "Library does not have a web reference.");
    134186    }
    135187  }
Note: See TracChangeset for help on using the changeset viewer.