PageRenderTime 63ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/Development/XmlStudio.Main/Dialogs/AddXsdAttributeDialog.xaml.cs

#
C# | 321 lines | 270 code | 37 blank | 14 comment | 37 complexity | 2d27b76616fef56b6ef906ef4ee1a6eb MD5 | raw file
Possible License(s): BSD-3-Clause
  1. using System;
  2. using System.Linq;
  3. using System.Windows;
  4. using XmlStudio.ViewModel.Node;
  5. using XmlStudio.DataLayer;
  6. using System.Collections.Generic;
  7. using XmlStudio.Interfaces;
  8. using System.Windows.Data;
  9. using System.Windows.Controls;
  10. using System.Text;
  11. namespace XmlStudio.Dialogs {
  12. /// <summary>
  13. /// Interaction logic for AddXsdAttributeDialog.xaml
  14. /// </summary>
  15. public partial class AddXsdAttributeDialog : Window {
  16. #region Properties
  17. /// <summary>
  18. /// Gets the <see cref="XmlAttributeViewModel"/> as a new attribute.
  19. /// </summary>
  20. public XsdAttributeViewModel Attribute {
  21. get;
  22. set;
  23. }
  24. public IEnumerable<IBigXmlAttribute> OptionalAttributes {
  25. get { return XsdAttributeFactory.ListOptionalAttributes(this.ElementType).Union(XsdAttributeFactory.ListRequiredAttributes(this.ElementType)); }
  26. }
  27. /// <summary>
  28. /// Gets or sets the type of element being edited.
  29. /// </summary>
  30. public XsdElement ElementType { get; set; }
  31. #endregion
  32. #region Fields
  33. IXsdModel model;
  34. #endregion
  35. #region Constructors
  36. /// <summary>
  37. /// Initializes a new instance of the <see cref="AddAttributeDialog"/> class.
  38. /// </summary>
  39. public AddXsdAttributeDialog(XsdElement elementType, IXsdModel model) {
  40. this.ElementType = elementType;
  41. this.model = model;
  42. InitializeComponent();
  43. this.UpdateValues(this.SelectedAttributeName, this.ElementType);
  44. }
  45. public AddXsdAttributeDialog(XsdElement elementType, XsdAttributeViewModel attribute, IXsdModel model) {
  46. this.ElementType = elementType;
  47. this.Attribute = attribute;
  48. this.model = model;
  49. InitializeComponent();
  50. this.SelectAttributeName(attribute.LocalName);
  51. this.UpdateValues(attribute.LocalName, elementType);
  52. this.SelectValue(attribute.Value);
  53. }
  54. #endregion
  55. #region Properties
  56. private bool IsValueComboBoxVisible {
  57. get {
  58. if(this.localValuesComboBox.Visibility == System.Windows.Visibility.Visible) {
  59. return true;
  60. }
  61. return false;
  62. }
  63. }
  64. private bool IsValueListBoxVisible {
  65. get {
  66. if(this.localValuesListBox.Visibility == System.Windows.Visibility.Visible) {
  67. return true;
  68. }
  69. return false;
  70. }
  71. }
  72. private bool IsValueTextBoxVisible {
  73. get {
  74. if(this.valueTextBox.Visibility == System.Windows.Visibility.Visible) {
  75. return true;
  76. }
  77. return false;
  78. }
  79. }
  80. private string SelectedAttributeName {
  81. get { return this.localNameComboBox.SelectedValue.ToString(); }
  82. }
  83. private string SelectedPrefix {
  84. get { return this.prefixTextBox.Text; }
  85. }
  86. private string SelectedNamespaceUri {
  87. get { return this.namespaceURITextBox.Text; }
  88. }
  89. #endregion
  90. #region Event handlers
  91. protected override void OnInitialized(EventArgs e) {
  92. base.OnInitialized(e);
  93. this.prefixTextBox.Focus();
  94. }
  95. private void OnSubmit(object sender, RoutedEventArgs e) {
  96. this.Attribute = new XsdAttributeViewModel {
  97. LocalName = this.SelectedAttributeName,
  98. Prefix = this.SelectedPrefix,
  99. NamespaceURI = this.SelectedNamespaceUri,
  100. Value = this.GetValue(),
  101. Required = this.Attribute != null ? this.Attribute.Required : false
  102. };
  103. this.DialogResult = true;
  104. this.Close();
  105. }
  106. private void OnLocalNameComboBoxSelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e) {
  107. var attributeName = localNameComboBox.SelectedValue as string;
  108. UpdateValues(attributeName, this.ElementType);
  109. }
  110. #endregion
  111. #region Helper methods
  112. private void SelectAttributeName(string attributeName) {
  113. this.localNameComboBox.SelectedValue = attributeName;
  114. }
  115. private void SelectValue(string value) {
  116. if(this.IsValueComboBoxVisible) {
  117. this.localValuesComboBox.SelectedValue = value;
  118. } else if(this.IsValueListBoxVisible) {
  119. var values = value.Split(' ');
  120. foreach(var v in values) {
  121. localValuesListBox.SelectedItems.Add(v);
  122. }
  123. } else {
  124. this.valueTextBox.Text = value;
  125. }
  126. }
  127. private string GetValue() {
  128. if(this.IsValueComboBoxVisible) {
  129. return localValuesComboBox.SelectedValue.ToString();
  130. } else if(this.IsValueListBoxVisible) {
  131. var sb = new StringBuilder();
  132. var selectedItems = localValuesListBox.SelectedItems;
  133. foreach(var item in selectedItems) {
  134. sb.Append(" " + item.ToString());
  135. }
  136. return sb.ToString().Substring(1, sb.Length - 1);
  137. } else {
  138. return valueTextBox.Text;
  139. }
  140. }
  141. private void UpdateValues(string attributeName, XsdElement elementType) {
  142. // try to get values bindable to combobox -> these are available only for certain attributes
  143. var binding = BindReferencableElements(attributeName, elementType, this.model);
  144. if(binding.Source != null) {
  145. // if we have values for combobox but attribute name is memberTypes, show the listbox insted
  146. if(attributeName == "memberTypes" && this.ElementType == XsdElement.Union) {
  147. localValuesListBox.SetBinding(ListBox.ItemsSourceProperty, binding);
  148. ShowValueListBox();
  149. HideValueComboBox();
  150. HideValueTextBox();
  151. } else {
  152. localValuesComboBox.SetBinding(ComboBox.ItemsSourceProperty, binding);
  153. localValuesComboBox.SelectedIndex = 0;
  154. ShowValueComboBox();
  155. HideValueListBox();
  156. HideValueTextBox();
  157. }
  158. } else {
  159. HideValueComboBox();
  160. HideValueListBox();
  161. ShowValueTextBox();
  162. }
  163. }
  164. #region Data
  165. public static IEnumerable<string> GetBuiltInTypes(IXsdModel model) {
  166. if(!string.IsNullOrEmpty(model.XsPrefix)) {
  167. return model.ListBuiltInDataTypes().Select(x => string.Format("{0}:{1}", model.XsPrefix, XsdDataTypeHelper.GetTypeName(x)));
  168. } else {
  169. return model.ListBuiltInDataTypes().Select(x => XsdDataTypeHelper.GetTypeName(x));
  170. }
  171. }
  172. public static Binding BindReferencableElements(string attributeName, XsdElement elementType, IXsdModel model) {
  173. var binding = new Binding();
  174. binding.Mode = BindingMode.OneWay;
  175. switch(attributeName) {
  176. case "ref":
  177. switch(elementType) {
  178. case XsdElement.Attribute: {
  179. binding.Source = model.NamedAttributes;
  180. }
  181. break;
  182. case XsdElement.AttributeGroup: {
  183. binding.Source = model.NamedAttributeGroups;
  184. }
  185. break;
  186. case XsdElement.Element: {
  187. binding.Source = model.NamedElements;
  188. }
  189. break;
  190. case XsdElement.Group: {
  191. binding.Source = model.NamedGroups;
  192. }
  193. break;
  194. }
  195. break;
  196. case "type":
  197. switch(elementType) {
  198. case XsdElement.Attribute:
  199. binding.Source = GetBuiltInTypes(model).Union(model.NamedSimpleTypes);
  200. break;
  201. case XsdElement.Element:
  202. binding.Source = GetBuiltInTypes(model).Union(model.NamedSimpleTypes).Union(model.NamedComplexTypes);
  203. break;
  204. }
  205. break;
  206. case "substitutionGroup":
  207. switch(elementType) {
  208. case XsdElement.Element:
  209. binding.Source = model.NamedElements;
  210. break;
  211. }
  212. break;
  213. case "base":
  214. switch(elementType) {
  215. case XsdElement.Extension:
  216. binding.Source = GetBuiltInTypes(model).Union(model.NamedSimpleTypes).Union(model.NamedComplexTypes);
  217. break;
  218. case XsdElement.Restriction:
  219. binding.Source = GetBuiltInTypes(model).Union(model.NamedSimpleTypes).Union(model.NamedComplexTypes);
  220. break;
  221. }
  222. break;
  223. case "refer":
  224. switch(elementType) {
  225. case XsdElement.KeyRef:
  226. binding.Source = model.NamedKeyElements.Union(model.NamedUniqueElements);
  227. break;
  228. }
  229. break;
  230. case "itemType":
  231. switch(elementType) {
  232. case XsdElement.List:
  233. binding.Source = GetBuiltInTypes(model).Union(model.NamedSimpleTypes);
  234. break;
  235. }
  236. break;
  237. case "memberTypes":
  238. switch(elementType) { // listbox
  239. case XsdElement.Union:
  240. binding.Source = GetBuiltInTypes(model).Union(model.NamedSimpleTypes);
  241. break;
  242. }
  243. break;
  244. }
  245. return binding;
  246. }
  247. #endregion
  248. #region Hiding
  249. private void HideValueComboBox() {
  250. if(localValuesComboBox != null) {
  251. localValuesComboBox.Visibility = System.Windows.Visibility.Collapsed;
  252. }
  253. }
  254. private void HideValueListBox() {
  255. if(localValuesListBox != null) {
  256. localValuesListBox.Visibility = System.Windows.Visibility.Collapsed;
  257. }
  258. }
  259. private void HideValueTextBox() {
  260. if(valueTextBox != null) {
  261. valueTextBox.Visibility = System.Windows.Visibility.Collapsed;
  262. }
  263. }
  264. #endregion
  265. #region Showing
  266. private void ShowValueComboBox() {
  267. if(localValuesComboBox != null) {
  268. localValuesComboBox.Visibility = System.Windows.Visibility.Visible;
  269. }
  270. }
  271. private void ShowValueListBox() {
  272. if(localValuesListBox != null) {
  273. localValuesListBox.Visibility = System.Windows.Visibility.Visible;
  274. }
  275. }
  276. private void ShowValueTextBox() {
  277. if(valueTextBox != null) {
  278. valueTextBox.Visibility = System.Windows.Visibility.Visible;
  279. }
  280. }
  281. #endregion
  282. #endregion
  283. }
  284. }