Example #1
0
        public System.Collections.ICollection Deserialize(object obj)
        {
            // string data = File.ReadAllText("save.txt");
            MemoryStream fs = obj as MemoryStream;

            if (fs != null)
            {
                ComponentSerializationService componentSerializationService = _serviceProvider.GetService(typeof(ComponentSerializationService)) as ComponentSerializationService;

                SerializationStore serializationStore = componentSerializationService.LoadStore(fs);
                ICollection        collection         = componentSerializationService.Deserialize(serializationStore);
                fs.Close();
                return(collection);
            }
            else
            {
                SerializationStore serializationStore = obj as SerializationStore;

                if (serializationStore != null)
                {
                    ComponentSerializationService componentSerializationService = _serviceProvider.GetService(typeof(ComponentSerializationService)) as ComponentSerializationService;
                    ICollection collection = componentSerializationService.Deserialize(serializationStore);
                    return(collection);
                }
            }


            return(new object[] { });
        }
 public System.Collections.ICollection Deserialize ( object serializationData ) {
     SerializationStore serializationStore = serializationData as SerializationStore;
     if ( serializationStore != null ) {
         ComponentSerializationService componentSerializationService = _serviceProvider.GetService ( typeof ( ComponentSerializationService ) ) as ComponentSerializationService;
         ICollection collection = componentSerializationService.Deserialize ( serializationStore );
         return collection;
     }
     return new object[] {};
 }
        /// <summary>
        /// Deserializes the serialization data object and returns a
        /// collection of objects represented by that data.
        /// </summary>
        public ICollection Deserialize(object serializationData)
        {
            IDesignerHost host = (IDesignerHost)this.serviceProvider.GetService(typeof(IDesignerHost));

            if (host != null)
            {
                ComponentSerializationService serializationService = (ComponentSerializationService)serviceProvider.GetService(typeof(ComponentSerializationService));
                return(serializationService.Deserialize((SerializationStore)serializationData, host.Container));
            }
            return(null);
        }
        public ICollection Deserialize(object serializationData)
        {
            SerializationStore serializationStore = serializationData as SerializationStore;

            if (serializationStore != null)
            {
                ComponentSerializationService componentSerializationService = this.serviceProvider.GetService(typeof(ComponentSerializationService)) as ComponentSerializationService;
                return(componentSerializationService.Deserialize(serializationStore));
            }
            return(new object[0]);
        }
Example #5
0
 public static ICollection CopyDragObjects(ICollection objects, IServiceProvider svcProvider)
 {
     if ((objects != null) && (svcProvider != null))
     {
         Cursor current = Cursor.Current;
         try
         {
             Cursor.Current = Cursors.WaitCursor;
             ComponentSerializationService service = svcProvider.GetService(typeof(ComponentSerializationService)) as ComponentSerializationService;
             IDesignerHost host = svcProvider.GetService(typeof(IDesignerHost)) as IDesignerHost;
             if ((service != null) && (host != null))
             {
                 SerializationStore store = null;
                 store = service.CreateStore();
                 foreach (IComponent component in GetCopySelection(objects, host))
                 {
                     service.Serialize(store, component);
                 }
                 store.Close();
                 ICollection is2  = service.Deserialize(store);
                 ArrayList   list = new ArrayList(objects.Count);
                 foreach (IComponent component2 in is2)
                 {
                     Control control = component2 as Control;
                     if ((control != null) && (control.Parent == null))
                     {
                         list.Add(component2);
                     }
                     else if (control == null)
                     {
                         ToolStripItem item = component2 as ToolStripItem;
                         if ((item != null) && (item.GetCurrentParent() == null))
                         {
                             list.Add(component2);
                         }
                     }
                 }
                 return(list);
             }
         }
         finally
         {
             Cursor.Current = current;
         }
     }
     return(null);
 }
Example #6
0
        public static Activity[] DeserializeActivitiesFromDataObject(IServiceProvider serviceProvider, IDataObject dataObj, bool addReference)
        {
            IDesignerHost designerHost = (IDesignerHost)serviceProvider.GetService(typeof(IDesignerHost));

            if (designerHost == null)
            {
                throw new InvalidOperationException("IDesignerHost is missing.");
            }

            if (dataObj == null)
            {
                return new Activity[] { }
            }
            ;

            object      data       = dataObj.GetData(CF_DESIGNER);
            ICollection activities = null;

            if (data is Stream)
            {
                BinaryFormatter formatter = new BinaryFormatter();

                ((Stream)data).Seek(0, SeekOrigin.Begin);
                object serializationData = formatter.Deserialize((Stream)data);
                if (serializationData is SerializationStore)
                {
                    // get component serialization service
                    ComponentSerializationService css = serviceProvider.GetService(typeof(ComponentSerializationService)) as ComponentSerializationService;
                    if (css == null)
                    {
                        throw new Exception("ComponentSerializationService is missing.");
                    }

                    // deserialize data
                    activities = css.Deserialize((SerializationStore)serializationData);
                }
            }
            else
            {
                // Now check for a toolbox item.
                IToolboxService ts = (IToolboxService)serviceProvider.GetService(typeof(IToolboxService));
                if (ts != null && ts.IsSupported(dataObj, designerHost))
                {
                    ToolboxItem toolBoxItem = ts.DeserializeToolboxItem(dataObj, designerHost);
                    if (toolBoxItem != null)
                    {
                        // this will make sure that we add the assembly reference to project
                        if (addReference && toolBoxItem.AssemblyName != null)
                        {
                            ITypeResolutionService trs = serviceProvider.GetService(typeof(ITypeResolutionService)) as ITypeResolutionService;
                            if (trs != null)
                            {
                                trs.ReferenceAssembly(toolBoxItem.AssemblyName);
                            }
                        }

                        ActivityToolboxItem ActivityToolboxItem = toolBoxItem as ActivityToolboxItem;
                        if (addReference && ActivityToolboxItem != null)
                        {
                            activities = ActivityToolboxItem.CreateComponentsWithUI(designerHost);
                        }
                        else
                        {
                            activities = toolBoxItem.CreateComponents(designerHost);
                        }
                    }
                }
            }

            return((activities != null) ? (Activity[])(new ArrayList(activities).ToArray(typeof(Activity))) : new Activity[] { });
        }
