/// <summary> /// Displays the value in the control. /// </summary> public void ShowValue( NodeId nodeId, uint attributeId, string name, object value, bool readOnly) { m_readOnly = readOnly; NavigationMENU.Items.Clear(); if (m_readOnly) { ValuesDV.EditMode = DataGridViewEditMode.EditProgrammatically; TextValueTB.ReadOnly = true; } Type type = null; // determine the expected data type for non-value attributes. if (attributeId != 0 && attributeId != Attributes.Value) { BuiltInType builtInType = TypeInfo.GetBuiltInType(Attributes.GetDataTypeId(attributeId)); int valueRank = Attributes.GetValueRank(attributeId); type = TypeInfo.GetSystemType(builtInType, valueRank); } // determine the expected data type for value attributes. else if (!NodeId.IsNull(nodeId)) { IVariableBase variable = m_session.NodeCache.Find(nodeId) as IVariableBase; if (variable != null) { BuiltInType builtInType = TypeInfo.GetBuiltInType(variable.DataType, m_session.TypeTree); int valueRank = variable.ValueRank; type = TypeInfo.GetSystemType(builtInType, valueRank); if (builtInType == BuiltInType.ExtensionObject && valueRank < 0) { type = TypeInfo.GetSystemType(variable.DataType, m_session.Factory); } } } // use the value. else if (value != null) { type = value.GetType(); } // go with default. else { type = typeof(string); } // assign a name. if (String.IsNullOrEmpty(name)) { if (attributeId != 0) { name = Attributes.GetBrowseName(attributeId); } else { name = type.Name; } } AccessInfo info = new AccessInfo(); info.Value = Utils.Clone(value); info.TypeInfo = TypeInfo.Construct(type); if (value == null && info.TypeInfo.ValueRank < 0) { info.Value = TypeInfo.GetDefaultValue(info.TypeInfo.BuiltInType); } info.Name = name; m_value = info; ShowValue(info); }
/// <summary> /// Adds the value at an array index to the control. /// </summary> private void ShowIndexedValue(AccessInfo info) { DataRow row = m_dataset.Tables[0].NewRow(); StringBuilder buffer = new StringBuilder(); buffer.Append("["); if (info.Indexes != null) { for (int ii = 0; ii < info.Indexes.Length; ii++) { if (ii > 0) { buffer.Append(","); } buffer.Append(info.Indexes[ii]); } } buffer.Append("]"); info.Name = buffer.ToString(); row[0] = info; row[1] = info.Name; row[2] = GetDataTypeString(info); row[3] = ValueToString(info.Value, info.TypeInfo); row[4] = ImageList.Images[ClientUtils.GetImageIndex(Attributes.Value, info.Value)]; m_dataset.Tables[0].Rows.Add(row); }
public void InvokeTableItemRemoveAccessMember(Authentication authentication, ITableItem tableItem, AccessInfo accessInfo, string memberID) { this.CremaHost.DebugMethod(authentication, this, nameof(InvokeTableItemRemoveAccessMember), tableItem, memberID); var accessInfoPath = tableItem.GetAccessInfoPath(); try { accessInfo.Remove(authentication.SignatureDate, memberID); tableItem.WriteAccessInfo(accessInfoPath, accessInfo); } catch (Exception e) { this.CremaHost.Error(e); this.repository.Revert(); throw e; } }
/// <summary> /// Displays the value in the control. /// </summary> private void ShowValue(AccessInfo parent) { ShowValueNoNotify(parent); if (m_ValueChanged != null) { m_ValueChanged(this, null); } }
/// <summary> /// Returns the data type of the value. /// </summary> private string GetDataTypeString(AccessInfo accessInfo) { Type type = GetDataType(accessInfo); if (type == null) { return accessInfo.TypeInfo.ToString(); } return type.Name; }
/// <summary> /// Whether the value can be edited in the grid view. /// </summary> private bool IsSimpleValue(AccessInfo info) { if (info == null || info.TypeInfo == null) { return true; } TypeInfo typeInfo = info.TypeInfo; object value = info.Value; if (value is Variant) { Variant variant = (Variant)info.Value; typeInfo = variant.TypeInfo; value = variant.Value; if (typeInfo == null) { typeInfo = TypeInfo.Construct(value); } } if (typeInfo.ValueRank >= 0) { return false; } switch (typeInfo.BuiltInType) { case BuiltInType.String: { string text = value as string; if (text != null && text.Length >= MaxDisplayTextLength) { return false; } return true; } case BuiltInType.ByteString: case BuiltInType.XmlElement: case BuiltInType.QualifiedName: case BuiltInType.LocalizedText: case BuiltInType.DataValue: case BuiltInType.ExtensionObject: { return false; } } return true; }
public void InvokeDataBaseSetPrivate(Authentication authentication, DataBase dataBase, AccessInfo accessInfo) { this.CremaHost.DebugMethod(authentication, this, nameof(InvokeDataBaseSetPrivate), dataBase); var accessInfoPath = dataBase.GetAccessInfoPath(); try { accessInfo.SetPrivate(dataBase.GetType().Name, authentication.SignatureDate); dataBase.WriteAccessInfo(accessInfoPath, accessInfo); this.repository.Add(accessInfoPath); } catch (Exception e) { this.CremaHost.Error(e); this.repository.Revert(dataBase.BasePath); throw e; } }
public void InvokeDataBaseSetPublic(Authentication authentication, DataBase dataBase, AccessInfo accessInfo) { this.CremaHost.DebugMethod(authentication, this, nameof(InvokeDataBaseSetPrivate), dataBase); var accessInfoPath = dataBase.GetAccessInfoPath(); try { accessInfo.SetPublic(); this.repository.Delete(accessInfoPath); } catch (Exception e) { this.CremaHost.Error(e); this.repository.Revert(dataBase.BasePath); throw e; } }
public void InvokeTableItemSetPublic(Authentication authentication, ITableItem tableItem, AccessInfo accessInfo) { this.CremaHost.DebugMethod(authentication, this, nameof(InvokeTableItemSetPrivate), tableItem); }
public void InvokeTableItemRemoveAccessMember(Authentication authentication, ITableItem tableItem, AccessInfo accessInfo, string memberID) { this.CremaHost.DebugMethod(authentication, this, nameof(InvokeTableItemRemoveAccessMember), tableItem, memberID); }
public void SetAccessInfo(AccessInfo accessInfo) { base.AccessInfo = accessInfo; }
public void InvokeTableItemSetPublic(Authentication authentication, ITableItem tableItem, AccessInfo accessInfo) { this.CremaHost.DebugMethod(authentication, this, nameof(InvokeTableItemSetPrivate), tableItem); var accessInfoPath = tableItem.GetAccessInfoPath(); try { accessInfo.SetPublic(); this.repository.Delete(accessInfoPath); } catch (Exception e) { this.CremaHost.Error(e); this.repository.Revert(); throw e; } }
public void InvokeTableItemSetPrivate(Authentication authentication, ITableItem tableItem, AccessInfo accessInfo) { this.CremaHost.DebugMethod(authentication, this, nameof(InvokeTableItemSetPrivate), tableItem); var accessInfoPath = tableItem.GetAccessInfoPath(); try { accessInfo.SetPrivate(tableItem.GetType().Name, authentication.SignatureDate); tableItem.WriteAccessInfo(accessInfoPath, accessInfo); this.repository.Add(accessInfoPath); } catch (Exception e) { this.CremaHost.Error(e); this.repository.Revert(); throw e; } }
/// <summary> /// Displays the value in the control. /// </summary> private void ShowValueNoNotify(AccessInfo parent) { m_dataset.Tables[0].Clear(); ValuesDV.Visible = true; TextValueTB.Visible = false; ToolStripItem item = NavigationMENU.Items.Add(parent.Name); item.Click += new EventHandler(NavigationMENU_Click); item.Tag = parent; TypeInfo typeInfo = parent.TypeInfo; object value = parent.Value; if (value is Variant) { Variant variant = (Variant)value; value = variant.Value; if (value != null) { parent.TypeInfo = typeInfo = variant.TypeInfo; if (typeInfo == null) { parent.TypeInfo = typeInfo = TypeInfo.Construct(value); } } } if (typeInfo.ValueRank >= 0) { Matrix matrix = value as Matrix; if (matrix != null) { value = matrix.ToArray(); } System.Collections.IEnumerable enumerable = value as System.Collections.IEnumerable; if (enumerable != null) { // get the dimensions of any array. int[] dimensions = null; // calculate them. if (matrix == null) { Array array = enumerable as Array; if (array != null) { dimensions = new int[array.Rank]; for (int ii = 0; ii < array.Rank; ii++) { dimensions[ii] = array.GetLength(ii); } } else { dimensions = new int[1]; System.Collections.IList list = enumerable as System.Collections.IList; if (list != null) { dimensions[0] = list.Count; } } } // get them from the matrix. else { dimensions = matrix.Dimensions; } // display the array elements. int count = 0; TypeInfo elementType = new TypeInfo(typeInfo.BuiltInType, ValueRanks.Scalar); ValuesDV.Visible = true; TextValueTB.Visible = false; foreach (object element in enumerable) { int[] indexes = GetIndexFromCount(count++, dimensions); AccessInfo info = new AccessInfo(); info.Parent = parent; info.Indexes = indexes; info.TypeInfo = elementType; info.Value = element; ShowIndexedValue(info); } } return; } // check for null. if (value == null) { if (parent.Parent != null && parent.Parent.Value is Array) { parent.Value = value = Activator.CreateInstance(parent.Parent.Value.GetType().GetElementType()); } else if (parent.Parent != null && parent.PropertyInfo != null) { parent.Value = value = Activator.CreateInstance(parent.PropertyInfo.PropertyType); } else { ShowTextValue(value, parent.TypeInfo); return; } } object structure = value; // check for extension object. ExtensionObject extension = structure as ExtensionObject; if (extension != null) { structure = extension.Body; } // check for XmlElements. if (structure is XmlElement) { ShowTextValue((XmlElement)structure); return; } // check for ByteString. if (structure is byte[]) { ShowTextValue((byte[])structure); return; } // check for NodeId. if (structure is NodeId) { ShowTextValue(((NodeId)structure).ToString()); return; } // check for ExpandedNodeId. if (structure is ExpandedNodeId) { ShowTextValue(((ExpandedNodeId)structure).ToString()); return; } // check for QualifiedName. if (structure is QualifiedName) { ShowTextValue(((QualifiedName)structure).ToString()); return; } // check for Guid. if (structure is Guid) { ShowTextValue(((Guid)structure).ToString()); return; } // check for Uuid. if (structure is Uuid) { ShowTextValue(((Uuid)structure).ToString()); return; } // check for StatusCode. if (structure is StatusCode) { ShowTextValue(Utils.Format("0x{0:X8}", ((StatusCode)structure).Code)); return; } ValuesDV.Visible = true; TextValueTB.Visible = false; // use reflection to display the properties of the structure. bool isStructure = false; PropertyInfo[] properties = structure.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic); foreach (PropertyInfo property in properties) { if (property.GetIndexParameters().Length > 0) { continue; } object element = property.GetValue(structure, null); string name = null; foreach (object attribute in property.GetCustomAttributes(true)) { if (typeof(System.Runtime.Serialization.DataMemberAttribute).IsInstanceOfType(attribute)) { name = ((System.Runtime.Serialization.DataMemberAttribute)attribute).Name; if (name == null) { name = property.Name; } break; } } if (name == null) { continue; } AccessInfo info = new AccessInfo(); info.Parent = parent; info.PropertyInfo = property; info.TypeInfo = TypeInfo.Construct(property.PropertyType); info.Value = element; info.Name = name; ShowNamedValue(info); isStructure = true; } if (!isStructure) { ShowTextValue(parent.Value, parent.TypeInfo); } }
public void InvokeDataBaseRemoveAccessMember(Authentication authentication, DataBase dataBase, AccessInfo accessInfo, string memberID) { this.CremaHost.DebugMethod(authentication, this, nameof(InvokeDataBaseRemoveAccessMember), dataBase, memberID); var accessInfoPath = dataBase.GetAccessInfoPath(); try { accessInfo.Remove(authentication.SignatureDate, memberID); dataBase.WriteAccessInfo(accessInfoPath, accessInfo); } catch (Exception e) { this.CremaHost.Error(e); this.repository.Revert(dataBase.BasePath); throw e; } }
public void SetAccessInfo(AccessInfo accessInfo) { accessInfo.Path = this.Path; base.AccessInfo = accessInfo; }
public static void WriteAccessInfo(this DataBase dataBase, string accessInfoPath, AccessInfo accessInfo) { WriteAccessInfo(accessInfoPath, accessInfo); }
/// <summary> /// Adds the value with the specified name to the control. /// </summary> private void ShowNamedValue(AccessInfo info) { DataRow row = m_dataset.Tables[0].NewRow(); row[0] = info; row[1] = (info.Name != null) ? info.Name : "unknown"; row[2] = GetDataTypeString(info); row[3] = ValueToString(info.Value, info.TypeInfo); row[4] = ImageList.Images[ClientUtils.GetImageIndex(Attributes.Value, info.Value)]; m_dataset.Tables[0].Rows.Add(row); }
private static void WriteAccessInfo(string accessInfoPath, AccessInfo accessInfo) { JsonSerializerUtility.Write(accessInfoPath, (AccessSerializationInfo)accessInfo, true); }
/// <summary> /// Recusivesly updates the parent values. /// </summary> private void UpdateParent(AccessInfo info) { if (info.Parent == null) { return; } object parentValue = info.Parent.Value; if (info.Parent.TypeInfo.BuiltInType == BuiltInType.Variant && info.Parent.TypeInfo.ValueRank < 0) { parentValue = ((Variant)info.Parent.Value).Value; } if (info.PropertyInfo != null && info.Parent.TypeInfo.ValueRank < 0) { ExtensionObject extension = parentValue as ExtensionObject; if (extension != null) { parentValue = extension.Body; } info.PropertyInfo.SetValue(parentValue, info.Value, null); } else if (info.Indexes != null) { int[] indexes = info.Indexes; Array array = parentValue as Array; Matrix matrix = parentValue as Matrix; if (matrix != null) { int count = 0; int block = 1; for (int ii = info.Indexes.Length-1; ii >= 0 ; ii--) { count += info.Indexes[ii] * block; block *= matrix.Dimensions[ii]; } array = matrix.Elements; indexes = new int[] { count }; } if (array != null) { if (info.Parent.TypeInfo.BuiltInType == BuiltInType.Variant && info.Parent.TypeInfo.ValueRank >= 0) { array.SetValue(new Variant(info.Value), indexes); } else { array.SetValue(info.Value, indexes); } } else { IList list = parentValue as IList; if (info.Parent.TypeInfo.BuiltInType == BuiltInType.Variant && info.Parent.TypeInfo.ValueRank >= 0) { list[indexes[0]] = new Variant(info.Value); } else { list[indexes[0]] = info.Value; } } } if (info.Parent != null) { UpdateParent(info.Parent); } }
public static void WriteAccessInfo(this ITableItem tableItem, string accessInfoPath, AccessInfo accessInfo) { if (tableItem is Table table) { WriteAccessInfo(accessInfoPath, accessInfo); } else if (tableItem is TableCategory category) { WriteAccessInfo(accessInfoPath, accessInfo); } else { throw new NotImplementedException(); } }
/// <summary> /// Displays the value in the control. /// </summary> public void ShowValue( TypeInfo expectedType, string name, object value) { m_readOnly = false; NavigationMENU.Items.Clear(); // assign a type. if (expectedType == null) { if (value == null) { expectedType = TypeInfo.Scalars.String; } else { expectedType = TypeInfo.Construct(value); } } // assign a name. if (String.IsNullOrEmpty(name)) { name = expectedType.ToString(); } AccessInfo info = new AccessInfo(); info.Value = Utils.Clone(value); info.TypeInfo = expectedType; if (value == null && info.TypeInfo.ValueRank < 0) { info.Value = TypeInfo.GetDefaultValue(info.TypeInfo.BuiltInType); } // ensure value is the target type. info.Value = TypeInfo.Cast(info.Value, expectedType.BuiltInType); info.Name = name; m_value = info; ShowValue(info); }
/// <summary> /// Recusivesly updates the parent values. /// </summary> private void UpdateParent(AccessInfo info) { if (info.Parent == null) { return; } object parentValue = info.Parent.Value; if (info.Parent.TypeInfo.BuiltInType == BuiltInType.Variant && info.Parent.TypeInfo.ValueRank < 0) { parentValue = ((Variant)info.Parent.Value).Value; } if (info.PropertyInfo != null && info.Parent.TypeInfo.ValueRank < 0) { ExtensionObject extension = parentValue as ExtensionObject; if (extension != null) { parentValue = extension.Body; } info.PropertyInfo.SetValue(parentValue, info.Value, null); } else if (info.Indexes != null) { int[] indexes = info.Indexes; Array array = parentValue as Array; Matrix matrix = parentValue as Matrix; if (matrix != null) { int count = 0; int block = 1; for (int ii = info.Indexes.Length - 1; ii >= 0; ii--) { count += info.Indexes[ii] * block; block *= matrix.Dimensions[ii]; } array = matrix.Elements; indexes = new int[] { count }; } if (array != null) { if (info.Parent.TypeInfo.BuiltInType == BuiltInType.Variant && info.Parent.TypeInfo.ValueRank >= 0) { array.SetValue(new Variant(info.Value), indexes); } else { array.SetValue(info.Value, indexes); } } else { IList list = parentValue as IList; if (info.Parent.TypeInfo.BuiltInType == BuiltInType.Variant && info.Parent.TypeInfo.ValueRank >= 0) { list[indexes[0]] = new Variant(info.Value); } else { list[indexes[0]] = info.Value; } } } if (info.Parent != null) { UpdateParent(info.Parent); } }
/// <summary> /// Changes the array size. /// </summary> public void SetArraySize() { if (!CanSetArraySize) { return; } EndEdit(); AccessInfo info = NavigationMENU.Items[NavigationMENU.Items.Count - 1].Tag as AccessInfo; TypeInfo currentType = info.TypeInfo; object currentValue = info.Value; if (info.Value is Variant) { Variant variant = (Variant)info.Value; currentValue = variant.Value; if (currentValue != null) { currentType = variant.TypeInfo; if (currentType == null) { currentType = TypeInfo.Construct(currentValue); } } } int[] dimensions = null; Array array = currentValue as Array; if (array != null) { dimensions = new int[array.Rank]; for (int ii = 0; ii < array.Rank; ii++) { dimensions[ii] = array.GetLength(ii); } } IList list = currentValue as IList; if (array == null && list != null) { dimensions = new int[1]; dimensions[0] = list.Count; } Matrix matrix = currentValue as Matrix; if (matrix != null) { dimensions = matrix.Dimensions; array = matrix.ToArray(); } SetTypeDlg.SetTypeResult result = new SetTypeDlg().ShowDialog(currentType, dimensions); if (result == null) { return; } // convert to new type. object newValue = currentValue; if (result.ArrayDimensions == null || result.ArrayDimensions.Length < 1) { newValue = Convert(currentValue, currentType, result.TypeInfo, result.UseDefaultOnError); } else { if (array == null && list != null) { Type elementType = GetListElementType(list); for (int ii = result.ArrayDimensions[0]; ii < list.Count; ii++) { list.RemoveAt(ii); } for (int ii = list.Count; ii < result.ArrayDimensions[0]; ii++) { list.Add(Activator.CreateInstance(elementType)); } newValue = list; } if (array != null) { Array newArray = null; if (currentValue is Array) { newArray = Array.CreateInstance(currentValue.GetType().GetElementType(), result.ArrayDimensions); } else { newArray = TypeInfo.CreateArray(result.TypeInfo.BuiltInType, result.ArrayDimensions); } int maxCount = result.ArrayDimensions[0]; for (int ii = 1; ii < result.ArrayDimensions.Length; ii++) { maxCount *= result.ArrayDimensions[ii]; } int count = 0; foreach (object element in array) { if (maxCount <= count) { break; } object newElement = Convert(element, currentType, result.TypeInfo, result.UseDefaultOnError); int[] indexes = GetIndexFromCount(count++, result.ArrayDimensions); newArray.SetValue(newElement, indexes); } newValue = newArray; } } NavigationMENU.Items.RemoveAt(NavigationMENU.Items.Count - 1); info.TypeInfo = result.TypeInfo; info.Value = newValue; ShowValue(info); }
/// <summary> /// Returns the data type of the value. /// </summary> private Type GetDataType(AccessInfo accessInfo) { if (accessInfo == null || accessInfo.TypeInfo == null) { return null; } if (accessInfo.TypeInfo.BuiltInType == BuiltInType.ExtensionObject) { if (accessInfo.Value != null) { return accessInfo.Value.GetType(); } if (accessInfo.PropertyInfo != null) { return accessInfo.PropertyInfo.PropertyType; } if (accessInfo.Parent != null) { if (accessInfo.Parent.Value is Array) { Array array = (Array)accessInfo.Parent.Value; return array.GetType().GetElementType(); } if (accessInfo.Parent.Value is IList) { IList list = (IList)accessInfo.Parent.Value; return GetListElementType(list); } } } return TypeInfo.GetSystemType(accessInfo.TypeInfo.BuiltInType, accessInfo.TypeInfo.ValueRank); }
private async void DataBase_AccessChanged(object sender, EventArgs e) { this.accessInfo = this.dataBase.AccessInfo; this.AccessType = this.dataBase.GetAccessType(this.authentication); await this.RefreshAsync(); }