Exemplo n.º 1
0
        public void TestDefaultValue()
        {
            AttributeType type = new AttributeType("test", typeof(string));
            AttributeInfo test = new AttributeInfo("test", type);
            test.DefaultValue = "foo";
            Assert.AreEqual(test.DefaultValue, "foo");
            test.DefaultValue = null;
            Assert.AreEqual(test.DefaultValue, type.GetDefault());
            Assert.Throws<InvalidOperationException>(delegate { test.DefaultValue = 1; });

            AttributeType length2Type = new AttributeType("length2Type", typeof(int[]), 2);
            AttributeInfo length2Info = new AttributeInfo("length2", length2Type);
            Assert.AreEqual(length2Info.DefaultValue, length2Type.GetDefault());
            Assert.AreEqual(length2Info.DefaultValue, new int[] { default(int), default(int) });
            DomNodeType nodeType = new DomNodeType("testNodeType");
            nodeType.Define(length2Info);
            DomNode node = new DomNode(nodeType);
            Assert.AreEqual(node.GetAttribute(length2Info), length2Info.DefaultValue);
            node.SetAttribute(length2Info, new int[] { 1, 2 });
            Assert.AreEqual(node.GetAttribute(length2Info), new int[] { 1, 2 });
            node.SetAttribute(length2Info, new int[] { 1 });
            Assert.AreEqual(node.GetAttribute(length2Info), new int[] { 1 });

            AttributeType length1Type = new AttributeType("length1Type", typeof(int[]), 1);
            AttributeInfo length1Info = new AttributeInfo("length1", length1Type);
            Assert.AreEqual(length1Info.DefaultValue, length1Type.GetDefault());
            Assert.AreEqual(length1Info.DefaultValue, new int[] { default(int) });
            nodeType = new DomNodeType("testNodeType");
            nodeType.Define(length1Info);
            node = new DomNode(nodeType);
            Assert.AreEqual(node.GetAttribute(length1Info), length1Info.DefaultValue);
            node.SetAttribute(length1Info, new int[] { 1 });
            Assert.AreEqual(node.GetAttribute(length1Info), new int[] { 1 });
        }
Exemplo n.º 2
0
 public void TestConstructor()
 {
     AttributeType type = new AttributeType("test", typeof(string));
     AttributeInfo test = new AttributeInfo("test", type);
     Assert.AreEqual(test.Name, "test");
     Assert.AreEqual(test.Type, type);
     Assert.AreEqual(test.DefaultValue, type.GetDefault());
 }
Exemplo n.º 3
0
        /// <summary>
        /// Validates the given value for assignment to the given attribute</summary>
        /// <param name="value">Value to validate</param>
        /// <param name="info">Attribute info</param>
        /// <returns>True, iff value is valid for the given attribute</returns>
        public override bool Validate(object value, AttributeInfo info)
        {
            foreach (string s in m_values)
                if (s.Equals(value))
                    return true;

            return false;
        }
        /// <summary>
        /// Constructor</summary>
        /// <param name="name">Attribute's display name</param>
        /// <param name="attribute">Attribute metadata</param>
        /// <param name="category">Category of property</param>
        /// <param name="description">Description of property</param>
        /// <param name="isReadOnly">Whether or not property is read-only</param>
        public AttributePropertyDescriptor(
            string name,
            AttributeInfo attribute,
            string category,
            string description,
            bool isReadOnly)

            : this(name, attribute, category, description, isReadOnly, null, null)
        {
        }
Exemplo n.º 5
0
        /// <summary>
        /// Validates the given value for assignment to the given attribute</summary>
        /// <param name="value">Value to validate</param>
        /// <param name="info">Attribute info</param>
        /// <returns>True iff value is valid for the given attribute</returns>
        public override bool Validate(object value, AttributeInfo info)
        {
            if (value is Byte)
            {
                Byte v = (Byte)value;
                return v < m_maximum || (m_inclusive && v == m_maximum);
            }
            if (value is SByte)
            {
                SByte v = (SByte)value;
                return v < m_maximum || (m_inclusive && v == m_maximum);
            }
            if (value is UInt16)
            {
                UInt16 v = (UInt16)value;
                return v < m_maximum || (m_inclusive && v == m_maximum);
            }
            if (value is Int16)
            {
                Int16 v = (Int16)value;
                return v < m_maximum || (m_inclusive && v == m_maximum);
            }
            if (value is UInt32)
            {
                UInt32 v = (UInt32)value;
                return v < m_maximum || (m_inclusive && v == m_maximum);
            }
            if (value is Int32)
            {
                Int32 v = (Int32)value;
                return v < m_maximum || (m_inclusive && v == m_maximum);
            }
            if (value is UInt64)
            {
                UInt64 v = (UInt64)value;
                return v < m_maximum || (m_inclusive && v == m_maximum);
            }
            if (value is Int64)
            {
                Int64 v = (Int64)value;
                return v < m_maximum || (m_inclusive && v == m_maximum);
            }
            if (value is Single)
            {
                Single v = (Single)value;
                return v < m_maximum || (m_inclusive && v == m_maximum);
            }
            if (value is Double)
            {
                Double v = (Double)value;
                return v < m_maximum || (m_inclusive && v == m_maximum);
            }

            return false;
        }
        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());
        }
