Browse Source

ButtonEditor and EmbeddedListEditor control support

Frank van den Bos 2 years ago
parent
commit
38627166c0

+ 2 - 1
InABox.Core/DigitalForms/Layouts/DFLayout.cs

@@ -527,9 +527,10 @@ namespace InABox.Core
                 case JsonEditor _:
                 case MemoEditor _:
                 case RichTextEditor _:
+                case ButtonEditor _:
                 case ScriptEditor _:
                     return new DFLayoutTextField();
-
+                
                 case TextBoxEditor _:
                     return new DFLayoutStringField();
 

+ 40 - 0
InABox.Core/Editors/ButtonEditor.cs

@@ -0,0 +1,40 @@
+using System;
+using System.ComponentModel;
+
+namespace InABox.Core
+{
+    public class ButtonEditorCommandArgs : CancelEventArgs
+    {
+        public String Data { get; set; }
+    }
+    
+    public abstract class ButtonEditorCommand
+    {
+        public abstract void Execute(object sender, ButtonEditorCommandArgs args);
+    }
+    
+    public class ButtonEditor : BaseEditor
+    {
+        private Type _commandtype;
+        
+        public String Label { get; set; }
+
+        public ButtonEditor(Type commandtype)
+        {
+            if (!typeof(ButtonEditorCommand).IsAssignableFrom(commandtype))
+                throw new InvalidCastException("Command must be of type ButtonEditorCommand");
+
+            _commandtype = commandtype;
+            Label = "Edit";
+            Alignment = Alignment.NotSet;
+        }
+
+        protected override BaseEditor DoClone()
+        {
+            return new ButtonEditor(_commandtype);
+        }
+
+        public ButtonEditorCommand? CreateCommand() => Activator.CreateInstance(_commandtype) as ButtonEditorCommand;
+
+    }
+}

+ 22 - 0
InABox.Core/Editors/EmbeddedListEditor.cs

@@ -0,0 +1,22 @@
+using System;
+
+namespace InABox.Core
+{
+    public class EmbeddedListEditor : BaseEditor
+    {
+        public Type DataType;
+        
+        public String Label { get; set; }
+
+        public EmbeddedListEditor(Type dataType, String label = "Edit")
+        {
+            if (!typeof(BaseObject).IsAssignableFrom(dataType))
+                throw new InvalidCastException("DataType must be of type BaseObject");
+            DataType = dataType;
+            Label = label;
+        }
+
+        protected override BaseEditor DoClone() => new EmbeddedListEditor(DataType, Label);
+
+    }
+}

+ 22 - 0
InABox.DynamicGrid/DynamicContentDialog.xaml

@@ -0,0 +1,22 @@
+<Window x:Class="InABox.DynamicGrid.DynamicContentDialog"
+        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:local="clr-namespace:InABox.DynamicGrid"
+        mc:Ignorable="d" Height="450" Width="800">
+    <Window.Resources>
+        <Style TargetType="Button">
+            <Setter Property="Width" Value="80"/>
+            <Setter Property="Height" Value="35"/>
+            <Setter Property="Margin" Value="5,5,0,0"/>
+        </Style>
+    </Window.Resources>
+    <DockPanel Margin="5">
+        <DockPanel DockPanel.Dock="Bottom" LastChildFill="False">
+            <Button x:Name="Cancel" DockPanel.Dock="Right" Content="Cancel" Click="Cancel_OnClick"/>
+            <Button x:Name="OK" DockPanel.Dock="Right" Content="OK" Click="OK_OnClick"/>
+        </DockPanel>
+        <ContentPresenter x:Name="Presenter" DockPanel.Dock="Top" />
+    </DockPanel>
+</Window>

+ 24 - 0
InABox.DynamicGrid/DynamicContentDialog.xaml.cs

