Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
01/20/17 09:55:38 (7 years ago)
Author:
gkronber
Message:

#2650 merged r14548:14582 from trunk to to branch

Location:
branches/symbreg-factors-2650
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • branches/symbreg-factors-2650

  • branches/symbreg-factors-2650/HeuristicLab.Data.Views

  • branches/symbreg-factors-2650/HeuristicLab.Data.Views/3.3

  • branches/symbreg-factors-2650/HeuristicLab.Data.Views/3.3/StringConvertibleArrayView.cs

    r14330 r14589  
    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.