/// <summary>
        /// Shows the FeatureWindow modally, using the specified window as the owner.
        /// </summary>
        /// <param name="owner">The window that owns the FeatureWindow</param>
        /// <returns></returns>
        public static DialogResult ShowFeatureWindow(IWin32Window owner, object sender)
        {
            DialogResult result = DialogResult.Cancel;

            // get the executing window manager
            WindowManager windowManager = WindowManager.GetExecutingInstance();

            // create the features window
            FeatureWindow window = new FeatureWindow();

            // ask the window manager if we can show the window
            if (windowManager.CanShow(window, new object[] {}))
            {
                // ask the window manager to track the window
                windowManager.BeginTrackingLifetime(window, SnapIns.SnapInHostingEngine.WindowKeys.FeaturesWindowKey);

                // build the list of features to display
                FeatureCollectionEventArgs cea = new FeatureCollectionEventArgs();
                FeatureEngine.OnBuildingFeatureList(sender, cea);

                // select the features into the feature window
                window.SelectedFeatures = cea.Features;

                // show the window modally
                result = (owner == null ? window.ShowDialog() : window.ShowDialog(owner));
            }

            // if the result is ok, then something may need to be dealt with
            if (result == DialogResult.OK)
            {
                // grab the checked features
                FeatureCollection features = window.CheckedFeatures;

                // iterate over each feature
                foreach (Feature feature in features)
                {
                    // see if anyone wants to cancel the action on the feature
                    FeatureCancelEventArgs fcea = new FeatureCancelEventArgs(feature, false);
                    FeatureEngine.OnBeforeActionTakenForFeature(sender, fcea);
                    if (!fcea.Cancel)
                    {
                        // take the action on the feature
                        FeatureEventArgs fea = new FeatureEventArgs(feature);
                        FeatureEngine.OnTakeActionForFeature(sender, fea);

                        // notify others that an action has been taken on a feature
                        FeatureEngine.OnAfterActionTakenForFeature(sender, fea);
                    }
                }
            }

            return(result);
        }
 /// <summary>
 /// Raises the AfterActionTakenForFeature event
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="e"></param>
 protected static void OnAfterActionTakenForFeature(object sender, FeatureEventArgs e)
 {
     try
     {
         if (FeatureEngine.AfterActionTakenForFeature != null)
         {
             FeatureEngine.AfterActionTakenForFeature(sender, e);
         }
     }
     catch (System.Exception systemException)
     {
         System.Diagnostics.Trace.WriteLine(systemException);
     }
 }
		/// <summary>
		/// Shows the FeatureWindow modally, using the specified window as the owner.
		/// </summary>
		/// <param name="owner">The window that owns the FeatureWindow</param>
		/// <returns></returns>
		public static DialogResult ShowFeatureWindow(IWin32Window owner, object sender)
		{
			DialogResult result = DialogResult.Cancel;

			// get the executing window manager
			WindowManager windowManager = WindowManager.GetExecutingInstance();

			// create the features window
			FeatureWindow window = new FeatureWindow();

			// ask the window manager if we can show the window
			if (windowManager.CanShow(window, new object[] {}))
			{
				// ask the window manager to track the window
				windowManager.BeginTrackingLifetime(window, SnapIns.SnapInHostingEngine.WindowKeys.FeaturesWindowKey);

				// build the list of features to display
				FeatureCollectionEventArgs cea = new FeatureCollectionEventArgs();			
				FeatureEngine.OnBuildingFeatureList(sender, cea);

				// select the features into the feature window
				window.SelectedFeatures = cea.Features;

				// show the window modally
				result = (owner == null ? window.ShowDialog() : window.ShowDialog(owner));
			}
			
			// if the result is ok, then something may need to be dealt with
			if (result == DialogResult.OK)
			{
				// grab the checked features
				FeatureCollection features = window.CheckedFeatures;

				// iterate over each feature 
				foreach(Feature feature in features)
				{
					// see if anyone wants to cancel the action on the feature
					FeatureCancelEventArgs fcea = new FeatureCancelEventArgs(feature, false);
					FeatureEngine.OnBeforeActionTakenForFeature(sender, fcea);
					if (!fcea.Cancel)
					{
						// take the action on the feature
						FeatureEventArgs fea = new FeatureEventArgs(feature);
						FeatureEngine.OnTakeActionForFeature(sender, fea);
							
						// notify others that an action has been taken on a feature
						FeatureEngine.OnAfterActionTakenForFeature(sender, fea);
					}
				}		
			}

			return result;
		}	
		/// <summary>
		/// Raises the AfterActionTakenForFeature event
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		protected static void OnAfterActionTakenForFeature(object sender, FeatureEventArgs e)
		{
			try
			{
				if (FeatureEngine.AfterActionTakenForFeature != null)
					FeatureEngine.AfterActionTakenForFeature(sender, e);
			}
			catch(System.Exception systemException)
			{
				System.Diagnostics.Trace.WriteLine(systemException);
			}
		}
		private void WindowPositioningEngineSnapIn_AfterActionTakenForFeature(object sender, FeatureEventArgs e)
		{
			if (e.Feature != null)
			{
//				/// is the feature a configuration? if so try and delete the file
//				if (e.Feature.GetType() == typeof(ConfigurationFeature))
//				{
//					ConfigurationFeature cf = e.Feature as ConfigurationFeature;
//					if (cf != null)
//					{
//						switch(cf.ConfigurationName)
//						{
//						case SnapInHostingEngine.COMMON_CONFIGURATION:
//							if (e.Feature.Action == FeatureActions.ResetToDefault)
//							{
//								this.InstallMyCommonOptions();
//								this.ReadMyCommonOptions();
//							}
//							break;
//
//						case SnapInHostingEngine.LOCALUSER_CONFIGURATION:
//							if (e.Feature.Action == FeatureActions.ResetToDefault)
//							{
//								this.InstallMyLocalUserOptions();
//								this.ReadMyLocalUserOptions();
//							}
//							break;
//						};
//					}
//				}
			
				if (e.Feature.GetType() == typeof(WindowPositionFeature))
				{
					WindowPositionFeature wpf = e.Feature as WindowPositionFeature;
					if (wpf != null)
					{
						WindowPositionListener wpl = wpf.Tag as WindowPositionListener;
						IWindowPositioningEngineFeaturable feature = wpl.Target as IWindowPositioningEngineFeaturable;
						wpl.Target.Size = feature.GetDefaultSize();
						wpl.Target.Location = feature.GetDefaultLocation();
						wpl.Target.WindowState = feature.GetDefaultWindowState();						 
					}
				}
			}
		}
		private void WindowPositioningEngineSnapIn_TakeActionForFeature(object sender, FeatureEventArgs e)
		{

		}
