Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Analysis.Views/3.3/DataTableVisualPropertiesDialog.cs @ 7216

Last change on this file since 7216 was 7216, checked in by abeham, 12 years ago

#1603

  • Made display name change transparent
  • Changed ok button

#1611

  • added warning and added label to show image size
  • fixed controls resizing
File size: 6.8 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.Collections.Generic;
23using System.ComponentModel;
24using System.Linq;
25using System.Windows.Forms;
26using HeuristicLab.Common.Resources;
27
28namespace HeuristicLab.Analysis.Views {
29  public partial class DataTableVisualPropertiesDialog : Form {
30    protected bool SuppressEvents { get; set; }
31    protected DataTable Content { get; private set; }
32    private DataTableVisualProperties originalDataTableVPs;
33    private Dictionary<string, DataRowVisualProperties> originalDataRowVPs;
34
35    private HashSet<string> modifiedDisplayNames;
36    public IEnumerable<string> RowsWithModifiedDisplayNames { get { return modifiedDisplayNames.AsEnumerable(); } }
37
38    public DataTableVisualPropertiesDialog(DataTable dataTable) {
39      InitializeComponent();
40      #region Prepare controls
41      upButton.Text = string.Empty;
42      upButton.Image = VSImageLibrary.ArrowUp;
43      downButton.Text = string.Empty;
44      downButton.Image = VSImageLibrary.ArrowDown;
45      seriesListView.Items.Clear();
46      seriesListView.SmallImageList = new ImageList();
47      seriesListView.SmallImageList.Images.Add(VSImageLibrary.Graph);
48      #endregion
49
50      Content = dataTable;
51      originalDataTableVPs = (DataTableVisualProperties)Content.VisualProperties.Clone();
52      originalDataRowVPs = new Dictionary<string, DataRowVisualProperties>();
53      foreach (DataRow row in Content.Rows)
54        originalDataRowVPs.Add(row.Name, (DataRowVisualProperties)row.VisualProperties.Clone());
55
56      dataTableVisualPropertiesControl.Content = Content.VisualProperties;
57
58      modifiedDisplayNames = new HashSet<string>();
59      FillSeriesListView();
60      RegisterContentEvents();
61    }
62
63    private void RegisterContentEvents() {
64      foreach (DataRow row in Content.Rows) {
65        row.VisualProperties.PropertyChanged += new PropertyChangedEventHandler(Row_VisualProperties_PropertyChanged);
66      }
67    }
68
69    private void DeregisterContentEvents() {
70      foreach (DataRow row in Content.Rows) {
71        row.VisualProperties.PropertyChanged -= new PropertyChangedEventHandler(Row_VisualProperties_PropertyChanged);
72      }
73    }
74
75    protected override void OnClosing(CancelEventArgs e) {
76      DeregisterContentEvents();
77      Application.DoEvents();
78      base.OnClosing(e);
79    }
80
81    private void Row_VisualProperties_PropertyChanged(object sender, PropertyChangedEventArgs e) {
82      foreach (DataRow row in Content.Rows) {
83        if (e.PropertyName == "DisplayName" && row.VisualProperties == sender) {
84          modifiedDisplayNames.Add(row.Name);
85          break;
86        }
87      }
88    }
89
90    private void seriesListView_SelectedIndexChanged(object sender, System.EventArgs e) {
91      if (!SuppressEvents) {
92        if (seriesListView.SelectedItems.Count != 1) {
93          dataRowVisualPropertiesControl.Content = null;
94        } else {
95          string rowName = seriesListView.SelectedItems[0].Text;
96          dataRowVisualPropertiesControl.Content = Content.Rows[rowName].VisualProperties;
97        }
98      }
99    }
100
101    private void okButton_Click(object sender, System.EventArgs e) {
102      DialogResult = DialogResult.OK;
103      Close();
104    }
105
106    private void cancelButton_Click(object sender, System.EventArgs e) {
107      DialogResult = DialogResult.Cancel;
108      foreach (DataRow row in Content.Rows) {
109        row.VisualProperties = originalDataRowVPs[row.Name];
110      }
111      modifiedDisplayNames.Clear();
112      Content.VisualProperties = originalDataTableVPs;
113      Close();
114    }
115
116    private void upButton_Click(object sender, System.EventArgs e) {
117      if (seriesListView.SelectedIndices.Count == 1 && seriesListView.SelectedIndices[0] > 0) {
118        int index = seriesListView.SelectedIndices[0];
119        SuppressEvents = true;
120        try {
121          seriesListView.BeginUpdate();
122          ListViewItem selectedSeriesItem = seriesListView.Items[index];
123          seriesListView.Items.RemoveAt(index);
124          ListViewItem temp = seriesListView.Items[index - 1];
125          seriesListView.Items.RemoveAt(index - 1);
126          seriesListView.Items.Insert(index - 1, selectedSeriesItem);
127          seriesListView.Items.Insert(index, temp);
128          seriesListView.SelectedIndices.Clear();
129          seriesListView.EndUpdate();
130        } finally { SuppressEvents = false; }
131        seriesListView.SelectedIndices.Add(index - 1);
132        UpdateAllSeriesPositions();
133      }
134    }
135
136    private void downButton_Click(object sender, System.EventArgs e) {
137      if (seriesListView.SelectedIndices.Count == 1 && seriesListView.SelectedIndices[0] < seriesListView.Items.Count - 1) {
138        int index = seriesListView.SelectedIndices[0];
139        SuppressEvents = true;
140        try {
141          seriesListView.BeginUpdate();
142          ListViewItem temp = seriesListView.Items[index + 1];
143          seriesListView.Items.RemoveAt(index + 1);
144          ListViewItem selectedSeriesItem = seriesListView.Items[index];
145          seriesListView.Items.RemoveAt(index);
146          seriesListView.Items.Insert(index, temp);
147          seriesListView.Items.Insert(index + 1, selectedSeriesItem);
148          seriesListView.SelectedIndices.Clear();
149          seriesListView.EndUpdate();
150        } finally { SuppressEvents = false; }
151        seriesListView.SelectedIndices.Add(index + 1);
152        UpdateAllSeriesPositions();
153      }
154    }
155
156    #region Helpers
157    private void FillSeriesListView() {
158      seriesListView.SelectedIndices.Clear();
159      foreach (DataRow row in Content.Rows) {
160        seriesListView.Items.Add(new ListViewItem(row.Name, 0));
161      }
162      seriesListView.SelectedIndices.Add(0);
163    }
164
165    private void UpdateAllSeriesPositions() {
166      Dictionary<string, DataRow> rows = Content.Rows.ToDictionary(x => x.Name);
167      Content.Rows.Clear();
168      for (int i = 0; i < seriesListView.Items.Count; i++) {
169        ListViewItem item = seriesListView.Items[i];
170        Content.Rows.Add(rows[item.Text]);
171      }
172    }
173    #endregion
174  }
175}
Note: See TracBrowser for help on using the repository browser.