Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 17369 was 17180, checked in by swagner, 5 years ago

#2875: Removed years in copyrights

File size: 7.1 KB
RevLine 
[6010]1#region License Information
2/* HeuristicLab
[17180]3 * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[6010]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;
[7216]23using System.ComponentModel;
[6020]24using System.Linq;
[6010]25using System.Windows.Forms;
26using HeuristicLab.Common.Resources;
27
28namespace HeuristicLab.Analysis.Views {
[16527]29  public partial class DataTableVisualPropertiesDialog<TDataRow> : Form where TDataRow : class, IDataRow {
[6020]30    protected bool SuppressEvents { get; set; }
[16527]31    protected IDataTable<TDataRow> Content { get; private set; }
[6010]32    private DataTableVisualProperties originalDataTableVPs;
33    private Dictionary<string, DataRowVisualProperties> originalDataRowVPs;
34
[7216]35    private HashSet<string> modifiedDisplayNames;
36    public IEnumerable<string> RowsWithModifiedDisplayNames { get { return modifiedDisplayNames.AsEnumerable(); } }
37
[16527]38    public DataTableVisualPropertiesDialog(IDataTable<TDataRow> dataTable) {
[6010]39      InitializeComponent();
[7216]40      #region Prepare controls
[6016]41      upButton.Text = string.Empty;
42      upButton.Image = VSImageLibrary.ArrowUp;
43      downButton.Text = string.Empty;
44      downButton.Image = VSImageLibrary.ArrowDown;
[7216]45      seriesListView.Items.Clear();
46      seriesListView.SmallImageList = new ImageList();
47      seriesListView.SmallImageList.Images.Add(VSImageLibrary.Graph);
48      #endregion
49
[6010]50      Content = dataTable;
51      originalDataTableVPs = (DataTableVisualProperties)Content.VisualProperties.Clone();
52      originalDataRowVPs = new Dictionary<string, DataRowVisualProperties>();
[16527]53      foreach (var row in Content.Rows)
[6010]54        originalDataRowVPs.Add(row.Name, (DataRowVisualProperties)row.VisualProperties.Clone());
[7216]55
56      dataTableVisualPropertiesControl.Content = Content.VisualProperties;
57
58      modifiedDisplayNames = new HashSet<string>();
[6010]59      FillSeriesListView();
[7216]60      RegisterContentEvents();
[6010]61    }
62
[7216]63    private void RegisterContentEvents() {
[16527]64      foreach (var row in Content.Rows) {
[7216]65        row.VisualProperties.PropertyChanged += new PropertyChangedEventHandler(Row_VisualProperties_PropertyChanged);
[6010]66      }
67    }
68
[7216]69    private void DeregisterContentEvents() {
[16527]70      foreach (var row in Content.Rows) {
[7216]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) {
[16527]82      foreach (var row in Content.Rows) {
[7216]83        if (e.PropertyName == "DisplayName" && row.VisualProperties == sender) {
84          modifiedDisplayNames.Add(row.Name);
85          break;
86        }
87      }
88    }
89
[6010]90    private void seriesListView_SelectedIndexChanged(object sender, System.EventArgs e) {
[6020]91      if (!SuppressEvents) {
92        if (seriesListView.SelectedItems.Count != 1) {
93          dataRowVisualPropertiesControl.Content = null;
[8015]94          upButton.Enabled = downButton.Enabled = false;
[6020]95        } else {
96          string rowName = seriesListView.SelectedItems[0].Text;
97          dataRowVisualPropertiesControl.Content = Content.Rows[rowName].VisualProperties;
[8015]98          upButton.Enabled = seriesListView.SelectedIndices[0] > 0;
99          downButton.Enabled = seriesListView.SelectedIndices[0] < seriesListView.Items.Count - 1;
[6020]100        }
101      }
[6010]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;
[16527]111      foreach (var row in Content.Rows) {
[6010]112        row.VisualProperties = originalDataRowVPs[row.Name];
113      }
[7216]114      modifiedDisplayNames.Clear();
[6011]115      Content.VisualProperties = originalDataTableVPs;
[6010]116      Close();
117    }
[6016]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];
[6020]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);
[6016]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];
[6020]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);
[6016]155        UpdateAllSeriesPositions();
156      }
157    }
158
159    #region Helpers
[7216]160    private void FillSeriesListView() {
161      seriesListView.SelectedIndices.Clear();
[16527]162      foreach (var row in Content.Rows) {
[7216]163        seriesListView.Items.Add(new ListViewItem(row.Name, 0));
164      }
[8015]165      if (seriesListView.Items.Count > 0)
166        seriesListView.SelectedIndices.Add(0);
[7216]167    }
168
[6016]169    private void UpdateAllSeriesPositions() {
[16527]170      var rows = Content.Rows.ToDictionary(x => x.Name);
[6016]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
[6010]178  }
179}
Note: See TracBrowser for help on using the repository browser.