#region License Information /* HeuristicLab * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL) * * This file is part of HeuristicLab. * * HeuristicLab is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * HeuristicLab is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with HeuristicLab. If not, see . */ #endregion using System.Globalization; using HeuristicLab.BioBoost.Representation; using HeuristicLab.Core.Views; using HeuristicLab.MainForm; using SharpMap.Data; using System; using System.Linq; using System.Windows.Forms; using System.Windows.Forms.DataVisualization.Charting; namespace HeuristicLab.BioBoost.Views { [Content(typeof(RegionDetailData), IsDefaultView = false)] public partial class BioBoostSolutionDetailView : ItemView { public new RegionDetailData Content { get { return (RegionDetailData)base.Content; } set { base.Content = value; } } public BioBoostSolutionDetailView() { InitializeComponent(); } protected override void DeregisterContentEvents() { base.DeregisterContentEvents(); } protected override void RegisterContentEvents() { base.RegisterContentEvents(); } protected override void SetEnabledStateOfControls() { base.SetEnabledStateOfControls(); plantsTreeView.Enabled = Content != null; } protected override void OnContentChanged() { base.OnContentChanged(); if (Content == null) { Clear(); } else { Clear(); Populate(Content.Solution, Content.RegionName); } } public event EventHandler RegionLabelClicked; protected void OnRegionLabelClicked() { EventHandler h = RegionLabelClicked; if (h != null) h.Invoke(this, EventArgs.Empty); } private void Populate(BioBoostCompoundSolution solution, string regionName) { regionNameLabel.Text = regionName; regionDescriptionLabel.Text = GetRegionDescription(regionName); var locidx = solution.LocationNames.IndexOfFirst(s => s == regionName); if (locidx < 0) return; var supplierCollector = new SupplierCollector(solution, regionName); foreach (var feedstock in supplierCollector.Feedstocks) AddFeedstock(feedstock); foreach (var product in supplierCollector.Products) AddPlant(product); } private void Clear() { regionNameLabel.Text = ""; regionDescriptionLabel.Text = ""; plantsTreeView.Nodes.Clear(); } private void AddFeedstock(SupplierCollector.ProductInfo product) { var potentialSeries = new Series(product.Name); potentialSeries.Points.Add(product.Potential); potentialSeries.ChartType = SeriesChartType.StackedBar100; } private void AddPlant(SupplierCollector.ProductInfo product) { var totalAmount = product.Suppliers.Values.Sum(); if (totalAmount > 0) { plantsTreeView.Nodes.Add(string.Format("{0} {1,11:### ### ###} t/a", product.Name, totalAmount)); foreach (var s in product.Suppliers) AddSupplier(product.Name, s.Key, s.Value); } } private void AddSupplier(string type, string sourceName, double feedstockAmount) { foreach (TreeNode node in plantsTreeView.Nodes) { if (node.Text.StartsWith(type)) { node.Nodes.Add(string.Format(CultureInfo.InvariantCulture, "{0} {1,9:# ### ###} t/a", sourceName, feedstockAmount)); if (plantsTreeView.Nodes.Count == 1) node.Expand(); else plantsTreeView.CollapseAll(); break; } } } private string GetRegionDescription(string regionName) { if (regionName != null && Content.Solution.Geometry.Features.Columns.Contains("name")) { var row = Content.Solution.Geometry.Features.Rows.Cast().FirstOrDefault(r => (string)r["NUTS_ID"] == regionName); if (row != null) return row["name"].ToString(); } return ""; } private void plantsTreeView_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e) { if (e.Node.Parent != null) { // child node clicked var labeltext = e.Node.Text.Split(' '); if (labeltext.Length > 0) TrySwitchToRegion(labeltext[0]); } } private void TrySwitchToRegion(string regionName) { if (Content.Solution.LocationNames.Contains(regionName)) { Content = new RegionDetailData { Solution = Content.Solution, RegionName = regionName }; } } private void regionNameLabel_Click(object sender, EventArgs e) { OnRegionLabelClicked(); } } }