#region License Information
/* HeuristicLab
* Copyright (C) 2002-2012 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
*
* This file is part of HeuristicLab.
*
* HeuristicLab is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* HeuristicLab is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with HeuristicLab. If not, see .
*/
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
namespace HeuristicLab.DataImporter.Data.Model {
[StorableClass]
public class ColumnGroup {
public ColumnGroup()
: base() {
this.columns = new List();
this.sortedColumnIndexes = new List();
}
public ColumnGroup(string name)
: this() {
this.name = name;
}
private bool active;
public bool Active {
get { return active; }
set { this.active = value; }
}
public IEnumerable SortOrdersForColumns {
get { return columns.Select(col => col.SortOrder); }
set {
int i = 0;
foreach (SortOrder order in value) {
columns[i].SortOrder = order;
i++;
}
}
}
[Storable]
private ICollection sortedColumnIndexes;
public ICollection SortedColumnIndexes {
get { return this.sortedColumnIndexes; }
set { this.sortedColumnIndexes = value; }
}
public bool Sorted {
get { return SortOrdersForColumns.Any(x => x != SortOrder.None); }
}
public int SortedColumnsCount {
get { return SortOrdersForColumns.Count(x => x != SortOrder.None); }
}
public void ResetSorting() {
foreach (ColumnBase column in this.columns)
column.SortOrder = SortOrder.None;
sortedColumnIndexes.Clear();
}
public string KeyString(int row) {
string x = "";
IComparable val;
foreach (int colIndex in sortedColumnIndexes) {
val = columns[colIndex].GetValue(row);
if (val != null)
x += val.ToString();
}
return x;
}
[Storable]
private string name;
public string Name {
get { return this.name; }
set { this.name = value; }
}
[Storable]
private List columns;
public IEnumerable Columns {
get { return this.columns; }
}
public int RowCount {
get {
return this.columns.Count == 0 ? 0 :
this.columns.Max(x => x.TotalValuesCount);
}
}
public IEnumerable SelectedColumns {
get { return this.columns.Where(col => col.Selected); }
}
public int[] SelectedColumnIndexes {
get {
List selectedIndexes = new List();
for (int i = 0; i < this.columns.Count; i++)
if (columns[i].Selected)
selectedIndexes.Add(i);
return selectedIndexes.ToArray();
}
}
public void ClearSelectedColumns() {
foreach (ColumnBase col in this.columns)
col.Selected = false;
}
public void SelectAllColumns() {
foreach (ColumnBase col in this.columns)
col.Selected = true;
}
public void AddColumn(ColumnBase column) {
column.Changed += ColumnChanged;
this.columns.Add(column);
}
public void AddColumns(IEnumerable columns) {
foreach (ColumnBase col in columns)
this.AddColumn(col);
}
public void InsertColumn(int position, ColumnBase column) {
column.Changed += ColumnChanged;
this.columns.Insert(position, column);
}
public void RemoveColumn(ColumnBase column) {
if (sortedColumnIndexes.Contains(IndexOfColumn(column)))
ResetSorting();
column.Changed -= ColumnChanged;
this.columns.Remove(column);
}
public void RemoveColumn(int columnIndex) {
if (sortedColumnIndexes.Contains(columnIndex))
ResetSorting();
columns[columnIndex].Changed -= ColumnChanged;
this.columns.RemoveAt(columnIndex);
}
public int IndexOfColumn(ColumnBase column) {
return this.columns.IndexOf(column);
}
public void ClearColumns() {
foreach (ColumnBase col in this.columns)
col.Changed -= ColumnChanged;
ResetSorting();
this.columns.Clear();
}
public ColumnBase GetColumn(int position) {
return this.columns[position];
}
public void ReplaceColumn(int position, ColumnBase column) {
if (sortedColumnIndexes.Contains(position))
ResetSorting();
this.columns[position].Changed -= ColumnChanged;
column.Changed += ColumnChanged;
this.columns[position] = column;
}
#region methods for row manipulation
public IComparable[] GetRow(int index) {
IComparable[] ret = new IComparable[this.columns.Count];
for (int i = 0; i < this.columns.Count; i++)
ret[i] = this.columns[i].GetValue(index);
return ret;
}
public IComparable[] GetEmptyRow() {
IComparable[] ret = new IComparable[this.columns.Count];
for (int i = 0; i < this.columns.Count; i++)
ret[i] = null;
return ret;
}
public void AddRow(IComparable[] row) {
for (int i = 0; i < this.columns.Count; i++)
this.columns[i].AddValueOrNull(row[i]);
}
public void DeleteRow(int index) {
for (int i = 0; i < this.columns.Count; i++)
this.columns[i].RemoveValue(index);
}
public void InsertRow(int index, IComparable[] row) {
for (int i = 0; i < this.columns.Count; i++)
this.columns[i].InsertValue(index, row[i]);
}
#endregion
public void ColumnChanged(object sender, EventArgs e) {
this.OnChanged();
}
public override string ToString() {
return this.name;
}
public event EventHandler Changed;
protected virtual void OnChanged() {
if (this.Changed != null)
this.Changed(this, EventArgs.Empty);
}
public void FireChanged() {
this.OnChanged();
}
}
}