Merge pull request #2687 from lambdageek/dev/monoerror-mono_param_get_objects_internal
[mono.git] / mcs / class / System.Design / System.ComponentModel.Design / DesignerHost.cs
index 7ff7206cc754f6cb4dd6e8b8ea0fc335b64fda8e..11852dc4bac8824b45de004f0294c6d8d7ffb8f1 100644 (file)
 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 //
 
-#if NET_2_0
 
 using System;
 using System.Collections;
 using System.ComponentModel;
 using System.ComponentModel.Design.Serialization;
+using System.Windows.Forms.Design;
 using System.Reflection;
 
 namespace System.ComponentModel.Design
@@ -45,7 +45,13 @@ namespace System.ComponentModel.Design
 
 
 #region DesignerHostTransaction : DesignerTransaction
-               
+
+               private enum TransactionAction
+               {
+                       Commit,
+                       Cancel
+               }
+
                private sealed class DesignerHostTransaction : DesignerTransaction
                {
                        
@@ -58,14 +64,14 @@ namespace System.ComponentModel.Design
 
                        protected override void OnCancel ()
                        {
-                               _designerHost.OnTransactionClosing (this, false);
-                               _designerHost.OnTransactionClosed (this, false);
+                               _designerHost.OnTransactionClosing (this, TransactionAction.Cancel);
+                               _designerHost.OnTransactionClosed (this, TransactionAction.Cancel);
                        }
                        
                        protected override void OnCommit ()
                        {
-                               _designerHost.OnTransactionClosing (this, true);
-                               _designerHost.OnTransactionClosed (this, true);
+                               _designerHost.OnTransactionClosing (this, TransactionAction.Commit);
+                               _designerHost.OnTransactionClosed (this, TransactionAction.Commit);
                        }
 
                } // DesignerHostTransaction
@@ -78,7 +84,7 @@ namespace System.ComponentModel.Design
                private Stack _transactions;
                private IServiceContainer _serviceContainer;
                private bool _loading;
-               
+               private bool _unloading;
                public DesignerHost (IServiceProvider serviceProvider)
                {
                        if (serviceProvider == null)
@@ -126,7 +132,12 @@ namespace System.ComponentModel.Design
                                _designers[component] = designer;
                                designer.Initialize (component);
                        } else {
-                               Console.WriteLine ("Unable to load a designer for " + component.GetType ().FullName);
+                               IUIService uiService = GetService (typeof (IUIService)) as IUIService;
+                               if (uiService != null) {
+                                       uiService.ShowError ("Unable to load a designer for component type '" +
+                                                            component.GetType ().Name + "'");
+                               }
+                               this.DestroyComponent (component);
                        }
 
                        // Activate the host and design surface once the root component is added to
@@ -146,14 +157,16 @@ namespace System.ComponentModel.Design
 
                public override void Remove (IComponent component)
                {
+                       DesignerTransaction transaction = this.CreateTransaction ("Remove " + component.Site.Name);
                        RemovePreProcess (component);
                        base.Remove (component);
                        RemovePostProcess (component);
+                       transaction.Commit ();
                }
 
                internal void RemovePreProcess (IComponent component)
                {
-                       if (ComponentRemoving != null)
+                       if (!_unloading && ComponentRemoving != null)
                                ComponentRemoving (this, new ComponentEventArgs (component));
 
                        IDesigner designer = _designers[component] as IDesigner;
@@ -174,7 +187,7 @@ namespace System.ComponentModel.Design
 
                internal void RemovePostProcess (IComponent component)
                {
-                       if (ComponentRemoved != null)
+                       if (!_unloading && ComponentRemoved != null)
                                ComponentRemoved (this, new ComponentEventArgs (component));
                }
 
@@ -342,12 +355,8 @@ namespace System.ComponentModel.Design
                public void DestroyComponent (IComponent component)
                {
                        if (component.Site != null && component.Site.Container == this) {
-                               OnComponentChanging (component, null);
-                               
                                this.Remove (component); // takes care for the designer as well
                                component.Dispose ();
-
-                               OnComponentChanged (component, null, null, null);
                        }
                }
 
@@ -409,26 +418,39 @@ namespace System.ComponentModel.Design
                public event DesignerTransactionCloseEventHandler TransactionClosing;
                public event EventHandler TransactionOpened;
                public event EventHandler TransactionOpening;
-               
-               
-               private void OnTransactionClosing (DesignerHostTransaction raiser, bool commit)
+
+               private void OnTransactionClosing (DesignerHostTransaction raiser, TransactionAction action)
                {
+                       bool commit = false;
                        bool lastTransaction = false;
-                       if (_transactions.Count > 0 && _transactions.Peek() == raiser)
+
+                       if (_transactions.Peek () != raiser)
+                               throw new InvalidOperationException ("Current transaction differs from the one a commit was requested for.");
+
+                       if (_transactions.Count == 1)
                                lastTransaction = true;
-                               
+                       if (action == TransactionAction.Commit)
+                               commit = true;
+
                        if (TransactionClosing != null)
                                TransactionClosing (this, new DesignerTransactionCloseEventArgs (commit, lastTransaction));  
                }
 
-               private void OnTransactionClosed (DesignerHostTransaction raiser, bool commit)
+               private void OnTransactionClosed (DesignerHostTransaction raiser, TransactionAction action)
                {
+                       bool commit = false;
                        bool lastTransaction = false;
-                       if (_transactions.Count > 0 && _transactions.Peek() == raiser) {
+
+                       if (_transactions.Peek () != raiser)
+                               throw new InvalidOperationException ("Current transaction differs from the one a commit was requested for.");
+
+                       if (_transactions.Count == 1)
                                lastTransaction = true;
-                               _transactions.Pop ();
-                       }
-                               
+                       if (action == TransactionAction.Commit)
+                               commit = true;
+
+                       _transactions.Pop ();
+
                        if (TransactionClosed != null)
                                TransactionClosed (this, new DesignerTransactionCloseEventArgs (commit, lastTransaction));
                }
@@ -468,6 +490,7 @@ namespace System.ComponentModel.Design
 
                private void Unload ()
                {
+                       _unloading = true;
                        if (DesignerLoaderHostUnloading != null)
                                DesignerLoaderHostUnloading (this, EventArgs.Empty);
 
@@ -481,6 +504,7 @@ namespace System.ComponentModel.Design
                        
                        if (DesignerLoaderHostUnloaded != null)
                                DesignerLoaderHostUnloaded (this, EventArgs.Empty);
+                       _unloading = false;
                }
                                                
 #endregion
@@ -559,7 +583,9 @@ namespace System.ComponentModel.Design
 
        public new object GetService (Type serviceType)
        {
-               return _serviceProvider.GetService (serviceType);
+               if (_serviceProvider != null)
+                       return _serviceProvider.GetService (serviceType);
+               return null;
        }
                
 #endregion
@@ -567,4 +593,3 @@ namespace System.ComponentModel.Design
                }
 
        }
-#endif