示例#1
0
        public void OptionWorkflow_CompareTo()
        {
            var expectedGuid         = Guid.NewGuid();
            var expectedWorkflowName = "WorkflowName";
            var expectedInputs       = new List <IServiceInputBase>();
            var workflow             = new WorkflowWithInputs
            {
                Name   = expectedWorkflowName,
                Value  = expectedGuid,
                Inputs = expectedInputs,
            };
            var optionWorkflow = new OptionWorkflow
            {
                Name     = "Suggestions",
                Workflow = workflow,
            };

            var expectedValue = optionWorkflow.CompareTo(null);

            Assert.AreEqual(-1, expectedValue);

            expectedValue = optionWorkflow.CompareTo(new object { });
            Assert.AreEqual(-1, expectedValue);

            expectedValue = optionWorkflow.CompareTo(optionWorkflow);
            Assert.AreEqual(0, expectedValue);
        }
示例#2
0
        public void OptionWorkflow_Default()
        {
            var optionWorkflow = new OptionWorkflow();

            Assert.IsNull(optionWorkflow.Name);
            Assert.IsNull(optionWorkflow.Workflow);

            optionWorkflow.Name = "Name";
            Assert.AreEqual("Name", optionWorkflow.Name);

            var expectedGuid         = Guid.NewGuid();
            var expectedWorkflowName = "WorkflowName";
            var expectedInputs       = new List <IServiceInputBase>();
            var workflow             = new WorkflowWithInputs
            {
                Name   = expectedWorkflowName,
                Value  = expectedGuid,
                Inputs = expectedInputs,
            };

            optionWorkflow.Workflow = workflow;
            Assert.AreEqual(expectedWorkflowName, optionWorkflow.Workflow.Name);
            Assert.AreEqual(expectedGuid, optionWorkflow.Workflow.Value);
            Assert.IsNotNull(optionWorkflow.Workflow.Inputs);

            Assert.AreEqual("OptionWorkflowHelpText", optionWorkflow.HelpText);
            Assert.AreEqual("OptionWorkflowTooltip", optionWorkflow.Tooltip);
        }
示例#3
0
        public static void Main()
        {
            var workflow = new OptionWorkflow();
            workflow.Calculate();
           

        }
示例#4
0
        public void OptionWorkflow_Clone()
        {
            var expectedGuid         = Guid.NewGuid();
            var expectedWorkflowName = "WorkflowName";
            var expectedInputs       = new List <IServiceInputBase>();
            var workflow             = new WorkflowWithInputs
            {
                Name   = expectedWorkflowName,
                Value  = expectedGuid,
                Inputs = expectedInputs,
            };
            var optionWorkflow = new OptionWorkflow
            {
                Name     = "Suggestions",
                Workflow = workflow,
            };

            var cloneWorkflow = optionWorkflow.Clone() as OptionWorkflow;

            Assert.AreEqual(optionWorkflow.Name, cloneWorkflow.Name);
            Assert.AreEqual(optionWorkflow.Workflow, cloneWorkflow.Workflow);
        }