Example #7
0
        /// <summary>
        /// Deserializes the serialization data object and returns a
        /// collection of objects represented by that data.
        /// </summary>
        public ICollection Deserialize(object serializationData)
        {
            ComponentSerializationService serializationService = (ComponentSerializationService)serviceProvider.GetService(typeof(ComponentSerializationService));

            return(serializationService.Deserialize((SerializationStore)serializationData));
        }
Example #8
0
        /// <summary>
        /// Used to create copies of the objects that we are dragging in a drag operation
        /// </summary>
        public static ICollection CopyDragObjects(ICollection objects, IServiceProvider svcProvider)
        {
            if (objects == null || svcProvider == null)
            {
                Debug.Fail("Invalid parameter passed to DesignerUtils.CopyObjects.");
                return(null);
            }

            Cursor oldCursor = Cursor.Current;

            try
            {
                Cursor.Current = Cursors.WaitCursor;
                ComponentSerializationService css = svcProvider.GetService(typeof(ComponentSerializationService)) as ComponentSerializationService;
                IDesignerHost host = svcProvider.GetService(typeof(IDesignerHost)) as IDesignerHost;

                Debug.Assert(css != null, "No component serialization service -- we cannot copy the objects");
                Debug.Assert(host != null, "No host -- we cannot copy the objects");
                if (css != null && host != null)
                {
                    SerializationStore store = null;

                    store = css.CreateStore();

                    // Get all the objects, meaning we want the children too
                    ICollection copyObjects = GetCopySelection(objects, host);

                    // The serialization service does not (yet) handle serializing collections
                    foreach (IComponent comp in copyObjects)
                    {
                        css.Serialize(store, comp);
                    }
                    store.Close();
                    copyObjects = css.Deserialize(store);

                    // Now, copyObjects contains a flattened list of all the controls contained in the original drag objects, that's not what we want to return.
                    // We only want to return the root drag objects, so that the caller gets an identical copy - identical in terms of objects.Count
                    ArrayList newObjects = new ArrayList(objects.Count);
                    foreach (IComponent comp in copyObjects)
                    {
                        Control c = comp as Control;
                        if (c != null && c.Parent == null)
                        {
                            newObjects.Add(comp);
                        }
                        else if (c == null)
                        { // this happens when we are dragging a toolstripitem
                            // TODO: Can we remove the ToolStrip specific code?
                            if (comp is ToolStripItem item && item.GetCurrentParent() == null)
                            {
                                newObjects.Add(comp);
                            }
                        }
                    }
                    Debug.Assert(newObjects.Count == objects.Count, "Why is the count of the copied objects not the same?");
                    return(newObjects);
                }
            }
            finally
            {
                Cursor.Current = oldCursor;
            }
            return(null);
        }
