Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Visualization/3.2/LabelProvider/StringLabelProvider.cs @ 2298

Last change on this file since 2298 was 1996, checked in by mstoeger, 15 years ago

implemented IStorable and made use of the PersistenceManager wherever possible. #639

File size: 1.9 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Xml;
4using HeuristicLab.Core;
5
6namespace HeuristicLab.Visualization.LabelProvider {
7  public class StringLabelProvider : StorableBase, ILabelProvider {
8    private readonly Dictionary<int, string> labels = new Dictionary<int, string>();
9
10    public void ClearLabels() {
11      labels.Clear();
12    }
13
14    public void SetLabel(int index, string label) {
15      labels[index] = label;
16    }
17
18    public string GetLabel(double value) {
19      int index = (int)Math.Round(value);
20      double delta = Math.Abs(index - value);
21
22      string label;
23
24      if (delta < 1e-10 && labels.TryGetValue(index, out label))
25        return label;
26      else
27        return string.Empty;
28    }
29
30    public override XmlNode GetXmlNode(string name, XmlDocument document, IDictionary<Guid, IStorable> persistedObjects) {
31      XmlNode node = base.GetXmlNode(name, document, persistedObjects);
32
33      XmlNode labelsNode = document.CreateElement("Labels");
34
35      foreach (KeyValuePair<int, string> pair in labels) {
36        int index = pair.Key;
37        string label = pair.Value;
38
39        XmlNode labelNode = document.CreateElement("Label");
40
41        XmlSupport.SetAttribute("Index", index.ToString(), labelNode);
42        XmlSupport.SetAttribute("Value", label, labelNode);
43
44        labelsNode.AppendChild(labelNode);
45      }
46
47      node.AppendChild(labelsNode);
48
49      return node;
50    }
51
52    public override void Populate(XmlNode node, IDictionary<Guid, IStorable> restoredObjects) {
53      base.Populate(node, restoredObjects);
54
55      foreach (XmlNode labelNode in node.SelectNodes("Labels/Label")) {
56        int index = int.Parse(XmlSupport.GetAttribute("Index", labelNode));
57        string label = XmlSupport.GetAttribute("Value", labelNode);
58
59        this.SetLabel(index, label);
60      }
61    }
62  }
63}
Note: See TracBrowser for help on using the repository browser.