using System; using System.Collections; using System.Linq; using System.Text; using System.Windows.Forms; using Inet.Viewer.Data; namespace Inet.Viewer.WinForms.Prompt { /// /// Responsible for creating the necessary panel for the given PromptData object /// public class PromptPanelBuilder { /// /// Creates the Control for adding to the right-hand panel of the prompt dialog /// /// PromptData object for which to create the prompt panel /// Validation Delegate when validation is complete /// Created prompt panel. Will return null if prompt type is unknown. public Control CreateControlFor(PromptData p, ValidationDelegate validationDelegate) { if (p.MultipleAllowed) { PromptControl c1 = null, c2 = null; if (p.DiscreteAllowed) { c1 = CreateControlFor(p, validationDelegate, false); } if (p.RangeAllowed) { c2 = CreateControlFor(p, validationDelegate, true); } return new MultiPromptField(p, c1, c2); } else { return CreateControlFor(p, validationDelegate, p.RangeAllowed); } } /// /// creates the control for the given PromptData object with the given parameters /// /// PromptData object to create the Control for /// validation delegate for validating chosen prompt values /// whether to force the panel to be range /// private PromptControl CreateControlFor(PromptData p, ValidationDelegate validationDelegate, bool isRange) { bool hasDefaults = p.DefaultValues != null && p.DefaultValues.Count > 0; bool hasRangeDefaults = p.DefaultValues != null && p.DefaultValues.Cast().Any(e => e is RangePromptValue); bool hasSingleDefaults = p.DefaultValues != null && p.DefaultValues.Cast().Any(e => !(e is RangePromptValue)); switch (p.Type) { case PromptData.String: case PromptData.Number: case PromptData.Currency: if (isRange) { if (hasDefaults) { return new RangePromptFieldWithDefaultValues(p); } else { return new RangePromptField(p); } } else { if (hasDefaults) { return new SinglePromptFieldWithDefaultValues(p); } else { return new SinglePromptField(p,validationDelegate); } } case PromptData.Boolean: return new SingleBooleanPromptField(p); case PromptData.Date: case PromptData.Datetime: if (isRange) { return new DateRangePromptField(p); } else if (hasDefaults) { return new DefaultValueField(p, new DatePromptField(p, true), false); } else { return new DatePromptField(p, false); } case PromptData.Time: PromptControl ctrl; if (isRange) { ctrl = new TimeRangePromptField(p, validationDelegate, hasDefaults); if (hasRangeDefaults) { ctrl = new DefaultValueField(p, ctrl, true); } } else { ctrl = new TimePromptField(p, validationDelegate, hasDefaults); if (hasSingleDefaults) { ctrl = new DefaultValueField(p, ctrl, false); } } return ctrl; default: return null; } } } }