-
Notifications
You must be signed in to change notification settings - Fork 0
/
OneDriveObjectBrowser.cs
244 lines (213 loc) · 7.93 KB
/
OneDriveObjectBrowser.cs
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
// ------------------------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the MIT License. See License in the project root for license information.
// ------------------------------------------------------------------------------
using Microsoft.Graph;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace OneDriveApiBrowser
{
public partial class OneDriveObjectBrowser : UserControl
{
private DriveItem _item;
private PropertyDisplayFormat _format;
private readonly Serializer serializer;
public OneDriveObjectBrowser()
{
this.serializer = new Serializer();
InitializeComponent();
comboBoxPropertyFormat.SelectedIndex = (int)this.DisplayFormat;
PropertyDisplayFormatChanged();
}
public DriveItem SelectedItem
{
get
{
return _item;
}
set
{
if (value == _item) return;
_item = value;
SelectedItemChanged();
}
}
[System.ComponentModel.Browsable(true)]
public PropertyDisplayFormat DisplayFormat
{
get { return _format; }
set
{
if (value == _format) return;
_format = value;
comboBoxPropertyFormat.SelectedIndex = (int)_format;
PropertyDisplayFormatChanged();
}
}
private void SelectedItemChanged()
{
BuildPropertyUI(this.SelectedItem);
}
private void PropertyDisplayFormatChanged()
{
switch (this.DisplayFormat)
{
case OneDriveApiBrowser.PropertyDisplayFormat.RawJson:
treeViewProperties.Visible = false;
propertyGridBrowser.Visible = false;
textBoxRawJson.Visible = true;
textBoxRawJson.Dock = DockStyle.Fill;
break;
case OneDriveApiBrowser.PropertyDisplayFormat.TreeNode:
textBoxRawJson.Visible = false;
propertyGridBrowser.Visible = false;
treeViewProperties.Visible = true;
treeViewProperties.Dock = DockStyle.Fill;
break;
case OneDriveApiBrowser.PropertyDisplayFormat.ObjectBrowser:
treeViewProperties.Visible = false;
textBoxRawJson.Visible = false;
propertyGridBrowser.Visible = true;
propertyGridBrowser.Dock = DockStyle.Fill;
break;
default:
throw new NotImplementedException();
}
BuildPropertyUI(this.SelectedItem);
}
private void BuildPropertyUI(DriveItem item)
{
if (null == item) return;
switch (this.DisplayFormat)
{
case OneDriveApiBrowser.PropertyDisplayFormat.TreeNode:
var propertyNodes = ObjectToTreeNodes(item);
treeViewProperties.Nodes.Clear();
treeViewProperties.Nodes.AddRange(propertyNodes.ToArray());
break;
case OneDriveApiBrowser.PropertyDisplayFormat.RawJson:
var jsonData = this.serializer.SerializeObject(item);
textBoxRawJson.Text = JsonHelper.FormatJson(jsonData);
break;
case OneDriveApiBrowser.PropertyDisplayFormat.ObjectBrowser:
default:
propertyGridBrowser.SelectedObject = item;
break;
}
}
private static List<TreeNode> DictionaryToTreeNodes(IDictionary<string, object> dict)
{
List<TreeNode> nodes = new List<TreeNode>();
foreach (var key in dict.Keys)
{
object value = dict[key];
if (value != null)
{
var node = CreateNode(key, value);
nodes.Add(node);
}
}
return nodes;
}
private static List<TreeNode> ObjectToTreeNodes(object obj)
{
List<TreeNode> nodes = new List<TreeNode>();
foreach (var property in obj.GetType().GetProperties())
{
// Make sure to ignore indexers during creation of these nodes
if (property.GetIndexParameters().Any())
{
continue;
}
object value = property.GetValue(obj);
if (value != null)
{
var node = CreateNode(property.Name, value);
nodes.Add(node);
}
}
return nodes;
}
private static TreeNode CreateNode(string key, object value)
{
TreeNode node = new TreeNode(key);
Type t = value.GetType();
if (t == typeof(string))
{
node.Text += "=\"" + value.ToString() + "\"";
}
else if (t == typeof(int) || t == typeof(double) || t == typeof(float) || t == typeof(Int64) || t == typeof(DateTime) || t == typeof(DateTimeOffset))
{
node.Text += "=" + value.ToString();
}
else if (typeof(IEnumerable<object>).IsAssignableFrom(t))
{
var counter = 0;
foreach(var child in value as IEnumerable<object>)
{
node.Nodes.Add(CreateNode(string.Format("[{0}]", counter), child));
counter++;
}
}
else if (t == typeof(object[]))
{
object[] values = value as object[];
for (int i = 0; i < values.Length; i++)
{
node.Nodes.Add(CreateNode(string.Format("[{0}]", i), values[i]));
}
}
else if (typeof(IDictionary<string, object>).IsAssignableFrom(t))
{
var childNodes = DictionaryToTreeNodes((IDictionary<string, object>)value);
node.Nodes.AddRange(childNodes.ToArray());
}
else
{
node.Nodes.AddRange(ObjectToTreeNodes(value).ToArray());
}
node.Tag = value.ToString();
return node;
}
private void comboBoxPropertyFormat_SelectedIndexChanged(object sender, EventArgs e)
{
this.DisplayFormat = (PropertyDisplayFormat)((ComboBox)sender).SelectedIndex;
}
[System.ComponentModel.Browsable(true)]
public override string Text
{
get { return labelSelectedItemProperties.Text; }
set { labelSelectedItemProperties.Text = value; }
}
private void copyValueToolStripMenuItem_Click(object sender, EventArgs e)
{
if (null == treeViewProperties.SelectedNode) return;
string value = treeViewProperties.SelectedNode.Tag as string;
if (null != value)
{
System.Windows.Forms.Clipboard.SetText(value);
}
}
private void copyRowToolStripMenuItem_Click(object sender, EventArgs e)
{
if (null == treeViewProperties.SelectedNode) return;
string value = treeViewProperties.SelectedNode.Text;
if (null != value)
{
System.Windows.Forms.Clipboard.SetText(value);
}
}
}
public enum PropertyDisplayFormat
{
RawJson = 0,
TreeNode = 1,
ObjectBrowser = 2
}
}