aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/FSE/Modules/Tango.FSE.Procedures/Dialogs/OpenFileDialogView.xaml
blob: 21584416c7efb3d930a4c16721ee03458445cb83 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
<UserControl x:Class="Tango.FSE.Procedures.Dialogs.OpenFileDialogView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:controls="clr-namespace:Tango.FSE.Common.Controls;assembly=Tango.FSE.Common"
             xmlns:material="http://materialdesigninxaml.net/winfx/xaml/themes"
             xmlns:local="clr-namespace:Tango.FSE.Procedures.Dialogs"
             mc:Ignorable="d" 
             Width="600" Height="200" d:DataContext="{d:DesignInstance Type=local:OpenFileDialogViewVM, IsDesignTimeCreatable=False}" Background="{StaticResource FSE_PrimaryBackgroundLightBrush}" Foreground="{StaticResource FSE_PrimaryForegroundBrush}">
    <Grid Margin="10">
        <DockPanel>
            <StackPanel DockPanel.Dock="Top" Orientation="Horizontal">
                <material:PackIcon Kind="FileDocumentBoxPlus" Width="32" Height="32" />
                <TextBlock Margin="10 0 0 0" FontSize="{StaticResource FSE_LargeFontSize}" VerticalAlignment="Center">Open File</TextBlock>
            </StackPanel>

            <StackPanel Margin="0 10 0 0">
                <TextBlock Text="{Binding Message}"></TextBlock>

                <DockPanel Margin="0 5 0 0">
                    <controls:IconButton Command="{Binding BrowseCommand}" Margin="5 0 0 0" DockPanel.Dock="Right" Icon="FolderOpen" />
                    <TextBox material:HintAssist.Hint="Selected File" material:HintAssist.IsFloating="True" Text="{Binding SelectedFile}" IsReadOnly="True"></TextBox>
                </DockPanel>
            </StackPanel>
        </DockPanel>
    </Grid>
