#region License Information
/* HeuristicLab
* Copyright (C) 2002-2013 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.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace HeuristicLab.DataImporter.Data.View {
public partial class DataSetView : UserControl {
private DataSetView() {
InitializeComponent();
columnGroupViews = new List();
this.AutoScroll = true;
this.Scroll += new ScrollEventHandler(DataSetView_Scroll);
this.state = new DataSetState(0, ColumnGroupState.None);
}
public DataSetView(Model.DataSet dataSet)
: this() {
this.DataSet = dataSet;
this.DataSet.Changed += this.DataSetChanged;
this.Refresh();
}
public DataSetView(Model.DataSet dataSet, CommandChain commandChain)
: this(dataSet) {
this.CommandChain = commandChain;
}
private List columnGroupViews;
private CommandChain commandChain;
public CommandChain CommandChain {
get { return this.commandChain; }
set {
this.commandChain = value;
foreach (ColumnGroupView view in this.columnGroupViews)
view.CommandChain = value;
}
}
private int columnGroupMaxWidth;
public int ColumnGroupMaxWidth {
get { return columnGroupMaxWidth; }
set {
columnGroupMaxWidth = value;
foreach (ColumnGroupView view in columnGroupViews)
view.MaxWidth = value;
}
}
private Model.DataSet dataSet;
public Model.DataSet DataSet {
get { return (Model.DataSet)this.dataSet; }
private set { this.dataSet = value; }
}
private DataSetState state;
public DataSetState State {
get { return this.state; }
protected set {
this.state = value;
FireStateChanged();
}
}
private bool allowReorderColumns;
public bool AllowReorderColumns {
set {
allowReorderColumns = value;
foreach (ColumnGroupView view in this.columnGroupViews) {
view.AllowReorderColumns = value;
}
}
get { return allowReorderColumns; }
}
public override void Refresh() {
base.Refresh();
//clean up - caution: view.dispose removes disposed view from controls collection
this.SuspendLayout();
this.Controls.Clear();
foreach (ColumnGroupView view in this.columnGroupViews.Reverse())
view.Dispose();
this.columnGroupViews.Clear();
ColumnGroupView ctrl;
//necessary because otherwise columnGroupsViews are in reverse order
for (int i = 0; i < this.DataSet.ColumnGroups.Count(); i++) {
ctrl = new ColumnGroupView(this.DataSet.ColumnGroups.ElementAt(i), this.commandChain);
ctrl.Activated += new ColumnGroupActivatedEventHandler(ColumnGroupActivated);
ctrl.StateChanged += new EventHandler(ColumnGroupStateChanged);
ctrl.MaxWidth = this.columnGroupMaxWidth;
if (this.DataSet.ColumnGroups.ElementAt(i).Active) {
ctrl.ColumnGroupActive = true;
ctrl.FireActivated(false);
}
ctrl.Dock = DockStyle.Top | DockStyle.Bottom;
this.columnGroupViews.Add(ctrl);
}
this.Controls.AddRange(this.columnGroupViews.Reverse().ToArray());
this.Invalidate();
this.ResumeLayout();
}
public void DataSetChanged(object sender, EventArgs e) {
this.Refresh();
this.UpdateStateInformation();
}
public void ColumnGroupActivated(object sender, bool addToActiveColumnGroups) {
if (!addToActiveColumnGroups) {
foreach (ColumnGroupView view in this.columnGroupViews)
view.ColumnGroupActive = view == sender;
}
UpdateStateInformation();
}
public void ColumnGroupStateChanged(object sender, EventArgs e) {
UpdateStateInformation();
}
private void UpdateStateInformation() {
DataSetState state = new DataSetState(0, ColumnGroupState.None);
foreach (ColumnGroupView view in this.columnGroupViews.Where(cg => cg.ColumnGroupActive))
state.AddColumnGroupState(view.State);
if (state.ActiveColumnGroups == 1)
state.SelectedColumns = DataSet.ActiveColumnGroups.ElementAt(0).SelectedColumns.Count();
if (!this.state.Equals(state))
this.State = state;
}
public event EventHandler StateChanged;
protected void FireStateChanged() {
OnStateChanged();
}
protected virtual void OnStateChanged() {
if (StateChanged != null) {
StateChanged(this, new EventArgs());
}
}
#region GUI interaction with scroll bars
private Point scrollposition;
private void DataSetView_Scroll(object sender, ScrollEventArgs e) {
scrollposition.X = this.DisplayRectangle.X;
scrollposition.Y = this.DisplayRectangle.Y;
}
protected override Point ScrollToControl(Control activeControl) {
return scrollposition;
}
#endregion
}
}