示例#5
0
        private static IOption PropertyToOption(object instance, PropertyInfo prop, IOptionConvertParameter topLevelInstance)
        {
            var helptextAttr = prop.GetCustomAttributes().Where(o => o is HelpTextAttribute).Cast <HelpTextAttribute>().FirstOrDefault();
            var tooltipAttr  = prop.GetCustomAttributes().Where(o => o is TooltipAttribute).Cast <TooltipAttribute>().FirstOrDefault();

            if (prop.PropertyType.IsAssignableFrom(typeof(string)))
            {
                var attr   = prop.GetCustomAttributes().Where(o => o is DataProviderAttribute).Cast <DataProviderAttribute>().FirstOrDefault();
                var result = new OptionAutocomplete()
                {
                    Name  = prop.Name,
                    Value = (string)prop.GetValue(instance)
                };
                if (attr != null)
                {
                    result.Suggestions = ((IOptionDataList)attr.Get()).Options;
                }
                if (helptextAttr != null)
                {
                    result.HelpText = helptextAttr.Get();
                }
                if (tooltipAttr != null)
                {
                    result.Tooltip = tooltipAttr.Get();
                }
                result.PropertyChanged += (o, e) => { prop.SetValue(instance, ((OptionAutocomplete)o).Value); topLevelInstance?.Notify(); };

                return(result);
            }
            else if (prop.PropertyType.IsAssignableFrom(typeof(int)))
            {
                var optionInt = new OptionInt
                {
                    Name  = prop.Name,
                    Value = (int)prop.GetValue(instance)
                };
                if (helptextAttr != null)
                {
                    optionInt.HelpText = helptextAttr.Get();
                }
                if (tooltipAttr != null)
                {
                    optionInt.Tooltip = tooltipAttr.Get();
                }

                optionInt.PropertyChanged += (o, e) => { prop.SetValue(instance, ((OptionInt)o).Value); topLevelInstance?.Notify(); };
                return(optionInt);
            }
            else if (prop.PropertyType.IsAssignableFrom(typeof(IWorkflow)))
            {
                var workflow       = (IWorkflow)prop.GetValue(instance);
                var optionWorkflow = new OptionWorkflow
                {
                    Name     = prop.Name,
                    Workflow = new WorkflowWithInputs {
                        Name = workflow.Name, Value = workflow.Value, Inputs = workflow.Inputs
                    },
                };
                if (helptextAttr != null)
                {
                    optionWorkflow.HelpText = helptextAttr.Get();
                }
                if (tooltipAttr != null)
                {
                    optionWorkflow.Tooltip = tooltipAttr.Get();
                }
                optionWorkflow.PropertyChanged += (o, e) =>
                {
                    if (e.PropertyName == nameof(OptionWorkflow.Workflow))
                    {
                        prop.SetValue(instance, ((OptionWorkflow)o).Workflow);
                    }
                    topLevelInstance?.Notify();
                };
                return(optionWorkflow);
            }
            else if (prop.PropertyType.IsAssignableFrom(typeof(bool)))
            {
                var optionBool = new OptionBool
                {
                    Name  = prop.Name,
                    Value = (bool)prop.GetValue(instance),
                };
                if (helptextAttr != null)
                {
                    optionBool.HelpText = helptextAttr.Get();
                }
                if (tooltipAttr != null)
                {
                    optionBool.Tooltip = tooltipAttr.Get();
                }
                optionBool.PropertyChanged += (o, e) => { prop.SetValue(instance, ((OptionBool)o).Value); topLevelInstance?.Notify(); };

                return(optionBool);
            }
            else if (prop.PropertyType.IsEnum)
            {
                var values = new List <KeyValuePair <string, int> >();
                foreach (var option in Enum.GetValues(prop.PropertyType))
                {
                    var key = Enum.GetName(prop.PropertyType, option);
                    values.Add(new KeyValuePair <string, int>(key, (int)option));
                }

                var result = new OptionEnum
                {
                    Values = values,
                    Name   = prop.Name,
                    Value  = (int)prop.GetValue(instance),
                };
                if (helptextAttr != null)
                {
                    result.HelpText = helptextAttr.Get();
                }
                if (tooltipAttr != null)
                {
                    result.Tooltip = tooltipAttr.Get();
                }
                result.PropertyChanged += (o, e) => { prop.SetValue(instance, ((OptionEnum)o).Value); topLevelInstance?.Notify(); };

                return(result);
            }
            else
            {
                var attrs            = prop.GetCustomAttributes();
                var optionTypeUXAttr = attrs.Where(o => o is OptionUXAttribute).Cast <OptionUXAttribute>().FirstOrDefault();
                var fieldValueAttr   = attrs.Where(o => o is DataValueAttribute).Cast <DataValueAttribute>().FirstOrDefault();
                var dataProviderAttr = attrs.Where(o => o is MultiDataProviderAttribute).Cast <MultiDataProviderAttribute>().FirstOrDefault();

                if (fieldValueAttr == null)
                {
                    throw new Exception("FieldValueAttribute required");
                }
                var fieldValueName = fieldValueAttr.Get();

                var propertyValue = prop.GetValue(instance);
                var types         = prop.PropertyType.GetProperties();
                var fieldNameProp = types.First(type => type.Name == fieldValueName);
                if (fieldNameProp == null)
                {
                    throw new Exception("property does not exist");
                }
                if (!fieldNameProp.PropertyType.IsEnum)
                {
                    throw new Exception("currently only enums supported");
                }
                if (propertyValue is null)
                {
                    throw NullException;
                }

                IOptionMultiData returnVal;
                var enumValue = fieldNameProp.GetValue(propertyValue);

                Orientation?orientation = null;
                if (optionTypeUXAttr?.Get() == typeof(OptionRadioButtons))
                {
                    var orientationAttr = attrs.Where(o => o is OrientationAttribute).Cast <OrientationAttribute>().FirstOrDefault();
                    if (orientationAttr != null)
                    {
                        orientation = orientationAttr.Get();
                    }
                    returnVal = new OptionRadioButtons
                    {
                        Name  = prop.Name,
                        Value = Enum.GetName(fieldNameProp.PropertyType, enumValue),
                    };
                }
                else
                { // optionTypeUXAttr is OptionCombobox
                    returnVal = new OptionCombobox
                    {
                        Name  = prop.Name,
                        Value = Enum.GetName(fieldNameProp.PropertyType, enumValue),
                    };
                }
                if (helptextAttr != null)
                {
                    returnVal.HelpText = helptextAttr.Get();
                }
                if (tooltipAttr != null)
                {
                    returnVal.Tooltip = tooltipAttr.Get();
                }

                var optionCount = 0;
                if (dataProviderAttr != null)
                {
                    var optionTypes = dataProviderAttr.Get();
                    foreach (var optionType in optionTypes)
                    {
                        object value = propertyValue != null && propertyValue.GetType() == optionType.GetType() ? propertyValue : optionType;

                        var type = optionType.GetType();
                        returnVal.Options[type.Name] = OptionConvertor.Convert(value, topLevelInstance).Where(o => o.Name != fieldValueName);
                    }
                    optionCount = optionTypes.Length;
                }
                if (returnVal is OptionRadioButtons optionRadioButton)
                {
                    var calcOrientation = optionCount == 2 ? Orientation.Horizontal : Orientation.Vertical;
                    optionRadioButton.Orientation = orientation ?? calcOrientation;

                    returnVal.PropertyChanged += (o, e) => { prop.SetValue(instance, ExtractValueFromOptionMultiData(instance, prop, (OptionRadioButtons)o)); topLevelInstance?.Notify(); };
                }
                else
                {
                    returnVal.PropertyChanged += (o, e) => { prop.SetValue(instance, ExtractValueFromOptionMultiData(instance, prop, (OptionCombobox)o)); topLevelInstance?.Notify(); };
                }
                return(returnVal);
            }
            throw UnhandledException;
        }