@@ -0,0 +1,24 @@
+using System.Windows;
+using NPOI.OpenXmlFormats.Spreadsheet;
+
+namespace InABox.DynamicGrid
+{
+    public partial class DynamicContentDialog : Window
+    {
+        public DynamicContentDialog(FrameworkElement element)
+        {
+            InitializeComponent();
+            Presenter.Content = element;
+        }
+
+        private void OK_OnClick(object sender, RoutedEventArgs e)
+        {
+            DialogResult = true;
+        }
+
+        private void Cancel_OnClick(object sender, RoutedEventArgs e)
+        {
+            DialogResult = false;
+        }
+    }
+}

+ 10 - 0
InABox.DynamicGrid/DynamicEditorGrid.xaml.cs

@@ -211,6 +211,16 @@ namespace InABox.DynamicGrid
                     {
                         SyntaxLanguage = scriptEditor.SyntaxLanguage
                     },
+                    ButtonEditor buttonEditor => new ButtonEditorControl()
+                    {
+                        Label = buttonEditor.Label,
+                        Command = buttonEditor.CreateCommand()
+                    },
+                    EmbeddedListEditor listEditor => new EmbeddedListEditorControl()
+                    {
+                        DataType = listEditor.DataType,
+                        Label = listEditor.Label
+                    },
                     TimestampEditor => new TimestampEditorControl(),
                     ColorEditor => new ColorEditorControl(),
                     FilterEditor filter => new FilterEditorControl { FilterType = filter.Type! },

+ 2 - 1
InABox.DynamicGrid/DynamicGridUtils.cs

@@ -444,11 +444,12 @@ namespace InABox.DynamicGrid
                         myType.IsClass
                         && !myType.IsAbstract
                         && !myType.IsGenericType
-                        && CoreUtils.IsSubclassOfRawGeneric(myType, gridType)
+                        && myType.IsAssignableTo(typeof(IDynamicGrid))
                         && !myType.IsAssignableTo(typeof(ISpecificGrid))
                 ).ToArray();
                 _dynamicGrids[gridType] = grids;
             }
+            grids = grids.Where(x=>x.IsSubclassOfRawGeneric(gridType)).ToArray();
             var entityGrids = grids.Where(x => x.ContainsInheritedGenericType(entityType)).ToList();
 
             var defaults = entityGrids.Where(x => x.IsAssignableTo(typeof(IDefaultGrid))).ToList();

+ 7 - 1
InABox.DynamicGrid/DynamicItemsListGrid.cs

