private void AddItem(NewItemFactoryTypeModel itemFactoryTypeModel) { if (itemFactoryTypeModel != null) { try { object instance = itemFactoryTypeModel.CreateInstance(); if (instance == null) { return; } this.SetPropertyAsObject(instance); } catch (Exception ex) { ((SceneNodeProperty)this.PropertyValue.get_ParentProperty()).SceneNodeObjectSet.DesignerContext.MessageLoggingService.WriteLine(string.Format((IFormatProvider)CultureInfo.CurrentCulture, ExceptionStringTable.ObjectEditorViewCollectionItemFactoryInstantiateFailed, new object[2] { (object)((object)itemFactoryTypeModel.ItemFactory).GetType().Name, (object)ExtensibilityMetadataHelper.GetExceptionMessage(ex) })); } } else { Type type = this.PromptForClrType(); if (!(type != (Type)null)) { return; } this.SetPropertyAsType(type); } }
// <summary> // Substitutes user-friendly display names for values of properties // </summary> // <param name="item">Item to attempt to identify</param> // <returns>String value for the item (guaranteed to be non-null)</returns> public static string GetDisplayName(object item) { if (item == null) { return(string.Empty); } // Display a user-friendly string for PropertyValues PropertyValue propertyValue = item as PropertyValue; if (propertyValue != null) { return(PropertyValueToDisplayNameConverter.Instance.Convert( propertyValue, typeof(string), null, CultureInfo.CurrentCulture).ToString()); } // Display a user-friendly string for NewItemFactoryTypeModels NewItemFactoryTypeModel model = item as NewItemFactoryTypeModel; if (model != null) { return(NewItemFactoryTypeModelToDisplayNameConverter.Instance.Convert( model, typeof(string), null, CultureInfo.CurrentCulture).ToString()); } // Otherwise, resort to ToString() implementation return(item.ToString()); }
// This method gets called when the CurrentItem on _quickTypeView changes private void OnCurrentQuickTypeChanged(object sender, EventArgs e) { if (_ignoreInternalChanges) { return; } NewItemFactoryTypeModel selectedTypeModel = _quickTypeView.CurrentItem as NewItemFactoryTypeModel; if (selectedTypeModel == null) { return; } Fx.Assert(this.PropertyEntry != null, "PropertyEntry should not be null"); if (this.PropertyEntry == null) { return; } bool previousValue = IgnoreInternalChanges(); try { this.PropertyEntry.PropertyValue.Value = selectedTypeModel.CreateInstance(); } finally { NoticeInternalChanges(previousValue); } }
// Converts an instance of NewItemFactoryTypeModel to its appropriate display name public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { if (typeof(string).IsAssignableFrom(targetType)) { NewItemFactoryTypeModel model = value as NewItemFactoryTypeModel; if (model != null) { return(model.DisplayName ?? string.Empty); } } return(string.Empty); }
// <summary> // Examines the specified ModelProperty for NewItemTypesAttributes and, if found, returns // an enumerable of all NewItemFactoryTypeModels specified through them. // </summary> // <param name="modelProperty">ModelProperty instance to look up</param> // <returns>Returns an enumerable of all NewItemFactoryTypeModels specified through custom // NewItemFactoryTypeModels, if any.</returns> public static IEnumerable <NewItemFactoryTypeModel> GetNewItemFactoryTypeModels(ModelProperty modelProperty, Size desiredIconSize) { List <NewItemTypesAttribute> attributes = GetNewItemTypesAttributes(modelProperty); if (attributes == null) { yield break; } foreach (NewItemTypesAttribute attribute in attributes) { NewItemFactory factory = (NewItemFactory)Activator.CreateInstance(attribute.FactoryType); foreach (Type type in attribute.Types) { NewItemFactoryTypeModel model = null; if (attribute.FactoryType == typeof(NewItemFactory)) { if (EditorUtilities.IsConcreteWithDefaultCtor(type)) { model = new NewItemFactoryTypeModel(type, factory, MessageLogger.Instance); } } else { model = new NewItemFactoryTypeModel(type, factory, MessageLogger.Instance); } if (model != null) { model.DesiredSize = desiredIconSize; yield return(model); } } } }
private void RefreshQuickTypes() { bool previousValue = IgnoreInternalChanges(); try { _quickTypeCollection.Clear(); PropertyEntry containedProperty = this.PropertyEntry; if (containedProperty == null) { return; } ModelProperty property = ((ModelPropertyEntry)containedProperty).FirstModelProperty; Type containerValueType = ((ModelPropertyEntryBase)containedProperty).CommonValueType; NewItemFactoryTypeModel selectedFactoryModel = null; Type defaultItemType = GetDefaultItemType(property); // Find all elligible NewItemFactoryTypes declared through metadata IEnumerable <NewItemFactoryTypeModel> factoryModels = ExtensibilityAccessor.GetNewItemFactoryTypeModels( property, ResourceUtilities.GetDesiredTypeIconSize(this)); if (factoryModels != null) { foreach (NewItemFactoryTypeModel factoryModel in factoryModels) { _quickTypeCollection.Add(factoryModel); if (selectedFactoryModel == null) { if (object.Equals(containerValueType, factoryModel.Type)) { selectedFactoryModel = factoryModel; } } if (defaultItemType != null && object.Equals(defaultItemType, factoryModel.Type)) { defaultItemType = null; } } } //add a null value - user should always have an option to clear property value NewItemFactoryTypeModel nullTypeFactoryTypeModel = new NewItemFactoryTypeModel(null, new NullItemFactory()); // Add a default item type based on the property type (if it wasn't also added through // metadata) if (defaultItemType != null) { NewItemFactoryTypeModel defaultItemFactoryTypeModel = new NewItemFactoryTypeModel(defaultItemType, new NewItemFactory()); _quickTypeCollection.Add(defaultItemFactoryTypeModel); if (selectedFactoryModel == null) { if (object.Equals(containerValueType, defaultItemFactoryTypeModel.Type)) { selectedFactoryModel = defaultItemFactoryTypeModel; } else if (containerValueType == null) { selectedFactoryModel = nullTypeFactoryTypeModel; } } } _quickTypeCollection.Add(nullTypeFactoryTypeModel); // Make sure the currently selected value on the CollectionView reflects the // actual value of the property _quickTypeView.MoveCurrentTo(selectedFactoryModel); this.CurrentQuickType = selectedFactoryModel; } finally { NoticeInternalChanges(previousValue); } }