Exemplo n.º 7
0
 /// <summary>
 /// Gets the DomNode attribute as a Sphere3F</summary>
 /// <param name="domNode">DomNode holding value</param>
 /// <param name="attribute">Attribute of the DomNode that contains the data</param>
 /// <returns>DomNode attribute as a Sphere3F</returns>
 public static Sphere3F GetSphere(DomNode domNode, AttributeInfo attribute)
 {
     Sphere3F s = new Sphere3F();
     float[] value = domNode.GetAttribute(attribute) as float[];
     if (value != null)
     {
         s.Center = new Vec3F(value[0], value[1], value[2]);
         s.Radius = value[3];
     }
     return s;
 }
Exemplo n.º 8
0
 /// <summary>
 /// Returns true iff the specified attribute is translation, rotation or scale</summary>
 /// <param name="attributeInfo"></param>
 /// <returns>True iff the specified attribute is translation, rotation or scale</returns>
 private bool IsTransformAttribute(AttributeInfo attributeInfo)
 {
     // Always use the Equivalent method when comparing two AttributeInfos
     // because using simple equality (==) doesn't respect inheritance
     // i.e. (Schema.baseType.someAttribute == Schema.derivedType.someAttribute) returns false
     // whereas (Schema.baseType.someAttribue.Equivalent(Schema.derivedType.someAttribute)) returns true
     return (attributeInfo.Equivalent(Schema.transformObjectType.translateAttribute)
             || attributeInfo.Equivalent(Schema.transformObjectType.rotateAttribute)
             || attributeInfo.Equivalent(Schema.transformObjectType.scaleAttribute)
             || attributeInfo.Equivalent(Schema.transformObjectType.pivotAttribute));
 }
Exemplo n.º 9
0
 /// <summary>
 /// Constructor</summary>
 /// <param name="node">DomNode whose attribute has or will change</param>
 /// <param name="attributeInfo">Attribute metadata for the changing or changed attribute</param>
 /// <param name="oldValue">Previous value, as determined by DomNode.GetAttribute(attributeInfo)</param>
 /// <param name="newValue">New value</param>
 public AttributeEventArgs(
     DomNode node,
     AttributeInfo attributeInfo,
     object oldValue,
     object newValue)
 {
     DomNode = node;
     AttributeInfo = attributeInfo;
     OldValue = oldValue;
     NewValue = newValue;
 }
        /// <summary>
        /// Constructor</summary>
        /// <param name="name">Value's display name</param>
        /// <param name="attributeInfos">An array of meta attributes in the collection</param>
        /// <param name="childInfo">Meta element identifying child that owns the attributes</param>
        /// <param name="category">Category of property</param>
        /// <param name="description">Description of property</param>
        /// <param name="isReadOnly">Whether or not property is read-only</param>
        public ChildAttributeCollectionPropertyDescriptor(
            string name,
            AttributeInfo[] attributeInfos,
            ChildInfo childInfo,
            string category,
            string description,
            bool isReadOnly)

            : this(name, attributeInfos, childInfo, category, description, isReadOnly, null, null)
        {
        }
Exemplo n.º 11
0
        public static Matrix4F GetMatrix4x4(this DomNodeAdapter node, AttributeInfo attributeInfo)
        {
            object value = node.DomNode.GetAttribute(attributeInfo);

            // if value is not null, attempt the cast; an invalid type will then cause
            //  an IllegalCastException; all value type attributes have a default
            //  value, so will return here
            if (value != null)
                return new Matrix4F((float[])value);

            return default(Matrix4F);
        }
Exemplo n.º 12
0
 /// <summary>
 /// Converts the give string to attribute value and set it to given node using attributeInfo</summary>
 /// <param name="node">DomNode </param>
 /// <param name="attributeInfo">attributeInfo to set</param>
 /// <param name="valueString">The string representation of the attribute value</param>
 protected override void ReadAttribute(DomNode node, AttributeInfo attributeInfo, string valueString)
 {
     base.ReadAttribute(node, attributeInfo, valueString);
     if (node.Type == Schema.templateFolderType.Type && attributeInfo.Name == "referenceFile")
     {
         Uri uri;
         if (Uri.TryCreate(valueString, UriKind.RelativeOrAbsolute, out uri))
         {
             ImportTemplateLibrary(node, uri);
         }
     }
 }
        /// <summary>
        /// Constructor</summary>
        /// <param name="name">Attribute's display name</param>
        /// <param name="attribute">Attribute metadata</param>
        /// <param name="category">Category of property</param>
        /// <param name="description">Description of property</param>
        /// <param name="isReadOnly">Whether or not property is read-only</param>
        /// <param name="editor">The editor for editing this property</param>
        /// <param name="typeConverter">TypeConverter for this property</param>
        public AttributePropertyDescriptor(
            string name,
            AttributeInfo attribute,
            string category,
            string description,
            bool isReadOnly,
            object editor,
            TypeConverter typeConverter)

            : this(name, attribute, category, description, isReadOnly, editor, typeConverter, null)
        {
        }
        /// <summary>
        /// Constructor</summary>
        /// <param name="name">Value's display name</param>
        /// <param name="attributeInfo">Attribute metadata</param>
        /// <param name="childInfo">ChildInfo identifying child</param>
        /// <param name="category">Category of property</param>
        /// <param name="description">Description of property</param>
        /// <param name="isReadOnly">Whether or not property is read-only</param>
        /// <param name="editor">The editor used to edit the property</param>
        public ChildAttributePropertyDescriptor(
            string name,
            AttributeInfo attributeInfo,
            ChildInfo childInfo,
            string category,
            string description,
            bool isReadOnly,
            object editor)

            : this(name, attributeInfo, childInfo, category, description, isReadOnly, editor, null)
        {
        }
