PageRenderTime 39ms CodeModel.GetById 10ms RepoModel.GetById 1ms app.codeStats 0ms

/trunk/src/Visualizer/frmMain.cs

http://mvcroutevisualizer.codeplex.com
C# | 196 lines | 165 code | 31 blank | 0 comment | 14 complexity | a71a8a11efb42f386049ab85140edddb MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.Web.Routing;
  10. using System.Web.Script.Serialization;
  11. namespace Sparrow.VisualStudio.Debugger.RouteVisualizer
  12. {
  13. public partial class frmMain : Form
  14. {
  15. public frmMain()
  16. {
  17. InitializeComponent();
  18. this.RouteCollection = new RouteCollection();
  19. }
  20. public RouteCollection RouteCollection
  21. {
  22. get;
  23. set;
  24. }
  25. public DebugHttpContext RequestContext
  26. {
  27. get;
  28. set;
  29. }
  30. private void frmMain_Shown(object sender, EventArgs e)
  31. {
  32. MatchRoutes();
  33. this.txtRelativeUrl.Text = this.RequestContext.Request.AppRelativeCurrentExecutionFilePath;
  34. this.txtPathInfo.Text = this.RequestContext.Request.PathInfo;
  35. }
  36. List<RouteRow> Rows
  37. {
  38. get;
  39. set;
  40. }
  41. void MatchRoutes()
  42. {
  43. this.Rows = new List<RouteRow>();
  44. using (this.RouteCollection.GetReadLock())
  45. {
  46. var json = new JavaScriptSerializer();
  47. for (var i = 0; i < this.RouteCollection.Count; i++)
  48. {
  49. var item = this.RouteCollection[i];
  50. if (item is Route)
  51. {
  52. var route = item as Route;
  53. var row = new RouteRow();
  54. row.RouteData = route.GetRouteData(this.RequestContext);
  55. row.Url = route.Url;
  56. row.Defaults = string.Join("&", route.Defaults.Select(m => m.Key + "=" +json.Serialize(m.Value)));
  57. row.DataTokens = string.Join("&", route.DataTokens.Select(m => m.Key + "=" +json.Serialize(m.Value)));
  58. row.Constraints = string.Join("&", route.Constraints.Select(m => m.Key + "=" +json.Serialize( m.Value)));
  59. row.RouteHandler = route.RouteHandler == null ? "" : route.RouteHandler.ToString();
  60. Rows.Add(row);
  61. }
  62. else
  63. {
  64. var row = new RouteRow();
  65. row.RouteData = item.GetRouteData(this.RequestContext);
  66. row.Url = item.ToString();
  67. row.Defaults = "";
  68. row.DataTokens = "";
  69. row.Constraints = "";
  70. row.RouteHandler = "";
  71. Rows.Add(row);
  72. }
  73. }
  74. }
  75. this.dgvRoutes.DataSource = Rows;
  76. this.txtRouteCount.Text = Rows.Count.ToString();
  77. this.txtMatchedRoutesCount.Text = Rows.Where(i => i.Matched).Count().ToString();
  78. SelectFirstMatched();
  79. }
  80. bool IsRender
  81. {
  82. get;
  83. set;
  84. }
  85. private void btnMatch_Click(object sender, EventArgs e)
  86. {
  87. if (this.txtRelativeUrl.Text.StartsWith("~/") == false)
  88. {
  89. this.txtRelativeUrl.Text = "~/" + this.txtRelativeUrl.Text;
  90. }
  91. this.RequestContext = new DebugHttpContext(new DebugHttpRequest(this.txtRelativeUrl.Text, this.txtPathInfo.Text));
  92. this.IsRender = false;
  93. this.MatchRoutes();
  94. this.IsRender = true;
  95. SelectFirstMatched();
  96. }
  97. private void SelectFirstMatched()
  98. {
  99. var firstMatched = this.Rows.Select((i, index) => new { i.Matched, index }).Where(i => i.Matched).FirstOrDefault();
  100. if (firstMatched != null)
  101. {
  102. dgvRoutes.Rows[firstMatched.index].Selected = true;
  103. ShowRouteData(this.Rows[firstMatched.index]);
  104. }
  105. else
  106. {
  107. ShowRouteData(null);
  108. }
  109. }
  110. private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
  111. {
  112. frmAbout about = new frmAbout();
  113. about.ShowDialog();
  114. }
  115. private void exitToolStripMenuItem_Click(object sender, EventArgs e)
  116. {
  117. this.Close();
  118. }
  119. private void dgvRoutes_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
  120. {
  121. Rectangle rectangle = new Rectangle(e.RowBounds.Location.X,
  122. e.RowBounds.Location.Y,
  123. dgvRoutes.RowHeadersWidth - 4,
  124. e.RowBounds.Height);
  125. TextRenderer.DrawText(e.Graphics, (e.RowIndex + 1).ToString(),
  126. dgvRoutes.RowHeadersDefaultCellStyle.Font,
  127. rectangle,
  128. dgvRoutes.RowHeadersDefaultCellStyle.ForeColor,
  129. TextFormatFlags.VerticalCenter | TextFormatFlags.Right);
  130. if (dgvRoutes.Rows[e.RowIndex].Cells["Matched"].Value.Equals(true))
  131. {
  132. dgvRoutes.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.LightGreen;
  133. }
  134. }
  135. private void dgvRoutes_SelectionChanged(object sender, EventArgs e)
  136. {
  137. if (this.IsRender)
  138. {
  139. if (this.dgvRoutes.SelectedRows.Count > 0)
  140. {
  141. var index = this.dgvRoutes.SelectedRows[0].Index;
  142. this.ShowRouteData(this.Rows[index]);
  143. }else
  144. {
  145. this.ShowRouteData(null);
  146. }
  147. }
  148. }
  149. void ShowRouteData(RouteRow row)
  150. {
  151. if (row != null && row.RouteData != null)
  152. {
  153. var json = new JavaScriptSerializer();
  154. this.dgvRouteData.DataSource = row.RouteData.Values.Select(i => new { Key = i.Key, Value = i.Value }).ToList();
  155. this.dgvDataTokens.DataSource = row.RouteData.DataTokens.Select(i => new { Key = i.Key, Value =json.Serialize(i.Value) }).ToList();
  156. }
  157. else
  158. {
  159. var empty = new List<object>().Select(i=> new { Key = "",Value=""}).ToList();
  160. this.dgvRouteData.DataSource = empty;
  161. this.dgvDataTokens.DataSource = empty;
  162. }
  163. }
  164. }
  165. }