-
Notifications
You must be signed in to change notification settings - Fork 1
/
DataEventList.cs
269 lines (238 loc) · 9.42 KB
/
DataEventList.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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
/* Copyright (c) Citrix Systems, Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms,
* with or without modification, are permitted provided
* that the following conditions are met:
*
* * Redistributions of source code must retain the above
* copyright notice, this list of conditions and the
* following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the
* following disclaimer in the documentation and/or other
* materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using XenAdmin.Core;
namespace XenAdmin.Controls.CustomDataGraph
{
public partial class DataEventList : FlickerFreeListBox
{
public List<DataEvent> DataEvents = new List<DataEvent>();
public DataPlot DataPlot;
private static int ITEM_PADDING = 3;
private DataPlotNav PlotNav;
public DataEventList()
{
InitializeComponent();
// we are using 16pixel icons
ItemHeight = 16 + ITEM_PADDING * 2;
DrawItem += new DrawItemEventHandler(DataEventList_DrawItem);
}
public void SetPlotNav(DataPlotNav PlotNav)
{
this.PlotNav = PlotNav;
}
public void RenderEvents(Graphics graphics, DataTimeRange xrange, Rectangle bounds, int spaceheight)
{
if (DataEvents == null)
return;
foreach (DataEvent vent in DataEvents)
{
if (vent.X < xrange.Max || vent.X > xrange.Min)
continue;
LongPoint loc = LongPoint.TranslateToScreen(vent.Point, xrange, DataRange.UnitRange, new LongRectangle(bounds));
vent.DrawToBuffer(graphics, loc.Point, bounds, spaceheight);
}
}
void DataEventList_DrawItem(object sender, DrawItemEventArgs e)
{
if(e.Index == -1)
return;
DataEvent vent = Items[e.Index] as DataEvent;
Image image = vent.TypeImage;
e.Graphics.DrawImage(image, new Rectangle(e.Bounds.Left + ITEM_PADDING, e.Bounds.Top + ITEM_PADDING, e.Bounds.Height - ITEM_PADDING * 2, e.Bounds.Height - ITEM_PADDING * 2));
int width = Drawing.MeasureText(e.Graphics, vent.ToString(), Palette.LabelFont).Width + e.Bounds.Height; ;
if (width > HorizontalExtent)
HorizontalExtent = width;
Drawing.DrawText(e.Graphics, vent.ToString(), Palette.LabelFont, new Point(e.Bounds.Location.X + e.Bounds.Height - ITEM_PADDING, e.Bounds.Location.Y + ITEM_PADDING), e.ForeColor, e.BackColor);
}
public void AddEvent(DataEvent dataEvent)
{
DataEvents.Add(dataEvent);
if (Items.Count == 0)
{
Items.Add(dataEvent);
return;
}
int index = FindInsersionPlace(dataEvent.Message.timestamp.Ticks, 0, Items.Count - 1);
Items.Insert(index, dataEvent);
}
private int FindInsersionPlace(long p, int i, int j)
{
if (p > (Items[i] as DataEvent).Message.timestamp.Ticks)
return i;
if (p <= (Items[j] as DataEvent).Message.timestamp.Ticks)
return j+1;
int mid = (j + i) / 2;
if (mid == i)
return j;
if (p < (Items[mid] as DataEvent).Message.timestamp.Ticks)
return FindInsersionPlace(p, mid, j);
else
return FindInsersionPlace(p, i, mid);
}
public DataEvent SelectedEvent = null;
private void DataEventList_SelectedIndexChanged(object sender, EventArgs e)
{
if (SelectedEvent != null)
SelectedEvent.Selected = false;
SelectedEvent = (DataEvent)SelectedItem;
if (SelectedEvent != null)
SelectedEvent.Selected = true;
}
internal void OnEventsMouseClick(MouseActionArgs args)
{
foreach(DataEvent e in DataEvents)
{
LongPoint pt = LongPoint.TranslateToScreen(e.Point, args.XRange, args.YRange, new LongRectangle(args.Rectangle));
if(pt.X - 8 <= args.Point.X && args.Point.X <= pt.X + 8)
{
SelectedItem = e;
return;
}
}
SelectedItem = null;
}
/// <summary>
/// For some reason mouse 'moves' are registered even though the mouse is stationary over the control
/// need to be able to ignore them if the user is trying to get the tooltip with the arrow keys
/// </summary>
private bool ignoreMouse = true;
protected override void OnMouseEnter(EventArgs e)
{
base.OnMouseEnter(e);
ignoreMouse = false;
}
protected override void OnMouseLeave(EventArgs e)
{
base.OnMouseLeave(e);
EventListToolTip.Hide(this);
toolTipIndex = -1;
}
protected override void OnLostFocus(EventArgs e)
{
base.OnLostFocus(e);
EventListToolTip.Hide(this);
toolTipIndex = -1;
}
private int toolTipIndex = -1;
protected override void OnMouseMove(MouseEventArgs args)
{
base.OnMouseMove(args);
if (ignoreMouse)
return;
try
{
int itemIndex = -1;
if (ItemHeight != 0)
{
itemIndex = args.Y / ItemHeight;
itemIndex += TopIndex;
}
if ((itemIndex >= 0) && (itemIndex < Items.Count))
{
if (itemIndex == toolTipIndex)
return;
DataEvent e = Items[itemIndex] as DataEvent;
if (e == null)
{
EventListToolTip.Hide(this);
toolTipIndex = -1;
}
else
{
toolTipIndex = itemIndex;
EventListToolTip.Show(string.Format(XenAPI.Message.FriendlyBody(e.Message.Type.ToString()), Helpers.GetName(e.xo)),
this,
-10,
(toolTipIndex - TopIndex + 1) * ItemHeight + 2);
}
}
else
{
EventListToolTip.Hide(this);
toolTipIndex = -1;
}
}
catch
{
EventListToolTip.Hide(this);
toolTipIndex = -1;
}
}
protected override void OnKeyUp(KeyEventArgs args)
{
base.OnKeyUp(args);
if (args.KeyCode == Keys.Enter)
{
DataEventList_MouseDoubleClick(null, null);
return;
}
if (args.KeyCode != Keys.Up
&& args.KeyCode != Keys.Down
&& args.KeyCode != Keys.Left
&& args.KeyCode != Keys.Right)
return;
toolTipIndex = SelectedIndex;
if (toolTipIndex < 0)
return;
ignoreMouse = true;
DataEvent e = Items[toolTipIndex] as DataEvent;
EventListToolTip.Show(string.Format(XenAPI.Message.FriendlyBody(e.Message.Type.ToString()), Helpers.GetName(e.xo)),
this,
-10,
(toolTipIndex - TopIndex + 1) * ItemHeight + 2);
}
private void DataEventList_MouseDoubleClick(object sender, MouseEventArgs e)
{
if (SelectedEvent == null)
return;
if (PlotNav != null)
PlotNav.ZoomToPoint(SelectedEvent);
}
internal void Clear()
{
DataEvents.Clear();
Items.Clear();
HorizontalExtent = 0;
}
internal void RemoveEvent(DataEvent dataEvent)
{
DataEvents.Remove(dataEvent);
Items.Remove(dataEvent);
}
}
}