namespace WinDemo
{
    using System;
    using System.ComponentModel;
    using System.Windows.Forms;
    using Telerik.Reporting;
    using ClassLibrary1;

    public partial class ReportSelector : Form
	{
		public ReportSelector()
		{
			InitializeComponent();
			
			this.PopulateGrid();
			this.dataGridView1.CellContentClick += new DataGridViewCellEventHandler(this.OnCellContentClick);
		}

		void OnCellContentClick(object sender, DataGridViewCellEventArgs e)
		{
			if (0 == e.ColumnIndex && e.RowIndex >= 0)
			{
				DataGridViewCell cell = this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex];
				Type t = cell.Tag as System.Type;
				if (null != t)
				{
					Report report = Activator.CreateInstance(t) as Report;
					if (null != report)
					{
						MainForm frm = new MainForm(report);
						frm.WindowState = FormWindowState.Normal;
						frm.Show(this);
					}
				}
			}
		}

		void PopulateGrid()
		{
			this.dataGridView1.Rows.Clear();
			
			foreach (Type t in ReportExplorer.GetReports())
			{
				DescriptionAttribute description = TypeDescriptor.GetAttributes(t)[typeof(DescriptionAttribute)] as DescriptionAttribute;
				
				object[] values = new object[]
					{
						t.Name
						, (!description.IsDefaultAttribute() ? description.Description : "-")
					};
				
				int rowIndex = this.dataGridView1.Rows.Add(values);
				this.dataGridView1.Rows[rowIndex].Cells[0].Tag = t;
			}
		}
	}
}