Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.DataImporter/HeuristicLab.DataImporter.Data/View/ColumnGroupView.cs @ 6312

Last change on this file since 6312 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: 24.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.ComponentModel;
25using System.Drawing;
26using System.Data;
27using System.Linq;
28using System.Text;
29using System.Windows.Forms;
30using HeuristicLab.DataImporter.Data.Model;
31using HeuristicLab.DataImporter.Data.Command;
32
33
34namespace HeuristicLab.DataImporter.Data.View {
35  public delegate void ColumnGroupActivatedEventHandler(object sender, bool addToActiveColumnGroups);
36  public partial class ColumnGroupView : UserControl {
37
38    private TextBox txtColumnName;
39    private ColumnGroupView()
40      : base() {
41      InitializeComponent();
42      txtColumnName = new TextBox();
43      this.splitContainer1.Panel2.Controls.Add(txtColumnName);
44      txtColumnName.Visible = false;
45      txtColumnName.Leave += new EventHandler(txtColumnName_Leave);
46      txtColumnName.KeyDown += new KeyEventHandler(txtColumnName_KeyDown);
47
48      this.dataGridView.EnableHeadersVisualStyles = false;
49      this.dataGridView.Dock = DockStyle.Top | DockStyle.Bottom;
50      this.dataGridView.VirtualMode = true;
51
52      this.dataGridView.ColumnHeaderMouseClick += new DataGridViewCellMouseEventHandler(dataGridView_ColumnHeaderMouseClick);
53      this.dataGridView.ColumnHeaderMouseDoubleClick += new DataGridViewCellMouseEventHandler(dataGridView_ColumnHeaderMouseDoubleClick);
54      this.dataGridView.ColumnWidthChanged += new DataGridViewColumnEventHandler(dataGridView_ColumnWidthChanged);
55      this.dataGridView.MouseClick += new MouseEventHandler(dataGridView_MouseClick);
56      this.dataGridView.SelectionChanged += new EventHandler(dataGridView_SelectionChanged);
57      this.dataGridView.AllowUserToOrderColumnsChanged += new EventHandler(dataGridView_AllowUserToOrderColumnsChanged);
58      this.dataGridView.KeyDown += new KeyEventHandler(dataGridView_KeyDown);
59      this.dataGridView.Scroll += new ScrollEventHandler(dataGridView_Scroll);
60      this.dataGridView.Resize += new EventHandler(dataGridView_Resize);
61      this.dataGridView.RowHeadersWidthChanged += new EventHandler(dataGridView_RowHeadersWidthChanged);
62
63      //delegates for virtual mode
64      this.dataGridView.CellValueNeeded += new DataGridViewCellValueEventHandler(dataGridView_CellValueNeeded);
65      this.dataGridView.CellValuePushed += new DataGridViewCellValueEventHandler(dataGridView_CellValuePushed);
66      this.dataGridView.UserDeletingRow += new DataGridViewRowCancelEventHandler(dataGridView_UserDeletingRow);
67    }
68
69    private ColumnGroupView(ColumnGroup columnGroup)
70      : this() {
71      this.ColumnGroup = columnGroup;
72      this.ColumnGroup.Changed += this.ColumnGroupChanged;
73      this.dataGridView.ClearSelection();
74      this.state = ColumnGroupState.None;
75      this.UpdateDataGridView();
76    }
77
78    public ColumnGroupView(ColumnGroup columnGroup, CommandChain commandChain)
79      : this(columnGroup) {
80      this.commandChain = commandChain;
81    }
82
83    public bool ColumnGroupActive {
84      get { return this.ColumnGroup.Active; }
85      set {
86        this.ColumnGroup.Active = value;
87        if (this.ColumnGroup.Active)
88          this.txtColumnGroupName.BackColor = Color.LightGreen;
89        else {
90          this.dataGridView.ClearSelection();
91          this.txtColumnGroupName.BackColor = Control.DefaultBackColor;
92        }
93        UpdateStateInformation();
94      }
95    }
96
97    private ColumnGroup columnGroup;
98    public ColumnGroup ColumnGroup {
99      get { return (ColumnGroup)this.columnGroup; }
100      private set { this.columnGroup = value; }
101    }
102
103    private Data.Model.DataSet DataSet {
104      get { return ((DataSetView)this.Parent).DataSet; }
105    }
106
107    private CommandChain commandChain;
108    public CommandChain CommandChain {
109      get { return this.commandChain; }
110      set { this.commandChain = value; }
111    }
112
113    public bool AllowReorderColumns {
114      get { return this.dataGridView.AllowUserToOrderColumns; }
115      set { this.dataGridView.AllowUserToOrderColumns = value; }
116    }
117
118    public int[] DisplayIndexes {
119      get {
120        int[] ret = new int[this.ColumnGroup.Columns.Count()];
121        for (int i = 0; i < this.dataGridView.Columns.Count; i++) {
122          ret[dataGridView.Columns[i].DisplayIndex] = i;
123        }
124        return ret;
125      }
126    }
127
128    public int MaxWidth {
129      get { return this.MaximumSize.Width; }
130      set {
131        this.MaximumSize = new Size(value, this.MaximumSize.Height);
132        this.dataGridView.MaximumSize = new Size(value, this.MaximumSize.Height);
133        RecalculateWidthOfControl();
134      }
135    }
136
137    //IMPORTANT: use the defined property to change the state, because the event StateChanged must be fired!
138    private ColumnGroupState state;
139    public ColumnGroupState State {
140      get { return this.state; }
141      protected set {
142        this.state = value;
143        FireStateChanged();
144      }
145    }
146
147    public event EventHandler StateChanged;
148    protected void FireStateChanged() {
149      OnStateChanged();
150    }
151
152    protected virtual void OnStateChanged() {
153      if (StateChanged != null) {
154        StateChanged(this, new EventArgs());
155      }
156    }
157
158    public event ColumnGroupActivatedEventHandler Activated;
159    public void FireActivated(bool addToActiveColumnGroups) {
160      OnActivated(addToActiveColumnGroups);
161    }
162
163    protected virtual void OnActivated(bool addToActiveColumnGroups) {
164      if (Activated != null) {
165        Activated(this, addToActiveColumnGroups);
166      }
167    }
168
169    private void UpdateStateInformation() {
170      this.UpdateStateInformation(false);
171    }
172
173    private void UpdateStateInformation(bool selectionChanged) {
174      ColumnGroupState newState = this.ColumnGroupActive ? ColumnGroupState.Active : ColumnGroupState.None;
175      foreach (ColumnBase col in ColumnGroup.SelectedColumns) {
176        if (col is DoubleColumn)
177          newState |= ColumnGroupState.DoubleColumnSelected;
178        else if (col is StringColumn)
179          newState |= ColumnGroupState.StringColumnSelected;
180        else if (col is DateTimeColumn)
181          newState |= ColumnGroupState.DateTimeColumnSelected;
182        else if (col is ProgrammableColumn)
183          newState |= ColumnGroupState.ProgrammableColumnSelected;
184        if (col.ContainsNullValues)
185          newState |= ColumnGroupState.AnySelectedColumnContainsNull;
186      }
187      if (ColumnGroup.Sorted) {
188        if (ColumnGroup.SelectedColumns.Any(col => col.SortOrder == SortOrder.None))
189          newState |= ColumnGroupState.AnySelectedColumnNotSorted;
190        else
191          newState |= ColumnGroupState.Sorted;
192      }
193      if (newState != this.State || selectionChanged)
194        this.State = newState;
195    }
196
197    private void PasteClipboardContent() {
198      if (dataGridView.CurrentCell != null) {
199        string values = Clipboard.GetText();
200        values = values.Remove(values.Length - Environment.NewLine.Length);
201        this.commandChain.Add(new PasteValuesCommand(DataSet, this.ColumnGroup.Name, dataGridView.CurrentCell.ColumnIndex,
202          dataGridView.CurrentCell.RowIndex, values));
203      }
204    }
205
206    private void CopyClipboardContent() {
207      if (dataGridView.SelectedCells.Count != 0) {
208        StringBuilder s = new StringBuilder();
209        DataGridViewCell cell;
210        int minRowIndex = dataGridView.SelectedCells[0].RowIndex;
211        int maxRowIndex = dataGridView.SelectedCells[dataGridView.SelectedCells.Count - 1].RowIndex;
212        int minColIndex = dataGridView.SelectedCells[0].ColumnIndex;
213        int maxColIndex = dataGridView.SelectedCells[dataGridView.SelectedCells.Count - 1].ColumnIndex;
214
215        if (minRowIndex > maxRowIndex) {
216          int temp = minRowIndex;
217          minRowIndex = maxRowIndex;
218          maxRowIndex = temp;
219        }
220
221        if (minColIndex > maxColIndex) {
222          int temp = minColIndex;
223          minColIndex = maxColIndex;
224          maxColIndex = temp;
225        }
226        if (maxRowIndex == dataGridView.RowCount - 1)
227          maxRowIndex--;
228
229        for (int i = minRowIndex; i < maxRowIndex + 1; i++) {
230          for (int j = minColIndex; j < maxColIndex + 1; j++) {
231            cell = dataGridView[j, i];
232            if (cell.Selected) {
233              if (cell.Value != null)
234                s.Append(cell.Value.ToString());
235            }
236            if (j != maxColIndex)
237              s.Append("\t");
238          }
239          s.Append(Environment.NewLine);
240        }
241        Clipboard.SetText(s.ToString());
242      }
243    }
244
245    public override void Refresh() {
246      base.Refresh();
247      if (ColumnGroup != null) {
248        this.UpdateDataGridView();
249      } else {
250        dataGridView.ColumnCount = 0;
251        dataGridView.RowCount = 0;
252      }
253    }
254
255    public void ColumnGroupChanged(object sender, EventArgs e) {
256      this.UpdateDataGridView();
257    }
258
259    private void UpdateDataGridView() {
260      int firstVisibleRowIndex = 0;
261      int firstVisibleColIndex = 0;
262      if (dataGridView.FirstDisplayedCell != null) {
263        if (firstVisibleColIndex < ColumnGroup.Columns.Count())
264          firstVisibleColIndex = dataGridView.FirstDisplayedCell.ColumnIndex;
265        if (firstVisibleRowIndex < ColumnGroup.RowCount)
266          firstVisibleRowIndex = dataGridView.FirstDisplayedCell.RowIndex;
267      }
268
269      //needed because otherwise columns could not be added
270      this.dataGridView.SelectionMode = DataGridViewSelectionMode.RowHeaderSelect;
271      if (dataGridView.ColumnCount != ColumnGroup.Columns.Count()) {
272        this.dataGridView.ColumnCount = ColumnGroup.Columns.Count();
273        for (int i = 0; i < ColumnGroup.Columns.Count(); i++) {
274          dataGridView.Columns[i].SortMode = DataGridViewColumnSortMode.Programmatic;
275        }
276      }
277      for (int i = 0; i < ColumnGroup.Columns.Count(); i++)
278        dataGridView.Columns[i].HeaderText = ColumnGroup.Columns.ElementAt(i).ToString().Insert(ColumnGroup.Columns.ElementAt(i).ToString().IndexOf('<'), "\n");
279
280      // needed for performance reasons; further information at http://whiletrue.nl/blog/?p=38
281      if (dataGridView.RowCount != ColumnGroup.RowCount + (ColumnGroup.Columns.Count() == 0 ? 0 : 1)) {
282        //event handler must be deregistered cause otherwise cellvaluepushed is fired again
283        this.dataGridView.CellValuePushed -= new DataGridViewCellValueEventHandler(dataGridView_CellValuePushed);
284        if (Math.Abs(dataGridView.RowCount - ColumnGroup.RowCount) > 10)
285          dataGridView.Rows.Clear();
286        bool rowAdded = false;
287        //new row added - dataGridView has always one row more because of the last empty row for inserting new rows
288        if (dataGridView.RowCount == ColumnGroup.RowCount && dataGridView.RowCount != 0)
289          rowAdded = true;
290
291        dataGridView.RowCount = ColumnGroup.RowCount + (ColumnGroup.Columns.Count() == 0 ? 0 : 1);
292
293        if (rowAdded) {
294          Point p = this.dataGridView.CurrentCellAddress;
295          p.Y += 1;
296          this.dataGridView.CurrentCell = this.dataGridView.Rows[p.Y].Cells[p.X];
297          this.dataGridView.ClearSelection();
298          this.dataGridView.CurrentCell.Selected = true;
299        }
300        UpdateRowHeaders();
301        this.dataGridView.CellValuePushed += new DataGridViewCellValueEventHandler(dataGridView_CellValuePushed);
302      }
303
304      this.dataGridView.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.DisplayedCells);
305      this.dataGridView.AutoResizeRowHeadersWidth(DataGridViewRowHeadersWidthSizeMode.AutoSizeToDisplayedHeaders);
306      if (dataGridView.RowCount != 0 && dataGridView.ColumnCount != 0) {
307        if (firstVisibleColIndex >= dataGridView.ColumnCount)
308          firstVisibleColIndex = 0;
309        if (firstVisibleRowIndex >= dataGridView.RowCount)
310          firstVisibleRowIndex = 0;
311        dataGridView.FirstDisplayedCell = dataGridView[firstVisibleColIndex, firstVisibleRowIndex];
312      }
313      UpdateSortGlyph();
314      this.txtColumnGroupName.Text = this.ColumnGroup.Name + "  " + ColumnGroup.RowCount + " rows";
315      RecalculateWidthOfControl();
316      this.dataGridView.Invalidate();
317      UpdateStateInformation();
318    }
319
320    private void dataGridView_ColumnHeaderMouseClick(object sender, System.Windows.Forms.DataGridViewCellMouseEventArgs e) {
321      if (e.Button == MouseButtons.Right) {
322        this.commandChain.Add(new SortCommand(DataSet, this.ColumnGroup.Name,
323          e.ColumnIndex, (Control.ModifierKeys & Keys.Control) == Keys.Control));
324        //this.UpdateSortGlyph();
325        //this.dataGridView.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.DisplayedCells);
326        //UpdateStateInformation();
327      }
328    }
329
330    private void dataGridView_ColumnHeaderMouseDoubleClick(object sender, System.Windows.Forms.DataGridViewCellMouseEventArgs e) {
331      if (e.Button == MouseButtons.Left) {
332        dataGridView.ClearSelection();
333        Rectangle rect = dataGridView.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, true);
334        ColumnBase col = this.ColumnGroup.Columns.ElementAt(e.ColumnIndex);
335        txtColumnName.Text = col.Name;
336        txtColumnName.Size = rect.Size;
337        txtColumnName.Location = rect.Location;
338        txtColumnName.Visible = true;
339        txtColumnName.Focus();
340        txtColumnName.BringToFront();
341      }
342    }
343
344    private void UpdateSortGlyph() {
345      System.Collections.IEnumerator e = this.dataGridView.Columns.GetEnumerator();
346      e.MoveNext();
347      foreach (SortOrder sortOrder in this.ColumnGroup.SortOrdersForColumns) {
348        ((DataGridViewColumn)e.Current).HeaderCell.SortGlyphDirection = sortOrder;
349        e.MoveNext();
350      }
351    }
352
353    private void dataGridView_SelectionChanged(object sender, EventArgs e) {
354      int nullValuesCount = 0;
355      int totalValuesCount = 0;
356      IComparable minimum = "";
357      IComparable maximum = "";
358      double? mean = null;
359      double? median = null;
360      double? stddev = null;
361      ColumnBase column;
362
363      this.ColumnGroup.ClearSelectedColumns();
364      if (this.dataGridView.SelectedColumns.Count != 0) {
365        foreach (DataGridViewColumn col in this.dataGridView.SelectedColumns) {
366          column = this.ColumnGroup.Columns.ElementAt(col.Index);
367          column.Selected = true;
368          nullValuesCount += column.NullValuesCount;
369          totalValuesCount += column.NonNullValuesCount;
370        }
371        if (this.dataGridView.SelectedColumns.Count == 1) {
372          column = this.ColumnGroup.Columns.ElementAt(dataGridView.SelectedColumns[0].Index);
373          minimum = column.Minimum;
374          maximum = column.Maximum;
375          if (column is DoubleColumn) {
376            mean = ((DoubleColumn)column).Mean;
377            stddev = ((DoubleColumn)column).StandardDeviation;
378            median = ((DoubleColumn)column).Median;
379          }
380        }
381      }
382      lblNullValues.Text = nullValuesCount.ToString();
383      lblValues.Text = totalValuesCount.ToString();
384      lblMinimum.Text = minimum == null ? "" : minimum.ToString();
385      lblMaximum.Text = maximum == null ? "" : maximum.ToString();
386      lblMean.Text = mean == null ? "" : mean.ToString();
387      lblStdDev.Text = stddev == null ? "" : stddev.ToString();
388      lblMedian.Text = median == null ? "" : median.ToString();
389      UpdateStateInformation(true);
390    }
391
392    private void dataGridView_ColumnWidthChanged(object sender, DataGridViewColumnEventArgs e) {
393      RecalculateWidthOfControl();
394    }
395
396    private void RecalculateWidthOfControl() {
397      int width = 0;
398      foreach (DataGridViewColumn col in this.dataGridView.Columns)
399        width += col.Width;
400      //no columns in datagrid
401      if (width != 0) {
402        width += this.dataGridView.RowHeadersWidth;
403        //datagridview.controls[1] is always the vertical scrollbar
404        if (dataGridView.Controls[1].Visible)
405          width += 20;
406      }
407      this.dataGridView.Width = width;
408      this.Width = width;
409    }
410
411    private void dataGridView_MouseClick(object sender, MouseEventArgs e) {
412      this.ColumnGroupActive = true;
413      this.FireActivated(false);
414
415      if (this.dataGridView.AllowUserToOrderColumns || e.Clicks >= 2)
416        return;
417
418      System.Windows.Forms.DataGridView.HitTestInfo hit = dataGridView.HitTest(e.X, e.Y);
419      // row header click
420      if (hit.ColumnIndex == -1 && hit.RowIndex >= 0) {
421        if (e.Button == MouseButtons.Right) {
422          this.commandChain.Add(new InsertRowCommand(DataSet, this.ColumnGroup.Name, hit.RowIndex));
423        } else {
424          if (dataGridView.SelectionMode != DataGridViewSelectionMode.RowHeaderSelect)
425            dataGridView.SelectionMode = DataGridViewSelectionMode.RowHeaderSelect;
426        }
427      } // column header click
428      else if (hit.RowIndex == -1 && hit.ColumnIndex >= 0 && e.Button == MouseButtons.Left) {
429        if (dataGridView.SelectionMode != DataGridViewSelectionMode.ColumnHeaderSelect)
430          dataGridView.SelectionMode = DataGridViewSelectionMode.ColumnHeaderSelect;
431      }
432    }
433
434    private void dataGridView_AllowUserToOrderColumnsChanged(object sender, EventArgs e) {
435      if (this.dataGridView.AllowUserToOrderColumns)
436        this.dataGridView.SelectionMode = DataGridViewSelectionMode.RowHeaderSelect;
437    }
438
439    private void dataGridView_KeyDown(object sender, KeyEventArgs e) {
440      //handle deletion of columns and emptying of cells
441      if (e.KeyCode == Keys.Delete) {
442        if (this.dataGridView.SelectedColumns.Count != 0)
443          this.commandChain.Add(new DeleteColumnCommand(DataSet, this.ColumnGroup.Name, ColumnGroup.SelectedColumnIndexes));
444        else if (this.dataGridView.SelectedRows.Count != 0) {
445          //to ensure that cells are not emptied before the rows got deleted
446          //deleting of rows handled by user deleting rows
447        } else if (this.dataGridView.SelectedCells.Count != 0) {
448          List<Point> cells = new List<Point>();
449          foreach (DataGridViewCell cell in this.dataGridView.SelectedCells) {
450            if (cell.RowIndex < this.ColumnGroup.Columns.ElementAt(cell.ColumnIndex).TotalValuesCount && cell.Value != null)
451              cells.Add(new Point(cell.ColumnIndex, cell.RowIndex));
452          }
453          if (cells.Count != 0)
454            this.commandChain.Add(new ChangeValuesToNullCommand(DataSet, this.ColumnGroup.Name, cells));
455        }
456      }
457        //handle paste of values
458      else if (e.Control && e.KeyCode == Keys.V)
459        PasteClipboardContent();
460      else if (e.Control && e.KeyCode == Keys.C)
461        CopyClipboardContent();
462    }
463
464    private void dataGridView_Scroll(object sender, ScrollEventArgs e) {
465      UpdateRowHeaders();
466    }
467
468    private void dataGridView_Resize(object sender, EventArgs e) {
469      UpdateRowHeaders();
470    }
471
472    private void UpdateRowHeaders() {
473      for (int i = dataGridView.FirstDisplayedScrollingRowIndex; i < dataGridView.FirstDisplayedScrollingRowIndex + dataGridView.DisplayedRowCount(true); i++)
474        dataGridView.Rows[i].HeaderCell.Value = i.ToString();
475      dataGridView.Invalidate();
476    }
477
478    private void dataGridView_RowHeadersWidthChanged(object sender, EventArgs e) {
479      this.RecalculateWidthOfControl();
480    }
481
482    #region DataGridView virtual mode event handler
483
484    private void dataGridView_CellValueNeeded(object sender, System.Windows.Forms.DataGridViewCellValueEventArgs e) {
485      if (e.ColumnIndex < this.ColumnGroup.Columns.Count())
486        e.Value = ColumnGroup.Columns.ElementAt(e.ColumnIndex).GetValue(e.RowIndex);
487    }
488
489    private void dataGridView_CellValuePushed(object sender, System.Windows.Forms.DataGridViewCellValueEventArgs e) {
490      IComparable value = null;
491      try {
492        if (e.Value == null)
493          value = null;
494        else if (this.ColumnGroup.Columns.ElementAt(e.ColumnIndex).DataType == typeof(double?))
495          value = double.Parse((string)e.Value);
496        else if (this.ColumnGroup.Columns.ElementAt(e.ColumnIndex).DataType == typeof(DateTime?))
497          value = DateTime.Parse((string)e.Value);
498        else
499          value = e.Value.ToString();
500      }
501      catch (FormatException) {
502      }
503
504      if (e.RowIndex == this.dataGridView.RowCount - 1) {
505        IComparable[] row = new IComparable[this.ColumnGroup.Columns.Count()];
506        row[e.ColumnIndex] = value;
507        this.commandChain.Add(new AddRowCommand(this.DataSet, this.ColumnGroup.Name, row));
508      } else {
509        this.commandChain.Add(new ChangeValueCommand(this.DataSet, this.ColumnGroup.Name, e.ColumnIndex, e.RowIndex, value));
510      }
511    }
512
513    private void dataGridView_UserDeletingRow(object sender, System.Windows.Forms.DataGridViewRowCancelEventArgs e) {
514      e.Cancel = true;
515      if (e.Row.Index < this.ColumnGroup.RowCount) {
516        List<int> positions;
517        if (this.dataGridView.AreAllCellsSelected(true))
518          positions = Enumerable.Range(0, this.ColumnGroup.RowCount).ToList();
519        else {
520          positions = new List<int>();
521          for (int i = 0; i < this.dataGridView.SelectedRows.Count; i++)
522            if (this.dataGridView.SelectedRows[i].Index < this.ColumnGroup.RowCount)
523              positions.Add(this.dataGridView.SelectedRows[i].Index);
524          positions.Sort();
525        }
526        if (positions.Count != 0) {
527          this.commandChain.Add(new DeleteRowsCommand(DataSet, this.ColumnGroup.Name, positions));
528        }
529        this.dataGridView.ClearSelection();
530      }
531    }
532
533    #endregion
534
535    #region txtColumnName event handler
536    private void txtColumnName_Leave(object source, EventArgs e) {
537      this.txtColumnName.Visible = false;
538    }
539
540    private void txtColumnName_KeyDown(object source, KeyEventArgs e) {
541      if (e.KeyCode != Keys.Enter && e.KeyCode != Keys.Escape)
542        return;
543      if (e.KeyCode == Keys.Enter) {
544        DataGridView.HitTestInfo h = this.dataGridView.HitTest(txtColumnName.Location.X, txtColumnName.Location.Y);
545        this.commandChain.Add(new RenameColumnCommand(DataSet, this.ColumnGroup.Name, h.ColumnIndex, txtColumnName.Text));
546      }
547      this.txtColumnName.Visible = false;
548    }
549    #endregion
550
551    #region txtColumnGroupName event handler
552    private void txtColumnGroupName_Click(object sender, EventArgs e) {
553      bool ctrlPressed = (Control.ModifierKeys & Keys.Control) == Keys.Control;
554      this.dataGridView.ClearSelection();
555      this.ColumnGroupActive = !this.ColumnGroupActive;
556      FireActivated(ctrlPressed);
557      UpdateStateInformation();
558    }
559
560    private void txtColumnGroupName_DoubleClick(object sender, EventArgs e) {
561      txtColumnGroupName.ReadOnly = false;
562      txtColumnGroupName.Text = ColumnGroup.Name;
563    }
564
565    private void txtColumnGroupName_Leave(object sender, EventArgs e) {
566      if (!txtColumnGroupName.ReadOnly) {
567        txtColumnGroupName.ReadOnly = true;
568        if (CheckIfNewColumnGroupNameIsAllowed(txtColumnGroupName.Text))
569          this.commandChain.Add(new RenameColumnGroupCommand(DataSet, this.ColumnGroup.Name, txtColumnGroupName.Text));
570      }
571    }
572
573    private void txtColumnGroupName_KeyDown(object sender, KeyEventArgs e) {
574      if (this.txtColumnGroupName.ReadOnly) //user can not change the name if it is readonly
575        return;
576      if (e.KeyCode != Keys.Enter && e.KeyCode != Keys.Escape)
577        return;
578      if (e.KeyCode == Keys.Enter && CheckIfNewColumnGroupNameIsAllowed(txtColumnGroupName.Text)) {
579        this.commandChain.Add(new RenameColumnGroupCommand(DataSet, this.ColumnGroup.Name, txtColumnGroupName.Text));
580      }
581      txtColumnGroupName.ReadOnly = true;
582      this.Focus();
583      this.Refresh();
584    }
585
586    private bool CheckIfNewColumnGroupNameIsAllowed(string newName) {
587      return !this.DataSet.ColumnGroups.Any(cg => cg.Name == newName);
588    }
589    #endregion
590  }
591}
Note: See TracBrowser for help on using the repository browser.