File size: 11,189 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 274 275 276 277 278 279 280 281 282 283 284 285 286 | // Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System;
using System.Collections.Generic;
using System.IO;
using System.Management.Automation;
using System.Management.Automation.Internal;
using System.Reflection;
using System.Threading;
using Microsoft.PowerShell.Commands.Internal.Format;
namespace Microsoft.PowerShell.Commands
{
internal sealed class OutWindowProxy : IDisposable
{
private const string OutGridViewWindowClassName = "Microsoft.Management.UI.Internal.OutGridViewWindow";
private const string OriginalTypePropertyName = "OriginalType";
internal const string OriginalObjectPropertyName = "OutGridViewOriginalObject";
private const string ToStringValuePropertyName = "ToStringValue";
private const string IndexPropertyName = "IndexValue";
private int _index;
/// <summary> Columns definition of the underlying Management List</summary>
private HeaderInfo _headerInfo;
private bool _isWindowStarted;
private readonly string _title;
private readonly OutputModeOption _outputMode;
private AutoResetEvent _closedEvent;
private readonly OutGridViewCommand _parentCmdlet;
private readonly GraphicalHostReflectionWrapper _graphicalHostReflectionWrapper;
/// <summary>
/// Initializes a new instance of the <see cref="OutWindowProxy"/> class.
/// </summary>
internal OutWindowProxy(string title, OutputModeOption outPutMode, OutGridViewCommand parentCmdlet)
{
_title = title;
_outputMode = outPutMode;
_parentCmdlet = parentCmdlet;
_graphicalHostReflectionWrapper = GraphicalHostReflectionWrapper.GetGraphicalHostReflectionWrapper(parentCmdlet, OutWindowProxy.OutGridViewWindowClassName);
}
/// <summary>
/// Adds columns to the output window.
/// </summary>
/// <param name="propertyNames">An array of property names to add.</param>
/// <param name="displayNames">An array of display names to add.</param>
/// <param name="types">An array of types to add.</param>
internal void AddColumns(string[] propertyNames, string[] displayNames, Type[] types)
{
ArgumentNullException.ThrowIfNull(propertyNames);
ArgumentNullException.ThrowIfNull(displayNames);
ArgumentNullException.ThrowIfNull(types);
try
{
_graphicalHostReflectionWrapper.CallMethod("AddColumns", propertyNames, displayNames, types);
}
catch (TargetInvocationException ex)
{
// Verify if this is an error loading the System.Core dll.
if (ex.InnerException is FileNotFoundException fileNotFoundEx && fileNotFoundEx.FileName.Contains("System.Core"))
{
_parentCmdlet.ThrowTerminatingError(
new ErrorRecord(new InvalidOperationException(
StringUtil.Format(FormatAndOut_out_gridview.RestartPowerShell,
_parentCmdlet.CommandInfo.Name), ex.InnerException),
"ErrorLoadingAssembly",
ErrorCategory.ObjectNotFound,
null));
}
else
{
// Let PowerShell take care of this problem.
throw;
}
}
}
// Types that are not defined in the database and are not scalar.
internal void AddColumnsAndItem(PSObject liveObject, TableView tableView)
{
// Create a header using only the input object's properties.
_headerInfo = tableView.GenerateHeaderInfo(liveObject, _parentCmdlet);
AddColumnsAndItemEnd(liveObject);
}
// Database defined types.
internal void AddColumnsAndItem(PSObject liveObject, TableView tableView, TableControlBody tableBody)
{
_headerInfo = tableView.GenerateHeaderInfo(liveObject, tableBody, _parentCmdlet);
AddColumnsAndItemEnd(liveObject);
}
// Scalar types.
internal void AddColumnsAndItem(PSObject liveObject)
{
_headerInfo = new HeaderInfo();
// On scalar types the type name is used as a column name.
_headerInfo.AddColumn(new ScalarTypeColumnInfo(liveObject.BaseObject.GetType()));
AddColumnsAndItemEnd(liveObject);
}
private void AddColumnsAndItemEnd(PSObject liveObject)
{
// Add columns to the underlying Management list and as a byproduct get a stale PSObject.
PSObject staleObject = _headerInfo.AddColumnsToWindow(this, liveObject);
// Add 3 extra properties, so that the stale PSObject has meaningful info in the Hetero-type header view.
AddExtraProperties(staleObject, liveObject);
// Add the stale PSObject to the underlying Management list.
_graphicalHostReflectionWrapper.CallMethod("AddItem", staleObject);
}
// Hetero types.
internal void AddHeteroViewColumnsAndItem(PSObject liveObject)
{
_headerInfo = new HeaderInfo();
_headerInfo.AddColumn(new IndexColumnInfo(OutWindowProxy.IndexPropertyName,
StringUtil.Format(FormatAndOut_out_gridview.IndexColumnName), _index));
_headerInfo.AddColumn(new ToStringColumnInfo(OutWindowProxy.ToStringValuePropertyName,
StringUtil.Format(FormatAndOut_out_gridview.ValueColumnName), _parentCmdlet));
_headerInfo.AddColumn(new TypeNameColumnInfo(OutWindowProxy.OriginalTypePropertyName,
StringUtil.Format(FormatAndOut_out_gridview.TypeColumnName)));
// Add columns to the underlying Management list and as a byproduct get a stale PSObject.
PSObject staleObject = _headerInfo.AddColumnsToWindow(this, liveObject);
// Add the stale PSObject to the underlying Management list.
_graphicalHostReflectionWrapper.CallMethod("AddItem", staleObject);
}
private void AddExtraProperties(PSObject staleObject, PSObject liveObject)
{
// Add 3 extra properties, so that the stale PSObject has meaningful info in the Hetero-type header view.
staleObject.Properties.Add(new PSNoteProperty(OutWindowProxy.IndexPropertyName, _index++));
staleObject.Properties.Add(new PSNoteProperty(OutWindowProxy.OriginalTypePropertyName, liveObject.BaseObject.GetType().FullName));
staleObject.Properties.Add(new PSNoteProperty(OutWindowProxy.OriginalObjectPropertyName, liveObject));
// Convert the LivePSObject to a string preserving PowerShell formatting.
staleObject.Properties.Add(new PSNoteProperty(OutWindowProxy.ToStringValuePropertyName,
_parentCmdlet.ConvertToString(liveObject)));
}
/// <summary>
/// Adds an item to the out window.
/// </summary>
/// <param name="livePSObject">
/// The item to add.
/// </param>
internal void AddItem(PSObject livePSObject)
{
ArgumentNullException.ThrowIfNull(livePSObject);
if (_headerInfo == null)
{
throw new InvalidOperationException();
}
PSObject stalePSObject = _headerInfo.CreateStalePSObject(livePSObject);
// Add 3 extra properties, so that the stale PSObject has meaningful info in the Hetero-type header view.
AddExtraProperties(stalePSObject, livePSObject);
_graphicalHostReflectionWrapper.CallMethod("AddItem", stalePSObject);
}
/// <summary>
/// Adds an item to the out window.
/// </summary>
/// <param name="livePSObject">
/// The item to add.
/// </param>
internal void AddHeteroViewItem(PSObject livePSObject)
{
ArgumentNullException.ThrowIfNull(livePSObject);
if (_headerInfo == null)
{
throw new InvalidOperationException();
}
PSObject stalePSObject = _headerInfo.CreateStalePSObject(livePSObject);
_graphicalHostReflectionWrapper.CallMethod("AddItem", stalePSObject);
}
/// <summary>
/// Shows the out window if it has not already been displayed.
/// </summary>
internal void ShowWindow()
{
if (!_isWindowStarted)
{
_closedEvent = new AutoResetEvent(false);
_graphicalHostReflectionWrapper.CallMethod("StartWindow", _title, _outputMode.ToString(), _closedEvent);
_isWindowStarted = true;
}
}
internal void BlockUntilClosed() => _closedEvent?.WaitOne();
/// <summary>
/// Implements IDisposable logic.
/// </summary>
/// <param name="isDisposing">True if being called from Dispose.</param>
private void Dispose(bool isDisposing)
{
if (isDisposing)
{
if (_closedEvent != null)
{
_closedEvent.Dispose();
_closedEvent = null;
}
}
}
/// <summary>
/// Dispose method in IDisposable.
/// </summary>
public void Dispose()
{
this.Dispose(true);
GC.SuppressFinalize(this);
}
/// <summary>
/// Close the window if it has already been displayed.
/// </summary>
internal void CloseWindow()
{
if (_isWindowStarted)
{
_graphicalHostReflectionWrapper.CallMethod("CloseWindow");
_isWindowStarted = false;
}
}
/// <summary>
/// Gets a value indicating whether the out window is closed.
/// </summary>
/// <returns>
/// True if the out window is closed, false otherwise.
/// </returns>
internal bool IsWindowClosed()
{
return (bool)_graphicalHostReflectionWrapper.CallMethod("GetWindowClosedStatus");
}
/// <summary>Returns any exception that has been thrown by previous method calls.</summary>
/// <returns>The thrown and caught exception. It returns null if no exceptions were thrown by any previous method calls.</returns>
internal Exception GetLastException()
{
return (Exception)_graphicalHostReflectionWrapper.CallMethod("GetLastException");
}
/// <summary>
/// Return the selected item of the OutGridView.
/// </summary>
/// <returns>
/// The selected item.
/// </returns>
internal List<PSObject> GetSelectedItems()
{
return (List<PSObject>)_graphicalHostReflectionWrapper.CallMethod("SelectedItems");
}
}
}
|