#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; using System.ComponentModel; using System.Globalization; using System.Linq; using System.Text; using System.Windows.Forms; using HeuristicLab.Core.Views; using HeuristicLab.Data; using HeuristicLab.MainForm; namespace HeuristicLab.BioBoost.Views { [Content(typeof(RegionDetailData), IsDefaultView = false)] public partial class BioBoostSummaryView : ItemView { public new RegionDetailData Content { get { return (RegionDetailData)base.Content; } set { base.Content = value; } } public BioBoostSummaryView() { InitializeComponent(); } protected override void OnContentChanged() { base.OnContentChanged(); if (Content == null) { Clear(); } else { Clear(); Populate(); } } protected void Clear() { treeView.Nodes.Clear(); gridView.Rows.Clear(); regionLabel.Text = "no region selected"; valueLabel.Text = "no value selected"; } protected void Populate() { foreach (var targets in Content.Solution.TransportTargets) { var node = treeView.Nodes.Add(targets.Key); node.Tag = node.Text; for (int i = 0; i < targets.Value.Length; i++) { if (targets.Value[i] != null && targets.Value[i].Count > 0) { var innerNode = node.Nodes.Add(Content.Solution.LocationNames[i]); innerNode.Tag = innerNode.Text; foreach (var suppliers in targets.Value[i]) { var supplierNode = innerNode.Nodes.Add(Content.Solution.LocationNames[suppliers.Value]); supplierNode.Tag = supplierNode.Text; } } } } valueLabel.Text = "no value selected"; regionLabel.Text = Content.RegionName ?? "no region selected"; } private void treeView_AfterSelect(object sender, TreeViewEventArgs e) { gridView.Rows.Clear(); if (treeView.SelectedNode != null) { var i = GetLocationIndex((string)treeView.SelectedNode.Tag); if (i >= 0) { PopulateGridView(i); } } } private int GetLocationIndex(string regionName) { return Content.Solution.LocationNames.IndexOfFirst(x => x == regionName); } private double? GetValue(string regionName, string valueName) { if (valueName == null) return null; var i = GetLocationIndex(regionName); if (i < 0) return 0; DoubleArray doubles; IntArray ints; if (Content.Solution.DoubleValues.TryGetValue(valueName, out doubles)) { return doubles[i]; } else if (Content.Solution.IntValues.TryGetValue(valueName, out ints)) { return ints[i]; } return null; } private void PopulateGridView(int index) { regionLabel.Text = Content.Solution.LocationNames[index]; foreach (var kvp in Content.Solution.DoubleValues.Where(kvp => kvp.Value[index] != 0)) { gridView.Rows.Add(kvp.Key, kvp.Value[index]); } foreach (var kvp in Content.Solution.IntValues.Where(kvp => kvp.Value[index] != 0)) { gridView.Rows.Add(kvp.Key, kvp.Value[index]); } foreach (var kvp in Content.Solution.StringValues.Where(kvp => !string.IsNullOrWhiteSpace(kvp.Value[index]))) { gridView.Rows.Add(kvp.Key, kvp.Value[index]); } foreach (var kvp in from kvp in Content.Solution.TransportTargets where kvp.Value[index] != null let s = string.Join(", ", kvp.Value[index]) where !string.IsNullOrWhiteSpace(s) select kvp) { gridView.Rows.Add(kvp.Key, string.Join(", ", kvp.Value[index])); } gridView.Sort(NameColumn, ListSortDirection.Ascending); } private void gridView_CurrentCellChanged(object sender, EventArgs e) { string valueName = null; if (gridView.SelectedRows.Count > 0) { valueName = (string) gridView.SelectedRows[0].Cells[0].Value; } foreach (TreeNode rootNodes in treeView.Nodes) { UpdateAggregatedValues(rootNodes, valueName); } } private static string F(double value, int significantDigits = 6) { int msd = (int)Math.Ceiling(Math.Log10(value)); int decimals = 6 - msd; if (decimals <= 0) { return value.ToString("### ### ### ### ### ### ### ### ### ### ### ### ###", CultureInfo.InvariantCulture); } else if (decimals > significantDigits) { return value.ToString("g2"); } else { StringBuilder sb = new StringBuilder(); for (int i = 0; i < msd; i+=3) { if (sb.Length > 0) sb.Append(" "); sb.Append("###"); } sb.Append("."); for (int i = 0; i < decimals; i += 3) { if (sb[sb.Length-1] != '.') sb.Append(" "); sb.Append("###"); } return value.ToString(sb.ToString(), CultureInfo.InvariantCulture); } } private double UpdateAggregatedValues(TreeNode node, string valueName) { valueLabel.Text = valueName; if (node.Nodes.Count == 0) { var value = GetValue((string) node.Tag, valueName); if (value.HasValue) { node.Text = string.Format("{0} ({1})", node.Tag, F(value.Value)); return value.Value; } return 0; } double sum = 0; int count = 0; foreach (TreeNode subnode in node.Nodes) { var subvalue = UpdateAggregatedValues(subnode, valueName); sum += subvalue; count++; } if (sum != 0) { node.Text = string.Format("{0} ({1} total, {2} avg, {3} count)", node.Tag, F(sum), F(sum/count), F(count)); } else { var value = GetValue((string) node.Tag, valueName); if (value.HasValue) { node.Text = string.Format("{0} ({1})", node.Tag, F(value.Value)); return value.Value; } node.Text = (string) node.Tag; } return sum; } } }