// 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
#region DesignerHostTransaction : DesignerTransaction
-
+
+ private enum TransactionAction
+ {
+ Commit,
+ Cancel
+ }
+
private sealed class DesignerHostTransaction : DesignerTransaction
{
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
private Stack _transactions;
private IServiceContainer _serviceContainer;
private bool _loading;
-
+ private bool _unloading;
public DesignerHost (IServiceProvider serviceProvider)
{
if (serviceProvider == null)
_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
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;
internal void RemovePostProcess (IComponent component)
{
- if (ComponentRemoved != null)
+ if (!_unloading && ComponentRemoved != null)
ComponentRemoved (this, new ComponentEventArgs (component));
}
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);
}
}
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));
}
private void Unload ()
{
+ _unloading = true;
if (DesignerLoaderHostUnloading != null)
DesignerLoaderHostUnloading (this, EventArgs.Empty);
if (DesignerLoaderHostUnloaded != null)
DesignerLoaderHostUnloaded (this, EventArgs.Empty);
+ _unloading = false;
}
#endregion
public new object GetService (Type serviceType)
{
- return _serviceProvider.GetService (serviceType);
+ if (_serviceProvider != null)
+ return _serviceProvider.GetService (serviceType);
+ return null;
}
#endregion
}
}
-#endif