Code:
/ 4.0 / 4.0 / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / cdf / src / NetFx40 / Tools / System.Activities.Presentation / System / Activities / Presentation / Toolbox / ToolboxItemWrapper.cs / 1305376 / ToolboxItemWrapper.cs
//------------------------------------------------------------ // Copyright (c) Microsoft Corporation. All rights reserved. //----------------------------------------------------------- namespace System.Activities.Presentation.Toolbox { using System; using System.ComponentModel; using System.Drawing; using System.Drawing.Design; using System.Reflection; using System.Windows; using System.Activities.Presentation.View; using System.Globalization; using System.Diagnostics.CodeAnalysis; // This class is a wrapper for ToolboxItem objects. It adds support // for cate----zation of toolbox items. ResolveToolboxItem method // establishes link between actual ToolboxItem instance and Tool representation // in the toolbox (via qualified assembly and type name properties) public sealed class ToolboxItemWrapper : INotifyPropertyChanged { string toolName; string assemblyName; string bitmapName; string customDisplayName; string defaultDisplayName; Bitmap defaultBitmap; ToolboxItem toolboxItem; Type toolType; public ToolboxItemWrapper() : this(string.Empty, string.Empty, string.Empty, string.Empty) { } public ToolboxItemWrapper(Type toolType) : this(toolType, string.Empty) { } public ToolboxItemWrapper(Type toolType, string displayName) : this(toolType, string.Empty, displayName) { } public ToolboxItemWrapper(Type toolType, string bitmapName, string displayName) : this(toolType.FullName, toolType.Assembly.FullName, bitmapName, displayName) { } public ToolboxItemWrapper(string toolName, string assemblyName, string bitmapName, string displayName) { this.ToolName = toolName; this.AssemblyName = assemblyName; this.BitmapName = bitmapName; this.DisplayName = displayName; this.defaultDisplayName = string.Empty; } public event PropertyChangedEventHandler PropertyChanged; internal ToolboxItem ToolboxItem { get { return this.toolboxItem; } private set { if (this.toolboxItem != value) { this.toolboxItem = value; RaisePropertyChanged("IsValid"); RaisePropertyChanged("ToolboxItem"); } } } public bool IsValid { get { return (null != this.toolboxItem); } } public string ToolName { get { return this.toolName; } set { if (null != this.toolboxItem) { throw FxTrace.Exception.AsError(new InvalidOperationException(SR.ToolboxItemFrozenDescription)); } bool isChanged = !string.Equals(value, this.toolName); if (isChanged) { this.toolName = value; RaisePropertyChanged("ToolName"); this.ToolboxItem = null; } } } public string AssemblyName { get { return this.assemblyName; } set { if (null != this.toolboxItem) { throw FxTrace.Exception.AsError(new InvalidOperationException(SR.ToolboxItemFrozenDescription)); } bool isChanged = !string.Equals(value, this.assemblyName); if (isChanged) { this.assemblyName = value; RaisePropertyChanged("AssemblyName"); this.ToolboxItem = null; } } } public string BitmapName { get { return this.bitmapName; } set { bool isChanged = !string.Equals(value, this.bitmapName); if (isChanged) { this.bitmapName = value; RaisePropertyChanged("BitmapName"); LoadBitmap(); } } } public Bitmap Bitmap { get { return this.ToolboxItem.Bitmap; } } public string DisplayName { get { return this.ToolboxItem != null ? this.ToolboxItem.DisplayName : this.customDisplayName; } set { bool isChanged = !string.Equals(value, this.customDisplayName); if (isChanged) { this.customDisplayName = value; ChangeToolboxDisplayName(); RaisePropertyChanged("DisplayName"); } } } [SuppressMessage("Microsoft.Naming", "CA1721:PropertyNamesShouldNotMatchGetMethods", Justification = "By design.")] public Type Type { get { return this.toolType; } private set { bool isChanged = !Type.Equals(this.toolType, value); if (isChanged) { this.toolType = value; RaisePropertyChanged("Type"); } } } internal bool ResolveToolboxItem() { if (null != this.ToolboxItem) { return true; } try { if (null == this.AssemblyName || null == this.ToolName) { throw FxTrace.Exception.AsError(new ArgumentNullException(null == AssemblyName ? "AssemblyName" : "ToolName")); } Assembly toolAssembly = Assembly.Load(this.AssemblyName); Type discoveredToolType = toolAssembly.GetType(this.ToolName, true, true); ValidateTool(discoveredToolType); ToolboxItemAttribute[] toolboxItemAttributes = discoveredToolType.GetCustomAttributes(typeof(ToolboxItemAttribute), true) as ToolboxItemAttribute[]; ToolboxItem instance = null; if (0 != toolboxItemAttributes.Length) { instance = Activator.CreateInstance(toolboxItemAttributes[0].ToolboxItemType) as ToolboxItem; } else { instance = new ToolboxItem(discoveredToolType); } this.ToolboxItem = instance; this.defaultDisplayName = instance.DisplayName; this.defaultBitmap = instance.Bitmap; LoadBitmap(); ChangeToolboxDisplayName(); this.Type = discoveredToolType; return true; } catch { this.ToolboxItem = null; this.Type = null; throw; } } void LoadBitmap() { try { if (null != this.toolboxItem && !string.IsNullOrEmpty(this.BitmapName)) { this.toolboxItem.Bitmap = new Bitmap(this.BitmapName); } } catch (ArgumentException) { this.toolboxItem.Bitmap = this.defaultBitmap; } RaisePropertyChanged("ToolboxItem"); RaisePropertyChanged("Bitmap"); } void ChangeToolboxDisplayName() { if (null != this.toolboxItem) { if (!string.IsNullOrEmpty(this.customDisplayName)) { this.toolboxItem.DisplayName = this.customDisplayName; } else { this.toolboxItem.DisplayName = this.defaultDisplayName; } RaisePropertyChanged("ToolboxItem"); } } void RaisePropertyChanged(string propertyName) { if (null != PropertyChanged) { PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); } } void ValidateTool(Type toolType) { bool isInvalid = toolType.IsAbstract; isInvalid |= toolType.IsInterface; isInvalid |= !toolType.IsVisible; ConstructorInfo ctor = toolType.GetConstructor(Type.EmptyTypes); isInvalid |= (null == ctor); if (isInvalid) { string reason = string.Empty; if (toolType.IsAbstract) { reason = "IsAbstract == true "; } if (toolType.IsInterface) { reason += "IsInterface == true "; } if (!toolType.IsVisible) { reason += "IsVisible == false "; } if (null == ctor) { reason += SR.NoDefaultCtorError; } string error = string.Format(CultureInfo.CurrentCulture, SR.NotSupportedToolboxTypeFormatString, toolType.Name, reason); throw FxTrace.Exception.AsError(new NotSupportedException(error)); } } public override string ToString() { return this.ToolName; } } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007. //------------------------------------------------------------ // Copyright (c) Microsoft Corporation. All rights reserved. //----------------------------------------------------------- namespace System.Activities.Presentation.Toolbox { using System; using System.ComponentModel; using System.Drawing; using System.Drawing.Design; using System.Reflection; using System.Windows; using System.Activities.Presentation.View; using System.Globalization; using System.Diagnostics.CodeAnalysis; // This class is a wrapper for ToolboxItem objects. It adds support // for cate----zation of toolbox items. ResolveToolboxItem method // establishes link between actual ToolboxItem instance and Tool representation // in the toolbox (via qualified assembly and type name properties) public sealed class ToolboxItemWrapper : INotifyPropertyChanged { string toolName; string assemblyName; string bitmapName; string customDisplayName; string defaultDisplayName; Bitmap defaultBitmap; ToolboxItem toolboxItem; Type toolType; public ToolboxItemWrapper() : this(string.Empty, string.Empty, string.Empty, string.Empty) { } public ToolboxItemWrapper(Type toolType) : this(toolType, string.Empty) { } public ToolboxItemWrapper(Type toolType, string displayName) : this(toolType, string.Empty, displayName) { } public ToolboxItemWrapper(Type toolType, string bitmapName, string displayName) : this(toolType.FullName, toolType.Assembly.FullName, bitmapName, displayName) { } public ToolboxItemWrapper(string toolName, string assemblyName, string bitmapName, string displayName) { this.ToolName = toolName; this.AssemblyName = assemblyName; this.BitmapName = bitmapName; this.DisplayName = displayName; this.defaultDisplayName = string.Empty; } public event PropertyChangedEventHandler PropertyChanged; internal ToolboxItem ToolboxItem { get { return this.toolboxItem; } private set { if (this.toolboxItem != value) { this.toolboxItem = value; RaisePropertyChanged("IsValid"); RaisePropertyChanged("ToolboxItem"); } } } public bool IsValid { get { return (null != this.toolboxItem); } } public string ToolName { get { return this.toolName; } set { if (null != this.toolboxItem) { throw FxTrace.Exception.AsError(new InvalidOperationException(SR.ToolboxItemFrozenDescription)); } bool isChanged = !string.Equals(value, this.toolName); if (isChanged) { this.toolName = value; RaisePropertyChanged("ToolName"); this.ToolboxItem = null; } } } public string AssemblyName { get { return this.assemblyName; } set { if (null != this.toolboxItem) { throw FxTrace.Exception.AsError(new InvalidOperationException(SR.ToolboxItemFrozenDescription)); } bool isChanged = !string.Equals(value, this.assemblyName); if (isChanged) { this.assemblyName = value; RaisePropertyChanged("AssemblyName"); this.ToolboxItem = null; } } } public string BitmapName { get { return this.bitmapName; } set { bool isChanged = !string.Equals(value, this.bitmapName); if (isChanged) { this.bitmapName = value; RaisePropertyChanged("BitmapName"); LoadBitmap(); } } } public Bitmap Bitmap { get { return this.ToolboxItem.Bitmap; } } public string DisplayName { get { return this.ToolboxItem != null ? this.ToolboxItem.DisplayName : this.customDisplayName; } set { bool isChanged = !string.Equals(value, this.customDisplayName); if (isChanged) { this.customDisplayName = value; ChangeToolboxDisplayName(); RaisePropertyChanged("DisplayName"); } } } [SuppressMessage("Microsoft.Naming", "CA1721:PropertyNamesShouldNotMatchGetMethods", Justification = "By design.")] public Type Type { get { return this.toolType; } private set { bool isChanged = !Type.Equals(this.toolType, value); if (isChanged) { this.toolType = value; RaisePropertyChanged("Type"); } } } internal bool ResolveToolboxItem() { if (null != this.ToolboxItem) { return true; } try { if (null == this.AssemblyName || null == this.ToolName) { throw FxTrace.Exception.AsError(new ArgumentNullException(null == AssemblyName ? "AssemblyName" : "ToolName")); } Assembly toolAssembly = Assembly.Load(this.AssemblyName); Type discoveredToolType = toolAssembly.GetType(this.ToolName, true, true); ValidateTool(discoveredToolType); ToolboxItemAttribute[] toolboxItemAttributes = discoveredToolType.GetCustomAttributes(typeof(ToolboxItemAttribute), true) as ToolboxItemAttribute[]; ToolboxItem instance = null; if (0 != toolboxItemAttributes.Length) { instance = Activator.CreateInstance(toolboxItemAttributes[0].ToolboxItemType) as ToolboxItem; } else { instance = new ToolboxItem(discoveredToolType); } this.ToolboxItem = instance; this.defaultDisplayName = instance.DisplayName; this.defaultBitmap = instance.Bitmap; LoadBitmap(); ChangeToolboxDisplayName(); this.Type = discoveredToolType; return true; } catch { this.ToolboxItem = null; this.Type = null; throw; } } void LoadBitmap() { try { if (null != this.toolboxItem && !string.IsNullOrEmpty(this.BitmapName)) { this.toolboxItem.Bitmap = new Bitmap(this.BitmapName); } } catch (ArgumentException) { this.toolboxItem.Bitmap = this.defaultBitmap; } RaisePropertyChanged("ToolboxItem"); RaisePropertyChanged("Bitmap"); } void ChangeToolboxDisplayName() { if (null != this.toolboxItem) { if (!string.IsNullOrEmpty(this.customDisplayName)) { this.toolboxItem.DisplayName = this.customDisplayName; } else { this.toolboxItem.DisplayName = this.defaultDisplayName; } RaisePropertyChanged("ToolboxItem"); } } void RaisePropertyChanged(string propertyName) { if (null != PropertyChanged) { PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); } } void ValidateTool(Type toolType) { bool isInvalid = toolType.IsAbstract; isInvalid |= toolType.IsInterface; isInvalid |= !toolType.IsVisible; ConstructorInfo ctor = toolType.GetConstructor(Type.EmptyTypes); isInvalid |= (null == ctor); if (isInvalid) { string reason = string.Empty; if (toolType.IsAbstract) { reason = "IsAbstract == true "; } if (toolType.IsInterface) { reason += "IsInterface == true "; } if (!toolType.IsVisible) { reason += "IsVisible == false "; } if (null == ctor) { reason += SR.NoDefaultCtorError; } string error = string.Format(CultureInfo.CurrentCulture, SR.NotSupportedToolboxTypeFormatString, toolType.Name, reason); throw FxTrace.Exception.AsError(new NotSupportedException(error)); } } public override string ToString() { return this.ToolName; } } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007.
Link Menu
This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- StrongNamePublicKeyBlob.cs
- CodeTypeConstructor.cs
- WinEventQueueItem.cs
- AsnEncodedData.cs
- CalculatedColumn.cs
- LoadItemsEventArgs.cs
- DeferredSelectedIndexReference.cs
- ProcessThread.cs
- _AuthenticationState.cs
- RectangleGeometry.cs
- BindingCompleteEventArgs.cs
- DetailsViewDeleteEventArgs.cs
- Camera.cs
- SizeConverter.cs
- OrthographicCamera.cs
- ManipulationStartedEventArgs.cs
- XmlAnyElementAttribute.cs
- MapPathBasedVirtualPathProvider.cs
- XmlCharCheckingWriter.cs
- AlgoModule.cs
- Pkcs9Attribute.cs
- _FtpDataStream.cs
- UpdatableWrapper.cs
- AtomicFile.cs
- CustomValidator.cs
- Nodes.cs
- SspiHelper.cs
- Icon.cs
- WebPartDescriptionCollection.cs
- Tile.cs
- filewebrequest.cs
- HTMLTagNameToTypeMapper.cs
- KeyTimeConverter.cs
- wgx_render.cs
- DataKey.cs
- brushes.cs
- StoreItemCollection.cs
- FontResourceCache.cs
- XdrBuilder.cs
- TokenCreationException.cs
- DataGridRow.cs
- TextBoxAutoCompleteSourceConverter.cs
- StateItem.cs
- InvokeBase.cs
- ToolBar.cs
- BindingOperations.cs
- Thumb.cs
- NamespaceInfo.cs
- OrderPreservingSpoolingTask.cs
- RangeValuePattern.cs
- BulletChrome.cs
- CanonicalFontFamilyReference.cs
- DbReferenceCollection.cs
- GenericsNotImplementedException.cs
- SemaphoreSecurity.cs
- DataGridViewLinkColumn.cs
- HtmlShim.cs
- StringValueConverter.cs
- EntityProviderServices.cs
- DrawingCollection.cs
- SqlAggregateChecker.cs
- PictureBoxDesigner.cs
- Profiler.cs
- BaseParser.cs
- EntryPointNotFoundException.cs
- MappingMetadataHelper.cs
- HyperLinkField.cs
- DataGridViewRowHeightInfoNeededEventArgs.cs
- StyleTypedPropertyAttribute.cs
- Hyperlink.cs
- DesignerTextWriter.cs
- HttpCachePolicyWrapper.cs
- AutoSizeToolBoxItem.cs
- TypeBuilder.cs
- DataObjectAttribute.cs
- Verify.cs
- columnmapfactory.cs
- OperatorExpressions.cs
- TaskDesigner.cs
- BitmapCodecInfo.cs
- NameTable.cs
- OleDbRowUpdatedEvent.cs
- ToolStripItemCollection.cs
- UiaCoreApi.cs
- SingleConverter.cs
- KnownBoxes.cs
- NumberFunctions.cs
- DeviceContext.cs
- TreeNodeBindingDepthConverter.cs
- InkPresenter.cs
- ComplexBindingPropertiesAttribute.cs
- ReadOnlyHierarchicalDataSource.cs
- SyntaxCheck.cs
- LayoutEditorPart.cs
- InputBuffer.cs
- ToolboxDataAttribute.cs
- ListViewItem.cs
- SystemUnicastIPAddressInformation.cs
- _NegoState.cs
- TextShapeableCharacters.cs