Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
11/07/12 16:28:33 (11 years ago)
Author:
mkommend
Message:

#1942: Reintegrated branch for CSV import.

Location:
trunk/sources/HeuristicLab.Problems.Instances.DataAnalysis.Views
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Problems.Instances.DataAnalysis.Views

  • trunk/sources/HeuristicLab.Problems.Instances.DataAnalysis.Views/3.3/DataAnalysisImportTypeDialog.cs

    r8693 r8877  
    2020#endregion
    2121
     22using System;
     23using System.Collections.Generic;
     24using System.ComponentModel;
     25using System.Globalization;
     26using System.Linq;
    2227using System.Windows.Forms;
     28using HeuristicLab.Core.Views;
     29using HeuristicLab.Problems.DataAnalysis;
    2330
    2431namespace HeuristicLab.Problems.Instances.DataAnalysis.Views {
    2532  public partial class DataAnalysisImportTypeDialog : Form {
     33
     34    public static readonly BindingList<KeyValuePair<DateTimeFormatInfo, string>> dateTimeFormats = new BindingList<KeyValuePair<DateTimeFormatInfo, string>>{
     35      new KeyValuePair<DateTimeFormatInfo, string>(DateTimeFormatInfo.GetInstance(new CultureInfo("de-DE")), "dd/mm/yyyy hh:MM:ss" ),
     36      new KeyValuePair<DateTimeFormatInfo, string>(DateTimeFormatInfo.InvariantInfo, "mm/dd/yyyy hh:MM:ss" ),
     37      new KeyValuePair<DateTimeFormatInfo, string>(DateTimeFormatInfo.InvariantInfo, "yyyy/mm/dd hh:MM:ss" ),
     38      new KeyValuePair<DateTimeFormatInfo, string>(DateTimeFormatInfo.InvariantInfo, "mm/yyyy/dd hh:MM:ss" )
     39    };
     40
     41    public static readonly BindingList<KeyValuePair<char, string>> POSSIBLE_SEPARATORS = new BindingList<KeyValuePair<char, string>>{
     42      new KeyValuePair<char, string>(',', ", (Comma)" ),
     43      new KeyValuePair<char, string>(';', "; (Semicolon)" ),
     44      new KeyValuePair<char, string>('\t', "\\t (Tab)")
     45    };
     46
     47    public static readonly BindingList<KeyValuePair<NumberFormatInfo, string>> POSSIBLE_DECIMAL_SEPARATORS = new BindingList<KeyValuePair<NumberFormatInfo, string>>{
     48      new KeyValuePair<NumberFormatInfo, string>(NumberFormatInfo.InvariantInfo, ". (Period)" ),
     49      new KeyValuePair<NumberFormatInfo, string>(NumberFormatInfo.GetInstance(new CultureInfo("de-DE")), ", (Comma)" )     
     50    };
    2651
    2752    public string Path {
     
    3863    }
    3964
     65    public DataAnalysisCSVFormat CSVFormat {
     66      get {
     67        return new DataAnalysisCSVFormat() {
     68          Separator = (char)SeparatorComboBox.SelectedValue,
     69          NumberFormatInfo = (NumberFormatInfo)DecimalSeparatorComboBox.SelectedValue,
     70          DateTimeFormatInfo = (DateTimeFormatInfo)DateTimeFormatComboBox.SelectedValue
     71        };
     72      }
     73    }
     74
    4075    public DataAnalysisImportTypeDialog() {
    4176      InitializeComponent();
     77      ToolTip.SetToolTip(SeparatorInfoLabel, (string)SeparatorInfoLabel.Tag);
     78      ToolTip.SetToolTip(DecimalSeparatorInfoLabel, (string)DecimalSeparatorInfoLabel.Tag);
     79      ToolTip.SetToolTip(DateTimeFormatInfoLabel, (string)DateTimeFormatInfoLabel.Tag);
     80      ToolTip.SetToolTip(ShuffelInfoLabel, (string)ShuffelInfoLabel.Tag);
     81
     82      SeparatorComboBox.DataSource = POSSIBLE_SEPARATORS;
     83      SeparatorComboBox.ValueMember = "Key";
     84      SeparatorComboBox.DisplayMember = "Value";
     85      DecimalSeparatorComboBox.DataSource = POSSIBLE_DECIMAL_SEPARATORS;
     86      DecimalSeparatorComboBox.ValueMember = "Key";
     87      DecimalSeparatorComboBox.DisplayMember = "Value";
     88      DateTimeFormatComboBox.DataSource = dateTimeFormats;
     89      DateTimeFormatComboBox.ValueMember = "Key";
     90      DateTimeFormatComboBox.DisplayMember = "Value";
    4291    }
    4392
     
    4796    }
    4897
    49     private void openFileButton_Click(object sender, System.EventArgs e) {
    50       if (openFileDialog.ShowDialog(this) == DialogResult.OK) {
    51         ProblemTextBox.Text = openFileDialog.FileName;
     98    protected virtual void OpenFileButtonClick(object sender, System.EventArgs e) {
     99      if (openFileDialog.ShowDialog(this) != DialogResult.OK) return;
     100
     101      ProblemTextBox.Text = openFileDialog.FileName;
     102      ParseCSVFile();
     103    }
     104
     105    protected virtual void CSVFormatComboBoxSelectionChangeCommitted(object sender, EventArgs e) {
     106      if (string.IsNullOrEmpty(ProblemTextBox.Text)) return;
     107
     108      ParseCSVFile();
     109    }
     110
     111    protected void ParseCSVFile() {
     112      PreviewDatasetMatrix.Content = null;
     113      try {
     114        TableFileParser csvParser = new TableFileParser();
     115        csvParser.Parse(ProblemTextBox.Text,
     116                        (NumberFormatInfo)DecimalSeparatorComboBox.SelectedValue,
     117                        (DateTimeFormatInfo)DateTimeFormatComboBox.SelectedValue,
     118                        (char)SeparatorComboBox.SelectedValue);
     119        IEnumerable<string> variableNamesWithType = GetVariableNamesWithType(csvParser);
     120        PreviewDatasetMatrix.Content = new Dataset(variableNamesWithType, csvParser.Values);
     121
     122        CheckAdditionalConstraints(csvParser);
     123
     124        ErrorTextBox.Text = String.Empty;
     125        ErrorTextBox.Visible = false;
    52126        OkButton.Enabled = true;
     127      }
     128      catch (Exception ex) {
     129        OkButton.Enabled = false;
     130        ErrorTextBox.Text = ex.Message;
     131        ErrorTextBox.Visible = true;
     132      }
     133    }
     134
     135    protected virtual void CheckAdditionalConstraints(TableFileParser csvParser) {
     136      if (!csvParser.Values.Any(x => x is List<double>)) {
     137        throw new ArgumentException("No double column could be found!");
     138      }
     139    }
     140
     141    private IEnumerable<string> GetVariableNamesWithType(TableFileParser csvParser) {
     142      IList<string> variableNamesWithType = csvParser.VariableNames.ToList();
     143      for (int i = 0; i < csvParser.Values.Count; i++) {
     144        if (csvParser.Values[i] is List<double>) {
     145          variableNamesWithType[i] += " (Double)";
     146        } else if (csvParser.Values[i] is List<string>) {
     147          variableNamesWithType[i] += " (String)";
     148        } else if (csvParser.Values[i] is List<DateTime>) {
     149          variableNamesWithType[i] += " (DateTime)";
     150        } else {
     151          throw new ArgumentException("The variable values must be of type List<double>, List<string> or List<DateTime>");
     152        }
     153      }
     154      return variableNamesWithType;
     155    }
     156
     157    protected void ControlToolTip_DoubleClick(object sender, EventArgs e) {
     158      Control control = sender as Control;
     159      if (control != null) {
     160        using (TextDialog dialog = new TextDialog(control.Name, (string)control.Tag, true)) {
     161          dialog.ShowDialog(this);
     162        }
    53163      }
    54164    }
Note: See TracChangeset for help on using the changeset viewer.