Click here to Skip to main content
15,885,216 members
Articles / Programming Languages / C#

EasiReports

Rate me:
Please Sign up or sign in to vote.
4.87/5 (64 votes)
13 Feb 2006CPOL6 min read 480K   9.7K   219  
A library to add reports to your application.
using System;
using System.Drawing;
using System.Collections;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;
using System.Reflection;
using System.Diagnostics;

using EasiReports;
using EasiReports.Metadata;

using Common;
using SortedViewNamespace;

namespace MetadataViewer
{
	/// <summary>
	/// Summary description for Form1.
	/// </summary>
	public class FormMain : System.Windows.Forms.Form
	{
		private System.Windows.Forms.MainMenu MainMenu;
		private System.Windows.Forms.MenuItem MenuItemFile;
		private System.Windows.Forms.MenuItem MenuItemFileOpen;
		private System.Windows.Forms.MenuItem MenuItemFileSave;
		private System.Windows.Forms.MenuItem menuItem1;
		private System.Windows.Forms.MenuItem MenuItemFileExit;
		private System.ComponentModel.IContainer components;
		private System.Windows.Forms.MenuItem MenuItemFileSaveAs;
		private System.Windows.Forms.MenuItem menuItem2;
		private System.Windows.Forms.MenuItem MenuItemDatabase;
		private System.Windows.Forms.MenuItem MenuItemDatabaseConnection;
		private System.Windows.Forms.MenuItem MenuItemDatabaseInterrogate;
		private System.Windows.Forms.StatusBar StatusBar;
		private System.Windows.Forms.MenuItem MenuItemFileNew;
		private System.Windows.Forms.MenuItem MenuItemFileClose;
		private System.Windows.Forms.MenuItem MenuItemOptions;
		private System.Windows.Forms.MenuItem MenuItemOptionsCheckVersion;
		private System.Windows.Forms.MenuItem MenuItemOptionsDefaultConnection;
		private System.Windows.Forms.Timer Timer;
		private System.Windows.Forms.MenuItem MenuItemDatabaseGuessRelationships;
		private System.Windows.Forms.TreeView TreeView;
		private System.Windows.Forms.Splitter Splitter;
		private UserDataGrid DataGrid;
		private System.Windows.Forms.ToolBar ToolBar;
		private System.Windows.Forms.ToolBarButton ToolBarButtonFileNew;
		private System.Windows.Forms.ImageList ImageList;
		private System.Windows.Forms.ToolBarButton ToolBarSeparator1;
		private System.Windows.Forms.MenuItem MenuItemAdd;
		private System.Windows.Forms.MenuItem MenuItemAddTable;
		private System.Windows.Forms.MenuItem MenuItemAddForeignKey;
		private System.Windows.Forms.ToolBarButton ToolBarButtonFileOpen;
		private System.Windows.Forms.ToolBarButton ToolBarButtonFileSave;
		private System.Windows.Forms.ToolBarButton ToolBarButtonDatabaseConnection;
		private System.Windows.Forms.ToolBarButton ToolBarButtonDatabaseInterrogate;
		private System.Windows.Forms.ToolBarButton ToolBarSeparator2;
		private System.Windows.Forms.MenuItem MenuItemRemove;
		private System.Windows.Forms.MenuItem MenuItemRemoveTable;
		private System.Windows.Forms.ToolBarButton ToolBarButtonDatabaseLogin;

		private RecentFileList _RecentFileList = null;

		private string _ReportFileName = "";

		private EasiReports.Report.EasiReport _SavedReport = null;

		public FormMain()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			EasiReports.License.Set( "MetadataViewer", "BuiltNicer;4;SquareOneSoftware;72552805;4B82DEEB" );

//			EasiReports.Glob.AllowSaveUserId   = true;
//			EasiReports.Glob.AllowSavePassword = true;

//			Settings.DefaultRegistrySettingsPath = Glob.RegistrySettingsPath;

			_RecentFileList = new RecentFileList();
			_RecentFileList.RegistryKey = Glob.RecentFilesRegistryPath;
			_RecentFileList.FileMenu = MenuItemFile;
			_RecentFileList.FileMenuPosition = 7;
			_RecentFileList.OnMenuClick += new RecentFileList.MenuClickEventHandler( OnRecentFileClick );

			AddTablesStyle();
			AddColumnsStyle();
			AddPrimaryKeysStyle();
			AddForeignKeysStyle();

//			OnFileNew();
			Timer.Enabled = true;
		}

		private void Timer_Tick( object sender, System.EventArgs e )
		{
			Timer.Enabled = false;

			string[] args = Environment.GetCommandLineArgs();

			if ( args.Length != 2 ) OnFileNew();
			else
				LoadFromFile( args[ 1 ] );
		}

//-----------------------------------------------------------------------------

		private void AddTablesStyle()
		{
			DataGridTableStyle ts = new DataGridTableStyle();
			ts.MappingName = "TableInfo[]";

			AddColumnStyles( ts, typeof( TableInfo ) );
			
			DataGrid.TableStyles.Add( ts );
		}

		private void AddColumnsStyle()
		{
			DataGridTableStyle ts = new DataGridTableStyle();
//			ts.MappingName = "ColumnInfoCollection";
			ts.MappingName = "ColumnInfoSortedView";

			AddColumnStyles( ts, typeof( ColumnInfo ) );
			
			DataGrid.TableStyles.Add( ts );
		}

