-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathTreeNodeData.cs
More file actions
167 lines (143 loc) · 4.14 KB
/
Copy pathTreeNodeData.cs
File metadata and controls
167 lines (143 loc) · 4.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Media;
using Newtonsoft;
using Newtonsoft.Json;
using System.IO;
using System.Windows;
namespace TreeMaps
{
public class TreeNodeData
{
[JsonProperty("id")]
public string Id { get; set; }
[JsonProperty("label")]
public string Label { get; set; }
[JsonProperty("color")]
public Color Color { get; set; }
[JsonProperty("size")]
public double Size { get; set; }
[JsonProperty("children")]
public List<TreeNodeData> Children { get; set; }
#region layout info
private double _area;
private Size _desiredSize;
private Point _desiredLocation;
private double _ratio;
private UIElement _ui;
private TreeNodeData _parent;
#endregion
public TreeNodeData(string id, string label)
{
this.Id = id;
this.Label = label;
}
public void SetColor(int dimension, Color color)
{
this.Color = color;
}
public void AddSize(int dimension, double size)
{
this.Size += size;
}
public override string ToString()
{
return Newtonsoft.Json.JsonConvert.SerializeObject(this, Newtonsoft.Json.Formatting.Indented);
}
public static TreeNodeData Load(string file)
{
using (StreamReader reader = new StreamReader(file))
{
string json = reader.ReadToEnd();
return Newtonsoft.Json.JsonConvert.DeserializeObject<TreeNodeData>(json);
}
}
public void Save(string file)
{
using (StreamWriter writer = new StreamWriter(file))
{
writer.WriteLine(this.ToString());
}
}
public TreeNodeData GetOrCreateNode(string id, string label)
{
if (Children == null)
{
Children = new List<TreeNodeData>();
}
TreeNodeData result = (from n in Children where n.Id == id select n).FirstOrDefault();
if (result == null)
{
result = new TreeNodeData(id, label);
result._parent = this;
Children.Add(result);
}
return result;
}
#region properties
[JsonIgnore]
internal Size ComputedSize
{
get { return _desiredSize; }
set { _desiredSize = value; }
}
[JsonIgnore]
internal Point ComputedLocation
{
get { return _desiredLocation; }
set { _desiredLocation = value; }
}
[JsonIgnore]
internal double AspectRatio
{
get { return _ratio; }
set { _ratio = value; }
}
[JsonIgnore]
internal double Weight
{
get { return this.Size; }
}
[JsonIgnore]
internal double RealArea
{
get { return _area; }
set { _area = value; }
}
[JsonIgnore]
internal UIElement UiElement
{
get { return _ui; }
set { _ui = value; }
}
[JsonIgnore]
public TreeNodeData Parent
{
get { return _parent; }
set { _parent = value; }
}
#endregion
#region static members
internal static int CompareByValueDecreasing(TreeNodeData x, TreeNodeData y)
{
if (x == null)
{
if (y == null)
return -1;
else
return 0;
}
else
{
if (y == null)
return 1;
else
return x.Weight.CompareTo(y.Weight) * -1;
}
}
#endregion
}
}