Ejemplo n.º 1
0
        public void Test_GetIndexerValue()
        {
            var nullArg = ReflectionMethods.GetIndexerValue(null);

            Assert.IsNull(nullArg);

            var noIndexer = ReflectionMethods.GetIndexerValue("TextColor");

            Assert.IsNull(noIndexer);

            var yesIndexer = ReflectionMethods.GetIndexerValue("Rows[555]");

            Assert.AreEqual(555, yesIndexer);
        }
Ejemplo n.º 2
0
        protected override void OnExecute(UIMessageContext ctx)
        {
            if (ctx.Request == null)
            {
                return;
            }

            var req = ctx.Get <GetObjectRequest>();

            if (req == null)
            {
                return;
            }

            var props = GetUIProperties(req.WidgetId, true, req.Path);

            if (!props.Any())
            {
                return;
            }

            var prop = props[0];

            if (prop.UIType.Descriptor.HasFlag(UIPropertyDescriptors.Enumerable))
            {
                if (prop.UIType.Descriptor.HasFlag(UIPropertyDescriptors.Enumerable))
                {
                    // grab index value; if null, return without creating an ObjectResponse.
                    var index = ReflectionMethods.GetIndexerValue(req.Path[0]);
                    if (index == null)
                    {
                        return;
                    }

                    var item = ReflectionMethods.GetItem(prop.Value, index.Value);
                    prop.Value = GetUIProperties(item);
                }
            }

            prop.Path = req.Path?.Union(prop.Path)?.ToArray();
            var cantCast = SetPath(prop.Value, req.Path);

            ctx.SetResponse <ObjectResponse>(res =>
            {
                res.UnknownCondition = cantCast;
                res.Property         = prop;
                res.WidgetId         = req.WidgetId;
                res.ObjectName       = UIProperty.GetLastPath(req.Path);
            });
        }
Ejemplo n.º 3
0
        protected override void OnExecute(UIMessageContext ctx)
        {
            var r = ctx.Get <EditCollectionRequest>();

            if (r == null)
            {
                return;
            }

            var pair = Surface[r.WidgetId];
            var prop = pair?.VisualElement.GetRefProperties(r.Path).FirstOrDefault();

            var target = prop?.GetTargetObject();

            if (target == null)
            {
                return;
            }

            var successful = false;
            var message    = string.Empty;

            if (target.IsKindOf(typeof(IList <>)))
            {
                var colType = target.GetType();
                if (colType == null)
                {
                    return;
                }

                var itemType = ReflectionMethods.GetEnumerableItemType(colType);
                if (itemType == null)
                {
                    return;
                }

                if (r.Type == EditCollectionType.Delete)
                {
                    try
                    {
                        var index = ReflectionMethods.GetIndexerValue(r.Path.Last());
                        if (index == null || index < 0)
                        {
                            return;
                        }

                        colType.GetMethod("RemoveAt").Invoke(target, new[] { (object)index.Value });
                        successful = true;
                    }
                    catch (Exception e)
                    {
                        message    = e.Message;
                        successful = false;
                    }
                }

                if (r.Type == EditCollectionType.Add)
                {
                    try
                    {
                        object newItem;

                        if (colType.GetTypeInfo().IsGenericTypeDefinition)
                        {
                            newItem = Activator.CreateInstance(colType.MakeGenericType(itemType));
                        }
                        else
                        {
                            newItem = Activator.CreateInstance(itemType);
                        }

                        colType.GetMethod("Add").Invoke(target, new[] { newItem });
                        successful = true;
                    }
                    catch (Exception e)
                    {
                        message    = e.Message;
                        successful = false;
                    }
                }
            }

            ctx.SetResponse <EditCollectionResponse>(c =>
            {
                if (successful)
                {
                    c.Suggest <GetWidgetPropertiesRequest>();
                }

                c.WidgetId   = r.WidgetId;
                c.Type       = r.Type;
                c.Successful = successful;
                c.Message    = message;
            });
        }
