/src/Core/IndirectUI/ControlInfo.cs
C# | 149 lines | 114 code | 27 blank | 8 comment | 14 complexity | e4cde36e12e3e35b2830f6f3b6a763c8 MD5 | raw file
- /////////////////////////////////////////////////////////////////////////////////
- // Paint.NET //
- // Copyright (C) Rick Brewster, Tom Jackson, and past contributors. //
- // Portions Copyright (C) Microsoft Corporation. All Rights Reserved. //
- // See src/Resources/Files/License.txt for full licensing and attribution //
- // details. //
- // . //
- /////////////////////////////////////////////////////////////////////////////////
-
- using System.Linq;
- using PaintDotNet.Base;
- using PaintDotNet.Base.PropertySystem;
- using System;
- using System.Collections.Generic;
- using System.Windows.Forms;
-
- namespace PaintDotNet.IndirectUI
- {
- public abstract class ControlInfo
- : ICloneable<ControlInfo>
- {
- private PropertyCollection _controlProperties;
- private readonly List<ControlInfo> _childControls = new List<ControlInfo>();
-
- public PropertyCollection ControlProperties
- {
- get
- {
- return _controlProperties;
- }
-
- protected set
- {
- _controlProperties = value.Clone();
- }
- }
-
- public IList<ControlInfo> ChildControls
- {
- get
- {
- return new List<ControlInfo>(_childControls);
- }
- }
-
- private static PropertyControlInfo FindControlForPropertyName(object propertyName, ControlInfo control)
- {
- var asPCI = control as PropertyControlInfo;
-
- if (asPCI != null && asPCI.Property.Name == propertyName.ToString())
- {
- return asPCI;
- }
- return control.GetChildControlsCore().Select(childControl => FindControlForPropertyName(propertyName, childControl)).FirstOrDefault(pci => pci != null);
- }
-
- public PropertyControlInfo FindControlForPropertyName(object propertyName)
- {
- return FindControlForPropertyName(propertyName, this);
- }
-
- public bool SetPropertyControlValue(object propertyName, object controlPropertyName, object propertyValue)
- {
- PropertyControlInfo pci = FindControlForPropertyName(propertyName);
-
- if (pci == null)
- {
- return false;
- }
-
- Property prop = pci.ControlProperties[controlPropertyName];
-
- if (prop == null)
- {
- return false;
- }
-
- prop.Value = propertyValue;
- return true;
- }
-
- public bool SetPropertyControlType(object propertyName, PropertyControlType newControlType)
- {
- PropertyControlInfo pci = FindControlForPropertyName(propertyName);
-
- if (pci == null)
- {
- return false;
- }
-
- if (-1 == Array.IndexOf(pci.ControlType.ValueChoices, newControlType))
- {
- return false;
- }
-
- pci.ControlType.Value = newControlType;
- return true;
- }
-
- protected List<ControlInfo> GetChildControlsCore()
- {
- return _childControls;
- }
-
- internal ControlInfo()
- {
- _controlProperties = new PropertyCollection(new Property[0], new PropertyCollectionRule[0]);
- }
-
- internal ControlInfo(ICloneable<PropertyCollection> controlProperties)
- {
- _controlProperties = controlProperties.Clone();
- }
-
- internal ControlInfo(ControlInfo cloneMe)
- {
- _controlProperties = cloneMe._controlProperties.Clone();
- _childControls = new List<ControlInfo>(cloneMe._childControls.Count);
-
- foreach (ControlInfo ciClone in cloneMe._childControls.Select(ci => ci.Clone()))
- {
- _childControls.Add(ciClone);
- }
- }
-
- public object CreateConcreteControl(object uiContainer)
- {
- return CreateConcreteControl(uiContainer.GetType());
- }
-
- public object CreateConcreteControl(Type uiContainerType)
- {
- if (typeof(Control).IsAssignableFrom(uiContainerType))
- {
- return CreateWinFormsControl();
- }
- throw new ArgumentException("uiContainerType is not from a supported UI technology");
- }
-
- internal abstract Control CreateWinFormsControl();
-
- public abstract ControlInfo Clone();
-
- object ICloneable.Clone()
- {
- return Clone();
- }
- }
- }