+ private class Action
+ {
+ public virtual void Undo (UndoEngine engine)
+ {
+ }
+ }
+
+ private class ComponentRenameAction : Action
+ {
+ private string _oldName;
+ private string _currentName;
+
+ public ComponentRenameAction (string currentName, string oldName)
+ {
+ // Console.WriteLine ("ComponentRenameAction (" + oldName + "): " + currentName);
+ _currentName = currentName;
+ _oldName = oldName;
+ }
+
+ public override void Undo (UndoEngine engine)
+ {
+ // Console.WriteLine ("ComponentRenameAction.Undo (" + _currentName + "): " + _oldName);
+ IDesignerHost host = engine.GetRequiredService (typeof (IDesignerHost)) as IDesignerHost;
+ IComponent component = host.Container.Components[_currentName];
+ component.Site.Name = _oldName;
+ string tmp = _currentName;
+ _currentName = _oldName;
+ _oldName = tmp;
+ }
+ } // ComponentRenameAction
+
+ private class ComponentAddRemoveAction : Action
+ {
+ private string _componentName;
+ private SerializationStore _serializedComponent;
+ private bool _added;
+
+ public ComponentAddRemoveAction (UndoEngine engine, IComponent component, bool added)
+ {
+ if (component == null)
+ throw new ArgumentNullException ("component");
+ // Console.WriteLine ((added ? "Component*Add*RemoveAction" : "ComponentAdd*Remove*Action") +
+ // " (" + component.Site.Name + ")");
+ ComponentSerializationService serializationService = engine.GetRequiredService (
+ typeof (ComponentSerializationService)) as ComponentSerializationService;
+
+ _serializedComponent = serializationService.CreateStore ();
+ serializationService.Serialize (_serializedComponent, component);
+ _serializedComponent.Close ();
+
+ _added = added;
+ _componentName = component.Site.Name;
+ }
+
+ public override void Undo (UndoEngine engine)
+ {
+ IDesignerHost host = engine.GetRequiredService (typeof (IDesignerHost)) as IDesignerHost;
+ if (_added) {
+ // Console.WriteLine ("Component*Add*RemoveAction.Undo (" + _componentName + ")");
+ IComponent component = host.Container.Components[_componentName];
+ if (component != null) // the component might have been destroyed already
+ host.DestroyComponent (component);
+ _added = false;
+ } else {
+ // Console.WriteLine ("ComponentAdd*Remove*Action.Undo (" + _componentName + ")");
+ ComponentSerializationService serializationService = engine.GetRequiredService (
+ typeof (ComponentSerializationService)) as ComponentSerializationService;
+
+ serializationService.DeserializeTo (_serializedComponent, host.Container);
+ _added = true;
+ }
+ }
+ } // ComponentAddRemoveAction
+
+
+ private class ComponentChangeAction : Action
+ {
+ private string _componentName;
+ private MemberDescriptor _member;
+ private IComponent _component;
+ private SerializationStore _afterChange;
+ private SerializationStore _beforeChange;
+
+ public ComponentChangeAction ()
+ {
+ }
+
+ public void SetOriginalState (UndoEngine engine, IComponent component, MemberDescriptor member)
+ {
+ _member = member;
+ _component = component;
+ _componentName = component.Site != null ? component.Site.Name : null;
+ // Console.WriteLine ("ComponentChangeAction.SetOriginalState (" + (_componentName != null ? (_componentName + ".") : "") +
+ // member.Name + "): " +
+ // (((PropertyDescriptor)member).GetValue (component) == null ? "null" :
+ // ((PropertyDescriptor)member).GetValue (component).ToString ()));
+ ComponentSerializationService serializationService = engine.GetRequiredService (
+ typeof (ComponentSerializationService)) as ComponentSerializationService;
+ _beforeChange = serializationService.CreateStore ();
+ serializationService.SerializeMemberAbsolute (_beforeChange, component, member);
+ _beforeChange.Close ();
+ }
+
+
+ public void SetModifiedState (UndoEngine engine, IComponent component, MemberDescriptor member)
+ {
+ // Console.WriteLine ("ComponentChangeAction.SetModifiedState (" + (_componentName != null ? (_componentName + ".") : "") +
+ // member.Name + "): " +
+ // (((PropertyDescriptor)member).GetValue (component) == null ? "null" :
+ // ((PropertyDescriptor)member).GetValue (component).ToString ()));
+ ComponentSerializationService serializationService = engine.GetRequiredService (
+ typeof (ComponentSerializationService)) as ComponentSerializationService;
+ _afterChange = serializationService.CreateStore ();
+ serializationService.SerializeMemberAbsolute (_afterChange, component, member);
+ _afterChange.Close ();
+ }
+
+ public bool IsComplete {
+ get { return (_beforeChange != null && _afterChange != null); }
+ }
+
+ public string ComponentName {
+ get { return _componentName; }
+ }
+
+ public IComponent Component {
+ get { return _component; }
+ }
+
+ public MemberDescriptor Member {
+ get { return _member; }
+ }
+
+ // Reminder: _component might no longer be a valid instance
+ // so one should request a new one.
+ //
+ public override void Undo (UndoEngine engine)
+ {
+ if (_beforeChange == null) {
+ // Console.WriteLine ("ComponentChangeAction.Undo: ERROR: UndoUnit is not complete.");
+ return;
+ }
+
+ // Console.WriteLine ("ComponentChangeAction.Undo (" + _componentName + "." + _member.Name + ")");
+ IDesignerHost host = (IDesignerHost)engine.GetRequiredService (typeof(IDesignerHost));
+ _component = host.Container.Components[_componentName];
+
+ ComponentSerializationService serializationService = engine.GetRequiredService (
+ typeof (ComponentSerializationService)) as ComponentSerializationService;
+ serializationService.DeserializeTo (_beforeChange, host.Container);
+
+ SerializationStore tmp = _beforeChange;
+ _beforeChange = _afterChange;
+ _afterChange = tmp;
+ }
+ } // ComponentChangeAction
+
+ private UndoEngine _engine;
+ private string _name;
+ private bool _closed;
+ private List<Action> _actions;