Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.DataImporter/HeuristicLab.DataImporter.Data/Command/PasteValuesCommand.cs @ 6133

Last change on this file since 6133 was 6133, checked in by gkronber, 14 years ago

#1471: imported generic parts of DataImporter from private code base

File size: 3.5 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using System.Globalization;
6using System.Xml;
7using System.Windows.Forms;
8using HeuristicLab.DataImporter.Data.CommandBase;
9using HeuristicLab.DataImporter.Data.Model;
10using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
11
12namespace HeuristicLab.DataImporter.Data.Command {
13  [StorableClass]
14  public class PasteValuesCommand : ColumnGroupCommandBase {
15    [Storable]
16    private IComparable[,] newValues;
17    [Storable]
18    private int columnIndex;
19    [Storable]
20    private int rowIndex;
21
22    private IComparable[,] oldValues;
23    private int insertedRows;
24    private ICollection<int> oldSortedColumnIndices;
25    private IEnumerable<SortOrder> oldSortOrder;
26
27    private PasteValuesCommand()
28      : base(null, string.Empty) {
29    }
30
31    public PasteValuesCommand(DataSet dataSet, string columnGroupName, int columnIndex, int rowIndex, string values)
32      : base(dataSet, columnGroupName) {
33      this.columnIndex = columnIndex;
34      this.rowIndex = rowIndex;
35
36      string[] lines = values.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);
37      string[] cells;
38      for (int i = 0; i < lines.Length; i++) {
39        cells = lines[i].Split('\t');
40        if (this.newValues == null) {
41          newValues = new IComparable[cells.Length, lines.Length];
42        }
43        for (int j = 0; j < cells.Length; j++)
44          newValues[j, i] = string.IsNullOrEmpty(cells[j]) ? null : cells[j];
45      }
46    }
47
48    public override void Execute() {
49      base.Execute();
50      oldValues = new IComparable[
51        Math.Min(ColumnGroup.Columns.Count() - columnIndex, newValues.GetLength(0)),
52        Math.Min(ColumnGroup.RowCount - rowIndex, newValues.GetLength(1))];
53      bool rowAdded = false;
54      oldSortedColumnIndices = new List<int>(ColumnGroup.SortedColumnIndexes);
55      oldSortOrder = ColumnGroup.SortOrdersForColumns.ToList();
56      for (int row = 0; row < newValues.GetLength(1); row++) {
57        if (row + rowIndex >= ColumnGroup.RowCount) {
58          ColumnGroup.AddRow(ColumnGroup.GetEmptyRow());
59          this.insertedRows++;
60          rowAdded = true;
61        }
62        for (int col = 0; col < newValues.GetLength(0); col++) {
63          if (col + columnIndex < ColumnGroup.Columns.Count()) {
64            if (!rowAdded)
65              oldValues[col, row] = ColumnGroup.Columns.ElementAt(col + columnIndex).GetValue(row + rowIndex);
66            ColumnGroup.Columns.ElementAt(col + columnIndex).ChangeValueOrNull(row + rowIndex, newValues[col, row]);
67          }
68        }
69      }
70      ColumnGroup.FireChanged();
71      this.ColumnGroup = null;
72    }
73
74    public override void UndoExecute() {
75      base.UndoExecute();
76      for (int row = 0; row < oldValues.GetLength(1); row++) {
77        for (int col = 0; col < oldValues.GetLength(0); col++) {
78          ColumnGroup.Columns.ElementAt(col + columnIndex).ChangeValue(row + rowIndex, oldValues[col, row]);
79        }
80      }
81      for (int i = 0; i < insertedRows; i++)
82        ColumnGroup.DeleteRow(ColumnGroup.RowCount - 1);
83      oldValues = null;
84      insertedRows = 0;
85      ColumnGroup.SortOrdersForColumns = oldSortOrder;
86      ColumnGroup.SortedColumnIndexes = oldSortedColumnIndices;
87      oldSortedColumnIndices = null;
88      oldSortOrder = null;
89      ColumnGroup.FireChanged();
90      this.ColumnGroup = null;
91    }
92
93    public override string Description {
94      get { return "Copied values pasted"; }
95    }
96  }
97}
Note: See TracBrowser for help on using the repository browser.