Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.DataImporter/HeuristicLab.DataImporter.Data/View/DataSetView.cs @ 6133

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

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

File size: 5.0 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.ComponentModel;
4using System.Drawing;
5using System.Data;
6using System.Linq;
7using System.Text;
8using System.Windows.Forms;
9
10namespace HeuristicLab.DataImporter.Data.View {
11  public partial class DataSetView : UserControl {
12    private DataSetView() {
13      InitializeComponent();
14      columnGroupViews = new List<ColumnGroupView>();
15      this.AutoScroll = true;
16      this.Scroll += new ScrollEventHandler(DataSetView_Scroll);
17      this.state = new DataSetState(0, ColumnGroupState.None);
18    }
19
20    public DataSetView(Model.DataSet dataSet)
21      : this() {
22      this.DataSet = dataSet;
23      this.DataSet.Changed += this.DataSetChanged;
24      this.Refresh();
25    }
26
27    public DataSetView(Model.DataSet dataSet, CommandChain commandChain)
28      : this(dataSet) {
29      this.CommandChain = commandChain;
30    }
31
32    private List<ColumnGroupView> columnGroupViews;
33
34    private CommandChain commandChain;
35    public CommandChain CommandChain {
36      get { return this.commandChain; }
37      set {
38        this.commandChain = value;
39        foreach (ColumnGroupView view in this.columnGroupViews)
40          view.CommandChain = value;
41      }
42    }
43
44    private int columnGroupMaxWidth;
45    public int ColumnGroupMaxWidth {
46      get { return columnGroupMaxWidth; }
47      set {
48        columnGroupMaxWidth = value;
49        foreach (ColumnGroupView view in columnGroupViews)
50          view.MaxWidth = value;
51      }
52    }
53
54    private Model.DataSet dataSet;
55    public Model.DataSet DataSet {
56      get { return (Model.DataSet)this.dataSet; }
57      private set { this.dataSet = value; }
58    }
59
60    private DataSetState state;
61    public DataSetState State {
62      get { return this.state; }
63      protected set {
64        this.state = value;
65        FireStateChanged();
66      }
67    }
68
69    private bool allowReorderColumns;
70    public bool AllowReorderColumns {
71      set {
72        allowReorderColumns = value;
73        foreach (ColumnGroupView view in this.columnGroupViews) {
74          view.AllowReorderColumns = value;
75        }
76      }
77      get { return allowReorderColumns; }
78    }
79
80    public override void Refresh() {
81      base.Refresh();
82
83      //clean up - caution: view.dispose removes disposed view from controls collection
84      this.SuspendLayout();
85      this.Controls.Clear();
86      foreach (ColumnGroupView view in this.columnGroupViews.Reverse<ColumnGroupView>())
87        view.Dispose();
88      this.columnGroupViews.Clear();
89
90      ColumnGroupView ctrl;
91      //necessary because otherwise columnGroupsViews are in reverse order
92      for (int i = 0; i < this.DataSet.ColumnGroups.Count(); i++) {
93        ctrl = new ColumnGroupView(this.DataSet.ColumnGroups.ElementAt(i), this.commandChain);
94        ctrl.Activated += new ColumnGroupActivatedEventHandler(ColumnGroupActivated);
95        ctrl.StateChanged += new EventHandler(ColumnGroupStateChanged);
96        ctrl.MaxWidth = this.columnGroupMaxWidth;
97        if (this.DataSet.ColumnGroups.ElementAt(i).Active) {
98          ctrl.ColumnGroupActive = true;
99          ctrl.FireActivated(false);
100        }
101        ctrl.Dock = DockStyle.Top | DockStyle.Bottom;
102        this.columnGroupViews.Add(ctrl);
103      }
104      this.Controls.AddRange(this.columnGroupViews.Reverse<ColumnGroupView>().ToArray());
105      this.Invalidate();
106      this.ResumeLayout();
107    }
108
109    public void DataSetChanged(object sender, EventArgs e) {
110      this.Refresh();
111      this.UpdateStateInformation();
112    }
113
114    public void ColumnGroupActivated(object sender, bool addToActiveColumnGroups) {
115      if (!addToActiveColumnGroups) {
116        foreach (ColumnGroupView view in this.columnGroupViews)
117          view.ColumnGroupActive = view == sender;
118      }
119      UpdateStateInformation();
120    }
121
122    public void ColumnGroupStateChanged(object sender, EventArgs e) {
123      UpdateStateInformation();
124    }
125
126    private void UpdateStateInformation() {
127      DataSetState state = new DataSetState(0, ColumnGroupState.None);
128      foreach (ColumnGroupView view in this.columnGroupViews.Where(cg => cg.ColumnGroupActive))
129        state.AddColumnGroupState(view.State);
130      if (state.ActiveColumnGroups == 1)
131        state.SelectedColumns = DataSet.ActiveColumnGroups.ElementAt(0).SelectedColumns.Count();
132      if (!this.state.Equals(state))
133        this.State = state;
134    }
135
136    public event EventHandler StateChanged;
137    protected void FireStateChanged() {
138      OnStateChanged();
139    }
140
141    protected virtual void OnStateChanged() {
142      if (StateChanged != null) {
143        StateChanged(this, new EventArgs());
144      }
145    }
146
147    #region GUI interaction with scroll bars
148    private Point scrollposition;
149    private void DataSetView_Scroll(object sender, ScrollEventArgs e) {
150      scrollposition.X = this.DisplayRectangle.X;
151      scrollposition.Y = this.DisplayRectangle.Y;
152    }
153
154    protected override Point ScrollToControl(Control activeControl) {
155      return scrollposition;
156    }
157    #endregion
158  }
159}
Note: See TracBrowser for help on using the repository browser.