Ejemplo n.º 4
0
        public static Dialog Create(PropertyEditorModel <object> model, VisualTreeNode treeNode)
        {
            var widgetId     = treeNode.Widget.Id;
            var propertyName = model.Property.PropertyName;
            var pv           = model.Property.XenType.PossibleValues;

            var grid = new PropertyEditorGridView();

            var dlg = new ConnectedDialog
            {
                Title   = $"Edit {model.DisplayName}",
                Padding = AppStyles.WindowPadding,
                Width   = 650,
                Height  = 475
            };

            var collection = new ListBox();

            var footer = new TableLayout(4, 1)
            {
                Padding = new Padding(0, 10, 0, 0),
                Spacing = new Size(5, 5)
            };

            var ok = new Button {
                Text = "Ok"
            };
            var add = new Button {
                Text = "Add"
            };
            var del = new Button {
                Text = "Delete"
            };

            footer.Add(del, 0, 0, false, false);
            footer.Add(add, 1, 0, false, false);
            footer.Add(null, 2, 0, true, false);
            footer.Add(ok, 3, 0, false, false);

            var splitter = new Splitter
            {
                SplitterWidth    = 5,
                FixedPanel       = SplitterFixedPanel.Panel1,
                Panel1           = collection,
                Panel2           = grid,
                RelativePosition = dlg.Width * .35
            };

            var container = new TableLayout(1, 2);

            container.Add(splitter, 0, 0, true, true);
            container.Add(footer, 0, 1, true, false);

            dlg.Content       = container;
            dlg.AbortButton   = ok;
            dlg.DefaultButton = ok;

            ok.Click += (s, e) => { dlg.Close(); };

            dlg.Shown += (sender, args) =>
            {
                ToolboxApp.Log.Trace($"Showing {nameof(ObjectEditDialog)} for {treeNode.DisplayName}.");
            };

            dlg.Closing += (sender, args) =>
            {
                ToolboxApp.Bus.StopListening <CollectionPropertiesReceived>();
                ToolboxApp.Bus.StopListening <EditCollectionResponseReceived>();
                ToolboxApp.Bus.StopListening <ReplacedWidgetCollection>();

                ToolboxApp.Log.Trace($"Closing {nameof(ObjectEditDialog)} for {treeNode.DisplayName}.");
            };

            del.Click += (sender, args) =>
            {
                var item = collection.SelectedValue as ListItem;
                var path = (string)item?.Tag;
                if (string.IsNullOrWhiteSpace(path))
                {
                    return;
                }

                var res = MessageBox.Show(Application.Instance.MainForm,
                                          $"Are you sure you want to remove {path}?",
                                          AppResource.Title, MessageBoxButtons.YesNo, MessageBoxType.Question);

                if (res == DialogResult.Yes)
                {
                    ToolboxApp
                    .Designer
                    .EditCollection(treeNode.Widget.Id, EditCollectionType.Delete, path);
                }
            };

            add.Click += (sender, args) =>
            {
                ToolboxApp
                .AppEvents
                .Designer
                .EditCollection(treeNode.Widget.Id, EditCollectionType.Add, model.Property.PropertyName);
            };

            collection.SelectedIndexChanged += (sender, args) =>
            {
                grid.Clear();
                var item = collection.SelectedValue as ListItem;

                if (item != null)
                {
                    var path = (string)item.Tag;
                    ToolboxApp.Designer.GetObjectProperties(widgetId, path);
                }
            };

            ToolboxApp.AppEvents.Bus.Listen <CollectionPropertiesReceived>(args =>
            {
                var index = ReflectionMethods.GetIndexerValue(args.WidgetName);
                if (index == null)
                {
                    return;
                }

                Application.Instance.Invoke(() =>
                {
                    grid.ShowEditors(treeNode, args.Properties);
                });
            });

            ToolboxApp.AppEvents.Bus.Listen <ReplacedWidgetCollection>(args =>
            {
                Application.Instance.Invoke(() =>
                {
                    var length = GetCollectionCount(args.Widget, propertyName);
                    CreateListItems(collection, propertyName, length);
                });
            });

            ToolboxApp.AppEvents.Bus.Listen <EditCollectionResponseReceived>(args =>
            {
                Application.Instance.Invoke(() =>
                {
                    if (!args.Successful)
                    {
                        MessageBox.Show(Application.Instance.MainForm,
                                        $"There was an error performing the '{args.Type}' operation. Check the log for more information.",
                                        AppResource.Title, MessageBoxButtons.OK, MessageBoxType.Error);

                        ToolboxApp.Log.Error(args.Message);
                    }
                });
            });

            // populate list box
            if (pv != null)
            {
                int length;
                var parsed = int.TryParse(pv[0], out length);

                if (parsed)
                {
                    CreateListItems(collection, propertyName, length);
                }
            }

            return(dlg);
        }