File size: 9,349 Bytes
8c763fb | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 | // Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System;
using System.ComponentModel;
using System.Globalization;
using System.Management.Automation;
using System.Text;
using Microsoft.Management.UI.Internal;
using Microsoft.PowerShell.Commands.ShowCommandExtension;
namespace Microsoft.PowerShell.Commands.ShowCommandInternal
{
/// <summary>
/// Contains information about a single parameter inside a parameter set.
/// If a parameter with the same name belongs to two (or more) parameter sets,
/// there will be two (or more) ParameterViewModel objects for the parameters,
/// each one inside its own ParameterSetViewModel.
/// </summary>
public class ParameterViewModel : INotifyPropertyChanged
{
/// <summary>
/// ParameterMetadata contains information that is the same throughout parameter sets
/// like Name and Type.
/// Note: It also happens to contain a list of all ParameterSetMetadata for the parametersets
/// in this cmdlet, but this information is not used in this class since if a parameter is
/// in multiple parametersets, there will be a ParameterViewModel for each time the parameter
/// appears in a parameterset.
/// </summary>
private ShowCommandParameterInfo parameter;
/// <summary>
/// value entered in the GUI for the parameter.
/// </summary>
private object parameterValue;
/// <summary>
/// Name of the parameter set this parameter is in.
/// </summary>
private string parameterSetName;
#region Construction and Destructor
/// <summary>
/// Initializes a new instance of the ParameterViewModel class.
/// </summary>
/// <param name="parameter">The parameter information for this parameter.</param>
/// <param name="parameterSetName">The name of the parameter set this parameter is in.</param>
public ParameterViewModel(ShowCommandParameterInfo parameter, string parameterSetName)
{
ArgumentNullException.ThrowIfNull(parameter);
ArgumentNullException.ThrowIfNull(parameterSetName);
this.parameter = parameter;
this.parameterSetName = parameterSetName;
if (this.parameter.ParameterType.IsSwitch)
{
this.parameterValue = false;
}
else
{
this.parameterValue = string.Empty;
}
}
#endregion
#region INotifyPropertyChanged Members
/// <summary>
/// PropertyChanged Event.
/// </summary>
public event PropertyChangedEventHandler PropertyChanged;
#endregion
#region Properties
/// <summary>
/// Gets the ParameterMetadata that contains information that is the same throughout parameter sets
/// like Name and Type.
/// </summary>
public ShowCommandParameterInfo Parameter
{
get { return this.parameter; }
}
/// <summary>
/// Gets or sets the value for this parameter from the GUI.
/// </summary>
public object Value
{
get
{
return this.parameterValue;
}
set
{
if (this.parameterValue != value)
{
this.parameterValue = value;
this.OnNotifyPropertyChanged("Value");
}
}
}
/// <summary>
/// Gets the parameter name.
/// </summary>
public string Name
{
get { return this.Parameter.Name; }
}
/// <summary>
/// Gets the name of the parameter set this parameter is in.
/// </summary>
public string ParameterSetName
{
get { return this.parameterSetName; }
}
/// <summary>
/// Gets a value indicating whether this parameter is in the shared parameterset.
/// </summary>
public bool IsInSharedParameterSet
{
get { return CommandViewModel.IsSharedParameterSetName(this.parameterSetName); }
}
/// <summary>
/// Gets Name with an extra suffix to indicate if the parameter is mandatory to serve.
/// </summary>
public string NameTextLabel
{
get
{
return this.Parameter.IsMandatory ?
string.Format(
CultureInfo.CurrentUICulture,
ShowCommandResources.MandatoryNameLabelFormat,
this.Name,
ShowCommandResources.MandatoryLabelSegment) :
string.Format(
CultureInfo.CurrentUICulture,
ShowCommandResources.NameLabelFormat,
this.Name);
}
}
/// <summary>
/// Gets Label in the case this parameter is used in a combo box.
/// </summary>
public string NameCheckLabel
{
get
{
string returnValue = this.Parameter.Name;
if (this.Parameter.IsMandatory)
{
returnValue = string.Create(CultureInfo.CurrentUICulture, $"{returnValue}{ShowCommandResources.MandatoryLabelSegment}");
}
return returnValue;
}
}
/// <summary>
/// Gets Tooltip string for the parameter.
/// </summary>
public string ToolTip
{
get
{
return ParameterViewModel.EvaluateTooltip(
this.Parameter.ParameterType.FullName,
this.Parameter.Position,
this.Parameter.IsMandatory,
this.IsInSharedParameterSet,
this.Parameter.ValueFromPipeline);
}
}
/// <summary>
/// Gets a value indicating whether the parameter is mandatory.
/// </summary>
public bool IsMandatory
{
get { return this.Parameter.IsMandatory; }
}
/// <summary>
/// Gets a value indicating whether the parameter has a value.
/// </summary>
public bool HasValue
{
get
{
if (this.Value == null)
{
return false;
}
if (this.Parameter.ParameterType.IsSwitch)
{
return ((bool?)this.Value) == true;
}
return this.Value.ToString().Length != 0;
}
}
#endregion
/// <summary>
/// Evaluates the tooltip based on the parameters.
/// </summary>
/// <param name="typeName">Parameter type name.</param>
/// <param name="position">Parameter position.</param>
/// <param name="mandatory">True if the parameter is mandatory.</param>
/// <param name="shared">True if the parameter is shared by parameter sets.</param>
/// <param name="valueFromPipeline">True if the parameter takes value from the pipeline.</param>
/// <returns> the tooltip based on the parameters.</returns>
internal static string EvaluateTooltip(string typeName, int position, bool mandatory, bool shared, bool valueFromPipeline)
{
StringBuilder returnValue = new StringBuilder(string.Format(
CultureInfo.CurrentCulture,
ShowCommandResources.TypeFormat,
typeName));
string newlineFormatString = Environment.NewLine + "{0}";
if (position >= 0)
{
string positionFormat = string.Format(
CultureInfo.CurrentCulture,
ShowCommandResources.PositionFormat,
position);
returnValue.AppendFormat(CultureInfo.InvariantCulture, newlineFormatString, positionFormat);
}
string optionalOrMandatory = mandatory ? ShowCommandResources.Mandatory : ShowCommandResources.Optional;
returnValue.AppendFormat(CultureInfo.InvariantCulture, newlineFormatString, optionalOrMandatory);
if (shared)
{
returnValue.AppendFormat(CultureInfo.InvariantCulture, newlineFormatString, ShowCommandResources.CommonToAllParameterSets);
}
if (valueFromPipeline)
{
returnValue.AppendFormat(CultureInfo.InvariantCulture, newlineFormatString, ShowCommandResources.CanReceiveValueFromPipeline);
}
return returnValue.ToString();
}
/// <summary>
/// If property changed will be notify.
/// </summary>
/// <param name="propertyName">The changed property.</param>
private void OnNotifyPropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = this.PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
}
}
|