</UserControl>
ric.Output */ .highlight .gp { color: #555555 } /* Generic.Prompt */ .highlight .gs { font-weight: bold } /* Generic.Strong */ .highlight .gu { color: #666666 } /* Generic.Subheading */ .highlight .gt { color: #aa0000 } /* Generic.Traceback */ .highlight .kc { color: #008800; font-weight: bold } /* Keyword.Constant */ .highlight .kd { color: #008800; font-weight: bold } /* Keyword.Declaration */ .highlight .kn { color: #008800; font-weight: bold } /* Keyword.Namespace */ .highlight .kp { color: #008800 } /* Keyword.Pseudo */ .highlight .kr { color: #008800; font-weight: bold } /* Keyword.Reserved */ .highlight .kt { color: #888888; font-weight: bold } /* Keyword.Type */ .highlight .m { color: #0000DD; font-weight: bold } /* Literal.Number */ .highlight .s { color: #dd2200; background-color: #fff0f0 } /* Literal.String */ .highlight .na { color: #336699 } /* Name.Attribute */ .highlight .nb { color: #003388 } /* Name.Builtin */ .highlight .nc { color: #bb0066; font-weight: bold } /* Name.Class */ .highlight .no { color: #003366; font-weight: bold } /* Name.Constant */ .highlight .nd { color: #555555 } /* Name.Decorator */ .highlight .ne { color: #bb0066; font-weight: bold } /* Name.Exception */ .highlight .nf { color: #0066bb; font-weight: bold } /* Name.Function */ .highlight .nl { color: #336699; font-style: italic } /* Name.Label */ .highlight .nn { color: #bb0066; font-weight: bold } /* Name.Namespace */ .highlight .py { color: #336699; font-weight: bold } /* Name.Property */ .highlight .nt { color: #bb0066; font-weight: bold } /* Name.Tag */ .highlight .nv { color: #336699 } /* Name.Variable */ .highlight .ow { color: #008800 } /* Operator.Word */ .highlight .w { color: #bbbbbb } /* Text.Whitespace */ .highlight .mb { color: #0000DD; font-weight: bold } /* Literal.Number.Bin */ .highlight .mf { color: #0000DD; font-weight: bold } /* Literal.Number.Float */ .highlight .mh { color: #0000DD; font-weight: bold } /* Literal.Number.Hex */ .highlight .mi { color: #0000DD; font-weight: bold } /* Literal.Number.Integer */ .highlight .mo { color: #0000DD; font-weight: bold } /* Literal.Number.Oct */ .highlight .sa { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Affix */ .highlight .sb { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Backtick */ .highlight .sc { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Char */ .highlight .dl { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Delimiter */ .highlight .sd { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Doc */ .highlight .s2 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Double */ .highlight .se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */ .highlight .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */ .highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */ .highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */ .highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */ .highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */ .highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */ .highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */ .highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */ .highlight .vc { color: #336699 } /* Name.Variable.Class */ .highlight .vg { color: #dd7700 } /* Name.Variable.Global */ .highlight .vi { color: #3333bb } /* Name.Variable.Instance */ .highlight .vm { color: #336699 } /* Name.Variable.Magic */ .highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using Tango.Core;
using Tango.FSE.Common;

namespace Tango.FSE.Procedures.Dialogs
{
    public class UserInputDialogViewVM : FSEDialogViewVM
    {
        public class Parameter
        {
            public PropertyInfo PropertyInfo { get; set; }
            public FieldInfo FieldInfo { get; set; }
            public String Name { get; set; }
            public String Value { get; set; }

            public String TypeName
            {
                get
                {
                    if (PropertyInfo != null)
                    {
                        return PropertyInfo.PropertyType.Name;
                    }
                    else if (FieldInfo != null)
                    {
                        return FieldInfo.FieldType.Name;
                    }

                    return null;
                }
            }

            public Type Type
            {
                get
                {
                    if (PropertyInfo != null)
                    {
                        return PropertyInfo.PropertyType;
                    }
                    else if (FieldInfo != null)
                    {
                        return FieldInfo.FieldType;
                    }
                    else
                    {
                        return null;
                    }
                }
            }

            public bool IsEnum
            {
                get { return Type.IsEnum; }
            }

            public void SetValue(Object obj, Object value)
            {
                if (PropertyInfo != null)
                {
                    PropertyInfo.SetValue(obj, value);
                }
                else if (FieldInfo != null)
                {
                    FieldInfo.SetValue(obj, value);
                }
            }

            public Object GetValue(Object obj)
            {
                if (PropertyInfo != null)
                {
                    return PropertyInfo.GetValue(obj);
                }
                else if (FieldInfo != null)
                {
                    return FieldInfo.GetValue(obj);
                }

                return null;
            }
        }

        public String Title { get; set; }
        public String Message { get; set; }
        public List<Parameter> Parameters { get; set; }
        public Object Model { get; set; }

        public UserInputDialogViewVM(String title, String message, Object model)
        {
            CanClose = false;
            Title = title;
            Message = message;
            Parameters = new List<Parameter>();
            Model = model;
        }

        public void Init()
        {
            try
            {
                if (Model.GetType().IsValueTypeOrString())
                {
                    Parameter p = new Parameter();
                    p.Value = Model.ToStringSafe();
                    p.Name = "Value";
                    Parameters.Add(p);
                }
                else
                {
                    foreach (var prop in Model.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public))
                    {
                        var att = prop.GetCustomAttribute<UserInput>();

                        Parameter p = new Parameter();
                        p.PropertyInfo = prop;
                        p.Name = att != null ? att.Name : prop.Name.ToTitle();

                        if (typeof(IEnumerable).IsAssignableFrom(prop.PropertyType) && prop.PropertyType != typeof(String))
                        {
                            List<String> objects = new List<string>();

                            foreach (var item in prop.GetValue(Model) as IEnumerable)
                            {
                                objects.Add(item.ToStringSafe());
                            }

                            p.Value = String.Join(",", objects);
                        }
                        else
                        {
                            p.Value = prop.GetValue(Model).ToStringSafe();
                        }
                        Parameters.Add(p);
                    }

                    foreach (var field in Model.GetType().GetFields(BindingFlags.Instance | BindingFlags.Public))
                    {
                        var att = field.GetCustomAttribute<UserInput>();

                        Parameter p = new Parameter();
                        p.FieldInfo = field;
                        p.Name = att != null ? att.Name : field.Name.ToTitle();

                        if (typeof(IEnumerable).IsAssignableFrom(field.FieldType) && field.FieldType != typeof(String))
                        {
                            List<String> objects = new List<string>();

                            foreach (var item in field.GetValue(Model) as IEnumerable)
                            {
                                objects.Add(item.ToStringSafe());
                            }

                            p.Value = String.Join(",", objects);
                        }
                        else
                        {
                            p.Value = field.GetValue(Model).ToStringSafe();
                        }
                        Parameters.Add(p);
                    }
                }
            }
            catch (Exception ex)
            {
                throw new Exception("Error initializing the user input model.", ex);
            }
        }

        public void FinalizeModel()
        {
            if (Model.GetType().IsValueTypeOrString())
            {
                Model = Convert.ChangeType(Parameters.First().Value, Model.GetType());
            }
            else
            {
                foreach (var p in Parameters)
                {
                    try
                    {
                        if (typeof(IEnumerable).IsAssignableFrom(p.Type) && p.Type != typeof(String))
                        {
                            List<String> items = p.Value.Split(',').ToList();

                            Type genericType = p.Type.GenericTypeArguments[0];
                            IList list = p.GetValue(Model) as IList;
                            list.Clear();

                            foreach (var item in items)
                            {
                                list.Add(Convert.ChangeType(item, genericType));
                            }
                        }
                        else
                        {
                            if (p.Type.IsEnum)
                            {
                                var enumValue = Enum.Parse(p.Type, p.Value.ToString());
                                p.SetValue(Model, enumValue);
                            }
                            else
                            {
                                p.SetValue(Model, Convert.ChangeType(p.Value, p.Type));
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        throw new ArgumentException($"Error setting the value '{p.Value}' for parameter '{p.Name}' of type '{p.Type.Name}'.", ex);
                    }
                }
            }
        }
    }
}