瀏覽代碼

Fix to custom properties

Kenric Nugteren 2 年之前
父節點
當前提交
a3ac0ebaf2
共有 1 個文件被更改,包括 55 次插入42 次删除
  1. 55 42
      InABox.Core/DatabaseSchema/CustomProperty.cs

+ 55 - 42
InABox.Core/DatabaseSchema/CustomProperty.cs

@@ -1,4 +1,5 @@
-using System;
+using Newtonsoft.Json;
+using System;
 using System.Linq;
 using System.Linq.Expressions;
 using System.Runtime.InteropServices;
@@ -9,7 +10,6 @@ namespace InABox.Core
     public class CustomProperty : Entity, IProperty, IPersistent, IRemotable, ISequenceable, ILicense<CoreLicense>
     {
         public Type? _class;
-        private BaseEditor? _editor;
 
         private Func<object, object> _getter;
 
@@ -24,13 +24,18 @@ namespace InABox.Core
         private string _caption;
         private long _sequence;
         private string _page;
+        private bool _visible;
+        private bool _editable;
 
-        public CustomProperty()
+        protected override void Init()
         {
+            base.Init();
+
             Visible = true;
             Editable = true;
             Required = false;
             HasEditor = true;
+            Editor = new NullEditor();
         }
 
         [MemoEditor(Visible = Core.Visible.Default)]
@@ -41,16 +46,24 @@ namespace InABox.Core
         [EditorSequence(8)]
         public bool Visible
         {
-            get => Editor.Visible == Core.Visible.Hidden ? false : true;
-            set => Editor.Visible = value ? Core.Visible.Optional : Core.Visible.Hidden;
+            get => _visible;
+            set
+            {
+                _visible = value;
+                RegenerateEditor();
+            }
         }
 
         [CheckBoxEditor(Visible = Core.Visible.Optional)]
         [EditorSequence(9)]
         public bool Editable
         {
-            get => Editor.Editable == Core.Editable.Enabled;
-            set => Editor.Editable = value ? Core.Editable.Enabled : Core.Editable.Hidden;
+            get => _editable;
+            set
+            {
+                _editable = value;
+                RegenerateEditor();
+            }
         }
 
         [ComboLookupEditor(typeof(PropertyClassLookups))]
@@ -73,6 +86,7 @@ namespace InABox.Core
             {
                 _name = value;
                 CheckExpressions();
+                RegenerateEditor();
             }
         }
 
@@ -93,9 +107,9 @@ namespace InABox.Core
                     type = typeof(object);
                 }
 
-                _editor = EditorUtils.GetEditor(type);
-
                 IsEntityLink = type.GetInterfaces().Contains(typeof(IEntityLink));
+
+                RegenerateEditor();
             }
         }
 
@@ -108,34 +122,21 @@ namespace InABox.Core
             {
                 type = value;
                 _type = value.EntityName();
-                _editor = EditorUtils.GetEditor(type);
+                Editor = null;
                 IsEntityLink = type.GetInterfaces().Contains(typeof(IEntityLink));
+
+                RegenerateEditor();
             }
         }
 
         [NullEditor]
-        public BaseEditor Editor
-        {
-            get
-            {
-                if (_editor == null)
-                {
-                    _editor = EditorUtils.GetEditor(PropertyType);
-                    if (_editor == null)
-                        _editor = new NullEditor();
-                    _editor.Caption = Caption;
-                    _editor.EditorSequence = (int)Sequence;
-                    _editor.Page = Page;
-                    //_editor.Editable = Editable ? Core.Editable.Enabled : Core.Editable.Disabled;
-                    //_editor.Visible = Visible ? Core.Visible.Optional : Core.Visible.Disabled;
-                }
-
-                return _editor;
-            }
-            set => _editor = value;
-        }
+        [DoNotPersist]
+        [DoNotSerialize]
+        public BaseEditor Editor { get; set; }
 
         [NullEditor]
+        [DoNotPersist]
+        [DoNotSerialize]
         public bool HasEditor { get; set; }
 
         [TextBoxEditor(Visible = Core.Visible.Optional)]
@@ -146,10 +147,7 @@ namespace InABox.Core
             set
             {
                 _caption = value;
-                if (_editor != null)
-                {
-                    _editor.Caption = _caption;
-                }
+                RegenerateEditor();
             }
         }
 
@@ -161,10 +159,7 @@ namespace InABox.Core
             set
             {
                 _page = value;
-                if(_editor != null)
-                {
-                    _editor.Page = _page;
-                }
+                RegenerateEditor();
             }
         }
 
@@ -183,17 +178,35 @@ namespace InABox.Core
             set
             {
                 _sequence = value;
-                if (_editor != null)
-                {
-                    _editor.EditorSequence = (int)_sequence;
-                }
+                RegenerateEditor();
             }
         }
 
+        [NullEditor]
+        [DoNotPersist]
+        [DoNotSerialize]
         public IProperty? Parent { get; set; }
 
+        [NullEditor]
+        [DoNotPersist]
+        [DoNotSerialize]
         public bool IsEntityLink { get; set; }
 
+        private static string? NonWhiteSpaceOrNull(string? name)
+        {
+            return string.IsNullOrWhiteSpace(name) ? null : name;
+        }
+
+        private void RegenerateEditor()
+        {
+            Editor = EditorUtils.GetEditor(PropertyType) ?? new NullEditor();
+            Editor.Caption = NonWhiteSpaceOrNull(Caption) ?? NonWhiteSpaceOrNull(Name) ?? "";
+            Editor.EditorSequence = (int)Sequence;
+            Editor.Page = Page;
+            Editor.Editable = Editable ? Core.Editable.Enabled : Core.Editable.Disabled;
+            Editor.Visible = Visible ? Core.Visible.Optional : Core.Visible.Hidden;
+        }
+
         public Expression Expression()
         {
             if (_class is null)