using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.ComponentModel; using CaeGlobals; using DynamicTypeDescriptor; using System.Diagnostics; namespace CPSO { [Serializable] public class ViewMultiRegion { // Variables private IMultiRegion _multiRegion; private Dictionary _regionTypePropertyNamePairs; private Dictionary _regionNameRegionTypePairs; private DynamicCustomTypeDescriptor _dctd = null; // Properties [Browsable(false)] public virtual string RegionType { // This function returns strings because using enum RegionType shows all items in the drop down box get { if (_multiRegion == null) return ""; if (_regionTypePropertyNamePairs.ContainsKey(_multiRegion.RegionType)) return _multiRegion.RegionType.ToFriendlyString(); else if (_multiRegion.RegionType == RegionTypeEnum.None) { if (Debugger.IsAttached) MessageBoxes.ShowWarning("ViewMultiRegion: The region does not exist!"); return _multiRegion.RegionType.ToFriendlyString(); } else throw new NotSupportedException(); } set { _multiRegion.RegionType = _regionNameRegionTypePairs[value]; CustomPropertyDescriptor cpd = _dctd.GetProperty(_regionTypePropertyNamePairs[_multiRegion.RegionType]); _multiRegion.RegionName = cpd.StatandardValues.First().ToString(); // UpdateRegionVisibility(); } } [Browsable(false)] public DynamicCustomTypeDescriptor DynamicCustomTypeDescriptor { get { return _dctd; } set { _dctd = value; } } // Constructors public ViewMultiRegion() { } // Methods public void SetBase(IMultiRegion multiRegion, Dictionary regionTypePropertyNamePairs) { _multiRegion = multiRegion; _regionTypePropertyNamePairs = regionTypePropertyNamePairs; // _regionNameRegionTypePairs = new Dictionary(); // if (_regionTypePropertyNamePairs != null) { foreach (var entry in _regionTypePropertyNamePairs) _regionNameRegionTypePairs.Add(entry.Key.ToFriendlyString(), entry.Key); } } public void PopulateDropDownLists(Dictionary regionTypeListItemsPairs) { foreach (var entry in regionTypeListItemsPairs) _dctd.PopulateProperty(_regionTypePropertyNamePairs[entry.Key], entry.Value); // Prepare the region drop down list PopulateDropDownListForRegionType(regionTypeListItemsPairs); // Update visible drop down lists UpdateRegionVisibility(); } private void PopulateDropDownListForRegionType(Dictionary regionTypeListItemsPairs) { List types = new List(); bool selection = false; foreach (var entry in regionTypeListItemsPairs) { if (entry.Value.Length > 0) types.Add(entry.Key.ToFriendlyString()); if (entry.Key == RegionTypeEnum.Selection) selection = true; } // int numOfItemsToBeBrowsable = 2; if (selection) numOfItemsToBeBrowsable = 1; _dctd.PopulateProperty(nameof(RegionType), types.ToArray(), false, numOfItemsToBeBrowsable); } public virtual void UpdateRegionVisibility() { bool browsable; foreach (var entry in _regionTypePropertyNamePairs) { browsable = _multiRegion.RegionType == entry.Key; _dctd.GetProperty(entry.Value).SetIsBrowsable(browsable); } } } }