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