Exemplo n.º 15
0
        /// <summary>
        /// Gets the DomNode attribute as a Vec3F and returns true, or returns false if the attribute
        /// doesn't exist</summary>
        /// <param name="domNode">DomNode holding the attribute</param>
        /// <param name="attribute">Attribute of the DomNode that contains the data</param>
        /// <param name="result">The resulting Vec3F. Is (0,0,0) if the attribute couldn't be found</param>
        /// <returns>True iff the attribute was found and was converted to a Vec3F</returns>
        public static bool GetVector(DomNode domNode, AttributeInfo attribute, out Vec3F result)
        {
            float[] floats = domNode.GetAttribute(attribute) as float[];
            if (floats != null)
            {
                result = new Vec3F(floats);
                return true;
            }

            result = new Vec3F();
            return false;
        }
Exemplo n.º 16
0
		protected override string Convert(DomNode node, AttributeInfo attributeInfo)
		{
			object value = node.GetAttribute(attributeInfo);
			if (attributeInfo.Type.Type == AttributeTypes.Reference && attributeInfo.Name == "guidRef")
			{
				// guidRef refers a template whose guid value should be persisted
				var templateNode = value as DomNode;
				return templateNode.GetAttribute(Schema.templateType.guidAttribute) as string;
			}

			return base.Convert(node, attributeInfo);
		}
 /// <summary>
 /// Constructor</summary>
 /// <param name="name">Value's display name</param>
 /// <param name="attributeInfos">An array of meta attributes in the collection</param>
 /// <param name="childInfo">Meta element identifying child that owns the attributes</param>
 /// <param name="category">Category of property</param>
 /// <param name="description">Description of property</param>
 /// <param name="isReadOnly">Whether or not property is read-only</param>
 /// <param name="editor">The editor used to edit the property</param>
 /// <param name="typeConverter">The type converter used for this property</param>
 public ChildAttributeCollectionPropertyDescriptor(
     string name,
     AttributeInfo[] attributeInfos,
     ChildInfo childInfo,
     string category,
     string description,
     bool isReadOnly,
     object editor,
     TypeConverter typeConverter)
     : this(name, attributeInfos, childInfo, category, description, isReadOnly, editor, typeConverter, null)
 {
 }
Exemplo n.º 18
0
        /// <summary>
        /// Converts the give string to attribute value and set it to given node
        /// using attributeInfo.         
        /// </summary>
        /// <param name="node">DomNode </param>
        /// <param name="attributeInfo">attributeInfo to set</param>
        /// <param name="valueString">The string representation of the attribute value</param>
        protected override void ReadAttribute(DomNode node, AttributeInfo attributeInfo, string valueString)
        {
            if (IsReferenceAttribute(attributeInfo))
            {
                // save reference so it can be resolved after all nodes have been read
                NodeReferences.Add(new XmlNodeReference(node, attributeInfo, valueString));
            }
            else if (!string.IsNullOrEmpty(valueString))
            {
                object value = attributeInfo.Type.Convert(valueString);

                if (value is Uri)
                {
                    //todo reference to objects in other documents must be made absolute using
                    //this Uri instead of resourceRoot.

                    // then convert it to absolute.
                    Uri ur = (Uri)value;
                    if (!ur.IsAbsoluteUri)
                    {
                        // todo use schema annotation to decide what to use
                        // for converting relative uri to absolute.
                        if (node.Type == Schema.gameReferenceType.Type
                            || node.Type == Schema.gameObjectReferenceType.Type
                            || LevelEditorXLE.Patches.IsReferenceType(node.Type))
                        {
                            string urStr = ur.ToString();
                            int fragIndex = urStr.LastIndexOf('#');
                            if (fragIndex == -1)
                            {
                                value = new Uri(Uri, ur);
                            }
                            else
                            {
                                string frag = urStr.Substring(fragIndex);
                                string path = urStr.Substring(0, fragIndex);
                                Uri absUri = new Uri(Uri, path);
                                value = new Uri(absUri + frag);
                            }

                        }
                        else
                        {
                            value = new Uri(m_resourceRoot, ur);
                        }

                    }
                }
                node.SetAttribute(attributeInfo, value);
            }
        }
