//******************************************************************************************************
// AdapterConnectionStringParameter.cs - Gbtc
//
// Copyright © 2012, Grid Protection Alliance. All Rights Reserved.
//
// Licensed to the Grid Protection Alliance (GPA) under one or more contributor license agreements. See
// the NOTICE file distributed with this work for additional information regarding copyright ownership.
// The GPA licenses this file to you under the MIT License (MIT), the "License"; you may
// not use this file except in compliance with the License. You may obtain a copy of the License at:
//
// http://www.opensource.org/licenses/MIT
//
// Unless agreed to in writing, the subject software distributed under the License is distributed on an
// "AS-IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. Refer to the
// License for the specific language governing permissions and limitations.
//
// Code Modification History:
// ----------------------------------------------------------------------------------------------------
// 06/09/2011 - Stephen C. Wills
// Generated original version of source code.
// 12/20/2012 - Starlynn Danyelle Gilliam
// Modified Header.
//
//******************************************************************************************************
using System;
using System.ComponentModel;
using System.Reflection;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using GSF.TimeSeries.Adapters;
// ReSharper disable ValueParameterNotUsed
namespace GSF.TimeSeries.UI.ViewModels
{
///
/// View-model for a key-value pair in an adapter connection string.
/// This can also represent key-value pairs which aren't necessarily
/// in the connection string, but rather are defined by properties
/// in an adapter class. This view-model is used by the
/// .
///
public class AdapterConnectionStringParameter : ViewModelBase
{
#region [ Members ]
// Fields
private PropertyInfo m_info;
private string m_name;
private string m_description;
private object m_value;
private object m_defaultValue;
private bool m_isRequired;
#endregion
#region [ Properties ]
///
/// Gets or sets the of the
///
/// associated with this .
///
public PropertyInfo Info
{
get => m_info;
set
{
m_info = value;
OnPropertyChanged("Info");
}
}
///
/// Gets or sets the name of the
/// which is either a key in the connection string or the name of a property in
/// the adapter class.
///
public string Name
{
get => m_name;
set
{
m_name = value;
OnPropertyChanged("Name");
}
}
///
/// Gets or sets the description of the
/// obtained through the using reflection. A property annotated with
///
/// must also define a for this
/// to become populated.
///
public string Description
{
get => m_description;
set
{
m_description = value;
OnPropertyChanged("Description");
}
}
///
/// Gets or sets the value of the as defined
/// by either the connection string or the of the parameter.
///
public object Value
{
get => m_value ?? m_defaultValue;
set
{
m_value = value;
OnPropertyChanged("Value");
OnPropertyChanged("Color");
OnPropertyChanged("Boldness");
OnPropertyChanged("EnumDescription");
}
}
///
/// Gets or sets the default value of the
/// obtained through the using reflection. A property annotated with
/// must
/// also define a for this to
/// be populated.
///
public object DefaultValue
{
get => m_defaultValue;
set
{
m_defaultValue = value;
OnPropertyChanged("DefaultValue");
}
}
///
/// Gets or sets a value that indicates whether this parameter is defined by a property
/// that is annotated with the .
///
public bool IsRequired
{
get => m_isRequired;
set
{
m_isRequired = value;
OnPropertyChanged("IsRequired");
}
}
///
/// Gets a list of enum types if this 's type is an enum.
/// If it is not an enum, this returns an empty array.
///
public string[] EnumValues
{
get => IsEnum ?
Enum.GetNames(m_info.PropertyType) :
Array.Empty();
set { }
}
///
/// Gets enum description, if defined.
///
public string EnumDescription
{
get
{
try
{
return IsEnum ?
((Enum)Enum.Parse(m_info.PropertyType, Value.ToString(), true)).GetDescription(false) :
null;
}
#if DEBUG
catch (Exception ex)
{
return ex.Message;
#else
catch
{
return null;
#endif
}
}
set { }
}
///
/// Gets or sets the color of the item in the
/// that the is modeling.
///
public Brush Color
{
get
{
bool red = m_isRequired && m_value is null;
return red ? Brushes.Red : Brushes.Black;
}
}
///
/// Gets or sets the boldness of the item in the that the
/// is modeling.
///
public FontWeight Boldness => m_value is null ? FontWeights.Normal : FontWeights.Bold;
///
/// Gets a value that indicates whether the
/// labeled "False" is checked. Since the actual value of this
/// is represented by , and that value is what goes into the connection string
/// this simply returns true if the value in the Value property is the word "false".
///
public bool IsFalseChecked => Value.ToNonNullString().Equals(false.ToString(), StringComparison.CurrentCultureIgnoreCase);
///
/// Gets a value that indicates whether the value of this parameter can be configured via a
/// custom control. This determines whether the hyper-link that links to the custom configuration
/// pop-up is visible.
///
public bool IsCustom
{
get
{
try
{
return m_info?.GetCustomAttribute() is not null;
}
catch
{
return false;
}
}
}
///
/// Gets a value that indicates whether the type of this
/// is defined to be a in the adapter type. This determines whether the
/// s labeled "True" and "False" are visible.
///
public bool IsBoolean => !(IsCustom || m_info is null || m_info.PropertyType != typeof(bool));
///
/// Gets a value that indicates whether the type of this
/// is defined to be an enum in the adapter type. This determines whether the
/// bound to the enum values is visible.
///
public bool IsEnum => !(IsCustom || m_info is null || !m_info.PropertyType.IsEnum);
///
/// Gets a value that indicates whether the type of this
/// is something other than a or an enum. This determines whether the
/// bound to the of the parameters is
/// visible. The value is true for most parameters, including those which are not defined by an adapter type.
///
public bool IsOther => !(IsCustom || IsBoolean || IsEnum);
#endregion
}
}