		private void AddPrimaryKeysStyle()
		{
			DataGridTableStyle ts = new DataGridTableStyle();
//			ts.MappingName = "PrimaryKeyInfoCollection";
			ts.MappingName = "PrimaryKeyInfoSortedView";

			AddColumnStyles( ts, typeof( PrimaryKeyInfo ) );

			DataGrid.TableStyles.Add( ts );
		}

		private void AddForeignKeysStyle()
		{
			DataGridTableStyle ts = new DataGridTableStyle();
//			ts.MappingName = "ForeignKeyInfoCollection";
			ts.MappingName = "ForeignKeyInfoSortedView";

			AddColumnStyles( ts, typeof( ForeignKeyInfo ) );

			DataGrid.TableStyles.Add( ts );
		}

		private void AddColumnStyles( DataGridTableStyle ts, Type t )
		{
			DataGrid.SortProperties[ ts.MappingName ] = new SortProperty( ts.MappingName );

			ColumnPropertyDictionary cpd = new ColumnPropertyDictionary( ts.MappingName, t );
			DataGrid.ColumnProperties[ ts.MappingName ] = cpd;

			cpd.SetColumnStyles( ts );
		}

//-----------------------------------------------------------------------------

		/// <summary>Clean up any resources being used.</summary>
		/// <param name="disposing"><b>true</b> to release both managed and unmanaged resources; <b>false</b> to release only unmanaged resources.</param>
		/// <remarks>Releases the unmanaged resources and optionally releases the managed resources.</remarks>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.components = new System.ComponentModel.Container();
			System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(FormMain));
			this.MainMenu = new System.Windows.Forms.MainMenu();
			this.MenuItemFile = new System.Windows.Forms.MenuItem();
			this.MenuItemFileNew = new System.Windows.Forms.MenuItem();
			this.MenuItemFileOpen = new System.Windows.Forms.MenuItem();
			this.MenuItemFileClose = new System.Windows.Forms.MenuItem();
			this.menuItem2 = new System.Windows.Forms.MenuItem();
			this.MenuItemFileSave = new System.Windows.Forms.MenuItem();
			this.MenuItemFileSaveAs = new System.Windows.Forms.MenuItem();
			this.menuItem1 = new System.Windows.Forms.MenuItem();
			this.MenuItemFileExit = new System.Windows.Forms.MenuItem();
			this.MenuItemDatabase = new System.Windows.Forms.MenuItem();
			this.MenuItemDatabaseConnection = new System.Windows.Forms.MenuItem();
			this.MenuItemDatabaseInterrogate = new System.Windows.Forms.MenuItem();
			this.MenuItemDatabaseGuessRelationships = new System.Windows.Forms.MenuItem();
			this.MenuItemAdd = new System.Windows.Forms.MenuItem();
			this.MenuItemAddTable = new System.Windows.Forms.MenuItem();
			this.MenuItemAddForeignKey = new System.Windows.Forms.MenuItem();
			this.MenuItemRemove = new System.Windows.Forms.MenuItem();
			this.MenuItemRemoveTable = new System.Windows.Forms.MenuItem();
			this.MenuItemOptions = new System.Windows.Forms.MenuItem();
			this.MenuItemOptionsCheckVersion = new System.Windows.Forms.MenuItem();
			this.MenuItemOptionsDefaultConnection = new System.Windows.Forms.MenuItem();
			this.ToolBar = new System.Windows.Forms.ToolBar();
			this.ToolBarButtonFileNew = new System.Windows.Forms.ToolBarButton();
			this.ToolBarButtonFileOpen = new System.Windows.Forms.ToolBarButton();
			this.ToolBarButtonFileSave = new System.Windows.Forms.ToolBarButton();
			this.ToolBarSeparator1 = new System.Windows.Forms.ToolBarButton();
			this.ToolBarButtonDatabaseConnection = new System.Windows.Forms.ToolBarButton();
			this.ToolBarButtonDatabaseLogin = new System.Windows.Forms.ToolBarButton();
			this.ToolBarButtonDatabaseInterrogate = new System.Windows.Forms.ToolBarButton();
			this.ToolBarSeparator2 = new System.Windows.Forms.ToolBarButton();
			this.ImageList = new System.Windows.Forms.ImageList(this.components);
			this.StatusBar = new System.Windows.Forms.StatusBar();
			this.Timer = new System.Windows.Forms.Timer(this.components);
			this.TreeView = new System.Windows.Forms.TreeView();
			this.Splitter = new System.Windows.Forms.Splitter();
			this.DataGrid = new MetadataViewer.UserDataGrid();
			((System.ComponentModel.ISupportInitialize)(this.DataGrid)).BeginInit();
			this.SuspendLayout();
			// 
			// MainMenu
			// 
			this.MainMenu.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																					 this.MenuItemFile,
																					 this.MenuItemDatabase,
																					 this.MenuItemAdd,
																					 this.MenuItemRemove,
																					 this.MenuItemOptions});
			// 
			// MenuItemFile
			// 
			this.MenuItemFile.Index = 0;
			this.MenuItemFile.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																						 this.MenuItemFileNew,
																						 this.MenuItemFileOpen,
																						 this.MenuItemFileClose,
																						 this.menuItem2,
																						 this.MenuItemFileSave,
																						 this.MenuItemFileSaveAs,
																						 this.menuItem1,
																						 this.MenuItemFileExit});
			this.MenuItemFile.Text = "&File";
			this.MenuItemFile.Popup += new System.EventHandler(this.MenuItemFile_Popup);
			// 
			// MenuItemFileNew
			// 
			this.MenuItemFileNew.Index = 0;
			this.MenuItemFileNew.Shortcut = System.Windows.Forms.Shortcut.CtrlN;
			this.MenuItemFileNew.Text = "&New";
			this.MenuItemFileNew.Click += new System.EventHandler(this.MenuItemFileNew_Click);
			// 
			// MenuItemFileOpen
			// 
			this.MenuItemFileOpen.Index = 1;
			this.MenuItemFileOpen.Shortcut = System.Windows.Forms.Shortcut.CtrlO;
			this.MenuItemFileOpen.Text = "&Open...";
			this.MenuItemFileOpen.Click += new System.EventHandler(this.MenuItemFileOpen_Click);
			// 
			// MenuItemFileClose
			// 
			this.MenuItemFileClose.Index = 2;
			this.MenuItemFileClose.Text = "&Close";
			this.MenuItemFileClose.Click += new System.EventHandler(this.MenuItemFileClose_Click);
			// 
			// menuItem2
			// 
			this.menuItem2.Index = 3;
			this.menuItem2.Text = "-";
			// 
			// MenuItemFileSave
			// 
			this.MenuItemFileSave.Index = 4;
			this.MenuItemFileSave.Shortcut = System.Windows.Forms.Shortcut.CtrlS;
			this.MenuItemFileSave.Text = "&Save";
			this.MenuItemFileSave.Click += new System.EventHandler(this.MenuItemFileSave_Click);
			// 
			// MenuItemFileSaveAs
			// 
			this.MenuItemFileSaveAs.Index = 5;
			this.MenuItemFileSaveAs.Text = "Save &As...";
			this.MenuItemFileSaveAs.Click += new System.EventHandler(this.MenuItemFileSaveAs_Click);
			// 
			// menuItem1
			// 
			this.menuItem1.Index = 6;
			this.menuItem1.Text = "-";
			// 
			// MenuItemFileExit
			// 
			this.MenuItemFileExit.Index = 7;
			this.MenuItemFileExit.Text = "E&xit";
			this.MenuItemFileExit.Click += new System.EventHandler(this.MenuItemFileExit_Click);
			// 
			// MenuItemDatabase
			// 
			this.MenuItemDatabase.Index = 1;
			this.MenuItemDatabase.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																							 this.MenuItemDatabaseConnection,
																							 this.MenuItemDatabaseInterrogate,
																							 this.MenuItemDatabaseGuessRelationships});
			this.MenuItemDatabase.Text = "&Database";
			// 
			// MenuItemDatabaseConnection
			// 
			this.MenuItemDatabaseConnection.Index = 0;
			this.MenuItemDatabaseConnection.Text = "&Connection...";
			this.MenuItemDatabaseConnection.Click += new System.EventHandler(this.MenuItemDatabaseConnection_Click);
			// 
			// MenuItemDatabaseInterrogate
			// 
			this.MenuItemDatabaseInterrogate.Index = 1;
			this.MenuItemDatabaseInterrogate.Text = "&Interrogate";
			this.MenuItemDatabaseInterrogate.Click += new System.EventHandler(this.MenuItemDatabaseInterrogate_Click);
			// 
			// MenuItemDatabaseGuessRelationships
			// 
			this.MenuItemDatabaseGuessRelationships.Index = 2;
			this.MenuItemDatabaseGuessRelationships.Text = "&Guess Relationships";
			this.MenuItemDatabaseGuessRelationships.Click += new System.EventHandler(this.MenuItemDatabaseGuessRelationships_Click);
			// 
			// MenuItemAdd
			// 
			this.MenuItemAdd.Index = 2;
			this.MenuItemAdd.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																						this.MenuItemAddTable,
																						this.MenuItemAddForeignKey});
			this.MenuItemAdd.Text = "&Add";
			// 
			// MenuItemAddTable
			// 
			this.MenuItemAddTable.Index = 0;
			this.MenuItemAddTable.Text = "&Table...";
			this.MenuItemAddTable.Click += new System.EventHandler(this.MenuItemAddTable_Click);
			// 
			// MenuItemAddForeignKey
			// 
			this.MenuItemAddForeignKey.Index = 1;
			this.MenuItemAddForeignKey.Text = "&Foreign Key...";
			this.MenuItemAddForeignKey.Click += new System.EventHandler(this.MenuItemAddForeignKey_Click);
			// 
			// MenuItemRemove
			// 
			this.MenuItemRemove.Index = 3;
			this.MenuItemRemove.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																						   this.MenuItemRemoveTable});
			this.MenuItemRemove.Text = "&Remove";
			// 
			// MenuItemRemoveTable
			// 
			this.MenuItemRemoveTable.Index = 0;
			this.MenuItemRemoveTable.Text = "&Table...";
			this.MenuItemRemoveTable.Click += new System.EventHandler(this.MenuItemRemoveTable_Click);
			// 
			// MenuItemOptions
			// 
			this.MenuItemOptions.Index = 4;
			this.MenuItemOptions.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																							this.MenuItemOptionsCheckVersion,
																							this.MenuItemOptionsDefaultConnection});
			this.MenuItemOptions.Text = "&Options";
			// 
			// MenuItemOptionsCheckVersion
			// 
			this.MenuItemOptionsCheckVersion.Checked = true;
			this.MenuItemOptionsCheckVersion.Index = 0;
			this.MenuItemOptionsCheckVersion.Text = "Check &Version On Start";
			this.MenuItemOptionsCheckVersion.Visible = false;
			// 
			// MenuItemOptionsDefaultConnection
			// 
			this.MenuItemOptionsDefaultConnection.Index = 1;
			this.MenuItemOptionsDefaultConnection.Text = "Default &Connection...";
			this.MenuItemOptionsDefaultConnection.Click += new System.EventHandler(this.MenuItemOptionsDefaultConnection_Click);
			// 
			// ToolBar
			// 
			this.ToolBar.Appearance = System.Windows.Forms.ToolBarAppearance.Flat;
			this.ToolBar.Buttons.AddRange(new System.Windows.Forms.ToolBarButton[] {
																					   this.ToolBarButtonFileNew,
																					   this.ToolBarButtonFileOpen,
																					   this.ToolBarButtonFileSave,
																					   this.ToolBarSeparator1,
																					   this.ToolBarButtonDatabaseConnection,
																					   this.ToolBarButtonDatabaseLogin,
																					   this.ToolBarButtonDatabaseInterrogate,
																					   this.ToolBarSeparator2});
			this.ToolBar.DropDownArrows = true;
			this.ToolBar.ImageList = this.ImageList;
			this.ToolBar.Location = new System.Drawing.Point(0, 0);
			this.ToolBar.Name = "ToolBar";
			this.ToolBar.ShowToolTips = true;
			this.ToolBar.Size = new System.Drawing.Size(792, 28);
			this.ToolBar.TabIndex = 0;
			this.ToolBar.ButtonClick += new System.Windows.Forms.ToolBarButtonClickEventHandler(this.ToolBar_ButtonClick);
			// 
			// ToolBarButtonFileNew
			// 
			this.ToolBarButtonFileNew.ImageIndex = 0;
			this.ToolBarButtonFileNew.ToolTipText = "New";
			// 
			// ToolBarButtonFileOpen
			// 
			this.ToolBarButtonFileOpen.ImageIndex = 1;
			this.ToolBarButtonFileOpen.ToolTipText = "Open";
			// 
			// ToolBarButtonFileSave
			// 
			this.ToolBarButtonFileSave.ImageIndex = 2;
			this.ToolBarButtonFileSave.ToolTipText = "Save";
			// 
			// ToolBarSeparator1
			// 
			this.ToolBarSeparator1.Style = System.Windows.Forms.ToolBarButtonStyle.Separator;
			// 
			// ToolBarButtonDatabaseConnection
			// 
			this.ToolBarButtonDatabaseConnection.ImageIndex = 3;
			this.ToolBarButtonDatabaseConnection.ToolTipText = "Connection";
			// 
			// ToolBarButtonDatabaseLogin
			// 
			this.ToolBarButtonDatabaseLogin.ImageIndex = 5;
			// 
			// ToolBarButtonDatabaseInterrogate
			// 
			this.ToolBarButtonDatabaseInterrogate.ImageIndex = 4;
			this.ToolBarButtonDatabaseInterrogate.ToolTipText = "Interrogate";
			// 
			// ToolBarSeparator2
			// 
			this.ToolBarSeparator2.Style = System.Windows.Forms.ToolBarButtonStyle.Separator;
			// 
			// ImageList
			// 
			this.ImageList.ImageSize = new System.Drawing.Size(16, 16);
			this.ImageList.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("ImageList.ImageStream")));
			this.ImageList.TransparentColor = System.Drawing.Color.Transparent;
			// 
			// StatusBar
			// 
			this.StatusBar.Location = new System.Drawing.Point(0, 528);
			this.StatusBar.Name = "StatusBar";
			this.StatusBar.Size = new System.Drawing.Size(792, 22);
			this.StatusBar.TabIndex = 1;
			this.StatusBar.Text = "Ready";
			// 
			// Timer
			// 
			this.Timer.Tick += new System.EventHandler(this.Timer_Tick);
			// 
			// TreeView
			// 
			this.TreeView.Dock = System.Windows.Forms.DockStyle.Left;
			this.TreeView.FullRowSelect = true;
			this.TreeView.HideSelection = false;
			this.TreeView.ImageIndex = -1;
			this.TreeView.Location = new System.Drawing.Point(0, 28);
			this.TreeView.Name = "TreeView";
			this.TreeView.SelectedImageIndex = -1;
			this.TreeView.ShowRootLines = false;
			this.TreeView.Size = new System.Drawing.Size(216, 500);
			this.TreeView.TabIndex = 2;
			this.TreeView.AfterSelect += new System.Windows.Forms.TreeViewEventHandler(this.TreeView_AfterSelect);
			// 
			// Splitter
			// 
			this.Splitter.Location = new System.Drawing.Point(216, 28);
			this.Splitter.Name = "Splitter";
			this.Splitter.Size = new System.Drawing.Size(3, 500);
			this.Splitter.TabIndex = 3;
			this.Splitter.TabStop = false;
			// 
			// DataGrid
			// 
			this.DataGrid.CaptionVisible = false;
			this.DataGrid.DataMember = "";
			this.DataGrid.Dock = System.Windows.Forms.DockStyle.Fill;
			this.DataGrid.HeaderForeColor = System.Drawing.SystemColors.ControlText;
			this.DataGrid.Location = new System.Drawing.Point(219, 28);
			this.DataGrid.Name = "DataGrid";
			this.DataGrid.Size = new System.Drawing.Size(573, 500);
			this.DataGrid.TabIndex = 4;
			// 
			// FormMain
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(792, 550);
			this.Controls.Add(this.DataGrid);
			this.Controls.Add(this.Splitter);
			this.Controls.Add(this.TreeView);
			this.Controls.Add(this.StatusBar);
			this.Controls.Add(this.ToolBar);
			this.Menu = this.MainMenu;
			this.Name = "FormMain";
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
			this.Text = "Metadata Viewer";
			this.Closing += new System.ComponentModel.CancelEventHandler(this.FormMain_Closing);
			((System.ComponentModel.ISupportInitialize)(this.DataGrid)).EndInit();
			this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.EnableVisualStyles();
			Application.DoEvents();
			Application.Run( new FormMain() );
		}

