Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1471: added plugin for DbExplorer interfaces, deleted .resx files, set svn:ignore properties, and added license header

File size: 4.4 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2011 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
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;
23using System.Collections.Generic;
24using System.Linq;
25using System.Text;
26using System.Globalization;
27using System.Xml;
28using System.Windows.Forms;
29using HeuristicLab.DataImporter.Data.CommandBase;
30using HeuristicLab.DataImporter.Data.Model;
31using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
32
33namespace HeuristicLab.DataImporter.Data.Command {
34  [StorableClass]
35  public class PasteValuesCommand : ColumnGroupCommandBase {
36    [Storable]
37    private IComparable[,] newValues;
38    [Storable]
39    private int columnIndex;
40    [Storable]
41    private int rowIndex;
42
43    private IComparable[,] oldValues;
44    private int insertedRows;
45    private ICollection<int> oldSortedColumnIndices;
46    private IEnumerable<SortOrder> oldSortOrder;
47
48    private PasteValuesCommand()
49      : base(null, string.Empty) {
50    }
51
52    public PasteValuesCommand(DataSet dataSet, string columnGroupName, int columnIndex, int rowIndex, string values)
53      : base(dataSet, columnGroupName) {
54      this.columnIndex = columnIndex;
55      this.rowIndex = rowIndex;
56
57      string[] lines = values.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);
58      string[] cells;
59      for (int i = 0; i < lines.Length; i++) {
60        cells = lines[i].Split('\t');
61        if (this.newValues == null) {
62          newValues = new IComparable[cells.Length, lines.Length];
63        }
64        for (int j = 0; j < cells.Length; j++)
65          newValues[j, i] = string.IsNullOrEmpty(cells[j]) ? null : cells[j];
66      }
67    }
68
69    public override void Execute() {
70      base.Execute();
71      oldValues = new IComparable[
72        Math.Min(ColumnGroup.Columns.Count() - columnIndex, newValues.GetLength(0)),
73        Math.Min(ColumnGroup.RowCount - rowIndex, newValues.GetLength(1))];
74      bool rowAdded = false;
75      oldSortedColumnIndices = new List<int>(ColumnGroup.SortedColumnIndexes);
76      oldSortOrder = ColumnGroup.SortOrdersForColumns.ToList();
77      for (int row = 0; row < newValues.GetLength(1); row++) {
78        if (row + rowIndex >= ColumnGroup.RowCount) {
79          ColumnGroup.AddRow(ColumnGroup.GetEmptyRow());
80          this.insertedRows++;
81          rowAdded = true;
82        }
83        for (int col = 0; col < newValues.GetLength(0); col++) {
84          if (col + columnIndex < ColumnGroup.Columns.Count()) {
85            if (!rowAdded)
86              oldValues[col, row] = ColumnGroup.Columns.ElementAt(col + columnIndex).GetValue(row + rowIndex);
87            ColumnGroup.Columns.ElementAt(col + columnIndex).ChangeValueOrNull(row + rowIndex, newValues[col, row]);
88          }
89        }
90      }
91      ColumnGroup.FireChanged();
92      this.ColumnGroup = null;
93    }
94
95    public override void UndoExecute() {
96      base.UndoExecute();
97      for (int row = 0; row < oldValues.GetLength(1); row++) {
98        for (int col = 0; col < oldValues.GetLength(0); col++) {
99          ColumnGroup.Columns.ElementAt(col + columnIndex).ChangeValue(row + rowIndex, oldValues[col, row]);
100        }
101      }
102      for (int i = 0; i < insertedRows; i++)
103        ColumnGroup.DeleteRow(ColumnGroup.RowCount - 1);
104      oldValues = null;
105      insertedRows = 0;
106      ColumnGroup.SortOrdersForColumns = oldSortOrder;
107      ColumnGroup.SortedColumnIndexes = oldSortedColumnIndices;
108      oldSortedColumnIndices = null;
109      oldSortOrder = null;
110      ColumnGroup.FireChanged();
111      this.ColumnGroup = null;
112    }
113
114    public override string Description {
115      get { return "Copied values pasted"; }
116    }
117  }
118}
Note: See TracBrowser for help on using the repository browser.