Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 15016 was 9615, checked in by mkommend, 12 years ago

#1734: Updated copyright information in all DataImporter classes.

File size: 4.3 KB
RevLine 
[6134]1#region License Information
2/* HeuristicLab
[9615]3 * Copyright (C) 2002-2013 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[6134]4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
[6133]23using System.Collections.Generic;
24using System.Linq;
25using System.Windows.Forms;
26using HeuristicLab.DataImporter.Data.CommandBase;
27using HeuristicLab.DataImporter.Data.Model;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29
30namespace HeuristicLab.DataImporter.Data.Command {
31  [StorableClass]
32  public class PasteValuesCommand : ColumnGroupCommandBase {
33    [Storable]
34    private IComparable[,] newValues;
35    [Storable]
36    private int columnIndex;
37    [Storable]
38    private int rowIndex;
39
40    private IComparable[,] oldValues;
41    private int insertedRows;
42    private ICollection<int> oldSortedColumnIndices;
43    private IEnumerable<SortOrder> oldSortOrder;
44
[9614]45    [StorableConstructor]
46    protected PasteValuesCommand(bool deserializing) : base(deserializing) { }
[6133]47
48    public PasteValuesCommand(DataSet dataSet, string columnGroupName, int columnIndex, int rowIndex, string values)
49      : base(dataSet, columnGroupName) {
50      this.columnIndex = columnIndex;
51      this.rowIndex = rowIndex;
52
53      string[] lines = values.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);
54      string[] cells;
55      for (int i = 0; i < lines.Length; i++) {
56        cells = lines[i].Split('\t');
57        if (this.newValues == null) {
58          newValues = new IComparable[cells.Length, lines.Length];
59        }
60        for (int j = 0; j < cells.Length; j++)
61          newValues[j, i] = string.IsNullOrEmpty(cells[j]) ? null : cells[j];
62      }
63    }
64
65    public override void Execute() {
66      base.Execute();
67      oldValues = new IComparable[
68        Math.Min(ColumnGroup.Columns.Count() - columnIndex, newValues.GetLength(0)),
69        Math.Min(ColumnGroup.RowCount - rowIndex, newValues.GetLength(1))];
70      bool rowAdded = false;
71      oldSortedColumnIndices = new List<int>(ColumnGroup.SortedColumnIndexes);
72      oldSortOrder = ColumnGroup.SortOrdersForColumns.ToList();
73      for (int row = 0; row < newValues.GetLength(1); row++) {
74        if (row + rowIndex >= ColumnGroup.RowCount) {
75          ColumnGroup.AddRow(ColumnGroup.GetEmptyRow());
76          this.insertedRows++;
77          rowAdded = true;
78        }
79        for (int col = 0; col < newValues.GetLength(0); col++) {
80          if (col + columnIndex < ColumnGroup.Columns.Count()) {
81            if (!rowAdded)
82              oldValues[col, row] = ColumnGroup.Columns.ElementAt(col + columnIndex).GetValue(row + rowIndex);
83            ColumnGroup.Columns.ElementAt(col + columnIndex).ChangeValueOrNull(row + rowIndex, newValues[col, row]);
84          }
85        }
86      }
87      ColumnGroup.FireChanged();
88      this.ColumnGroup = null;
89    }
90
91    public override void UndoExecute() {
92      base.UndoExecute();
93      for (int row = 0; row < oldValues.GetLength(1); row++) {
94        for (int col = 0; col < oldValues.GetLength(0); col++) {
95          ColumnGroup.Columns.ElementAt(col + columnIndex).ChangeValue(row + rowIndex, oldValues[col, row]);
96        }
97      }
98      for (int i = 0; i < insertedRows; i++)
99        ColumnGroup.DeleteRow(ColumnGroup.RowCount - 1);
100      oldValues = null;
101      insertedRows = 0;
102      ColumnGroup.SortOrdersForColumns = oldSortOrder;
103      ColumnGroup.SortedColumnIndexes = oldSortedColumnIndices;
104      oldSortedColumnIndices = null;
105      oldSortOrder = null;
106      ColumnGroup.FireChanged();
107      this.ColumnGroup = null;
108    }
109
110    public override string Description {
111      get { return "Copied values pasted"; }
112    }
113  }
114}
Note: See TracBrowser for help on using the repository browser.