//		/// <summary>
//		/// Handles the BeforeActionTakenForFeature event from the FeatureEngine
//		/// </summary>
//		/// <param name="sender"></param>
//		/// <param name="e"></param>
//		internal static void FeatureEngine_BeforeActionTakenForFeature(object sender, FeatureCancelEventArgs e)
//		{
//
//		}

		/// <summary>
		///  Handles the TakeActionForFeature event from the FeatureEngine
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		internal static void OnFeatureEngineTakeActionForFeature(object sender, FeatureEventArgs e)
		{									
			if (e.Feature != null)
			{
				// is the feature a configuration? if so try and delete the file
				if (e.Feature.GetType() == typeof(ConfigurationFeature))
				{
					ConfigurationFeature cf = e.Feature as ConfigurationFeature;
					if (cf != null)
					{
						switch(cf.ConfigurationName)
						{
						case SnapInHostingEngine.DefaultCommonConfigurationName:
							if (e.Feature.Action == FeatureActions.ResetToDefault)
							{
								if (PathCreationEngine.DeleteFile(SnapInHostingEngine.CommonConfigurationFilename, SnapInHostingEngine.GetExecutingInstance(), SnapInHostingEngine.DefaultCommonConfigurationName))
								{																		
									// read or create the common configuration
									XmlConfiguration configuration;									
									ConfigurationEngine.ReadOrCreateConfiguration(_verbose, SnapInHostingEngine.DefaultCommonConfigurationName, SnapInHostingEngine.CommonConfigurationFilename, out configuration, null, new XmlConfigurationEventHandler(SnapInHostingEngine.GetExecutingInstance().OnFormatCommonConfiguration));
									SnapInHostingEngine.GetExecutingInstance()._commonConfiguration = configuration;

									if (SnapInHostingEngine.GetExecutingInstance()._commonConfiguration != null)
									{
										SnapInHostingEngine.GetExecutingInstance()._commonConfiguration.TimeToSave += new EventHandler(SnapInHostingEngine.GetExecutingInstance().OnCommonConfigurationTimeToSave);
									}
								}
							}
							break;

						case SnapInHostingEngine.DefaultLocalUserConfigurationName:
							if (e.Feature.Action == FeatureActions.ResetToDefault)
							{
								if (PathCreationEngine.DeleteFile(SnapInHostingEngine.LocalUserConfigurationFilename, SnapInHostingEngine.GetExecutingInstance(), SnapInHostingEngine.DefaultLocalUserConfigurationName))
								{									
									// read or create the local user configuration, use a place holder object because of referencing issues
									XmlConfiguration configuration;									
									ConfigurationEngine.ReadOrCreateConfiguration(_verbose, SnapInHostingEngine.DefaultLocalUserConfigurationName, SnapInHostingEngine.LocalUserConfigurationFilename, out configuration, null, new XmlConfigurationEventHandler(SnapInHostingEngine.GetExecutingInstance().OnFormatLocalUserConfiguration));									
									SnapInHostingEngine.GetExecutingInstance()._localUserConfiguration = configuration;

									if (SnapInHostingEngine.GetExecutingInstance()._localUserConfiguration != null)
									{
										SnapInHostingEngine.GetExecutingInstance()._localUserConfiguration.TimeToSave += new EventHandler(SnapInHostingEngine.GetExecutingInstance().OnLocalUserConfigurationTimeToSave);
									}
								}
							}
							break;
						};
					}
				}

				if (e.Feature.GetType() == typeof(SnapInFeature))
				{
					SnapInFeature sf = e.Feature as SnapInFeature;
					if (sf != null)
					{						
						SnapInDescriptor descriptor = SnapInHostingEngine.GetExecutingInstance().FindDescriptorByType(sf.Type);
						if (descriptor != null)
						{
							if (e.Feature.Action == FeatureActions.Reinstall)
								SnapInHostingEngine.Install(descriptor, true, null);
						}
					}
				}
			}
		}