//-----------------------------------------------------------------------------

		protected override void WndProc( ref Message m )
		{
			base.WndProc( ref m );

			if ( m.Msg == Glob.WM_DeleteHangingForeignKey ) OnDeleteHangingForeignKey();
		}


//-----------------------------------------------------------------------------

		private void ToolBar_ButtonClick( object sender, System.Windows.Forms.ToolBarButtonClickEventArgs e )
		{
			if ( e.Button == ToolBarButtonFileNew             ) OnFileNew             ();
			if ( e.Button == ToolBarButtonFileOpen            ) OnFileOpen            ();
			if ( e.Button == ToolBarButtonFileSave            ) OnFileSave            ();

			if ( e.Button == ToolBarButtonDatabaseConnection  ) OnDatabaseConnection  ();
			if ( e.Button == ToolBarButtonDatabaseLogin       ) OnDatabaseLogin       ();
			if ( e.Button == ToolBarButtonDatabaseInterrogate ) OnDatabaseInterrogate ();
		}

		private void MenuItemFileNew_Click(object sender, System.EventArgs e)
		{
			OnFileNew();
		}

		private void MenuItemFileOpen_Click(object sender, System.EventArgs e)
		{
			OnFileOpen();
		}

		private void MenuItemFileClose_Click(object sender, System.EventArgs e)
		{
			OnFileNew();
		}

		private void MenuItemFileSave_Click(object sender, System.EventArgs e)
		{
			OnFileSave();
		}

		private void MenuItemFileSaveAs_Click(object sender, System.EventArgs e)
		{
			OnFileSaveAs();
		}

		private void MenuItemFileExit_Click(object sender, System.EventArgs e)
		{
			OnFileExit();
		}

		private void MenuItemDatabaseConnection_Click(object sender, System.EventArgs e)
		{
			OnDatabaseConnection();
		}

		private void MenuItemDatabaseInterrogate_Click(object sender, System.EventArgs e)
		{
			OnDatabaseInterrogate();
		}

		private void MenuItemDatabaseGuessRelationships_Click(object sender, System.EventArgs e)
		{
			OnDatabaseGuessRelationships();
		}

		private void MenuItemAddTable_Click(object sender, System.EventArgs e)
		{
			OnAddTable();
		}

		private void MenuItemAddForeignKey_Click(object sender, System.EventArgs e)
		{
			OnAddForeignKey();
		}

		private void MenuItemRemoveTable_Click(object sender, System.EventArgs e)
		{
			OnRemoveTable();
		}

		private void MenuItemOptionsDefaultConnection_Click(object sender, System.EventArgs e)
		{
			OnOptionsDefaultConnection();
		}

		private void TreeView_AfterSelect(object sender, System.Windows.Forms.TreeViewEventArgs e)
		{
			OnTreeViewSelect();
		}

