PropertyBasedSaveConfigWidget.cs :  » GUI » Paint.net » PaintDotNet » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » GUI » Paint.net 
Paint.net » PaintDotNet » PropertyBasedSaveConfigWidget.cs
/////////////////////////////////////////////////////////////////////////////////
// Paint.NET                                                                   //
// Copyright (C) dotPDN LLC, Rick Brewster, Tom Jackson, and contributors.     //
// Portions Copyright (C) Microsoft Corporation. All Rights Reserved.          //
// See src/Resources/Files/License.txt for full licensing and attribution      //
// details.                                                                    //
// .                                                                           //
/////////////////////////////////////////////////////////////////////////////////

using PaintDotNet.IndirectUI;
using PaintDotNet.PropertySystem;
using System;
using System.Windows.Forms;

namespace PaintDotNet{
    public sealed class PropertyBasedSaveConfigWidget
        : SaveConfigWidget<PropertyBasedFileType, PropertyBasedSaveConfigToken>
    {
        private PropertyCollection originalProps;
        private ControlInfo configUI;
        private Control configUIControl;

        protected override void InitWidgetFromToken(PropertyBasedSaveConfigToken sourceToken)
        {
            foreach (string propertyName in sourceToken.PropertyNames)
            {
                Property srcProperty = sourceToken.GetProperty(propertyName);
                PropertyControlInfo dstPropertyControlInfo = this.configUI.FindControlForPropertyName(propertyName);

                if (dstPropertyControlInfo != null)
                {
                    Property dstProperty = dstPropertyControlInfo.Property;

                    if (dstProperty.ReadOnly)
                    {
                        dstProperty.ReadOnly = false;
                        dstProperty.Value = srcProperty.Value;
                        dstProperty.ReadOnly = true;
                    }
                    else
                    {
                        dstProperty.Value = srcProperty.Value;
                    }
                }
            }
        }

        protected override PropertyBasedSaveConfigToken CreateTokenFromWidget()
        {
            PropertyCollection props = this.originalProps.Clone();

            foreach (string propertyName in props.PropertyNames)
            {
                PropertyControlInfo srcPropertyControlInfo = this.configUI.FindControlForPropertyName(propertyName);

                if (srcPropertyControlInfo != null)
                {
                    Property srcProperty = srcPropertyControlInfo.Property;
                    Property dstProperty = props[propertyName];

                    if (dstProperty.ReadOnly)
                    {
                        dstProperty.ReadOnly = false;
                        dstProperty.Value = srcProperty.Value;
                        dstProperty.ReadOnly = true;
                    }
                    else
                    {
                        dstProperty.Value = srcProperty.Value;
                    }
                }
            }

            PropertyBasedSaveConfigToken pbsct = new PropertyBasedSaveConfigToken(props);

            return pbsct;
        }

        public PropertyBasedSaveConfigWidget(PropertyBasedFileType fileType, PropertyCollection props, ControlInfo configUI)
            : base(fileType)
        {
            this.originalProps = props.Clone();
            this.configUI = configUI.Clone();

            // Make sure that the properties in props and configUI are not the same objects
            foreach (Property property in props)
            {
                PropertyControlInfo pci = this.configUI.FindControlForPropertyName(property.Name);

                if (pci != null)
                {
                    if (object.ReferenceEquals(property, pci.Property))
                    {
                        throw new ArgumentException("Property references in propertyCollection must not be the same as those in configUI");
                    }
                }
            }

            SuspendLayout();

            this.configUIControl = (Control)this.configUI.CreateConcreteControl(this);
            this.configUIControl.SuspendLayout();

            this.configUIControl.TabIndex = 0;

            // Set up data binding
            foreach (Property property in this.originalProps)
            {
                PropertyControlInfo pci = this.configUI.FindControlForPropertyName(property.Name);

                if (pci == null)
                {
                    throw new InvalidOperationException("Every property must have a control associated with it");
                }
                else
                {
                    Property controlsProperty = pci.Property;

                    // ASSUMPTION: We assume that the concrete WinForms Control holds a reference to
                    //             the same Property instance as the ControlInfo it was created from.

                    controlsProperty.ValueChanged += ControlsProperty_ValueChanged;
                }
            }

            this.Controls.Add(this.configUIControl);

            this.configUIControl.ResumeLayout(false);
            ResumeLayout(false);
            PerformLayout();
        }

        protected override void OnLayout(LayoutEventArgs e)
        {
            this.configUIControl.Width = this.ClientSize.Width;
            this.configUIControl.PerformLayout();
            this.ClientSize = this.configUIControl.Size;
            base.OnLayout(e);
        }

        private void ControlsProperty_ValueChanged(object sender, EventArgs e)
        {
            UpdateToken();
        }
    }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.