Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Analysis.Views/3.3/GanttDataView.cs @ 15625

Last change on this file since 15625 was 15583, checked in by swagner, 7 years ago

#2640: Updated year of copyrights in license headers

File size: 6.5 KB
Line 
1#region License Information
2
3/* HeuristicLab
4 * Copyright (C) 2002-2018 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
5 *
6 * This file is part of HeuristicLab.
7 *
8 * HeuristicLab is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * HeuristicLab is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22#endregion
23
24using System;
25using System.Drawing;
26using System.Linq;
27using HeuristicLab.Collections;
28using HeuristicLab.Core.Views;
29using HeuristicLab.MainForm;
30
31namespace HeuristicLab.Analysis.Views {
32  [View("Gantt Data View")]
33  [Content(typeof(GanttData), IsDefaultView = true)]
34  public partial class GanttDataView : ItemView {
35
36    public new GanttData Content {
37      get { return (GanttData)base.Content; }
38      set { base.Content = value; }
39    }
40
41    public GanttDataView() {
42      InitializeComponent();
43    }
44
45    #region De/Register Content Events
46    protected override void RegisterContentEvents() {
47      base.RegisterContentEvents();
48      Content.Rows.ItemsAdded += GanttRowsOnChanged;
49      Content.Rows.ItemsRemoved += GanttRowsOnRemoved;
50      Content.Rows.ItemsReplaced += GanttRowsOnChanged;
51      Content.Rows.CollectionReset += GanttRowsOnChanged;
52      Content.CategoryColors.ItemsAdded += GanttCategoryColorsOnChanged;
53      Content.CategoryColors.ItemsRemoved += GanttCategoryColorsOnChanged;
54      Content.CategoryColors.ItemsReplaced += GanttCategoryColorsOnChanged;
55      Content.CategoryColors.CollectionReset += GanttCategoryColorsOnChanged;
56      foreach (var row in Content.Rows)
57        RegisterGanttRowEvents(row);
58    }
59
60    private void RegisterGanttRowEvents(GanttRow row) {
61      row.Items.ItemsAdded += GanttRowItemsOnChanged;
62      row.Items.ItemsReplaced += GanttRowItemsOnChanged;
63      row.Items.ItemsRemoved += GanttRowItemsOnRemoved;
64      row.Items.CollectionReset += GanttRowItemsOnChanged;
65      foreach (var item in row.Items)
66        RegisterGanttRowItemEvents(item);
67    }
68
69    private void RegisterGanttRowItemEvents(GanttItem item) {
70      item.PropertyChanged += GanttRowItemsItemOnChanged;
71    }
72
73    protected override void DeregisterContentEvents() {
74      base.DeregisterContentEvents();
75      Content.Rows.ItemsAdded -= GanttRowsOnChanged;
76      Content.Rows.ItemsRemoved -= GanttRowsOnRemoved;
77      Content.Rows.ItemsReplaced -= GanttRowsOnChanged;
78      Content.Rows.CollectionReset -= GanttRowsOnChanged;
79      Content.CategoryColors.ItemsAdded -= GanttCategoryColorsOnChanged;
80      Content.CategoryColors.ItemsRemoved -= GanttCategoryColorsOnChanged;
81      Content.CategoryColors.ItemsReplaced -= GanttCategoryColorsOnChanged;
82      Content.CategoryColors.CollectionReset -= GanttCategoryColorsOnChanged;
83      foreach (var row in Content.Rows)
84        DeregisterGanttRowEvents(row);
85    }
86
87    private void DeregisterGanttRowEvents(GanttRow row) {
88      row.Items.ItemsAdded -= GanttRowItemsOnChanged;
89      row.Items.ItemsReplaced -= GanttRowItemsOnChanged;
90      row.Items.ItemsRemoved -= GanttRowItemsOnRemoved;
91      row.Items.CollectionReset -= GanttRowItemsOnChanged;
92      foreach (var item in row.Items)
93        DeregisterGanttRowItemEvents(item);
94    }
95
96    private void DeregisterGanttRowItemEvents(GanttItem item) {
97      item.PropertyChanged -= GanttRowItemsItemOnChanged;
98    }
99    #endregion
100
101    protected override void OnContentChanged() {
102      base.OnContentChanged();
103
104      UpdateVisualization();
105    }
106
107    protected override void SetEnabledStateOfControls() {
108      base.SetEnabledStateOfControls();
109      ganttChart.Enabled = !Locked && !ReadOnly && Content != null;
110    }
111
112    private void UpdateVisualization() {
113      ganttChart.Reset();
114
115      if (Content == null) return;
116
117      var colors = (KnownColor[])Enum.GetValues(typeof(KnownColor));
118
119      var categories = Content.Rows.SelectMany(x => x.Items.Select(y => y.Category ?? string.Empty)).Distinct();
120      foreach (var cat in categories) {
121        Color color;
122        if (!Content.CategoryColors.TryGetValue(cat, out color)) {
123          // if no color is defined, a random color is chosen for each category
124          var rand = new Random(cat.GetHashCode());
125          color = Color.FromKnownColor(colors[rand.Next(colors.Length)]);
126        }
127        ganttChart.AddCategory(cat, color);
128      }
129     
130      ganttChart.Hide();
131      ganttChart.Title = string.IsNullOrWhiteSpace(Content.Name) ? null : Content.Name;
132      foreach (var row in Content.Rows) {
133        foreach (var item in row.Items) {
134          ganttChart.AddData(row.Name, item.Category ?? string.Empty, item.StartDate, item.EndDate, item.ToolTip, item.ShowLabel);
135        }
136      }
137      ganttChart.Show();
138    }
139
140    #region Content Event Handlers
141    private void GanttRowsOnChanged(object sender, CollectionItemsChangedEventArgs<GanttRow> e) {
142      foreach (var row in e.Items)
143        RegisterGanttRowEvents(row);
144      foreach (var row in e.OldItems)
145        DeregisterGanttRowEvents(row);
146
147      UpdateVisualization();
148    }
149    private void GanttRowsOnRemoved(object sender, CollectionItemsChangedEventArgs<GanttRow> e) {
150      foreach (var row in e.Items)
151        DeregisterGanttRowEvents(row);
152
153      UpdateVisualization();
154    }
155    private void GanttCategoryColorsOnChanged(object sender, EventArgs e) {
156      UpdateVisualization();
157    }
158    private void GanttRowItemsOnChanged(object sender, CollectionItemsChangedEventArgs<IndexedItem<GanttItem>> e) {
159      foreach (var item in e.Items)
160        RegisterGanttRowItemEvents(item.Value);
161      foreach (var item in e.OldItems)
162        DeregisterGanttRowItemEvents(item.Value);
163
164      UpdateVisualization();
165    }
166    private void GanttRowItemsOnRemoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<GanttItem>> e) {
167      foreach (var item in e.Items)
168        DeregisterGanttRowItemEvents(item.Value);
169
170      UpdateVisualization();
171    }
172    private void GanttRowItemsItemOnChanged(object sender, EventArgs e) {
173      UpdateVisualization();
174    }
175    #endregion
176  }
177}
Note: See TracBrowser for help on using the repository browser.