@@ -7,7 +7,13 @@ using System.Threading.Tasks;
 
 namespace InABox.DynamicGrid
 {
-    public class DynamicItemsListGrid<T> : DynamicGrid<T>
+
+    public interface IDynamicItemsListGrid
+    {
+        
+    }
+    
+    public class DynamicItemsListGrid<T> : DynamicGrid<T>, IDynamicItemsListGrid
         where T : BaseObject, new()
     {
         public List<T> Items { get; set; }

+ 71 - 0
InABox.DynamicGrid/Editors/ButtonEditorControl.cs

@@ -0,0 +1,71 @@
+using System.ComponentModel;
+using System.Windows;
+using System.Windows.Controls;
+using System.Windows.Media;
+using InABox.Core;
+using Syncfusion.XlsIO.Parser.Biff_Records.Charts;
+
+namespace InABox.DynamicGrid
+{
+    public class ButtonEditorControl : DynamicEditorControl<string>
+    {
+        private Button Editor;
+
+        private string _data = "";
+
+        public string? Label { get; set; }
+        
+        public ButtonEditorCommand? Command { get; set; }
+        
+        protected override FrameworkElement CreateEditor()
+        {
+            Editor = new Button
+            {
+                Content = Label,
+                HorizontalAlignment = HorizontalAlignment.Stretch,
+                VerticalAlignment = VerticalAlignment.Stretch,
+                VerticalContentAlignment = VerticalAlignment.Center,
+            };
+            Editor.Click += Editor_Click;
+
+            return Editor;
+        }
+
+        private void Editor_Click(object sender, RoutedEventArgs e)
+        {
+            var args = new ButtonEditorCommandArgs() { Cancel = false, Data = _data };
+            Command?.Execute(this, args);
+            if (!args.Cancel)
+                _data = args.Data;
+        }
+
+        public override int DesiredHeight()
+        {
+            return 25;
+        }
+
+        public override int DesiredWidth()
+        {
+            return 100;
+        }
+
+        protected override string RetrieveValue()
+        {
+            return _data;
+        }
+
+        protected override void UpdateValue(string value)
+        {
+            _data = value;
+        }
+
+        public override void SetFocus()
+        {
+            Editor.Focus();
+        }
+
+        public override void SetColor(Color color)
+        {
+        }
+    }
+}

+ 88 - 0
InABox.DynamicGrid/Editors/EmbeddedListEditorControl.cs

@@ -0,0 +1,88 @@
+using System;
+using System.Collections;
+using System.Collections.Generic;
+using System.Windows;
+using System.Windows.Controls;
+using System.Windows.Media;
+using InABox.Core;
+
+namespace InABox.DynamicGrid
+{
+    public class EmbeddedListEditorControl : DynamicEditorControl<string>
+    {
+        private Button Editor;
+
+        private string _data = "";
+
+        public string? Label { get; set; }
+        
+        public Type DataType { get; set; }
+        
+        protected override FrameworkElement CreateEditor()
+        {
+            Editor = new Button
+            {
+                Content = Label,
+                HorizontalAlignment = HorizontalAlignment.Stretch,
+                VerticalAlignment = VerticalAlignment.Stretch,
+                VerticalContentAlignment = VerticalAlignment.Center,
+            };
+            Editor.Click += Editor_Click;
+
+            return Editor;
+        }
+
+        private void Editor_Click(object sender, RoutedEventArgs e)
+        {
+            var listtype = typeof(List<>).MakeGenericType(DataType);
+            var list = Serialization.Deserialize(listtype, _data) ?? (IList)Activator.CreateInstance(listtype)!;
+            var gridtype = DynamicGridUtils.FindDynamicGrid(typeof(DynamicItemsListGrid<>), DataType);
+            
+            var grid = Activator.CreateInstance(gridtype, new object[] { list })!;
+            ((IDynamicGrid)grid).Options
+                .BeginUpdate()
+                .Add(DynamicGridOption.AddRows)
+                .Add(DynamicGridOption.DeleteRows)
+                .Add(DynamicGridOption.DirectEdit)
+                .Add(DynamicGridOption.RecordCount)
+                .EndUpdate();
+            ((IDynamicGrid)grid).Refresh(true, true);
+            var window = new DynamicContentDialog((FrameworkElement)grid);
+            if (window.ShowDialog() == true)
+            {
+                _data = Serialization.Serialize(list);
+                CheckChanged();
+            }
+
+        }
+
+        public override int DesiredHeight()
+        {
+            return 25;
+        }
+
+        public override int DesiredWidth()
+        {
+            return 100;
+        }
+
+        protected override string RetrieveValue()
+        {
+            return _data;
+        }
+
+        protected override void UpdateValue(string value)
+        {
+            _data = value;
+        }
+
+        public override void SetFocus()
+        {
+            Editor.Focus();
+        }
+
+        public override void SetColor(Color color)
+        {
+        }
+    }
+}

+ 4 - 1
InABox.DynamicGrid/Editors/ScriptEditorControl.cs

@@ -1,10 +1,13 @@
-using System.Windows;
+using System;
+using System.ComponentModel;
+using System.Windows;
 using System.Windows.Controls;
 using System.Windows.Media;
 using InABox.Core;
 
 namespace InABox.DynamicGrid
 {
+    
     public class ScriptEditorControl : DynamicEditorControl<string>
     {
         private Button Editor;