Click here to Skip to main content
15,895,740 members
Articles / Programming Languages / XML

Event Logging in Cx

Rate me:
Please Sign up or sign in to vote.
4.86/5 (8 votes)
30 Sep 2009CPOL7 min read 34.9K   284   16  
Adding an event logger to Cx.
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Windows.Forms;

using Cx;
using Cx.Attributes;
using Cx.EventArgs;
using Cx.Interfaces;

namespace App
{
	static class Program
	{
		public static Form mainForm;
		public static Form currentForm;

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main()
		{
			Application.EnableVisualStyles();
			Application.SetCompatibleTextRenderingDefault(false);
			EventHelpers.LogEvents = true;

			try
			{
				// Initialize all components.
				CxApp designer = CxApp.Initialize(@"..\..\..\Cx.DataService\bin\debug\Cx.DataService.dll", Path.GetFullPath("cx.xml"));
				EventHelpers.App = designer;

				// Once initialized, get our App instance.	We create an app instance so it is registered in our component list for wireups.
				App app = designer.GetComponent<App>();

				// Now we want to initialize the designer model.  For now, we're passing
				// in the same data service and metadata file, but in a more sophisticated implementation
				// we could let the user pick the metadata file and also the data service associated with that metadata.

				// app.Initialize(@"..\..\..\Cx.DataService\bin\debug\Cx.DataService.dll", Path.GetFullPath("cxdesigner.xml"));

				// Register all controls, including the menubar, which generates the events automatically for the menu items.
				mainForm = new Form1(designer.VisualComponents);
				// Do wireup after the menubar has been registered and menu item events are generated.
				designer.WireUpComponents();
				// Initialize the process.
				// app.Initialize(Path.GetFullPath("addComponent.xml"));
				// Run the app.
				Application.Run(mainForm);
			}
			catch (Exception e)
			{
				MessageBox.Show(e.Message, "Error During Startup", MessageBoxButtons.OK, MessageBoxIcon.Error);
			}
		}

		public static Size UpdateExtents(Form parentForm, ICxVisualComponent comp, Size extents)
		{
			if (((Control)comp.Instance).Parent == parentForm)
			{
				if (!String.IsNullOrEmpty(comp.Location) && !String.IsNullOrEmpty(comp.Size))
				{
					string[] loc = comp.Location.Split(',');
					Point p = new Point(Convert.ToInt32(loc[0].Trim()), Convert.ToInt32(loc[1].Trim()));
					string[] size = comp.Size.Split(',');
					Size s = new Size(Convert.ToInt32(size[0].Trim()), Convert.ToInt32(size[1].Trim()));

					int dx = p.X + s.Width;
					int dy = p.Y + s.Height;

					if (dx > extents.Width)
					{
						extents.Width = dx;
					}

					if (dy > extents.Height)
					{
						extents.Height = dy;
					}
				}
			}

			return extents;
		}
	}

	[CxExplicitEvent("Initialize")]
	[CxExplicitEvent("Save")]
	[CxExplicitEvent("FormInitialized")]
	public class App : ICxBusinessComponentClass
	{
		protected EventHelper initialize;
		protected EventHelper save;
		protected EventHelper formInitialized;

		public string Caption
		{
			get { return Program.currentForm.Text; }
			set { Program.currentForm.Text = value; }
		}

		public App()
		{
			initialize = EventHelpers.CreateEvent<String>(this, "Initialize");
			save = EventHelpers.CreateEvent<string>(this, "Save");
			formInitialized = EventHelpers.CreateEvent(this, "FormInitialized");
		}

		public void Initialize(string configUri)
		{
			initialize.Fire(configUri);
		}

		[CxConsumer]
		public void ShowModalForm(object sender, CxEventArgs<string> args)
		{
			// Create the form first and assign it to the current form so that any
			// property initialization supported by this App class can work with the
			// this new form.
			Form form = new Form();
			Program.currentForm = form;

			using (CxApp dlg = CxApp.Initialize(@"..\..\..\Cx.DataService\bin\debug\Cx.DataService.dll", Path.GetFullPath(args.Data)))
			{
				Size extents = new Size(0, 0);

				foreach (ICxVisualComponent comp in dlg.VisualComponents)
				{
					((ICxVisualComponentClass)comp.Instance).Register(form, comp);
					extents = Program.UpdateExtents(form, comp, extents);
				}

				dlg.WireUpComponents();
				form.Size = extents + new Size(25, 40);
				form.Activated += new EventHandler(OnActivated);
				formInitialized.Fire();
				form.ShowDialog();
				// The dispose method call is critical to remove wireups to components used in this dialog.
			}
		}

		/// <summary>
		/// Saves the last activated form.  See CloseDialog below.
		/// </summary>
		protected void OnActivated(object sender, System.EventArgs e)
		{
			Program.currentForm = (Form)sender;
		}

		[CxConsumer]
		public void CloseDialog(object sender, System.EventArgs args)
		{
			// We use the current form instead of Form.ActiveForm because the latter is null when debugging events, 
			// because VS gets focus and so our app is no longer the current form!
			Program.currentForm.Close();
		}

		[CxConsumer]
		public void ShowSaveAsDialog(object sender, CxEventArgs<string> args)
		{
			SaveFileDialog sfd = new SaveFileDialog();
			sfd.Filter = args.Data;
			DialogResult ret = sfd.ShowDialog();

			if (ret == DialogResult.OK)
			{
				save.Fire(sfd.FileName);
			}
		}

		[CxConsumer]
		public void SetCaption(object sender, CxEventArgs<string> args)
		{
		}
	}
}

/*
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Windows.Forms;

using Cx;
using Cx.Designer;

namespace App
{
	static class Program
	{
		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main()
		{
			Application.EnableVisualStyles();
			Application.SetCompatibleTextRenderingDefault(false);
			CxApp cx = CxApp.Initialize(@"..\..\..\Cx.DataService\bin\debug\Cx.DataService.dll", Path.GetFullPath("cx.xml"));
			Application.Run(new Form1(cx.VisualComponents));
		}
	}
}
*/

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
Architect Interacx
United States United States
Blog: https://marcclifton.wordpress.com/
Home Page: http://www.marcclifton.com
Research: http://www.higherorderprogramming.com/
GitHub: https://github.com/cliftonm

All my life I have been passionate about architecture / software design, as this is the cornerstone to a maintainable and extensible application. As such, I have enjoyed exploring some crazy ideas and discovering that they are not so crazy after all. I also love writing about my ideas and seeing the community response. As a consultant, I've enjoyed working in a wide range of industries such as aerospace, boatyard management, remote sensing, emergency services / data management, and casino operations. I've done a variety of pro-bono work non-profit organizations related to nature conservancy, drug recovery and women's health.

Comments and Discussions