Пример #1
0
        private void MessageTreeView_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs <object> e)
        {
            AddItemButton.IsEnabled    = false;
            DeleteItemButton.IsEnabled = false;

            if (sender is TreeView)
            {
                TreeView treeView = sender as TreeView;
                if (treeView.SelectedItem is PbMessage)
                {
                    PbMessage message = treeView.SelectedItem as PbMessage;

                    if (message.Source != null)
                    {
                        Type messageType = message.Source.GetType();

                        if (messageType.IsGenericType)
                        {
                            AddItemButton.IsEnabled = true;
                        }
                        else if (message.Parent != null && (message.Parent is PbMessage))
                        {
                            PbMessage parent = message.Parent as PbMessage;

                            if (parent.Source.GetType().IsGenericType)
                            {
                                DeleteItemButton.IsEnabled = true;
                            }
                        }
                    }

                    PropertyListBox.ItemsSource = message.Properties;
                }
            }
        }
Пример #2
0
        private void AddItemButton_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (MessageTreeView.SelectedItem is PbMessage)
                {
                    PbMessage message = MessageTreeView.SelectedItem as PbMessage;

                    if (message.Source != null)
                    {
                        Type messageType = message.Source.GetType();

                        Type      itemType  = messageType.GetGenericArguments()[0];
                        PbMessage pbMessage = GateWay.CreateMessage(itemType, message.Name.Substring(0, message.Name.Length - 4), message);

                        message.Messages.Add(pbMessage);

                        TreeViewItem tviNew = FindTreeViewItem(MessageTreeView, pbMessage);
                        if (null != tviNew)
                        {
                            tviNew.IsSelected = true;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex);
            }
        }
Пример #3
0
        public static PbMessage CreateMessage(Type messageType, string name, object parent)
        {
            PbMessage _message = new PbMessage()
            {
                Messages   = new ObservableCollection <PbMessage>(),
                Properties = new ObservableCollection <PbProperty>(),
                Name       = name,
                Source     = null,
                Parent     = parent
            };

            try
            {
                if (!messageType.IsGenericType)
                {
                    foreach (PropertyInfo propertyInfo in messageType.GetProperties())
                    {
                        string propertyName = string.Empty;

                        if (propertyInfo.Name.StartsWith("Has") && propertyInfo.PropertyType.Equals(typeof(bool)))
                        {
                            propertyName = propertyInfo.Name.Substring(3);
                        }
                        else if (propertyInfo.Name.EndsWith("Count") && propertyInfo.PropertyType.Equals(typeof(int)))
                        {
                            propertyName = propertyInfo.Name.Substring(0, propertyInfo.Name.Length - 5) + "List";
                        }
                        else
                        {
                            continue;
                        }

                        PropertyInfo _propertyInfo = messageType.GetProperty(propertyName);

                        if (_propertyInfo.PropertyType.IsValueType || _propertyInfo.PropertyType.Equals(typeof(string)))
                        {
                            PbProperty pbProperty = new PbProperty()
                            {
                                Type = _propertyInfo.PropertyType, Name = _propertyInfo.Name, Value = ""
                            };
                            _message.Properties.Add(pbProperty);
                        }
                        else
                        {
                            PbMessage pbMessage = GetMessage(_propertyInfo.PropertyType, _propertyInfo.Name, _message);
                            _message.Messages.Add(pbMessage);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex);
            }

            return(_message);
        }
Пример #4
0
        private void FindTreeView(ItemsControl container)
        {
            if (null == container)
            {
                return;
            }

            if (container.DataContext is PbMessage)
            {
                PbMessage dataContext = (container.DataContext as PbMessage);

                if (dataContext.Parent != null && dataContext.Parent is PbMessage)
                {
                    PbMessage parent = dataContext.Parent as PbMessage;

                    if (parent.Source.GetType().IsGenericType)
                    {
                        if (container is TreeViewItem)
                        {
                            (container as TreeViewItem).IsExpanded = false;
                        }
                    }
                }
            }

            int count = container.Items.Count;

            for (int i = 0; i < count; i++)
            {
                TreeViewItem subContainer = (TreeViewItem)container.ItemContainerGenerator.ContainerFromIndex(i);

                if (null == subContainer)
                {
                    continue;
                }

                FindTreeView(subContainer);
            }

            return;
        }
Пример #5
0
        private void DeleteItemButton_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (MessageTreeView.SelectedItem is PbMessage)
                {
                    PbMessage message = MessageTreeView.SelectedItem as PbMessage;

                    if (message.Parent is PbMessage)
                    {
                        PbMessage parent = message.Parent as PbMessage;

                        parent.Messages.Remove(message);
                    }
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex);
            }
        }