Example #9
0
        internal virtual System.Windows.Forms.ToolStripItem MorphCurrentItem(System.Type t)
        {
            System.Windows.Forms.ToolStripItem component = null;
            IDesignerHost host = (IDesignerHost)this.GetService(typeof(IDesignerHost));

            if (host != null)
            {
                DesignerTransaction transaction     = host.CreateTransaction(System.Design.SR.GetString("ToolStripMorphingItemTransaction"));
                ToolStrip           immediateParent = (ToolStrip)this.ImmediateParent;
                if (immediateParent is ToolStripOverflow)
                {
                    immediateParent = this.ToolStripItem.Owner;
                }
                ToolStripMenuItemDesigner designer = null;
                int    index = immediateParent.Items.IndexOf(this.ToolStripItem);
                string name  = this.ToolStripItem.Name;
                System.Windows.Forms.ToolStripItem ownerItem = null;
                if (this.ToolStripItem.IsOnDropDown)
                {
                    ToolStripDropDown down = this.ImmediateParent as ToolStripDropDown;
                    if (down != null)
                    {
                        ownerItem = down.OwnerItem;
                        if (ownerItem != null)
                        {
                            designer = (ToolStripMenuItemDesigner)host.GetDesigner(ownerItem);
                        }
                    }
                }
                try
                {
                    ToolStripDesigner._autoAddNewItems = false;
                    ComponentSerializationService service = this.GetService(typeof(ComponentSerializationService)) as ComponentSerializationService;
                    SerializationStore            store   = null;
                    if (service != null)
                    {
                        store = service.CreateStore();
                        service.Serialize(store, base.Component);
                        SerializationStore    store2        = null;
                        ToolStripDropDownItem toolStripItem = this.ToolStripItem as ToolStripDropDownItem;
                        if ((toolStripItem != null) && typeof(ToolStripDropDownItem).IsAssignableFrom(t))
                        {
                            toolStripItem.HideDropDown();
                            store2 = service.CreateStore();
                            this.SerializeDropDownItems(toolStripItem, ref store2, service);
                            store2.Close();
                        }
                        store.Close();
                        IComponentChangeService service2 = (IComponentChangeService)this.GetService(typeof(IComponentChangeService));
                        if (service2 != null)
                        {
                            if (immediateParent.Site != null)
                            {
                                service2.OnComponentChanging(immediateParent, TypeDescriptor.GetProperties(immediateParent)["Items"]);
                            }
                            else if (ownerItem != null)
                            {
                                service2.OnComponentChanging(ownerItem, TypeDescriptor.GetProperties(ownerItem)["DropDownItems"]);
                                service2.OnComponentChanged(ownerItem, TypeDescriptor.GetProperties(ownerItem)["DropDownItems"], null, null);
                            }
                        }
                        this.FireComponentChanging(toolStripItem);
                        immediateParent.Items.Remove(this.ToolStripItem);
                        host.DestroyComponent(this.ToolStripItem);
                        System.Windows.Forms.ToolStripItem item4 = (System.Windows.Forms.ToolStripItem)host.CreateComponent(t, name);
                        if ((item4 is ToolStripDropDownItem) && (store2 != null))
                        {
                            service.Deserialize(store2);
                        }
                        service.DeserializeTo(store, host.Container, false, true);
                        component = (System.Windows.Forms.ToolStripItem)host.Container.Components[name];
                        if ((component.Image == null) && (component is ToolStripButton))
                        {
                            Image image = null;
                            try
                            {
                                image = new Bitmap(typeof(ToolStripButton), "blank.bmp");
                            }
                            catch (Exception exception)
                            {
                                if (System.Windows.Forms.ClientUtils.IsCriticalException(exception))
                                {
                                    throw;
                                }
                            }
                            PropertyDescriptor descriptor = TypeDescriptor.GetProperties(component)["Image"];
                            if ((descriptor != null) && (image != null))
                            {
                                descriptor.SetValue(component, image);
                            }
                            PropertyDescriptor descriptor2 = TypeDescriptor.GetProperties(component)["DisplayStyle"];
                            if (descriptor2 != null)
                            {
                                descriptor2.SetValue(component, ToolStripItemDisplayStyle.Image);
                            }
                            PropertyDescriptor descriptor3 = TypeDescriptor.GetProperties(component)["ImageTransparentColor"];
                            if (descriptor3 != null)
                            {
                                descriptor3.SetValue(component, Color.Magenta);
                            }
                        }
                        immediateParent.Items.Insert(index, component);
                        if (service2 != null)
                        {
                            if (immediateParent.Site != null)
                            {
                                service2.OnComponentChanged(immediateParent, TypeDescriptor.GetProperties(immediateParent)["Items"], null, null);
                            }
                            else if (ownerItem != null)
                            {
                                service2.OnComponentChanging(ownerItem, TypeDescriptor.GetProperties(ownerItem)["DropDownItems"]);
                                service2.OnComponentChanged(ownerItem, TypeDescriptor.GetProperties(ownerItem)["DropDownItems"], null, null);
                            }
                        }
                        this.FireComponentChanged(toolStripItem);
                        if (component.IsOnDropDown && (designer != null))
                        {
                            designer.RemoveItemBodyGlyph(component);
                            designer.AddItemBodyGlyph(component);
                        }
                        ToolStripDesigner._autoAddNewItems = true;
                        if (component != null)
                        {
                            if (component is ToolStripSeparator)
                            {
                                immediateParent.PerformLayout();
                            }
                            BehaviorService service3 = (BehaviorService)component.Site.GetService(typeof(BehaviorService));
                            if (service3 != null)
                            {
                                service3.Invalidate();
                            }
                            ISelectionService service4 = (ISelectionService)component.Site.GetService(typeof(ISelectionService));
                            if (service4 != null)
                            {
                                service4.SetSelectedComponents(new object[] { component }, SelectionTypes.Replace);
                            }
                        }
                    }
                    return(component);
                }
                catch
                {
                    host.Container.Add(this.ToolStripItem);
                    immediateParent.Items.Insert(index, this.ToolStripItem);
                    if (transaction != null)
                    {
                        transaction.Cancel();
                        transaction = null;
                    }
                }
                finally
                {
                    if (transaction != null)
                    {
                        transaction.Commit();
                        transaction = null;
                    }
                }
            }
            return(component);
        }