io commander
This commit is contained in:
@@ -1,4 +1,6 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Reflection;
|
||||
using System.Windows.Forms;
|
||||
using Inspectron.Settings.Attributes;
|
||||
@@ -36,7 +38,12 @@ public class DefaultControlFactory : IControlFactory
|
||||
};
|
||||
}
|
||||
|
||||
if (type == typeof(string) && name.EndsWith("Path", StringComparison.OrdinalIgnoreCase))
|
||||
// Check for List<T> types
|
||||
if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(List<>))
|
||||
{
|
||||
return CreateListControl(name, description, type, initialValue, valueChangedCallback, optionsWindow, settingDescription, previewLabel, getPreview);
|
||||
}
|
||||
else if (type == typeof(string) && name.EndsWith("Path", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
return CreatePathControl(name, description, initialValue, valueChangedCallback, optionsWindow, settingDescription, previewLabel, getPreview);
|
||||
}
|
||||
@@ -62,6 +69,63 @@ public class DefaultControlFactory : IControlFactory
|
||||
}
|
||||
}
|
||||
|
||||
private Control CreateListControl(string name, string description, Type listType, object initialValue,
|
||||
Action<object> valueChangedCallback, OptionsWindow optionsWindow, string settingDescription,
|
||||
Label previewLabel, Func<object, string> getPreview)
|
||||
{
|
||||
var elementType = listType.GetGenericArguments()[0];
|
||||
var list = (IList)initialValue ?? (IList)Activator.CreateInstance(listType);
|
||||
|
||||
var label = new Label { Text = description, AutoSize = true, Padding = new Padding(0, 5, 0, 0) };
|
||||
var textBox = new TextBox
|
||||
{
|
||||
Text = GetListDisplayText(list),
|
||||
Width = 500,
|
||||
Height = 60,
|
||||
Multiline = true,
|
||||
ReadOnly = true,
|
||||
ScrollBars = ScrollBars.Vertical
|
||||
};
|
||||
var editButton = new Button { Text = "Edit", AutoSize = true };
|
||||
|
||||
editButton.Click += (s, e) =>
|
||||
{
|
||||
using (var listEditor = new ListEditorDialog(elementType, list, this, optionsWindow, name))
|
||||
{
|
||||
if (listEditor.ShowDialog(optionsWindow) == DialogResult.OK)
|
||||
{
|
||||
var newList = listEditor.GetEditedList();
|
||||
list.Clear();
|
||||
foreach (object o in newList)
|
||||
{
|
||||
list.Add(o);
|
||||
}
|
||||
|
||||
textBox.Text = GetListDisplayText(list);
|
||||
|
||||
valueChangedCallback(list);
|
||||
if (previewLabel != null && getPreview != null)
|
||||
previewLabel.Text = getPreview(list);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
return CreateOuterPanel(settingDescription, previewLabel, label, textBox, editButton);
|
||||
}
|
||||
|
||||
private string GetListDisplayText(IList list)
|
||||
{
|
||||
if (list == null || list.Count == 0)
|
||||
return "(empty list)";
|
||||
|
||||
var items = new List<string>();
|
||||
foreach (var item in list)
|
||||
{
|
||||
items.Add(item?.ToString() ?? "(null)");
|
||||
}
|
||||
return string.Join(Environment.NewLine, items);
|
||||
}
|
||||
|
||||
private Control CreatePathControl(string name, string description, object initialValue, Action<object> valueChangedCallback,
|
||||
OptionsWindow optionsWindow, string settingDescription, Label previewLabel, Func<object, string> getPreview)
|
||||
{
|
||||
@@ -186,3 +250,9 @@ public class DefaultControlFactory : IControlFactory
|
||||
return outerPanel;
|
||||
}
|
||||
}
|
||||
|
||||
// List Editor Dialog
|
||||
|
||||
// Item Editor Dialog
|
||||
|
||||
// Helper class for simple property info
|
||||
Reference in New Issue
Block a user