Пример #6
0
        private static object SetMessage(PbMessage pbMessage)
        {
            try
            {
                if (pbMessage is null)
                {
                    return(null);
                }

                object builder = pbMessage.Source.GetType().GetMethod("CreateBuilder", new Type[] { }).Invoke(null, new object[] { });

                foreach (PbProperty pbProperty in pbMessage.Properties)
                {
                    object value = null;

                    if (pbProperty.Type == pbProperty.Value.GetType())
                    {
                        value = pbProperty.Value;
                    }
                    else if (pbProperty.Type.BaseType.Name == "Enum")
                    {
                        value = Enum.Parse(pbProperty.Type, pbProperty.Value.ToString());
                    }
                    else
                    {
                        value = pbProperty.Type.GetMethod("Parse", new Type[] { typeof(string) }).Invoke(null, new object[] { pbProperty.Value });
                    }

                    builder.GetType().GetMethod("Set" + pbProperty.Name, new Type[] { pbProperty.Type }).Invoke(builder, new object[] { value });
                }

                foreach (PbMessage _pbMessage in pbMessage.Messages)
                {
                    if (_pbMessage.Source.GetType().IsGenericType)
                    {
                        Type itemType = _pbMessage.Source.GetType().GetGenericArguments()[0];
                        foreach (PbMessage item in _pbMessage.Messages)
                        {
                            string     methodName = "Add" + _pbMessage.Name.Substring(0, _pbMessage.Name.Length - 4);
                            MethodInfo methodInfo = builder.GetType().GetMethod(methodName, new Type[] { itemType });

                            if (item.Source.GetType().IsValueType || item.Source.GetType().Equals(typeof(string)))
                            {
                                if (item.Properties.Count > 0)
                                {
                                    methodInfo.Invoke(builder, new object[] { item.Properties[0].Value });
                                }
                            }
                            else
                            {
                                methodInfo.Invoke(builder, new object[] { SetMessage(item) });
                            }
                        }
                    }
                    else
                    {
                        builder.GetType().GetMethod("Set" + _pbMessage.Name, new Type[] { _pbMessage.Source.GetType() }).Invoke(builder, new object[] { SetMessage(_pbMessage) });
                    }
                }

                return(builder.GetType().GetMethod("Build").Invoke(builder, new object[] { }));
            }
            catch (Exception ex)
            {
                logger.Error(ex);
            }

            return(null);
        }
Пример #7
0
        private static PbMessage GetMessage(object message, string name, object parent)
        {
            PbMessage _message = new PbMessage()
            {
                Messages   = new ObservableCollection <PbMessage>(),
                Properties = new ObservableCollection <PbProperty>(),
                Name       = name,
                Source     = message,
                Parent     = parent
            };

            try
            {
                Type messageType = message.GetType();

                if (messageType.IsGenericType)
                {
                    int    count        = Convert.ToInt32(messageType.GetProperty("Count").GetValue(message, null));
                    string propertyName = name.Substring(0, name.Length - 4);
                    for (int i = 0; i < count; i++)
                    {
                        PropertyInfo _propertyInfo = messageType.GetProperty("Item");
                        object       value         = _propertyInfo.GetValue(message, new object[] { i });

                        if (_propertyInfo.PropertyType.IsValueType || _propertyInfo.PropertyType.Equals(typeof(string)))
                        {
                            PbMessage pbMessage = new PbMessage()
                            {
                                Messages   = new ObservableCollection <PbMessage>(),
                                Properties = new ObservableCollection <PbProperty>()
                                {
                                    new PbProperty()
                                    {
                                        Type  = _propertyInfo.PropertyType,
                                        Name  = propertyName,
                                        Value = value
                                    }
                                },
                                Name   = propertyName,
                                Source = value,
                                Parent = _message
                            };
                            _message.Messages.Add(pbMessage);
                        }
                        else
                        {
                            PbMessage pbMessage = GetMessage(value, propertyName, _message);
                            _message.Messages.Add(pbMessage);
                        }
                    }
                }
                else
                {
                    foreach (PropertyInfo propertyInfo in messageType.GetProperties())
                    {
                        string propertyName = string.Empty;

                        if (propertyInfo.Name.StartsWith("Has") && propertyInfo.PropertyType.Equals(typeof(bool)))
                        {
                            propertyName = propertyInfo.Name.Substring(3);
                        }
                        else if (propertyInfo.Name.EndsWith("Count") && propertyInfo.PropertyType.Equals(typeof(int)))
                        {
                            propertyName = propertyInfo.Name.Substring(0, propertyInfo.Name.Length - 5) + "List";
                        }
                        else
                        {
                            continue;
                        }

                        PropertyInfo _propertyInfo = messageType.GetProperty(propertyName);

                        if (_propertyInfo.PropertyType.IsValueType || _propertyInfo.PropertyType.Equals(typeof(string)))
                        {
                            PbProperty pbProperty = new PbProperty()
                            {
                                Type = _propertyInfo.PropertyType, Name = _propertyInfo.Name, Value = _propertyInfo.GetValue(message, null)
                            };

                            if (_propertyInfo.PropertyType.IsEnum)
                            {
                                pbProperty.Enum = new List <EnumEntity>();
                                foreach (var value in Enum.GetValues(_propertyInfo.PropertyType))
                                {
                                    pbProperty.Enum.Add(new EnumEntity()
                                    {
                                        Name = value.ToString()
                                    });
                                }
                            }

                            _message.Properties.Add(pbProperty);
                        }
                        else
                        {
                            PbMessage pbMessage = GetMessage(_propertyInfo.GetValue(message, null), _propertyInfo.Name, _message);
                            _message.Messages.Add(pbMessage);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex);
            }

            return(_message);
        }