diff --git a/MBS.Framework/CommandStylePreset.cs b/MBS.Framework/CommandStylePreset.cs new file mode 100644 index 0000000..eed5422 --- /dev/null +++ b/MBS.Framework/CommandStylePreset.cs @@ -0,0 +1,30 @@ +// +// CommandStylePresets.cs +// +// Author: +// Michael Becker +// +// Copyright (c) 2021 Mike Becker's Software +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . +using System; +namespace MBS.Framework +{ + public enum CommandStylePreset + { + None = 0, + Destructive, + Suggested + } +} diff --git a/MBS.Framework/MBS.Framework.csproj b/MBS.Framework/MBS.Framework.csproj index 41355ba..fd248ce 100644 --- a/MBS.Framework/MBS.Framework.csproj +++ b/MBS.Framework/MBS.Framework.csproj @@ -96,6 +96,20 @@ + + + + + + + + + + + + + + @@ -104,6 +118,7 @@ + diff --git a/MBS.Framework/Setting.cs b/MBS.Framework/Setting.cs new file mode 100644 index 0000000..ad9aa1c --- /dev/null +++ b/MBS.Framework/Setting.cs @@ -0,0 +1,147 @@ +// +// Option.cs +// +// Author: +// Mike Becker +// +// Copyright (c) 2019 Mike Becker +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . +using System; +using System.Collections.Generic; + +namespace MBS.Framework +{ + public abstract class Setting + { + public Setting(string name, string title, object defaultValue = null) + { + Name = name; + Title = title; + DefaultValue = defaultValue; + mvarValue = defaultValue; + } + + public Guid ID { get; set; } = Guid.Empty; + + [Obsolete("This is no longer implemented. Please set a proper ID in order for your setting to be saved.")] + public string Name { get; set; } = String.Empty; + + public string Title { get; set; } = String.Empty; + public string Description { get; set; } = String.Empty; + + public class SettingCollection + : System.Collections.ObjectModel.Collection + { + + public Setting this[string name] + { + get + { + foreach (Setting item in this) + { + if (item.Title.Replace("_", String.Empty).Replace(' ', '_').Equals(name)) + { + return item; + } + } + return null; + } + } + + private Dictionary _itemsByID = new Dictionary(); + protected override void ClearItems() + { + base.ClearItems(); + _itemsByID.Clear(); + } + protected override void InsertItem(int index, Setting item) + { + base.InsertItem(index, item); + _itemsByID[item.ID] = item; + } + protected override void RemoveItem(int index) + { + _itemsByID.Remove(this[index].ID); + base.RemoveItem(index); + } + public bool Contains(Guid id) + { + return _itemsByID.ContainsKey(id); + } + } + + protected Setting() + { + } + + public object DefaultValue { get; set; } = null; + public SettingsValue.SettingsValueCollection ScopedValues { get; } = new SettingsValue.SettingsValueCollection(); + + private object mvarValue = null; + + public virtual object GetValue(Guid? scopeId = null) + { + if (scopeId != null) + { + if (ScopedValues.Contains(scopeId.Value)) + { + return ScopedValues[scopeId.Value].Value; + } + } + return mvarValue; + } + public virtual void SetValue(object value, Guid? scopeId = null) + { + if (scopeId != null) + { + if (ScopedValues.Contains(scopeId.Value)) + { + ScopedValues[scopeId.Value].Value = value; + } + else + { + ScopedValues.Add(scopeId.Value, value); + } + } + else + { + mvarValue = value; + } + } + public T GetValue(T defaultValue = default(T), Guid? scopeId = null) + { + try + { + object val = GetValue(scopeId); + if (val is T) + return (T)val; + if (val is string) + { + return (val as string).Parse(); + } + return defaultValue; + } + catch + { + return defaultValue; + } + } + public void SetValue(T value) + { + mvarValue = value; + } + } +} + diff --git a/MBS.Framework/Settings/BooleanSetting.cs b/MBS.Framework/Settings/BooleanSetting.cs new file mode 100644 index 0000000..0b82ca0 --- /dev/null +++ b/MBS.Framework/Settings/BooleanSetting.cs @@ -0,0 +1,40 @@ +// +// BooleanSetting.cs +// +// Author: +// Michael Becker +// +// Copyright (c) 2021 Mike Becker's Software +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . +using System; +namespace MBS.Framework.Settings +{ + public class BooleanSetting : Setting + { + public BooleanSetting(string name, string title, bool defaultValue = false) : base(name, title, defaultValue) + { + } + + public override void SetValue(object value, Guid? scopeId = null) + { + bool val = false; + if (value != null) + { + val = (value.ToString().ToLower().Equals("true")); + } + base.SetValue(val); + } + } +} diff --git a/MBS.Framework/Settings/ChoiceSetting.cs b/MBS.Framework/Settings/ChoiceSetting.cs new file mode 100644 index 0000000..e4e82ee --- /dev/null +++ b/MBS.Framework/Settings/ChoiceSetting.cs @@ -0,0 +1,74 @@ +// +// ChoiceSetting.cs +// +// Author: +// Michael Becker +// +// Copyright (c) 2021 Mike Becker's Software +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . +using System; +namespace MBS.Framework.Settings +{ + public class ChoiceSetting : Setting + { + public ChoiceSetting(string name, string title, ChoiceSettingValue defaultValue = null, ChoiceSettingValue[] values = null) : base(name, title, null) + { + if (values == null) + { + values = new ChoiceSettingValue[0]; + } + if (defaultValue != null) + { + if (defaultValue.Value != null) + { + base.DefaultValue = defaultValue.Value.ToString(); + } + else + { + base.DefaultValue = null; + } + } + + foreach (ChoiceSettingValue value in values) + { + ValidValues.Add(value); + } + } + + public class ChoiceSettingValue + { + public class ChoiceSettingValueCollection + : System.Collections.ObjectModel.Collection + { + } + + public string Name { get; set; } = String.Empty; + public string Title { get; set; } = String.Empty; + public object Value { get; set; } = null; + + public ChoiceSettingValue(string name, string title, object value) + { + Name = name; + Title = title; + Value = value; + } + } + + public ChoiceSettingValue.ChoiceSettingValueCollection ValidValues { get; } = new ChoiceSettingValue.ChoiceSettingValueCollection(); + public ChoiceSettingValue SelectedValue { get; set; } = null; + + public bool RequireSelectionFromList { get; set; } = true; + } +} diff --git a/MBS.Framework/Settings/CollectionSetting.cs b/MBS.Framework/Settings/CollectionSetting.cs new file mode 100644 index 0000000..c03fb7b --- /dev/null +++ b/MBS.Framework/Settings/CollectionSetting.cs @@ -0,0 +1,37 @@ +// +// CollectionSetting.cs +// +// Author: +// Michael Becker +// +// Copyright (c) 2021 Mike Becker's Software +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . +using System; +namespace MBS.Framework.Settings +{ + public class CollectionSetting : Setting + { + public Setting.SettingCollection Settings { get; } = new Setting.SettingCollection(); + public SettingsGroup.SettingsGroupCollection Items { get; } = new SettingsGroup.SettingsGroupCollection(); + + public CollectionSetting(string name, string title, SettingsGroup group) : base(name, title, null) + { + for (int i = 0; i < group.Settings.Count; i++) + { + Settings.Add(group.Settings[i]); + } + } + } +} diff --git a/MBS.Framework/Settings/CommandSetting.cs b/MBS.Framework/Settings/CommandSetting.cs new file mode 100644 index 0000000..a453e15 --- /dev/null +++ b/MBS.Framework/Settings/CommandSetting.cs @@ -0,0 +1,34 @@ +// +// CommandSetting.cs +// +// Author: +// Michael Becker +// +// Copyright (c) 2021 Mike Becker's Software +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . +using System; +namespace MBS.Framework.Settings +{ + public class CommandSetting : Setting + { + public string CommandID { get; set; } = null; + public CommandStylePreset StylePreset { get; set; } = CommandStylePreset.None; + + public CommandSetting(string name, string title, string commandID) : base(name, title) + { + CommandID = commandID; + } + } +} diff --git a/MBS.Framework/Settings/FileSetting.cs b/MBS.Framework/Settings/FileSetting.cs new file mode 100644 index 0000000..2f808b5 --- /dev/null +++ b/MBS.Framework/Settings/FileSetting.cs @@ -0,0 +1,41 @@ +// +// FileSetting.cs +// +// Author: +// Michael Becker +// +// Copyright (c) 2021 Mike Becker's Software +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . +using System; +namespace MBS.Framework.Settings +{ + public enum FileSettingMode + { + Open, + Save, + SelectFolder, + CreateFolder + } + public class FileSetting : TextSetting + { + public bool RequireExistingFile { get; set; } = true; + public FileSettingMode Mode { get; set; } = FileSettingMode.Open; + + public FileSetting(string name, string title, string defaultValue = "", bool requireExistingFile = true) : base(name, title, defaultValue) + { + RequireExistingFile = requireExistingFile; + } + } +} diff --git a/MBS.Framework/Settings/GroupSetting.cs b/MBS.Framework/Settings/GroupSetting.cs new file mode 100644 index 0000000..b9c358d --- /dev/null +++ b/MBS.Framework/Settings/GroupSetting.cs @@ -0,0 +1,40 @@ +// +// GroupSetting.cs +// +// Author: +// Michael Becker +// +// Copyright (c) 2021 Mike Becker's Software +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . +using System; +namespace MBS.Framework.Settings +{ + public class GroupSetting : Setting + { + public Setting.SettingCollection Options { get; } = new Setting.SettingCollection(); + public Setting.SettingCollection HeaderSettings { get; } = new Setting.SettingCollection(); + + public GroupSetting(string name, string title, Setting[] options = null) : base(name, title) + { + if (options != null) + { + foreach (Setting option in options) + { + Options.Add(option); + } + } + } + } +} diff --git a/MBS.Framework/Settings/RangeSetting.cs b/MBS.Framework/Settings/RangeSetting.cs new file mode 100644 index 0000000..33b8f83 --- /dev/null +++ b/MBS.Framework/Settings/RangeSetting.cs @@ -0,0 +1,35 @@ +// +// RangeSetting.cs +// +// Author: +// Michael Becker +// +// Copyright (c) 2021 Mike Becker's Software +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . +using System; +namespace MBS.Framework.Settings +{ + public class RangeSetting : Setting + { + public decimal? MinimumValue { get; set; } = null; + public decimal? MaximumValue { get; set; } = null; + + public RangeSetting(string name, string title, decimal defaultValue = 0.0M, decimal? minimumValue = null, decimal? maximumValue = null) : base(name, title, defaultValue) + { + MinimumValue = minimumValue; + MaximumValue = maximumValue; + } + } +} diff --git a/MBS.Framework/Settings/TextSetting.cs b/MBS.Framework/Settings/TextSetting.cs new file mode 100644 index 0000000..8698e33 --- /dev/null +++ b/MBS.Framework/Settings/TextSetting.cs @@ -0,0 +1,30 @@ +// +// TextSetting.cs +// +// Author: +// Michael Becker +// +// Copyright (c) 2021 Mike Becker's Software +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . +using System; +namespace MBS.Framework.Settings +{ + public class TextSetting : Setting + { + public TextSetting(string name, string title, string defaultValue = "") : base(name, title, defaultValue) + { + } + } +} diff --git a/MBS.Framework/SettingsGroup.cs b/MBS.Framework/SettingsGroup.cs new file mode 100644 index 0000000..e7c71d8 --- /dev/null +++ b/MBS.Framework/SettingsGroup.cs @@ -0,0 +1,146 @@ +// +// OptionPanel.cs +// +// Author: +// Mike Becker +// +// Copyright (c) 2019 Mike Becker +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . +using System; +using System.Collections.Generic; + +namespace MBS.Framework +{ + public class SettingsGroup : IComparable + { + public class SettingsGroupCollection + : System.Collections.ObjectModel.Collection + { + public SettingsGroup Add(string path, Setting[] options) + { + string[] paths = new string[0]; + if (!String.IsNullOrEmpty (path)) { + paths = path.Split (new char[] { ':' }); + } + return Add (paths, options); + } + public SettingsGroup Add(string[] path, Setting[] options) + { + SettingsGroup grp = new SettingsGroup(); + grp.Path = path; + if (options != null) { + foreach (Setting option in options) { + grp.Settings.Add (option); + } + } + Add (grp); + return grp; + } + + private Dictionary _itemsByID = new Dictionary(); + protected override void ClearItems() + { + base.ClearItems(); + _itemsByID.Clear(); + } + protected override void InsertItem(int index, SettingsGroup item) + { + base.InsertItem(index, item); + _itemsByID[item.ID] = item; + } + protected override void RemoveItem(int index) + { + _itemsByID.Remove(this[index].ID); + base.RemoveItem(index); + } + public bool Contains(Guid id) + { + return _itemsByID.ContainsKey(id); + } + } + + public SettingsGroup() + { + } + public SettingsGroup(string path, Setting[] options = null) + { + string[] paths = new string[0]; + if (!String.IsNullOrEmpty (path)) { + paths = path.Split (new char[] { ':' }); + } + Path = paths; + if (options != null) + { + foreach (Setting option in options) + { + Settings.Add(option); + } + } + } + public SettingsGroup(string[] paths, Setting[] options = null) + { + Path = paths; + if (options != null) + { + foreach (Setting option in options) + { + Settings.Add(option); + } + } + } + + public int CompareTo(SettingsGroup other) + { + int xprior = this.Priority; + int yprior = other.Priority; + if (xprior == -1 && yprior == -1) + { + string xpath = String.Join(":", this.GetPath()); + string ypath = String.Join(":", other.GetPath()); + return xpath.CompareTo(ypath); + } + else + { + return yprior.CompareTo(xprior); + } + } + + public Guid ID { get; set; } = Guid.Empty; + + public string[] GetPath() + { + if (Path == null) return new string[0]; + return Path; + } + + public string[] Path { get; set; } = null; + public string Title + { + get + { + if (Path.Length > 0) return Path[Path.Length - 1]; + return null; + } + } + public Setting.SettingCollection Settings { get; } = new Setting.SettingCollection(); + public int Priority { get; set; } = -1; + + public override string ToString () + { + return String.Join (":", Path); + } + } +} + diff --git a/MBS.Framework/SettingsProfile.cs b/MBS.Framework/SettingsProfile.cs new file mode 100644 index 0000000..a3d3f94 --- /dev/null +++ b/MBS.Framework/SettingsProfile.cs @@ -0,0 +1,38 @@ +// +// SettingsProfile.cs +// +// Author: +// Michael Becker +// +// Copyright (c) 2020 Mike Becker's Software +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . +using System; +namespace MBS.Framework +{ + public class SettingsProfile + { + public class SettingsProfileCollection + : System.Collections.ObjectModel.Collection + { + + } + + public static readonly Guid AllUsersGUID = new Guid("{6c1e84c6-7cb8-4798-b000-349dba816114}"); + public static readonly Guid ThisUserGUID = new Guid("{a550229d-05e1-4a93-96a6-98ae1c69b847}"); + + public Guid ID { get; set; } = Guid.Empty; + public string Title { get; set; } = null; + } +} diff --git a/MBS.Framework/SettingsProvider.cs b/MBS.Framework/SettingsProvider.cs new file mode 100644 index 0000000..b27e0a0 --- /dev/null +++ b/MBS.Framework/SettingsProvider.cs @@ -0,0 +1,81 @@ +// +// OptionProvider.cs +// +// Author: +// Mike Becker +// +// Copyright (c) 2019 Mike Becker +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . +using System; + +namespace MBS.Framework +{ + public abstract class SettingsProvider + { + public class SettingsProviderCollection + : System.Collections.ObjectModel.Collection + { + private System.Collections.Generic.Dictionary _itemsByID = new System.Collections.Generic.Dictionary(); + public bool Contains(Guid id) + { + return _itemsByID.ContainsKey(id); + } + protected override void ClearItems() + { + base.ClearItems(); + _itemsByID.Clear(); + } + protected override void InsertItem(int index, SettingsProvider item) + { + _itemsByID[item.ID] = item; + base.InsertItem(index, item); + } + protected override void RemoveItem(int index) + { + _itemsByID.Remove(this[index].ID); + base.RemoveItem(index); + } + } + + public Guid ID { get; set; } = Guid.Empty; + + public SettingsGroup.SettingsGroupCollection SettingsGroups { get; } = new SettingsGroup.SettingsGroupCollection(); + + protected virtual void InitializeInternal() + { + } + public void Initialize() + { + InitializeInternal(); + } + + protected virtual void LoadSettingsInternal() + { + } + public void LoadSettings() + { + LoadSettingsInternal (); + } + + protected virtual void SaveSettingsInternal() + { + } + public void SaveSettings() + { + SaveSettingsInternal (); + } + } +} + diff --git a/MBS.Framework/SettingsValue.cs b/MBS.Framework/SettingsValue.cs new file mode 100644 index 0000000..4153c98 --- /dev/null +++ b/MBS.Framework/SettingsValue.cs @@ -0,0 +1,61 @@ +// +// SettingValue.cs +// +// Author: +// Michael Becker +// +// Copyright (c) 2020 Mike Becker's Software +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . +using System; +using System.Collections.Generic; + +namespace MBS.Framework +{ + public class SettingsValue + { + public class SettingsValueCollection : System.Collections.ObjectModel.Collection + { + Dictionary _itemsByGuid = new Dictionary(); + + public int Count { get { return _itemsByGuid.Count; } } + + public bool Contains(Guid scopeId) + { + return _itemsByGuid.ContainsKey(scopeId); + } + + public void Add(Guid scopeId, object value) + { + SettingsValue item = new SettingsValue(); + item.ScopeId = scopeId; + item.Value = value; + Add(item); + } + public SettingsValue this[Guid scopeId] + { + get + { + if (_itemsByGuid.ContainsKey(scopeId)) + return _itemsByGuid[scopeId]; + return null; + } + } + } + + public Guid ScopeId { get; set; } + public object Value { get; set; } = null; + + } +}