-
Notifications
You must be signed in to change notification settings - Fork 3
/
TimingReview.cs
137 lines (123 loc) · 5.08 KB
/
TimingReview.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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace NvTimingsEd
{
public partial class TimingReview : Form
{
public TimingReview(string monitorName, ICollection<ResourceTiming> existingTimings, ICollection<ResourceTiming> newTimings)
{
InitializeComponent();
if (monitorName != null)
this.Text = String.Format("Monitor timings for " + monitorName);
var type = typeof(ResourceTiming);
var fieldInfos = type.GetFields();
DataGridView[] views = { dataGridExisting, dataGridNew };
foreach (var currView in views)
{
DataTable table = new DataTable(type.Name);
foreach (var fld in fieldInfos)
{
var column = new DataColumn(fld.Name, fld.FieldType);
var dispAttr = (DisplayAttribute)fld.GetCustomAttributes(typeof(DisplayAttribute), false).FirstOrDefault();
if (dispAttr != null)
{
if (!dispAttr.Visible)
continue;
if (dispAttr.Caption != null)
column.Caption = dispAttr.Caption;
else
column.Caption = fld.Name;
if (dispAttr.CustomType != null)
column.DataType = dispAttr.CustomType;
}
else
column.Caption = fld.Name;
column.ReadOnly = false;
column.AutoIncrement = false;
column.Unique = false;
table.Columns.Add(column);
}
ICollection<ResourceTiming> currTimings = null;
if (currView == dataGridExisting)
currTimings = existingTimings;
else if (currView == dataGridNew)
currTimings = newTimings;
if (currTimings != null)
{
foreach (var theTiming in currTimings)
{
if (theTiming.IsEmpty())
continue;
var row = table.NewRow();
foreach (DataColumn col in table.Columns)
{
var p = type.GetField(col.ColumnName);
if (p != null)
row[col.ColumnName] = Convert.ChangeType(p.GetValue(theTiming), col.DataType);
}
table.Rows.Add(row);
}
}
currView.DataSource = table;
foreach (DataGridViewColumn col in currView.Columns)
col.HeaderText = table.Columns[col.HeaderText].Caption;
currView.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.ColumnHeader;
}
}
private void TimingReview_Shown(object sender, EventArgs e)
{
DataGridView[] views = { dataGridExisting, dataGridNew };
foreach (var currView in views)
{
currView.AutoResizeColumns(currView.AutoSizeColumnsMode);
currView.Height = currView.ColumnHeadersHeight + currView.RowTemplate.Height * (6 + 1);
Size newSize = currView.GetPreferredSize(new Size(0, 0));
newSize.Width += currView.Location.X;
newSize.Height += currView.Location.Y + flowBtns.Height;
if (currView != dataGridExisting)
{
if (newSize.Height < this.ClientSize.Height)
newSize.Height = this.ClientSize.Height;
}
this.ClientSize = newSize;
}
}
public ResourceTiming[] GetNewTimings()
{
var table = (DataTable)dataGridNew.DataSource;
var cols = table.Columns;
var rows = table.Rows;
var dst = new ResourceTiming[rows.Count];
var type = dst.GetType().GetElementType();
for (int i=0; i<dst.Length; i++)
{
var row = rows[i];
object temp = dst[i];
foreach (DataColumn col in table.Columns)
{
var p = type.GetField(col.ColumnName);
if (p != null)
p.SetValue(temp, Convert.ChangeType(row[col.ColumnName], p.FieldType));
}
dst[i] = (ResourceTiming)temp;
}
return dst;
}
private void btnDialog_Click(object sender, EventArgs e)
{
if (sender == btnOK)
this.DialogResult = DialogResult.OK;
else if (sender == btnCancel)
this.DialogResult = DialogResult.Cancel;
else
return;
this.Close();
}
}
}