Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2839_HiveProjectManagement/HeuristicLab.Analysis.Views/3.3/DataTableVisualPropertiesDialog.cs @ 16057

Last change on this file since 16057 was 16057, checked in by jkarder, 6 years ago

#2839:

File size: 7.1 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2018 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          upButton.Enabled = downButton.Enabled = false;
95        } else {
96          string rowName = seriesListView.SelectedItems[0].Text;
97          dataRowVisualPropertiesControl.Content = Content.Rows[rowName].VisualProperties;
98          upButton.Enabled = seriesListView.SelectedIndices[0] > 0;
99          downButton.Enabled = seriesListView.SelectedIndices[0] < seriesListView.Items.Count - 1;
100        }
101      }
102    }
103
104    private void okButton_Click(object sender, System.EventArgs e) {
105      DialogResult = DialogResult.OK;
106      Close();
107    }
108
109    private void cancelButton_Click(object sender, System.EventArgs e) {
110      DialogResult = DialogResult.Cancel;
111      foreach (DataRow row in Content.Rows) {
112        row.VisualProperties = originalDataRowVPs[row.Name];
113      }
114      modifiedDisplayNames.Clear();
115      Content.VisualProperties = originalDataTableVPs;
116      Close();
117    }
118
119    private void upButton_Click(object sender, System.EventArgs e) {
120      if (seriesListView.SelectedIndices.Count == 1 && seriesListView.SelectedIndices[0] > 0) {
121        int index = seriesListView.SelectedIndices[0];
122        SuppressEvents = true;
123        try {
124          seriesListView.BeginUpdate();
125          ListViewItem selectedSeriesItem = seriesListView.Items[index];
126          seriesListView.Items.RemoveAt(index);
127          ListViewItem temp = seriesListView.Items[index - 1];
128          seriesListView.Items.RemoveAt(index - 1);
129          seriesListView.Items.Insert(index - 1, selectedSeriesItem);
130          seriesListView.Items.Insert(index, temp);
131          seriesListView.SelectedIndices.Clear();
132          seriesListView.EndUpdate();
133        } finally { SuppressEvents = false; }
134        seriesListView.SelectedIndices.Add(index - 1);
135        UpdateAllSeriesPositions();
136      }
137    }
138
139    private void downButton_Click(object sender, System.EventArgs e) {
140      if (seriesListView.SelectedIndices.Count == 1 && seriesListView.SelectedIndices[0] < seriesListView.Items.Count - 1) {
141        int index = seriesListView.SelectedIndices[0];
142        SuppressEvents = true;
143        try {
144          seriesListView.BeginUpdate();
145          ListViewItem temp = seriesListView.Items[index + 1];
146          seriesListView.Items.RemoveAt(index + 1);
147          ListViewItem selectedSeriesItem = seriesListView.Items[index];
148          seriesListView.Items.RemoveAt(index);
149          seriesListView.Items.Insert(index, temp);
150          seriesListView.Items.Insert(index + 1, selectedSeriesItem);
151          seriesListView.SelectedIndices.Clear();
152          seriesListView.EndUpdate();
153        } finally { SuppressEvents = false; }
154        seriesListView.SelectedIndices.Add(index + 1);
155        UpdateAllSeriesPositions();
156      }
157    }
158
159    #region Helpers
160    private void FillSeriesListView() {
161      seriesListView.SelectedIndices.Clear();
162      foreach (DataRow row in Content.Rows) {
163        seriesListView.Items.Add(new ListViewItem(row.Name, 0));
164      }
165      if (seriesListView.Items.Count > 0)
166        seriesListView.SelectedIndices.Add(0);
167    }
168
169    private void UpdateAllSeriesPositions() {
170      Dictionary<string, DataRow> rows = Content.Rows.ToDictionary(x => x.Name);
171      Content.Rows.Clear();
172      for (int i = 0; i < seriesListView.Items.Count; i++) {
173        ListViewItem item = seriesListView.Items[i];
174        Content.Rows.Add(rows[item.Text]);
175      }
176    }
177    #endregion
178  }
179}
Note: See TracBrowser for help on using the repository browser.