//-----------------------------------------------------------------------------

		private void SetTitle()
		{
			this.Text = Path.GetFileNameWithoutExtension( _ReportFileName );
			if ( _ReportFileName.Length != 0 ) Text += " - ";
			this.Text += "Metadata Viewer v" + Glob.Version;
		}

//-----------------------------------------------------------------------------

		private void MenuItemFile_Popup(object sender, System.EventArgs e)
		{
			_RecentFileList.SetMenuItems();
		}

		private void OnFileNew()
		{
			if ( CheckSavedReport() == DialogResult.Cancel ) return;

			_ReportFileName = "";
			SetTitle();

			EasiReports.Metadata.CMetadata metadata = new EasiReports.Metadata.CMetadata();

			string DefaultConnection  = Settings.Get( Glob.EasiReporterRegistrySettingsPath, null, Glob.DefaultConnectionValueName  , Glob.DefaultDefaultConnection  );
			bool   DefaultInterrogate = Settings.Get( Glob.EasiReporterRegistrySettingsPath, null, Glob.DefaultInterrogateValueName , Glob.DefaultDefaultInterrogate );

			if ( DefaultInterrogate )
			{
				using ( new StatusBarText( StatusBar, "Interrogating Database..." ) )
					if ( ! metadata.Interrogate( DefaultConnection ) )
						MessageBox.Show( "Failed to interrogate database", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error );
					else
						User32.MessageBeep( MB.OK );

				RemoveUnwantedOracleTables( metadata );
			}

			Glob.Report = new EasiReports.Report.EasiReport( DefaultConnection, metadata );

			_SavedReport = new EasiReports.Report.EasiReport( Glob.Report );

			SetFromReport();
		}

		private void OnRecentFileClick( object sender, RecentFileList.MenuClickEventArgs e )
		{
			if ( CheckSavedReport() == DialogResult.Cancel ) return;

			if ( ! LoadFromFile( e.Filename ) )
			{
				_RecentFileList.RemoveFile( e.Filename );
				return;
			}
		}

		private void OnFileOpen()
		{
			if ( CheckSavedReport() == DialogResult.Cancel ) return;

			string ReportFileDirectory = Settings.Get( Glob.EasiReporterRegistrySettingsPath, null, Glob.ReportFileDirectoryValueName, Glob.DefaultReportFileDirectory );

			OpenFileDialog dlg = new OpenFileDialog();
			dlg.InitialDirectory = ReportFileDirectory;
			dlg.Filter = "EasiReport Files (*.EasiReport)|*.EasiReport|All Files (*.*)|*.*";

			if ( dlg.ShowDialog( this ) != DialogResult.OK ) return;

			if ( ! LoadFromFile( dlg.FileName ) ) return;

			Settings.Set( Glob.EasiReporterRegistrySettingsPath, null, Glob.ReportFileDirectoryValueName, Path.GetDirectoryName( dlg.FileName ) );
		}

		private bool LoadFromFile( string filename )
		{
			try
			{
				if ( Glob.Report == null ) Glob.Report = new EasiReports.Report.EasiReport();
				Glob.Report.LoadFromFile( filename );

				_RecentFileList.InsertFile( filename );
				_ReportFileName = filename;
				SetTitle();

				_SavedReport = new EasiReports.Report.EasiReport( Glob.Report );

				SetFromReport();

				return true;
			}
			catch ( DirectoryNotFoundException )
			{
				MessageBox.Show(
					"Directory does not exist :\n\n" + Path.GetDirectoryName( filename ),
					"Failed to load report",
					MessageBoxButtons.OK,
					MessageBoxIcon.Warning );
				return false;
			}
			catch ( FileNotFoundException )
			{
				MessageBox.Show(
					"File does not exist :\n\n" + filename,
					"Failed to load report",
					MessageBoxButtons.OK,
					MessageBoxIcon.Warning );
				return false;
			}
			catch ( Exception e )
			{
				MessageBox.Show(
					e.ToString(),
					"Failed to load report",
					MessageBoxButtons.OK,
					MessageBoxIcon.Warning );
				return false;
			}
		}

		private DialogResult OnFileSave()
		{
			if ( _ReportFileName.Length == 0 ) return OnFileSaveAs();
			if ( ( File.GetAttributes( _ReportFileName ) & FileAttributes.ReadOnly ) > 0 ) return OnFileSaveAs();

			try
			{
				Glob.Report.SaveToFile( _ReportFileName );
				_SavedReport = new EasiReports.Report.EasiReport( Glob.Report );
			}
			catch ( Exception e )
			{
				MessageBox.Show(
					e.ToString(),
					"Failed to save report",
					MessageBoxButtons.OK,
					MessageBoxIcon.Warning );
				
				return DialogResult.Cancel;
			}

			return DialogResult.OK;
		}

		private DialogResult OnFileSaveAs()
		{
			string ReportFileDirectory = Settings.Get( Glob.EasiReporterRegistrySettingsPath, null, Glob.ReportFileDirectoryValueName, Glob.DefaultReportFileDirectory );

			SaveFileDialog dlg = new SaveFileDialog();
			dlg.InitialDirectory = ReportFileDirectory;
			dlg.Filter = "EasiReport Files (*.EasiReport)|*.EasiReport";

			if ( dlg.ShowDialog( this ) != DialogResult.OK ) return DialogResult.Cancel;

			bool bReadOnly = false;
			if ( File.Exists( dlg.FileName ) )
				if ( ( File.GetAttributes( dlg.FileName ) & FileAttributes.ReadOnly ) > 0 )
				{
					DialogResult r = MessageBox.Show( "File is readonly.\n\nAre you sure you want to overwrite ?", "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2 );
					if ( r != DialogResult.Yes ) return DialogResult.Cancel;

					bReadOnly = true;

					FileAttributes a = File.GetAttributes( dlg.FileName );
					File.SetAttributes( dlg.FileName, a & ~FileAttributes.ReadOnly );
				}

			try
			{
				Glob.Report.SaveToFile( dlg.FileName );
				_SavedReport = new EasiReports.Report.EasiReport( Glob.Report );
			}
			catch ( Exception e )
			{
				MessageBox.Show(
					e.ToString(),
					"Failed to save report",
					MessageBoxButtons.OK,
					MessageBoxIcon.Warning );
				return DialogResult.Cancel;
			}

			if ( bReadOnly )
			{
				FileAttributes a = File.GetAttributes( dlg.FileName );
				File.SetAttributes( dlg.FileName, a | FileAttributes.ReadOnly );
			}

			_ReportFileName = dlg.FileName;
			SetTitle();

			Settings.Set( Glob.EasiReporterRegistrySettingsPath, null, Glob.ReportFileDirectoryValueName, Path.GetDirectoryName( dlg.FileName ) );

			_RecentFileList.InsertFile( dlg.FileName );

			return  DialogResult.OK;
		}

		private void OnFileExit()
		{
			Close();
		}

		private void FormMain_Closing( object sender, System.ComponentModel.CancelEventArgs e )
		{
			if ( CheckSavedReport() == DialogResult.Cancel ) { e.Cancel = true; return; }

			foreach ( SortProperty sp in DataGrid.SortProperties.Values )
				sp.SaveToRegistry();

			foreach ( DataGridTableStyle ts in DataGrid.TableStyles )
			{
				ColumnPropertyDictionary cpd = (ColumnPropertyDictionary) DataGrid.ColumnProperties[ ts.MappingName ];
				if ( cpd == null ) { Debug.Assert( false ); continue; }

				cpd.UpdateFromGrid( ts );
			}

			foreach ( ColumnPropertyDictionary cpd in DataGrid.ColumnProperties.Values )
				cpd.SaveToRegistry();
		}

		private DialogResult CheckSavedReport()
		{
			if ( Glob.Report == null ) return DialogResult.No;
			if ( Glob.Report.ValueEquals( _SavedReport ) ) return DialogResult.No;

			string s = "Do you want to save the changes to this report ?";
			if ( _ReportFileName.Length != 0 )
				s = "Do you want to save the changes to " + Path.GetFileNameWithoutExtension( _ReportFileName ) + " ?";

			DialogResult r = MessageBox.Show( s, "Report has changed",
				MessageBoxButtons.YesNoCancel,
				MessageBoxIcon.Warning );

			if ( r == DialogResult.Yes )
				if ( OnFileSave() == DialogResult.Cancel )
					return DialogResult.Cancel;
			
			return r;
		}


//-----------------------------------------------------------------------------
		
		private void OnDatabaseConnection()
		{
			DlgConnection dlg = new DlgConnection();
			dlg.Connection = Glob.Report.Connection ;
//			dlg.UserID     = Glob.Report.UserId         ;
//			dlg.Password   = Glob.Report.Password       ;
			if ( dlg.ShowDialog() != DialogResult.OK ) return;

//			Glob.Report.SetConnectionAndLogin( dlg.Connection, dlg.UserID, dlg.Password );
			Glob.Report.Connection = dlg.Connection;

			if ( dlg.Interrogate ) OnDatabaseInterrogate();
//			else ReportControl.RefreshView();
		}

		private void OnDatabaseLogin()
		{
//			Glob.Report.PromptForLogin();
		}

		private void RemoveUnwantedOracleTables( EasiReports.Metadata.CMetadata m )
		{
			StringCollection sc = new StringCollection();

			foreach ( TableInfo t in m.Tables.Values )
			{
				if ( t.Schema == "XDB"    ) sc.Add( t.Name );
				if ( t.Schema == "MDSYS"  ) sc.Add( t.Name );
				if ( t.Schema == "WKSYS"  ) sc.Add( t.Name );
				if ( t.Schema == "WMSYS"  ) sc.Add( t.Name );
				if ( t.Schema == "CTXSYS" ) sc.Add( t.Name );
			}

			foreach ( string s in sc )
				m.RemoveTable( s );
		}

		private void OnDatabaseInterrogate()
		{
			EasiReports.Metadata.CMetadata metadata = new EasiReports.Metadata.CMetadata();

			using ( new StatusBarText( StatusBar, "Interrogating Database..." ) )
				if ( ! metadata.Interrogate( Glob.Report.Connection ) )
				{
					MessageBox.Show( "Failed to interrogate database.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error );
					return;
				}

			User32.MessageBeep( MB.OK );

			RemoveUnwantedOracleTables( metadata );

			Glob.Report.Metadata = metadata;

			if ( metadata.ForeignKeyCount == 0 )
				MessageBox.Show( "This database contains no foreign key information.\n\nPlease contact your database developer.", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning );

			SetFromReport();
		}

		private void OnDatabaseGuessRelationships()
		{
			int i = Glob.Report.Metadata.GuessRelationships();
			MessageBox.Show( string.Format( "{0} ForeignKeys added.", i ) );

			SetFromReport();
		}

//-----------------------------------------------------------------------------

		private void OnAddTable()
		{
			DlgAddTable dlg = new DlgAddTable();
			if ( dlg.ShowDialog( this ) != DialogResult.OK ) return;

			EasiReports.Metadata.CMetadata Metadata = Glob.Report.Metadata;

			TableInfo ti = new TableInfo();
			ti.Name = dlg.Table;

			Metadata.AddTable( ti );

			SetFromReport();

			SelectTreeNode( "Tables/" + ti.Name );
		}

		private void OnAddForeignKey()
		{
			EasiReports.Metadata.CMetadata Metadata = Glob.Report.Metadata;

			DlgAddForeignKey dlg = new DlgAddForeignKey( Metadata );
			if ( dlg.ShowDialog( this ) != DialogResult.OK ) return;

			EasiReports.Metadata.ForeignKeyInfo fki = new ForeignKeyInfo();
			fki.PKTableName = dlg.PrimaryTable.Name;
			fki.PKColumnName = "ColumnName";
			fki.FKTableName = dlg.ForeignTable.Name;
			fki.FKColumnName = "ColumnName";
			fki.Ordinal = 1;
	
			Metadata.AddForeignKey( fki );

			SetFromReport();

			SelectTreeNode( "Tables/" + fki.PKTableName + "/ForeignKeys/" + fki.FKTableName );
		}

//-----------------------------------------------------------------------------

		private void OnRemoveTable()
		{
			EasiReports.Metadata.CMetadata Metadata = Glob.Report.Metadata;

			DlgRemoveTable dlg = new DlgRemoveTable( Metadata );
			if ( dlg.ShowDialog( this ) != DialogResult.OK ) return;

			Metadata.RemoveTable( dlg.Table.Name );

			SetFromReport();
		}

//-----------------------------------------------------------------------------

		private void OnOptionsDefaultConnection()
		{
			string DefaultConnection  = Settings.Get( Glob.EasiReporterRegistrySettingsPath, null, Glob.DefaultConnectionValueName  , Glob.DefaultDefaultConnection  );
			bool   DefaultInterrogate = Settings.Get( Glob.EasiReporterRegistrySettingsPath, null, Glob.DefaultInterrogateValueName , Glob.DefaultDefaultInterrogate );

			DlgConnection dlg = new DlgConnection();
			dlg.Connection = DefaultConnection;
			dlg.Interrogate = DefaultInterrogate;
			if ( dlg.ShowDialog( this ) != DialogResult.OK ) return;

			Settings.Set( Glob.EasiReporterRegistrySettingsPath, null, Glob.DefaultConnectionValueName  , dlg.Connection  );
			Settings.Set( Glob.EasiReporterRegistrySettingsPath, null, Glob.DefaultInterrogateValueName , dlg.Interrogate );
		}

//-----------------------------------------------------------------------------

		private void SelectTreeNode( string Path )
		{
			SelectTreeNode( TreeView.Nodes, Path );
		}

		private void SelectTreeNode( TreeNodeCollection Nodes, string Path )
		{
			string[] sa = Path.Split( new char[] { '\\', '/' } );

			TreeNode Node = null;
			foreach ( TreeNode n in Nodes )
				if ( n.Text == sa[ 0 ] )
					Node = n;
			if ( Node == null ) { Debug.Assert( false ); return; }

			if ( sa.Length == 1 )
			{
				TreeView.SelectedNode = Node;
				return;
			}

			SelectTreeNode( Node.Nodes, string.Join( "/", sa, 1, sa.Length - 1 ) );
		}

//-----------------------------------------------------------------------------

		private void SetFromReport()
		{
			TreeView.Nodes.Clear();

			if ( Glob.Report.Metadata == null )
			{
				TreeView.Nodes.Add( "Null metadata" );
				return;
			}

			TreeView.BeginUpdate();

			TreeNode nTables = TreeView.Nodes.Add( "Tables" );

			foreach ( TableItem table in Glob.Report.Metadata.Tables.Values )
			{
				TreeNode nTable = nTables.Nodes.Add( table.Name );
				nTable.Tag = new TableInfo[] { table };

				nTable.Nodes.Add( "Columns" ).Tag = new ColumnInfoSortedView( table.Columns );
				nTable.Nodes.Add( "PrimaryKeys" ).Tag = new PrimaryKeyInfoSortedView( table.PrimaryKeys );

				TreeNode nFK = nTable.Nodes.Add( "ForeignKeys" );

				foreach ( DictionaryEntry d in table.ForeignKeys )
				{
					ForeignKeyInfoSortedView v = new ForeignKeyInfoSortedView( (IList) d.Value );
					v.ListChanged += new ListChangedEventHandler( OnForeignKeyListChanged );
					nFK.Nodes.Add( (string) d.Key ).Tag = v;
				}
			}

			TreeView.SelectedNode = nTables;
			nTables.Expand();

			TreeView.EndUpdate();
		}

		private void OnForeignKeyListChanged( object sender, ListChangedEventArgs e )
		{
			TreeNode nForeignTable = TreeView.SelectedNode;
			object o = nForeignTable.Tag;
			if ( ! ( o is ForeignKeyInfoSortedView ) ) { Debug.Assert( true ); return; }
			ForeignKeyInfoSortedView v = (ForeignKeyInfoSortedView) o;
			IList l = v.List;
			if ( ! ( l is ForeignKeyInfoCollection ) ) { Debug.Assert( false ); return; }
			ForeignKeyInfoCollection fkic = (ForeignKeyInfoCollection) l;

			if ( fkic.Count > 0 ) return;

			TreeNode nForeignKeys = nForeignTable.Parent;

			TreeNode nPrimaryTable = nForeignKeys.Parent;

			Glob.Report.Metadata.RemoveForeignKeys( nPrimaryTable.Text, nForeignTable.Text );

			bool b = User32.PostMessage( Handle, Glob.WM_DeleteHangingForeignKey, IntPtr.Zero, IntPtr.Zero );
			Debug.Assert( b );
		}

		private void OnDeleteHangingForeignKey()
		{
			TreeNode nForeignTable = TreeView.SelectedNode;
			object o = nForeignTable.Tag;
			if ( ! ( o is ForeignKeyInfoSortedView ) ) { Debug.Assert( true ); return; }
			ForeignKeyInfoSortedView v = (ForeignKeyInfoSortedView) o;
			IList l = v.List;
			if ( ! ( l is ForeignKeyInfoCollection ) ) { Debug.Assert( false ); return; }
			ForeignKeyInfoCollection fkic = (ForeignKeyInfoCollection) l;

			if ( fkic.Count > 0 ) return;

			TreeNode nForeignKeys = nForeignTable.Parent;

			TreeNode nPrimaryTable = nForeignKeys.Parent;

			nForeignKeys.Nodes.Remove( nForeignTable );

			TreeView.SelectedNode = nForeignKeys;
		}

		private void OnTreeViewSelect()
		{
			object o = TreeView.SelectedNode.Tag;
			DataGrid.SetDataBinding( o, null );
		}

//-----------------------------------------------------------------------------

	}

//-----------------------------------------------------------------------------

	public class ColumnInfoSortedView : SortedView
	{
		public ColumnInfoSortedView( IList list ) : base ( list ) {}
	}

	public class PrimaryKeyInfoSortedView : SortedView
	{
		public PrimaryKeyInfoSortedView( IList list ) : base ( list ) {}
	}

	public class ForeignKeyInfoSortedView : SortedView
	{
		public ForeignKeyInfoSortedView( IList list ) : base ( list ) {}
	}

//-----------------------------------------------------------------------------

}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United Kingdom United Kingdom
I discovered C# and .NET 1.0 Beta 1 in late 2000 and loved them immediately.
I have been writing software professionally in C# ever since

In real life, I have spent 3 years travelling abroad,
I have held a UK Private Pilots Licence for 20 years,
and I am a PADI Divemaster.

I now live near idyllic Bournemouth in England.

I can work 'virtually' anywhere!

Comments and Discussions