Exemplo n.º 19
0
        public static Vec3F GetVec3(this DomNodeAdapter node, AttributeInfo attributeInfo)
        {
            return LevelEditorCore.DomNodeUtil.GetVector(node.DomNode, attributeInfo);

            // object value = node.DomNode.GetAttribute(attributeInfo);
            //
            // // if value is not null, attempt the cast; an invalid type will then cause
            // //  an IllegalCastException; all value type attributes have a default
            // //  value, so will return here
            // if (value != null)
            //     return new Vec3F((float[])value);
            //
            // return default(Vec3F);
        }
        /// <summary>
        /// Constructor</summary>
        /// <param name="name">Value's display name</param>
        /// <param name="attributeInfo">Attribute metadata</param>
        /// <param name="childInfo">ChildInfo identifying child</param>
        /// <param name="category">Category of property</param>
        /// <param name="description">Description of property</param>
        /// <param name="isReadOnly">Whether or not property is read-only</param>
        /// <param name="editor">The editor used to edit the property</param>
        /// <param name="typeConverter">The type converter used for this property</param>
        public ChildAttributePropertyDescriptor(
            string name,
            AttributeInfo attributeInfo,
            ChildInfo childInfo,
            string category,
            string description,
            bool isReadOnly,
            object editor,
            TypeConverter typeConverter)

            : base(name, attributeInfo, category, description, isReadOnly, editor, typeConverter)
        {
            m_childPath = new[] { childInfo };
        }
Exemplo n.º 21
0
 /// <summary>
 /// Observe the passed attribute on the given node.
 /// if the attribute changed then expression needs to run.
 /// </summary>        
 public void ObserveAttribute(DomNode node, AttributeInfo attrib)
 {
     if (m_running)
     {
         string key = CreateKey(node, attrib);
         HashSet<string> exprSet;
         if (!m_sourceAttributeMap.TryGetValue(key, out exprSet))
         {
             exprSet = new HashSet<string>();
             m_sourceAttributeMap.Add(key, exprSet);
         }
         exprSet.Add(m_runningExpressionId);
     }
 }
        /// <summary>
        /// Constructor</summary>
        /// <param name="name">Attribute's display name</param>
        /// <param name="attribute">Attribute metadata</param>
        /// <param name="category">Category of property</param>
        /// <param name="description">Description of property</param>
        /// <param name="isReadOnly">Whether or not property is read-only</param>
        /// <param name="editor">The editor for editing this property</param>
        /// <param name="typeConverter">TypeConverter for this property</param>
        /// <param name="attributes">An array of attributes for this property</param>
        public AttributePropertyDescriptor(
            string name,
            AttributeInfo attribute,
            string category,
            string description,
            bool isReadOnly,
            object editor,
            TypeConverter typeConverter,
            Attribute[] attributes)

            : base(name, attribute.Type.ClrType, category, description, isReadOnly, editor, typeConverter, attributes)
        {
            m_attributeInfo = attribute;
        }
Exemplo n.º 23
0
        public void TestValidation()
        {
            AttributeType type = new AttributeType("test", typeof(string));
            AttributeInfo test = new AttributeInfo("test", type);
            CollectionAssert.IsEmpty(test.Rules);

            var rule = new SimpleAttributeRule();
            test.AddRule(rule);

            Utilities.TestSequenceEqual(test.Rules, rule);

            Assert.True(test.Validate("bar"));
            Assert.True(rule.Validated);

            Assert.False(test.Validate(1)); // wrong type
        }
Exemplo n.º 24
0
        public TestValidator()
        {
            // define a tree of validation contexts
            m_childType = new DomNodeType("test");
            m_stringAttrInfo = GetStringAttribute("string");
            m_childType.Define(m_stringAttrInfo);
            m_refAttrInfo = GetRefAttribute("ref");
            m_childType.Define(m_refAttrInfo);
            m_childInfo = new ChildInfo("child", m_childType);
            m_childType.Define(m_childInfo);
            m_childType.Define(new ExtensionInfo<ValidationContext>());

            // define a distinct root type with the validator
            m_rootType = new DomNodeType("root");
            m_rootType.BaseType = m_childType;
            m_rootType.Define(new ExtensionInfo<Validator>());

            IEnumerable<AttributeInfo> attributes = m_rootType.Attributes; // freezes the types
        }
Exemplo n.º 25
0
        /// <summary>
        /// Converts attribute to string.
        /// WriteAttributes(..) call this method to convert dom attribute to string
        /// before writing.        
        /// </summary>
        /// <param name="node">DomNode that owns the attribute to be converted</param>
        /// <param name="attributeInfo">The attribute that need to be converted</param>
        /// <returns>the string value of the attribute</returns>
        protected override string Convert(DomNode node, AttributeInfo attributeInfo)
        {
            // if attribute is uri, then convert it to relative if it is absolute.

            string valueString = string.Empty;
            object value = node.GetAttribute(attributeInfo);
            if (value == null) return valueString;

            if (attributeInfo.Type.Type == AttributeTypes.Reference)
            {
                // if reference is a valid node, convert to string
                DomNode refNode = value as DomNode;
                if (refNode != null)
                    valueString = GetNodeReferenceString(refNode, Root, Uri);
            }
            else if (attributeInfo.Type.Type == AttributeTypes.Uri)
            {
                Uri ur = (Uri)value;
                if (ur.IsAbsoluteUri)
                {
                    // todo use schema annotation to choose between resource root and this uri                    
                    if (node.Type == Schema.gameReferenceType.Type
                        || node.Type == Schema.gameObjectReferenceType.Type)
                    {// use this Uri to make it relative.
                        ur = Uri.MakeRelativeUri(ur);
                    }
                    else
                    {// use resource root to make it relative
                        ur = m_resourceRoot.MakeRelativeUri(ur);
                    }

                    ur = new Uri(Uri.UnescapeDataString(ur.ToString()), UriKind.Relative);
                    valueString = ur.ToString();
                }
            }
            else
            {
                valueString = attributeInfo.Type.Convert(value);
            }

            return valueString;
        }
