private void MoveItem(int fromIndex, int toIndex, bool select, bool report = true)
        {
            IList list = CurrentList;

            if (list == null)
            {
                return;
            }
            if (fromIndex < 0 || fromIndex >= list.Count)
            {
                return;
            }
            if (toIndex < 0 || toIndex >= list.Count)
            {
                return;
            }

            MoveEditAction action = new MoveEditAction(fromIndex, toIndex, Index.RemoveNext(), Index.RemoveNext());

            Json.DataObject item = (Json.DataObject)list[fromIndex];
            DeleteItem(fromIndex, false, false);
            AddItem(item, toIndex, select, false);

            action.Index1.itemIndex = Index.itemIndex;
            if (report && OnObjectEdited != null)
            {
                OnObjectEdited(this, action);
            }
        }
        private void AddNewItem()
        {
            if (comboBoxList.SelectedIndex < 0)
            {
                return;
            }

            IList list   = CurrentList;
            Type  listOf = GetFirstGenericType(list);

            Json.DataObject obj = CreateInstance(listOf, classSelector);
            if (obj == null)
            {
                return;
            }

            int index = listBoxList.SelectedIndex;

            if (index == -1)
            {
                index = list.Count - 1;
            }
            AddItem(obj, index + 1, true);
            if (nextEditor != null)
            {
                nextEditor.Collapsed = false;
            }
        }
        private void AddRows(Json.DataObject obj, TableLayoutPanel tableLayout, string listPrefix = "")
        {
            tableLayout.RowCount = 0;
            List <Accessor> accessors = FieldAccessor.GetForObject(obj).ToList();

            accessors.AddRange(PropertyAccessor.GetForObject(obj));
            foreach (Accessor accessor in accessors)
            {
                IList list = accessor.Get() as IList;
                if (list != null)
                {
                    Type listOf = GetFirstGenericType(list);
                    if (!typeof(ObjectEditor.Json.DataObject).IsAssignableFrom(listOf))
                    {
                        continue;
                    }

                    comboBoxList.Items.Add(listPrefix + GetHumanReadableField(accessor.GetName()));
                    lists.Add(list);
                }
            }
            foreach (Accessor accessor in accessors)
            {
                if (!IsGenericList(accessor.GetAccessorType()))
                {
                    AddRow(accessor, tableLayout);
                }
            }
        }
        private IWritableContext CreateRootContext()
        {
            DataObjectEditor parent = this;

            while (parent.parent != null)
            {
                parent = parent.parent;
            }
            Json.DataObject  root    = parent.DataObject;
            IWritableContext context = JsonSerializer.createContext(root);

            return(context);
        }
 private Json.DataObject CreateInstance(Type type, ClassSelector selector)
 {
     Json.DataObject obj = null;
     if (type.IsAbstract)
     {
         if (selector.Types == null)
         {
             selector.Types = Constructor.RegisteredTypes.Where((t) =>
                                                                !t.IsAbstract && type.IsAssignableFrom(t));
         }
         selector.StartPosition = FormStartPosition.CenterParent;
         if (selector.ShowDialog() == DialogResult.OK &&
             selector.SelectedType != null)
         {
             obj = Constructor.Construct(selector.SelectedType);
         }
     }
     else
     {
         obj = Constructor.Construct(type);
     }
     return(obj);
 }
        private void AddInlineRow(Accessor accessor, TableLayoutPanel tableLayout)
        {
            Type type = accessor.GetAccessorType();

            if (!typeof(Json.DataObject).IsAssignableFrom(type))
            {
                Console.WriteLine("Cannot have non-DataObject inline fields.");
                return;
            }

            string fieldName = GetHumanReadableField(accessor.GetName());
            string typeName  = GetHumanReadableField(accessor.GetAccessorType().Name);

            GroupBox box = new GroupBox();

            box.Text    = fieldName;
            box.Padding = new Padding(3);
            tableLayout.RowCount++;
            tableLayout.Controls.Add(box, 0, tableLayout.RowCount - 1);
            tableLayout.SetColumnSpan(box, 2);

            TableLayoutPanel innerPanel = new TableLayoutPanel();

            innerPanel.Location = new Point(3, 20);
            innerPanel.ColumnStyles.Add(new ColumnStyle(SizeType.AutoSize));
            innerPanel.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 100));
            innerPanel.RowStyles.Add(new RowStyle());
            box.Controls.Add(innerPanel);

            Json.DataObject obj = accessor.Get() as Json.DataObject;

            if (obj != null)
            {
                AddRows(obj, innerPanel, "[" + fieldName + "] ");
            }

            if (!accessor.IsReadOnly())
            {
                int index = inlineAccessors.Count;
                inlineAccessors.Add(accessor);

                FlowLayoutPanel flp = new FlowLayoutPanel();
                flp.AutoSize = true;
                innerPanel.RowCount++;
                innerPanel.Controls.Add(flp, 0, innerPanel.RowCount - 1);
                innerPanel.SetColumnSpan(flp, 2);

                Button buttonAdd = new Button();
                buttonAdd.Text     = "New " + typeName;
                buttonAdd.AutoSize = true;
                flp.Controls.Add(buttonAdd);

                Button buttonDelete = new Button();
                buttonDelete.Text     = "Delete";
                buttonDelete.AutoSize = true;
                flp.Controls.Add(buttonDelete);


                ClassSelector selector = new ClassSelector();
                buttonAdd.Click += (o, e) =>
                {
                    Json.DataObject nObj = CreateInstance(accessor.GetAccessorType(), selector);
                    if (nObj != null)
                    {
                        if (obj != null)
                        {
                            JsonSerializer.copyFields(obj, nObj);
                        }
                        accessor.Set(nObj);
                        UnloadObject();
                        LoadObject();
                        OnObjectEdited(this, new InlineEditAction(Index, index, obj, nObj));
                    }
                };

                buttonDelete.Click += (o, e) =>
                {
                    accessor.Set(null);
                    UnloadObject();
                    LoadObject();
                    OnObjectEdited(this, new InlineEditAction(Index, index, obj, null));
                };
            }

            innerPanel.Height = innerPanel.PreferredSize.Height;
            box.Height        = innerPanel.Height + 30;
            box.Width         = tableLayout.Width - 10;
        }