public LookupControl(PropertyInfo prop, IAfterglowPlugin plugin) : base(prop) { ConfigLookupAttribute configAttribute = Attribute.GetCustomAttribute(prop, typeof(ConfigLookupAttribute)) as ConfigLookupAttribute; if (configAttribute == null) { throw new Exception("prop is not a ConfigLookupAttribute"); } _propertyInfo = prop; _plugin = plugin; //Create value Combo Box _valueComboBox = new ComboBox(); _valueComboBox.Name = Guid.NewGuid().ToString(); //Populate Combo and set current item IEnumerable <object> availableValues = GetLookupValues(prop, plugin, configAttribute); if (availableValues != null) { _valueComboBox.DataSource = availableValues; _valueComboBox.AutoCompleteMode = AutoCompleteMode.SuggestAppend; _valueComboBox.DropDownStyle = ComboBoxStyle.DropDownList; } else { plugin.Logger.Error("{0} could not be set as no lookup values could not be loaded", prop.Name); } this.Controls.Add(_valueComboBox); InitializeComponent(); }
/// <summary> /// Loads settings from storage /// </summary> public T LoadFromStorage <T>(string propertyName) { T result = default(T); if (Table == null) { //TODO Logger.Error("Storage has not be setup for " + this.Name); return(result); } Type t = this.GetType(); PropertyInfo prop = t.GetProperty(propertyName); if (prop.CanWrite) { if (Attribute.IsDefined(prop, typeof(ConfigLookupAttribute))) { ConfigLookupAttribute configAttribute = Attribute.GetCustomAttribute(prop, typeof(ConfigLookupAttribute)) as ConfigLookupAttribute; Type propertyType = prop.PropertyType; if (propertyType.IsEnum) { string value = Table.GetString(prop.Name); if (!string.IsNullOrEmpty(value)) { result = (T)(Enum.Parse(propertyType, value) as Object); } } else if (prop.PropertyType == typeof(int?)) { result = (T)(Table.GetInt(prop.Name) as object); } else if (prop.PropertyType == typeof(string)) { result = (T)(Table.GetString(prop.Name) as object); } else { //TODO change to log error throw new Exception(prop.PropertyType.ToString() + " is not supported update BasePlugin.LoadFromStorage() to implement support"); } } else if (Attribute.IsDefined(prop, typeof(ConfigNumberAttribute))) { ConfigNumberAttribute configAttribute = Attribute.GetCustomAttribute(prop, typeof(ConfigNumberAttribute)) as ConfigNumberAttribute; if (prop.PropertyType == typeof(int?)) { result = (T)(Table.GetInt(prop.Name) as object); } else if (prop.PropertyType == typeof(double?)) { result = (T)(Table.GetDouble(prop.Name) as object); } else { throw new Exception(prop.PropertyType.ToString() + " is not supported update BasePlugin.LoadFromStorage() to implement support"); } } else if (Attribute.IsDefined(prop, typeof(ConfigStringAttribute))) { ConfigStringAttribute configAttribute = Attribute.GetCustomAttribute(prop, typeof(ConfigStringAttribute)) as ConfigStringAttribute; result = (T)(Table.GetString(prop.Name) as object); } else if (Attribute.IsDefined(prop, typeof(ConfigTableAttribute))) { ConfigTableAttribute configAttribute = Attribute.GetCustomAttribute(prop, typeof(ConfigTableAttribute)) as ConfigTableAttribute; //Single Item if (typeof(T).IsInterface) { ITable table = Table.GetTables(prop.Name).FirstOrDefault(); if (table != null) { string tableTypeString = table.GetString("Type"); if (!string.IsNullOrEmpty(tableTypeString)) { //Get type from local project if possible, if not use other loader Type tableType = System.Type.GetType(tableTypeString) ?? Runtime.Loader.GetObjectType(tableTypeString); string id = table.GetString("Id"); BaseRecord createdTable = Activator.CreateInstance(tableType, Table.Database.AddTable(id), Logger, this.Runtime) as BaseRecord; result = (T)(createdTable as object); } } } //List of items else { result = (T)Activator.CreateInstance(typeof(T)); foreach (ITable table in Table.GetTables(prop.Name)) { string tableTypeString = table.GetString("Type"); if (!string.IsNullOrEmpty(tableTypeString)) { //Get type from local project if possible, if not use other loader Type tableType = System.Type.GetType(tableTypeString) ?? Runtime.Loader.GetObjectType(tableTypeString); string id = table.GetString("Id"); try { BaseRecord createdTable = Activator.CreateInstance(tableType, Table.Database.AddTable(id), Logger, this.Runtime) as BaseRecord; result.GetType().GetMethod("Add").Invoke(result, new object[] { createdTable }); } catch { // TODO: There is a frequent error here when loading lights plugin where Afterglow.ini is already in use. } } } } } } return(result); }
private IEnumerable <object> GetLookupValues(PropertyInfo prop, IAfterglowPlugin plugin, ConfigLookupAttribute configAttribute) { Type pluginType = plugin.GetType(); Type propertyType = prop.PropertyType; string displayName = configAttribute.DisplayName; IEnumerable <object> availableValues = null; if (propertyType.IsEnum) { availableValues = propertyType.GetEnumNames() as IEnumerable <object>; } else if (configAttribute.RetrieveValuesFrom != null) { var member = pluginType.GetMember(configAttribute.RetrieveValuesFrom); if (member.Length > 0) { if (member[0].MemberType == MemberTypes.Property) { PropertyInfo pi = pluginType.GetProperty(configAttribute.RetrieveValuesFrom); var propertyValue = pi.GetValue(plugin, null); if (typeof(IEnumerable <>).MakeGenericType(propertyType).IsAssignableFrom(propertyValue.GetType())) { availableValues = propertyValue as IEnumerable <object>; } else { throw new ArgumentException("incorrect type", "RetrieveValuesFrom"); } } else if (member[0].MemberType == MemberTypes.Method) { MethodInfo mi = pluginType.GetMethod(configAttribute.RetrieveValuesFrom); var propertyValue = mi.Invoke(plugin, null); if (typeof(IEnumerable <>).MakeGenericType(propertyType).IsAssignableFrom(propertyValue.GetType())) { availableValues = propertyValue as IEnumerable <object>; } else { throw new ArgumentException("incorrect type", "RetrieveValuesFrom"); } } else if (member[0].MemberType == MemberTypes.Field) { FieldInfo fi = pluginType.GetField(configAttribute.RetrieveValuesFrom); var propertyValue = fi.GetValue(plugin); if (typeof(System.Collections.ObjectModel.ObservableCollection <string>) == propertyValue.GetType()) { availableValues = propertyValue as IEnumerable <object>; _useCollectionIndex = true; } else { throw new ArgumentException("incorrect type", "RetrieveValuesFrom"); } } } } return(availableValues); }
private PluginProperty[] GetPluginProperties(object plugin) { List <PluginProperty> pluginProperties = new List <PluginProperty>(); if (plugin == null) { return(pluginProperties.ToArray()); } Type pluginObjectType = plugin.GetType(); foreach (System.Reflection.PropertyInfo objectProperty in pluginObjectType.GetProperties()) { PluginProperty pluginProperty = new PluginProperty(); bool exclude = false; bool dataMember = false; if (!objectProperty.CanWrite) { continue; } pluginProperty.Name = objectProperty.Name; pluginProperty.Value = objectProperty.GetValue(plugin, null); Type propertyType = objectProperty.PropertyType; if (propertyType == typeof(string)) { pluginProperty.Type = "text"; } else if (propertyType == typeof(int)) { pluginProperty.Type = "number"; } else if (propertyType == typeof(bool)) { pluginProperty.Type = "boolean"; } else if (propertyType == typeof(List <Core.Light>)) { pluginProperty.Type = "lights"; string[] colourClasses = new string[] { "btn-primary", "btn-success", "btn-info", "btn-warning", "btn-danger" }; int colourPosition = 0; List <Core.Light> lights = pluginProperty.Value as List <Core.Light>; LightSetup lightSetup = new LightSetup(); if (lights != null && lights.Any()) { //convert back to 2d array for setup lightSetup.LightRows = new List <LightRow>(); int numberOfRows = lights.Max(l => l.Top); int numberOfColumns = lights.Max(l => l.Left); for (int row = 0; row <= numberOfRows; row++) { LightRow lightRow = new LightRow(); lightRow.RowIndex = row; lightRow.LightColumns = new List <LightColumn>(); for (int column = 0; column <= numberOfColumns; column++) { LightColumn lightColumn = new LightColumn(); lightColumn.ColumnIndex = column; if (column == 0 || column == numberOfColumns || row == 0 || row == numberOfRows) { Light light = (from l in lights where l.Top == row && l.Left == column select l).FirstOrDefault(); if (light != null) { lightColumn.Id = light.Id.ToString(); lightColumn.Index = light.Index.ToString(); lightColumn.ColourClass = colourClasses[colourPosition++]; lightColumn.Enabled = !string.IsNullOrEmpty(lightColumn.Id); if (lightColumn.Id == "1") { lightSetup.FirstColumnIndex = column; lightSetup.FirstRowIndex = row; } } else { lightColumn.ColourClass = colourClasses[colourPosition++]; } } else { lightColumn.ColourClass = "disabled"; lightColumn.Enabled = false; } if (colourPosition >= colourClasses.Length) { colourPosition = 0; } lightRow.LightColumns.Add(lightColumn); } lightSetup.LightRows.Add(lightRow); } } pluginProperty.Value = lightSetup; } foreach (System.Attribute attr in objectProperty.GetCustomAttributes(true)) { if ((attr as DisplayAttribute) != null) { DisplayAttribute displayName = (DisplayAttribute)attr; pluginProperty.DisplayName = displayName.Name; pluginProperty.Description = displayName.Description; } else if ((attr as RangeAttribute) != null) { RangeAttribute range = (RangeAttribute)attr; pluginProperty.MinValue = range.Minimum as int?; pluginProperty.MaxValue = range.Maximum as int?; } else if ((attr as RequiredAttribute) != null) { RequiredAttribute required = (RequiredAttribute)attr; pluginProperty.Required = required.AllowEmptyStrings; } else if ((attr as DataMemberAttribute) != null) { dataMember = true; } else if ((attr as KeyAttribute) != null) { exclude = true; break; } else if ((attr as ConfigLookupAttribute) != null) { ConfigLookupAttribute lookup = (ConfigLookupAttribute)attr; pluginProperty.Type = "lookup"; PropertyInfo optionsProperty = pluginObjectType.GetProperty(lookup.RetrieveValuesFrom); IEnumerable <object> options = optionsProperty.GetValue(plugin, null) as IEnumerable <object>; List <SelectOption> parsedOptions = new List <SelectOption>(); if (options.Any()) { foreach (object option in options) { LookupItem lookupItem = option as LookupItem; LookupItemString lookupItemString = option as LookupItemString; SelectOption pluginOption = new SelectOption(); if (lookupItem != null) { pluginOption.Id = lookupItem.Id; pluginOption.Name = lookupItem.Name; } else if (lookupItemString != null) { pluginOption.Id = lookupItemString.Id; pluginOption.Name = lookupItemString.Name; } else { pluginOption.Id = option; pluginOption.Name = option.ToString(); } parsedOptions.Add(pluginOption); } } pluginProperty.Options = parsedOptions; } } if (dataMember && !exclude) { if (pluginProperty.Type == "lookup" && pluginProperty.Value != null) { var currentValue = (from o in pluginProperty.Options where o.Id.ToString() == pluginProperty.Value.ToString() select o).FirstOrDefault(); if (currentValue == null) { List <SelectOption> options = pluginProperty.Options as List <SelectOption>; options.Add(new SelectOption() { Id = pluginProperty.Value, Name = "Invalid! " + pluginProperty.Value.ToString() }); pluginProperty.Options = options; } } pluginProperties.Add(pluginProperty); } } return(pluginProperties.ToArray()); }