Free cookie consent management tool by TermsFeed Policy Generator

source: addons/HeuristicLab.DataImporter/HeuristicLab.DataImporter.Data/View/DataSetView.cs @ 17724

Last change on this file since 17724 was 16994, checked in by gkronber, 5 years ago

#2520 Update plugin dependencies and references for HL.DataImporter for new persistence

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