Exemplo n.º 26
0
        public void TestCustomAttributeInfo()
        {
            AttributeInfo info = new AttributeInfo("foo", new AttributeType("foo", typeof(string)));
            DomNodeType test = new DomNodeType(
                "test",
                null,
                new AttributeInfo[] { info },
                EmptyEnumerable<ChildInfo>.Instance,
                EmptyEnumerable<ExtensionInfo>.Instance);

            Utilities.TestSequenceEqual(test.Attributes, info);
            Assert.True(test.IsValid(info));
            Assert.AreSame(test.GetAttributeInfo("foo"), info);
            // check that type is now frozen
            Assert.Throws<InvalidOperationException>(delegate { test.Define(GetStringAttribute("notFoo")); });

            Assert.AreEqual(info.OwningType, test);
            Assert.AreEqual(info.DefiningType, test);
            Assert.Null(test.GetAttributeInfo("bar"));
        }
Exemplo n.º 27
0
        protected DomNodeType RootType;//derives from ChildType

        public TestValidator()
        {
            // define a tree of validation contexts
            ChildType = new DomNodeType("test");
            StringAttrInfo = GetStringAttribute("string");
            ChildType.Define(StringAttrInfo);
            RefAttrInfo = GetRefAttribute("ref");
            ChildType.Define(RefAttrInfo);
            ChildInfo = new ChildInfo("child", ChildType);
            ChildType.Define(ChildInfo);
            ChildType.Define(new ExtensionInfo<ValidationContext>());

            // define a distinct root type with the validator
            RootType = new DomNodeType("root");
            RootType.BaseType = ChildType;
            RootType.Define(new ExtensionInfo<Validator>());
            AttributeInfo overriddenInfo = GetStringAttribute("string");
            RootType.Define(overriddenInfo);

            IEnumerable<AttributeInfo> attributes = RootType.Attributes; // freezes the types
        }
Exemplo n.º 28
0
 /// <summary>
 /// Sets the value of the adapted DomNode's attribute</summary>
 /// <param name="attributeInfo">The attribute metadata of the adapted DomNode to look for</param>
 /// <param name="value">New value</param>
 protected void SetAttribute(AttributeInfo attributeInfo, object value)
 {
     DomNode.SetAttribute(attributeInfo, value);
 }
Exemplo n.º 29
0
        /// <summary>
        /// Reads the node specified by the child metadata</summary>
        /// <param name="nodeInfo">Child metadata for node</param>
        /// <param name="reader">XML reader</param>
        /// <returns>DomNode specified by the child metadata</returns>
        protected virtual DomNode ReadElement(ChildInfo nodeInfo, XmlReader reader)
        {
            // handle polymorphism, if necessary
            DomNodeType type = null;
            var         substitutionGroupRule = nodeInfo.Rules.OfType <SubstitutionGroupChildRule>().FirstOrDefault();

            if (substitutionGroupRule != null)
            {
                foreach (var sub in substitutionGroupRule.Substitutions)
                {
                    if (sub.Name == reader.LocalName)
                    {
                        type = sub.Type;
                        break;
                    }
                }

                // Fallback to non-substituted version (for example loading an old schema).
                if (type == null)
                {
                    type = GetChildType(nodeInfo.Type, reader);
                }

                if (type == null)
                {
                    throw new InvalidOperationException("Could not match substitution group for child " + nodeInfo.Name);
                }
            }
            else
            {
                type = GetChildType(nodeInfo.Type, reader);
            }

            int    index  = type.Name.LastIndexOf(':');
            string typeNS = index >= 0 ? type.Name.Substring(0, index) : "";

            DomNode node = new DomNode(type, nodeInfo);

            // read attributes
            while (reader.MoveToNextAttribute())
            {
                if (reader.Prefix == string.Empty ||
                    reader.LookupNamespace(reader.Prefix) == typeNS)
                {
                    AttributeInfo attributeInfo = type.GetAttributeInfo(reader.LocalName);
                    if (attributeInfo != null)
                    {
                        ReadAttribute(node, attributeInfo, reader.Value);
                    }
                }
            }

            // add node to map if it has an id
            if (node.Type.IdAttribute != null)
            {
                string id = node.GetId();
                if (!string.IsNullOrEmpty(id))
                {
                    m_nodeDictionary[id] = node; // don't Add, in case there are multiple DomNodes with the same id
                }
            }

            reader.MoveToElement();

            if (!reader.IsEmptyElement)
            {
                // read child elements
                while (reader.Read())
                {
                    if (reader.NodeType == XmlNodeType.Element)
                    {
                        // look up metadata for this element
                        ChildInfo childInfo = type.GetChildInfo(reader.LocalName);
                        if (childInfo == null)
                        {
                            // Try and get substitution group
                            childInfo = GetSubsitutionGroup(type, reader.LocalName);
                        }

                        if (childInfo != null)
                        {
                            DomNode childNode = ReadElement(childInfo, reader);
                            if (childNode != null)
                            {
                                // childNode is fully populated sub-tree
                                if (childInfo.IsList)
                                {
                                    node.GetChildList(childInfo).Add(childNode);
                                }
                                else
                                {
                                    node.SetChild(childInfo, childNode);
                                }
                            }
                        }
                        else
                        {
                            // try reading as an attribute
                            AttributeInfo attributeInfo = type.GetAttributeInfo(reader.LocalName);
                            if (attributeInfo != null)
                            {
                                reader.MoveToElement();

                                if (!reader.IsEmptyElement)
                                {
                                    // read element text
                                    while (reader.Read())
                                    {
                                        if (reader.NodeType == XmlNodeType.Text)
                                        {
                                            ReadAttribute(node, attributeInfo, reader.Value);
                                            // skip child elements, as this is an attribute value
                                            reader.Skip();
                                            break;
                                        }
                                        if (reader.NodeType == XmlNodeType.EndElement)
                                        {
                                            break;
                                        }
                                    }

                                    reader.MoveToContent();
                                }
                            }
                            else
                            {
                                // skip unrecognized element
                                reader.Skip();
                                // if that takes us to the end of the enclosing element, break
                                if (reader.NodeType == XmlNodeType.EndElement)
                                {
                                    break;
                                }
                            }
                        }
                    }
                    else if (reader.NodeType == XmlNodeType.Text)
                    {
                        AttributeInfo attributeInfo = type.GetAttributeInfo(string.Empty);
                        if (attributeInfo != null)
                        {
                            ReadAttribute(node, attributeInfo, reader.Value);
                        }
                    }
                    else if (reader.NodeType == XmlNodeType.EndElement)
                    {
                        break;
                    }
                }
            }

            reader.MoveToContent();

            return(node);
        }
