Example #1
0
        protected override void Bind(ISBSFormField field)
        {
            IConsoleFieldView fieldView = null;

            switch (field)
            {
            case SBSUploadField _:
                fieldView = new ConsoleUploadField(field, this);
                break;

            case SBSEditField _:
                if (field is SBSPasswordEditField)
                {
                    fieldView = new ConsolePasswordField(field, this);
                }
                else
                {
                    fieldView = new ConsoleEditField(field, this);
                }
                break;

            case SBSSectionField _:
                fieldView = new ConsoleSectionField(field, this);
                break;

            case SBSLabelField _:
                fieldView = new ConsoleLabelField(field, this);
                break;

            case SBSComboboxField _:
            case SBSListboxField _:
                fieldView = new ConsoleListboxField(field, this);
                break;

            case SBSButtonField _:
                fieldView = new ConsoleButtonField(field, this);
                break;

            case SBSCheckboxField _:
                fieldView = new ConsoleCheckboxField(field, this);
                break;

            case SBSRadioButtonField _:
                fieldView = new ConsoleRadiobuttonField(field, this, this.FieldViews);
                break;

            case SBSRepeaterField _:
                fieldView = new ConsoleRepeaterField(field, this);
                break;

            default:
                ConsoleHelpers.ColoredWriteLine($"Bind: The field {field.Prompt} of type {field.FieldTypeName} was not recognized", ConsoleColor.Magenta);
                break;
            }

            if (fieldView != null)
            {
                this.FieldViews.Add(fieldView);
            }
        }
Example #2
0
        protected virtual void Render(List <IConsoleFieldView> fields, int idxStart, int idxEnd, int level)
        {
            int idx = idxStart;

            while (!this.ExitRequested && idx <= idxEnd && idx < fields.Count)
            {
                IConsoleFieldView field = fields[idx];
                field.Render();
                this.Logger.Info($"ConsoleFormView: Rendered field {field.SBSFormField.Name} with value {field.SBSFormField.Value}");
                if (field is ConsoleRepeaterField repeater)
                {
                    // The max repeater iterations can be controlled by a global property defined in the App Context
                    int maxIterations = ApplicationContext.MaxRepeaterIterations;

                    while (!repeater.IsEnded && repeater.IterationCount < maxIterations)
                    {
                        this.Render(fields, idx + 1, repeater.EndingIndex, level + 1);
                        repeater.IterationCount++;
                    }
                    idx = repeater.EndingIndex;
                }
                var result = field.PerformActions(idx);
                if (result != null && result.Success && result.NewIndex != idx)
                {
                    idx = result.NewIndex;  // PerformActions may have jumped to a new field
                }
                else
                {
                    idx++;                  // go to next field in the list
                }
            }
        }
Example #3
0
        public override void Render()
        {
            if (!this.CanDisplay)
            {
                return;
            }

            // Ignore any radio button fields that are not the first member of a radio group
            if (!this.IsStartOfRadioGroup())
            {
                return;
            }

            // Scan the list of fields to find the end of the radio group. The end is either a field that is not a radio button or the start of another radio group.
            int idxStart = -1;
            int idxEnd   = -1;

            for (int i = 0; i < this.FieldViews.Count; i++)
            {
                IConsoleFieldView fv = this.FieldViews[i];
                if (fv == this)
                {
                    idxStart = idxEnd = i;
                }
                else if (idxStart >= 0)
                {
                    if (!(fv is ConsoleRadiobuttonField rb) || rb.IsStartOfRadioGroup())
                    {
                        break;
                    }
                    idxEnd = i;
                }
            }

            // Print out the members of the radio group
            for (int i = idxStart; i <= idxEnd; i++)
            {
                this.ColoredOutput($"{i - idxStart + 1}) {this.FieldViews[i].SBSFormField.Prompt}", ConsoleColor.Cyan);
            }

            // Get the input. The inoput should be a number between 1 and the count of radio buttons in the group.
            bool isValid = false;

            while (!isValid)
            {
                string line = this.Input();
                if (!string.IsNullOrEmpty(line))
                {
                    continue;
                }

                if (int.TryParse(line, out int answer))
                {
                    continue;
                }

                if (answer < 1 || answer > idxEnd - idxStart + 1)
                {
                    continue;
                }

                // Get the actual index of the answer within the entire list of fields.
                answer = idxStart + answer - 1;

                this.SBSFormField.Value = this.FieldViews[answer].SBSFormField.Prompt;
                isValid = this.Validate();
            }
        }