Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Problems.BioBoost/HeuristicLab.Problems.BioBoost.Views/3.3/BioBoostSolutionDetailView.cs @ 13074

Last change on this file since 13074 was 13074, checked in by gkronber, 8 years ago

#2499: added license header and removed unused usings

File size: 5.2 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 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.Globalization;
23using HeuristicLab.BioBoost.Representation;
24using HeuristicLab.Core.Views;
25using HeuristicLab.MainForm;
26using SharpMap.Data;
27using System;
28using System.Linq;
29using System.Windows.Forms;
30using System.Windows.Forms.DataVisualization.Charting;
31
32namespace HeuristicLab.BioBoost.Views {
33
34  [Content(typeof(RegionDetailData), IsDefaultView = false)]
35  public partial class BioBoostSolutionDetailView : ItemView {
36
37    public new RegionDetailData Content {
38      get { return (RegionDetailData)base.Content; }
39      set { base.Content = value; }
40    }
41
42    public BioBoostSolutionDetailView() {
43      InitializeComponent();
44    }
45
46    protected override void DeregisterContentEvents() {
47      base.DeregisterContentEvents();
48    }
49
50    protected override void RegisterContentEvents() {
51      base.RegisterContentEvents();
52    }
53
54    protected override void SetEnabledStateOfControls() {
55      base.SetEnabledStateOfControls();
56      plantsTreeView.Enabled = Content != null;
57    }
58
59    protected override void OnContentChanged() {
60      base.OnContentChanged();
61      if (Content == null) {
62        Clear();
63      } else {
64        Clear();
65        Populate(Content.Solution, Content.RegionName);
66      }
67    }
68
69    public event EventHandler RegionLabelClicked;
70
71    protected void OnRegionLabelClicked() {
72      EventHandler h = RegionLabelClicked;
73      if (h != null)
74        h.Invoke(this, EventArgs.Empty);
75    }
76
77    private void Populate(BioBoostCompoundSolution solution, string regionName) {
78      regionNameLabel.Text = regionName; regionDescriptionLabel.Text = GetRegionDescription(regionName);
79      var locidx = solution.LocationNames.IndexOfFirst(s => s == regionName);
80      if (locidx < 0) return;
81      var supplierCollector = new SupplierCollector(solution, regionName);
82      foreach (var feedstock in supplierCollector.Feedstocks)
83        AddFeedstock(feedstock);
84      foreach (var product in supplierCollector.Products)
85        AddPlant(product);
86    }
87
88    private void Clear() {
89      regionNameLabel.Text = "";
90      regionDescriptionLabel.Text = "";
91      plantsTreeView.Nodes.Clear();
92    }
93
94    private void AddFeedstock(SupplierCollector.ProductInfo product) {
95      var potentialSeries = new Series(product.Name);
96      potentialSeries.Points.Add(product.Potential);
97      potentialSeries.ChartType = SeriesChartType.StackedBar100;
98    }
99
100    private void AddPlant(SupplierCollector.ProductInfo product) {
101      var totalAmount = product.Suppliers.Values.Sum();
102      if (totalAmount > 0) {
103        plantsTreeView.Nodes.Add(string.Format("{0} {1,11:### ### ###} t/a", product.Name, totalAmount));
104        foreach (var s in product.Suppliers)
105          AddSupplier(product.Name, s.Key, s.Value);
106      }
107    }
108
109    private void AddSupplier(string type, string sourceName, double feedstockAmount) {
110      foreach (TreeNode node in plantsTreeView.Nodes) {
111        if (node.Text.StartsWith(type)) {
112          node.Nodes.Add(string.Format(CultureInfo.InvariantCulture, "{0} {1,9:# ### ###} t/a", sourceName, feedstockAmount));
113          if (plantsTreeView.Nodes.Count == 1)
114            node.Expand();
115          else
116            plantsTreeView.CollapseAll();
117          break;
118        }
119      }
120    }
121
122    private string GetRegionDescription(string regionName) {
123      if (regionName != null && Content.Solution.Geometry.Features.Columns.Contains("name")) {
124        var row = Content.Solution.Geometry.Features.Rows.Cast<FeatureDataRow>().FirstOrDefault(r => (string)r["NUTS_ID"] == regionName);
125        if (row != null)
126          return row["name"].ToString();
127      }
128      return "";
129    }
130
131    private void plantsTreeView_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e) {
132      if (e.Node.Parent != null) { // child node clicked
133        var labeltext = e.Node.Text.Split(' ');
134        if (labeltext.Length > 0)
135          TrySwitchToRegion(labeltext[0]);
136      }
137    }
138
139    private void TrySwitchToRegion(string regionName) {
140      if (Content.Solution.LocationNames.Contains(regionName)) {
141        Content = new RegionDetailData {
142          Solution = Content.Solution,
143          RegionName = regionName
144        };
145      }
146    }
147
148    private void regionNameLabel_Click(object sender, EventArgs e) {
149      OnRegionLabelClicked();
150    }
151  }
152}
Note: See TracBrowser for help on using the repository browser.