Exemplo n.º 30
0
        /// <summary>
        /// Checks whether the given timeline object's attribute is editable for the current
        /// context and document</summary>
        /// <param name="item">Timeline object that changed</param>
        /// <param name="attribute">Attribute on the timeline object that changed</param>
        /// <returns>True iff this timeline object attribute is editable for the current
        /// ActiveControl, ActiveContext, and ActiveDocument properties</returns>
        public virtual bool IsEditable(ITimelineObject item, AttributeInfo attribute)
        {
            if (attribute == Schema.groupType.expandedAttribute)
                return true;

            TimelinePath path = new TimelinePath(item);
            return ActiveControl.IsEditable(path);
        }
Exemplo n.º 31
0
 /// <summary>
 /// Determines if attribute is a reference</summary>
 /// <param name="attributeInfo">Attribute</param>
 /// <returns>True iff attribute is reference</returns>
 protected virtual bool IsReferenceAttribute(AttributeInfo attributeInfo)
 {
     return(attributeInfo.Type.Type == AttributeTypes.Reference);
 }
        /// <summary>
        /// Constructor</summary>
        /// <param name="name">Value's display name</param>
        /// <param name="attributeInfo">Attribute metadata</param>
        /// <param name="childPath">ChildInfo array describing path to descendant child</param>
        /// <param name="childIndices">The index to use in each ChildInfo if that ChildInfo is a list</param>
        /// <param name="category">Category of property</param>
        /// <param name="description">Description of property</param>
        /// <param name="isReadOnly">Whether or not property is read-only</param>
        /// <param name="editor">The editor used to edit the property</param>
        /// <param name="typeConverter">The type converter used for this property</param>
        public ChildAttributePropertyDescriptor(
            string name,
            AttributeInfo attributeInfo,
            IEnumerable<ChildInfo> childPath,
            IEnumerable<int> childIndices,
            string category,
            string description,
            bool isReadOnly,
            object editor,
            TypeConverter typeConverter)

            : base(name, attributeInfo, category, description, isReadOnly, editor, typeConverter)
        {
            m_childPath = childPath;
            m_childIndices = childIndices;
        }
