Free cookie consent management tool by TermsFeed Policy Generator

Changeset 14561 for trunk


Ignore:
Timestamp:
01/13/17 11:24:30 (8 years ago)
Author:
abeham
Message:

#2674:

  • Corrected copy & paste from StringConvertibleArrays
    • There are four possible cases that are handled by this change (rows are newline separated, columns tab separated)
      1. There is just one column of values
      2. There is one column of element names and one column of values
      3. There is one row of values
      4. There is one row of element names and one row of values
    • A message box is shown if the user selects a 2 by n or n by 2 range with n > 2
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Data.Views/3.3/StringConvertibleArrayView.cs

    r14299 r14561  
    2121
    2222using System;
     23using System.Collections.Generic;
    2324using System.ComponentModel;
    2425using System.Drawing;
     
    200201      }
    201202
     203      var elementNames = Content.ElementNames.ToList();
    202204      for (int i = minRowIndex; i <= maxRowIndex; i++) {
    203205        DataGridViewColumn column = dataGridView.Columns.GetFirstColumn(DataGridViewElementStates.Visible);
    204206        DataGridViewCell cell = dataGridView[column.Index, i];
    205207        if (cell.Selected) {
    206           s.Append(Content.ElementNames.ElementAt(i));
    207           s.Append("\t");
     208          if (i < elementNames.Count) {
     209            s.Append(elementNames[i]);
     210            s.Append("\t");
     211          } else if (elementNames.Count > 0) {
     212            s.Append("Index " + i);
     213            s.Append("\t");
     214          }
    208215          s.Append(Content.GetValue(i));
    209216          s.Append(Environment.NewLine);
     
    213220    }
    214221    private void PasteValuesToDataGridView() {
    215       string[] values = SplitClipboardString(Clipboard.GetText());
     222      Tuple<string, string>[] values = null;
     223      try {
     224        values = SplitClipboardString(Clipboard.GetText()).ToArray();
     225      } catch (ArgumentException ex) {
     226        MessageBox.Show(this, ex.Message, "Error while parsing clipboard text.", MessageBoxButtons.OK, MessageBoxIcon.Error);
     227        return;
     228      }
    216229      int rowIndex = 0;
    217230      if (dataGridView.CurrentCell != null)
     
    219232
    220233      if (Content.Length < rowIndex + values.Length) Content.Length = rowIndex + values.Length;
    221       for (int row = 0; row < values.Length; row++)
    222         Content.SetValue(values[row], row + rowIndex);
    223     }
    224     private string[] SplitClipboardString(string clipboardText) {
     234      for (int row = 0; row < values.Length; row++) {
     235        Content.SetValue(values[row].Item2, row + rowIndex);
     236      }
     237      if (values.Any(x => !string.IsNullOrEmpty(x.Item1))) {
     238        var elementNames = Content.ElementNames.ToList();
     239        for (int row = 0; row < values.Length; row++) {
     240          if (row + rowIndex < elementNames.Count)
     241            elementNames[row + rowIndex] = values[row].Item1;
     242          else elementNames.Add(values[row].Item1);
     243        }
     244        Content.ElementNames = elementNames;
     245      }
     246    }
     247    private IEnumerable<Tuple<string, string>> SplitClipboardString(string clipboardText) {
    225248      if (clipboardText.EndsWith(Environment.NewLine))
    226249        clipboardText = clipboardText.Remove(clipboardText.Length - Environment.NewLine.Length);  //remove last newline constant
    227       return clipboardText.Split(new string[] { Environment.NewLine, "\t" }, StringSplitOptions.None);
     250
     251      var lines = clipboardText.Split(new [] { Environment.NewLine }, StringSplitOptions.None);
     252      var tabSep = new[] { '\t' };
     253      if (lines.Length > 2) {
     254        // Case 1: Each line contains either "elementName \t value" or just "value" (one or two vertical vectors)
     255        foreach (var l in lines) {
     256          var row = l.Split(tabSep, StringSplitOptions.RemoveEmptyEntries);
     257          if (row.Length > 2) throw new ArgumentException("Clipboard may have either at most two rows or at most two columns.");
     258          if (row.Length == 2) yield return Tuple.Create(row[0], row[1]);
     259          else if (row.Length == 1) yield return Tuple.Create(string.Empty, row[0]);
     260          else yield return Tuple.Create(string.Empty, string.Empty);
     261        }
     262      } else if (lines.Length == 2) {
     263        var firstLine = lines[0].Split(tabSep, StringSplitOptions.None);
     264        var secondLine = lines[1].Split(tabSep, StringSplitOptions.None);
     265        if (firstLine.Length <= 2 && secondLine.Length <= 2) {
     266          // Case 2a: The two lines contain either "elementName \t value" or just "value" (one or two vertical vectors)
     267          yield return firstLine.Length >= 2 ? Tuple.Create(firstLine[0], firstLine[1]) : Tuple.Create(string.Empty, firstLine[0]);
     268          yield return secondLine.Length >= 2 ? Tuple.Create(secondLine[0], secondLine[1]) : Tuple.Create(string.Empty, secondLine[0]);
     269        } else {
     270          // Case 2b: The first line contains the elementNames, the second line contains the values (two horizontal vectors)
     271          var max = Math.Max(firstLine.Length, secondLine.Length);
     272          for (var i = 0; i < max; i++) {
     273            var elemName = i < firstLine.Length ? firstLine[i] : string.Empty;
     274            var value = i < secondLine.Length ? secondLine[i] : string.Empty;
     275            yield return Tuple.Create(elemName, value);
     276          }
     277        }
     278      } else if (lines.Length == 1) {
     279        // Case 3: The line contains the values (one horizontal vector)
     280        var entries = lines[0].Split(tabSep, StringSplitOptions.None);
     281        foreach (var e in entries) {
     282          yield return Tuple.Create(string.Empty, e);
     283        }
     284      }
    228285    }
    229286    #endregion
Note: See TracChangeset for help on using the changeset viewer.