/// <summary> /// Gets tags for context menu (right click) commands</summary> /// <param name="context">Context containing target object</param> /// <param name="target">Right clicked object, or null if none</param> /// <returns>Enumeration of command tags for context menu</returns> public IEnumerable <object> GetCommands(object context, object target) { EditingContext = context.As <IPropertyEditingContext>(); //ok if context is null Descriptor = target as ChildAttributePropertyDescriptor; m_multiPropertyDescriptor = null; // Check for MultiPropertyDescriptor in case multiple objects are selected. if (Descriptor == null) { var multi = target as MultiPropertyDescriptor; if (multi != null) { bool allAreGood = true; ChildAttributePropertyDescriptor oneDynamic = null; foreach (var single in multi.GetDescriptors()) { oneDynamic = single as ChildAttributePropertyDescriptor; if (oneDynamic == null) { allAreGood = false; break; } } if (allAreGood && oneDynamic != null) { Descriptor = oneDynamic; m_multiPropertyDescriptor = multi; } } } // Try to get a client-defined IPropertyEditingContext. if (EditingContext == null) { // Otherwise, try to get a client-defined ISelectionContext and adapt it. ISelectionContext selectionContext = context.As <ISelectionContext>(); DefaultContext.SelectionContext = selectionContext; if (selectionContext != null) { EditingContext = DefaultContext; } } if (CanDoCommand(Command.Add)) { yield return(Command.Add); } if (CanDoCommand(Command.Edit)) { yield return(Command.Edit); } if (CanDoCommand(Command.Remove)) { yield return(Command.Remove); } }
protected DomNode GetDynamicDomNode(ScriptNode module) { ChildAttributePropertyDescriptor dynamicPropertyDescriptor = Descriptor; if (m_multiPropertyDescriptor != null) { dynamicPropertyDescriptor = (ChildAttributePropertyDescriptor) m_multiPropertyDescriptor.FindDescriptor(module); } if (dynamicPropertyDescriptor != null) { return(dynamicPropertyDescriptor.GetNode(module)); } return(null); }
/// <summary> /// Creates an array of property descriptors that are associated with the adapted DomNode's /// DomNodeType. No duplicates are in the array (based on the property descriptor's Name /// property).</summary> /// <returns>Array of property descriptors</returns> protected override System.ComponentModel.PropertyDescriptor[] GetPropertyDescriptors() { System.ComponentModel.PropertyDescriptor[] baseDescriptors = base.GetPropertyDescriptors(); IList <DomNode> dynamicPropertyChildren = DomNode.GetChildList(groupType.dynamicPropertyChild); if (dynamicPropertyChildren.Count == 0) { return(baseDescriptors); } var result = new List <System.ComponentModel.PropertyDescriptor>(baseDescriptors); int childIndex = 0; foreach (var child in dynamicPropertyChildren) { var displayName = (string)child.GetAttribute(dynamicPropertyType.nameAttribute); var category = (string)child.GetAttribute(dynamicPropertyType.categoryAttribute); var description = (string)child.GetAttribute(dynamicPropertyType.descriptionAttribute); bool readOnly = false; var editorTypeAndParameters = (string)child.GetAttribute(dynamicPropertyType.editorAttribute); object editor = CreateObject(editorTypeAndParameters); var typeConverterAndParameters = (string)child.GetAttribute(dynamicPropertyType.converterAttribute); var typeConverter = (TypeConverter)CreateObject(typeConverterAndParameters); string valueType = (string)child.GetAttribute(dynamicPropertyType.valueTypeAttribute); PropertyDescriptor newDescriptor; if (valueType == "stringValue") { newDescriptor = new ChildAttributePropertyDescriptor( displayName, dynamicPropertyType.stringValueAttribute, moduleType.dynamicPropertyChild, childIndex, category, description, readOnly, editor, typeConverter); } else if (valueType == "floatValue") { newDescriptor = new ChildAttributePropertyDescriptor( displayName, dynamicPropertyType.floatValueAttribute, moduleType.dynamicPropertyChild, childIndex, category, description, readOnly, editor, typeConverter); } else if (valueType == "vector3Value") { newDescriptor = new ChildAttributePropertyDescriptor( displayName, dynamicPropertyType.vector3ValueAttribute, moduleType.dynamicPropertyChild, childIndex, category, description, readOnly, editor, typeConverter); } else if (valueType == "boolValue") { newDescriptor = new ChildAttributePropertyDescriptor( displayName, dynamicPropertyType.boolValueAttribute, moduleType.dynamicPropertyChild, childIndex, category, description, readOnly, editor, typeConverter); } else if (valueType == "intValue") { newDescriptor = new ChildAttributePropertyDescriptor( displayName, dynamicPropertyType.intValueAttribute, moduleType.dynamicPropertyChild, childIndex, category, description, readOnly, editor, typeConverter); } else { throw new InvalidOperationException("Unknown valueType attribute '" + valueType + "' for dynamic property: " + displayName); } result.Add(newDescriptor); childIndex++; } return(result.ToArray()); }
public void TestEquality() { var attrType1 = new AttributeType("xkcd", typeof(string)); var attrInfo1 = new AttributeInfo("xkcd", attrType1); var domNodeType = new DomNodeType("WebComic", DomNodeType.BaseOfAllTypes); var childInfo1 = new ChildInfo("xkcd", domNodeType); attrInfo1.DefaultValue = "Firefly"; var desc1 = new ChildAttributePropertyDescriptor( "xkcd", attrInfo1, childInfo1, "Category 1", "A commonly used word or phrase in the xkcd comic", true); int originalHashCode = desc1.GetHashCode(); // test if two identically created property descriptors compare as being equal var desc2 = new ChildAttributePropertyDescriptor( "xkcd", attrInfo1, childInfo1, "Category 1", "A commonly used word or phrase in the xkcd comic", true); Assert.AreEqual(desc1, desc2); Assert.AreEqual(desc1.GetHashCode(), desc2.GetHashCode()); // test category being different; oddly, although I think they should not be considered equal, // the .NET PropertyDescriptor ignores the difference in category name. For our purposes, we // want to allow for two different property editors to appear with the same name but different // categories. This is how our PropertyUtils.PropertyDescriptorsEqual() and GetPropertyDescriptorHash() // work. var desc3 = new ChildAttributePropertyDescriptor( "xkcd", attrInfo1, childInfo1, "Category 2", "A commonly used word or phrase in the xkcd comic", true); Assert.AreNotEqual(desc1, desc3); Assert.AreNotEqual(desc1.GetHashCode(), desc3.GetHashCode()); // test description being different; similarly here, the .Net PropertyDescriptor doesn't care. var desc4 = new ChildAttributePropertyDescriptor( "xkcd", attrInfo1, childInfo1, "Category 1", "slightly different description", true); Assert.AreEqual(desc1, desc4); Assert.AreEqual(desc1.GetHashCode(), desc4.GetHashCode()); // test readOnly being different; ditto for read-only flag! var desc5 = new ChildAttributePropertyDescriptor( "xkcd", attrInfo1, childInfo1, "Category 1", "A commonly used word or phrase in the xkcd comic", false); Assert.AreEqual(desc1, desc5); Assert.AreEqual(desc1.GetHashCode(), desc5.GetHashCode()); // test that the hash code hasn't changed after using the AttributeInfo var attrInfo2 = new AttributeInfo("xkcd", attrType1); domNodeType.Define(attrInfo2); Assert.AreEqual(desc1.GetHashCode(), originalHashCode); // test that the hash code hasn't changed after creating a derived DomNodeType var derivedDomNodeType = new DomNodeType("ScientificWebComic", domNodeType); var derivedAttrInfo = new AttributeInfo("xkcd", attrType1); var derivedChildInfo = new ChildInfo("xkcd", derivedDomNodeType); derivedDomNodeType.Define(derivedAttrInfo); Assert.AreEqual(desc1.GetHashCode(), originalHashCode); // test that an AttributeInfo used in a derived DomNodeType doesn't change equality or hash code var desc6 = new ChildAttributePropertyDescriptor( "xkcd", derivedAttrInfo, derivedChildInfo, "Category 1", "A commonly used word or phrase in the xkcd comic", true); Assert.AreEqual(desc1, desc6); Assert.AreEqual(desc1.GetHashCode(), desc6.GetHashCode()); // test that a different name counts as a different property descriptor var desc7 = new ChildAttributePropertyDescriptor( "xkcd2", attrInfo1, childInfo1, "Category 1", "different name", true); Assert.AreNotEqual(desc1, desc7); Assert.AreNotEqual(desc1.GetHashCode(), desc7.GetHashCode()); }
public void TestEquality() { var attrType1 = new AttributeType("xkcd", typeof(string)); var attrInfo1 = new AttributeInfo("xkcd", attrType1); var domNodeType = new DomNodeType("WebComic", DomNodeType.BaseOfAllTypes); var childInfo1 = new ChildInfo("xkcd", domNodeType); attrInfo1.DefaultValue = "Firefly"; var desc1 = new ChildAttributePropertyDescriptor( "xkcd", attrInfo1, childInfo1, "Category 1", "A commonly used word or phrase in the xkcd comic", true); int originalHashCode = desc1.GetHashCode(); // test if two identically created property descriptors compare as being equal var desc2 = new ChildAttributePropertyDescriptor( "xkcd", attrInfo1, childInfo1, "Category 1", "A commonly used word or phrase in the xkcd comic", true); Assert.AreEqual(desc1, desc2); Assert.AreEqual(desc1.GetHashCode(), desc2.GetHashCode()); // test category being different; oddly, although I think they should not be considered equal, // the .Net PropertyDescriptor ignores the difference in category name. So, I'm guessing that // the AttributePropertyDescriptor should behave the same as PropertyDescriptor. var desc3 = new ChildAttributePropertyDescriptor( "xkcd", attrInfo1, childInfo1, "Category 2", "A commonly used word or phrase in the xkcd comic", true); Assert.AreEqual(desc1, desc3); Assert.AreEqual(desc1.GetHashCode(), desc3.GetHashCode()); // test description being different; similarly here, the .Net PropertyDescriptor doesn't care. var desc4 = new ChildAttributePropertyDescriptor( "xkcd", attrInfo1, childInfo1, "Category 1", "slightly different description", true); Assert.AreEqual(desc1, desc4); Assert.AreEqual(desc1.GetHashCode(), desc4.GetHashCode()); // test readOnly being different; ditto for read-only flag! var desc5 = new ChildAttributePropertyDescriptor( "xkcd", attrInfo1, childInfo1, "Category 1", "A commonly used word or phrase in the xkcd comic", false); Assert.AreEqual(desc1, desc5); Assert.AreEqual(desc1.GetHashCode(), desc5.GetHashCode()); // test that the hash code hasn't changed after using the AttributeInfo var attrInfo2 = new AttributeInfo("xkcd", attrType1); domNodeType.Define(attrInfo2); Assert.AreEqual(desc1.GetHashCode(), originalHashCode); // test that the hash code hasn't changed after creating a derived DomNodeType var derivedDomNodeType = new DomNodeType("ScientificWebComic", domNodeType); var derivedAttrInfo = new AttributeInfo("xkcd", attrType1); var derivedChildInfo = new ChildInfo("xkcd", derivedDomNodeType); derivedDomNodeType.Define(derivedAttrInfo); Assert.AreEqual(desc1.GetHashCode(), originalHashCode); // test that an AttributeInfo used in a derived DomNodeType doesn't change equality or hash code var desc6 = new ChildAttributePropertyDescriptor( "xkcd", derivedAttrInfo, derivedChildInfo, "Category 1", "A commonly used word or phrase in the xkcd comic", true); Assert.AreEqual(desc1, desc6); Assert.AreEqual(desc1.GetHashCode(), desc6.GetHashCode()); }