Exemplo n.º 33
0
        /// <summary>
        /// Finishes initializing component by adding palette information and defining module types</summary>
        public virtual void Initialize()
        {
            // add palette info to annotation type, and register with palette
            var annotationItem = new NodeTypePaletteItem(
                Schema.annotationType.Type,
                "Comment".Localize(),
                "Create a moveable resizable comment on the circuit canvas".Localize(),
                Resources.AnnotationImage);

            m_paletteService.AddItem(
                annotationItem, "Misc".Localize("abbreviation for miscellaneous"), this);

            // define editable properties on annotation
            Schema.annotationType.Type.SetTag(
                new PropertyDescriptorCollection(
                    new PropertyDescriptor[] {
                            new AttributePropertyDescriptor(
                                "Text".Localize(),
                                Schema.annotationType.textAttribute,
                                null,
                                "Comment Text".Localize(),
                                false),
                            new AttributePropertyDescriptor(
                                "Background Color".Localize(),  // name
                                Schema.annotationType.backcolorAttribute, //AttributeInfo
                                null, // category
                                "Comment's background color".Localize(), //description
                                false, //isReadOnly
                                new Sce.Atf.Controls.PropertyEditing.ColorEditor(), // editor
                                new Sce.Atf.Controls.PropertyEditing.IntColorConverter() // typeConverter
                                ),
                           new AttributePropertyDescriptor(
                                "Foreground Color".Localize(),  // name
                                Schema.annotationType.foreColorAttribute, //AttributeInfo
                                null, // category
                                "Comment's foreground color".Localize(), //description
                                false, //isReadOnly
                                new Sce.Atf.Controls.PropertyEditing.ColorEditor(), // editor
                                new Sce.Atf.Controls.PropertyEditing.IntColorConverter() // typeConverter
                                ),
                   }));

            // define module types

            DefineModuleType(
                new XmlQualifiedName("buttonType", Schema.NS),
                "Button".Localize(),
                "On/Off Button".Localize(),
                Resources.ButtonImage,
                EmptyArray<ElementType.Pin>.Instance,
                new ElementType.Pin[]
                {
                    new ElementType.Pin("Out".Localize(), BooleanPinTypeName, 0)
                },
                m_schemaLoader);

            var lightType = DefineModuleType(
                new XmlQualifiedName("lightType", Schema.NS),
                "Light".Localize(),
                "Light source".Localize(),
                Resources.LightImage,
                new ElementType.Pin[]
                {
                    new ElementType.Pin("In".Localize(), BooleanPinTypeName, 0)
                },
                EmptyArray<ElementType.Pin>.Instance,
                m_schemaLoader);

            // define and add new attribute to light type.
            var featuresAttribute = new AttributeInfo("features".Localize(), AttributeType.IntType);
            featuresAttribute.DefaultValue = 3;
            lightType.Define(featuresAttribute);
            // create property descriptor for the above attribute.
            FlagsUITypeEditor featuresEditor = new FlagsUITypeEditor(new[] { "ENERGY STAR", "High CRI" }, new[] { 1, 2 });
            FlagsTypeConverter featuresConverter = new FlagsTypeConverter(new[] { "Energy Star", "High CRI" }, new[] { 1, 2 });
            lightType.SetTag(
               new PropertyDescriptorCollection(
                   new PropertyDescriptor[] {
                        new AttributePropertyDescriptor(
                            "Features".Localize(),
                            featuresAttribute,
                            null, //category
                            "Features".Localize(), //description
                            false,
                            featuresEditor,
                            featuresConverter) //is not read-only
                    }));

            


            DomNodeType speakerNodeType = DefineModuleType(
                new XmlQualifiedName("speakerType", Schema.NS),
                "Speaker".Localize("an electronic speaker, for playing sounds"),
                "Speaker".Localize("an electronic speaker, for playing sounds"),
                Resources.SpeakerImage,
                new ElementType.Pin[]
                {
                    new ElementType.Pin("In".Localize(), FloatPinTypeName, 0),
                },
                EmptyArray<ElementType.Pin>.Instance,
                m_schemaLoader);
            var speakerManufacturerInfo = new AttributeInfo("Manufacturer".Localize(), AttributeType.StringType);

            // add bass level attribute to demo/test BoundedFloatEditor()
            float bassMin = -1;
            float bassMax = 1;
            var bassLevel = new AttributeInfo("bassLevel".Localize("bass level"), AttributeType.FloatType);
            bassLevel.AddRule(new NumericMinRule(bassMin, true));
            bassLevel.AddRule(new NumericMaxRule(bassMax, true));
            bassLevel.DefaultValue = 0.1f;
            //bassLevel.def
            speakerNodeType.Define(bassLevel);

            speakerNodeType.Define(speakerManufacturerInfo);
            speakerNodeType.SetTag(
                new PropertyDescriptorCollection(
                    new PropertyDescriptor[] {
                        new AttributePropertyDescriptor(
                            "Manufacturer".Localize(),
                            speakerManufacturerInfo,
                            null, //category
                            "Manufacturer".Localize(), //description
                            false),
             
                        new AttributePropertyDescriptor(
                            "Bass Level".Localize(),
                            bassLevel,
                            null, //category
                            "Bass Level".Localize(), //description
                            false,
                            new BoundedFloatEditor(bassMin,bassMax))
                    }));

            DefineModuleType(
                new XmlQualifiedName("soundType", Schema.NS),
                "Sound".Localize(),
                "Sound Player".Localize(),
                Resources.SoundImage,
                new ElementType.Pin[]
                {
                    new ElementType.Pin("On".Localize(), "boolean", 0),
                    new ElementType.Pin("Reset".Localize(), "boolean", 1),
                    new ElementType.Pin("Pause".Localize(), "boolean", 2),
                },
                new ElementType.Pin[]
                {
                    new ElementType.Pin("Out".Localize(), "float", 0),
                },
                m_schemaLoader);

            DefineModuleType(
                new XmlQualifiedName("andType", Schema.NS),
                "And".Localize(),
                "Logical AND".Localize(),
                Resources.AndImage,
                new ElementType.Pin[]
                {
                    new ElementType.Pin("In1".Localize("input pin #1"), "boolean", 0),
                    new ElementType.Pin("In2".Localize("input pin #2"), "boolean", 1),
                },
                new ElementType.Pin[]
                {
                    new ElementType.Pin("Out".Localize(), "boolean", 0),
                },
                m_schemaLoader);

            DefineModuleType(
                new XmlQualifiedName("orType", Schema.NS),
                "Or".Localize(),
                "Logical OR".Localize(),
                Resources.OrImage,
                new ElementType.Pin[]
                {
                    new ElementType.Pin("In1".Localize("input pin #1"), "boolean", 0),
                    new ElementType.Pin("In2".Localize("input pin #2"), "boolean", 1),
                },
                new ElementType.Pin[]
                {
                    new ElementType.Pin("Out".Localize(), "boolean", 0),
                },
                m_schemaLoader);

            DefineModuleType(
                new XmlQualifiedName("16To1MultiplexerType", Schema.NS),
                "16-to-1 Multiplexer".Localize(),
                "16-to-1 Multiplexer".Localize(),
                Resources.AndImage,
                new ElementType.Pin[]
                {
                    new ElementType.Pin("In1".Localize("input pin #1"), "boolean", 0),
                    new ElementType.Pin("In2".Localize("input pin #2"), "boolean", 1),
                    new ElementType.Pin("In3".Localize(), "boolean", 2),
                    new ElementType.Pin("In4".Localize(), "boolean", 3),
                    new ElementType.Pin("In5".Localize(), "boolean", 4),
                    new ElementType.Pin("In6".Localize(), "boolean", 5),
                    new ElementType.Pin("In7".Localize(), "boolean", 6),
                    new ElementType.Pin("In8".Localize(), "boolean", 7),
                    new ElementType.Pin("In9".Localize(), "boolean", 8),
                    new ElementType.Pin("In10".Localize(), "boolean", 9),
                    new ElementType.Pin("In11".Localize(), "boolean", 10),
                    new ElementType.Pin("In12".Localize(), "boolean", 11),
                    new ElementType.Pin("In13".Localize(), "boolean", 12),
                    new ElementType.Pin("In14".Localize(), "boolean", 13),
                    new ElementType.Pin("In15".Localize(), "boolean", 14),
                    new ElementType.Pin("In16".Localize(), "boolean", 15),
                    new ElementType.Pin("Select1".Localize("The name of a pin on a circuit element"), "boolean", 16),
                    new ElementType.Pin("Select2".Localize("The name of a pin on a circuit element"), "boolean", 17),
                    new ElementType.Pin("Select3".Localize("The name of a pin on a circuit element"), "boolean", 18),
                    new ElementType.Pin("Select4".Localize("The name of a pin on a circuit element"), "boolean", 19),
                },
                new ElementType.Pin[]
                {
                    new ElementType.Pin("Out".Localize(), "boolean", 0),
                },
                m_schemaLoader);

            DefineModuleType(
                new XmlQualifiedName("1To16DemultiplexerType", Schema.NS),
                "1-to-16 Demultiplexer".Localize(),
                "1-to-16 Demultiplexer".Localize(),
                Resources.OrImage,
                new ElementType.Pin[]
                {
                    new ElementType.Pin("Data".Localize(), "boolean", 0),
                    new ElementType.Pin("Select1".Localize("The name of a pin on a circuit element"), "boolean", 1),
                    new ElementType.Pin("Select2".Localize("The name of a pin on a circuit element"), "boolean", 2),
                    new ElementType.Pin("Select3".Localize("The name of a pin on a circuit element"), "boolean", 3),
                    new ElementType.Pin("Select4".Localize("The name of a pin on a circuit element"), "boolean", 4),
                },
                new ElementType.Pin[]
                {
                    new ElementType.Pin("Out1".Localize(), "boolean", 0),
                    new ElementType.Pin("Out2".Localize(), "boolean", 1),
                    new ElementType.Pin("Out3".Localize(), "boolean", 2),
                    new ElementType.Pin("Out4".Localize(), "boolean", 3),
                    new ElementType.Pin("Out5".Localize(), "boolean", 4),
                    new ElementType.Pin("Out6".Localize(), "boolean", 5),
                    new ElementType.Pin("Out7".Localize(), "boolean", 6),
                    new ElementType.Pin("Out8".Localize(), "boolean", 7),
                    new ElementType.Pin("Out9".Localize(), "boolean", 8),
                    new ElementType.Pin("Out10".Localize(), "boolean", 9),
                    new ElementType.Pin("Out11".Localize(), "boolean", 10),
                    new ElementType.Pin("Out12".Localize(), "boolean", 11),
                    new ElementType.Pin("Out13".Localize(), "boolean", 12),
                    new ElementType.Pin("Out14".Localize(), "boolean", 13),
                    new ElementType.Pin("Out15".Localize(), "boolean", 14),
                    new ElementType.Pin("Out16".Localize(), "boolean", 15),
                },
                m_schemaLoader);
        }
Exemplo n.º 34
0
 /// <summary>
 /// Determines if attribute is a reference</summary>
 /// <param name="attributeInfo">Attribute</param>
 /// <returns>True iff attribute is reference</returns>
 protected override bool IsReferenceAttribute(Sce.Atf.Dom.AttributeInfo attributeInfo)
 {
     return(base.IsReferenceAttribute(attributeInfo) || attributeInfo.Type.Name.EndsWith("aifPathType"));
 }
Exemplo n.º 35
0
        /// <summary>
        /// Sets the attribute of the adapted DomNode to reference a new DomNode</summary>
        /// <param name="attributeInfo">Metadata to indicate which attribute to set</param>
        /// <param name="value">Any IAdaptable, such as DomNodeAdapter, DomNode, or IAdapter</param>
        protected void SetReference(AttributeInfo attributeInfo, IAdaptable value)
        {
            DomNode refNode = value.As <DomNode>();

            DomNode.SetAttribute(attributeInfo, refNode);
        }