PageRenderTime 55ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/DataSourceEditor.xaml.cs

https://bitbucket.org/rstarkov/tankiconmaker
C# | 122 lines | 87 code | 14 blank | 21 comment | 13 complexity | 57369cb92cf95a4d69170e9fab88013a MD5 | raw file
Possible License(s): Apache-2.0, BSD-3-Clause, GPL-3.0, CC-BY-SA-3.0
  1. using System;
  2. using System.ComponentModel;
  3. using System.Linq;
  4. using System.Windows;
  5. using System.Windows.Controls;
  6. using System.Windows.Data;
  7. using WotDataLib;
  8. using WpfCrutches;
  9. using Xceed.Wpf.Toolkit.PropertyGrid;
  10. using Xceed.Wpf.Toolkit.PropertyGrid.Editors;
  11. namespace TankIconMaker
  12. {
  13. /// <summary>
  14. /// Implements a drop-down editor for selecting one of the extra property files in the maker property editor.
  15. /// Apply to your maker's property as follows: <c>[Editor(typeof(DataSourceEditor), typeof(DataSourceEditor))]</c>.
  16. /// </summary>
  17. public partial class DataSourceEditor : UserControl, ITypeEditor
  18. {
  19. public DataSourceEditor()
  20. {
  21. InitializeComponent();
  22. ctCombo.ItemsSource = App.DataSources;
  23. }
  24. public FrameworkElement ResolveEditor(PropertyItem propertyItem)
  25. {
  26. BindingOperations.SetBinding(ctCombo, ComboBox.SelectedItemProperty, LambdaBinding.New(
  27. new Binding("Value") { Source = propertyItem, Mode = propertyItem.IsReadOnly ? BindingMode.OneWay : BindingMode.TwoWay },
  28. (ExtraPropertyId source) => { return App.DataSources.FirstOrDefault(d => d.PropertyId.Equals(source)); },
  29. (DataSourceInfo source) => { return source == null ? null : source.PropertyId; }
  30. ));
  31. return this;
  32. }
  33. }
  34. /// <summary>
  35. /// Selects one of the two data templates: one for "no data source", and the other for all the real data sources.
  36. /// </summary>
  37. class DataSourceTemplateSelector : DataTemplateSelector
  38. {
  39. public override DataTemplate SelectTemplate(object item, DependencyObject container)
  40. {
  41. var element = container as FrameworkElement;
  42. if (item is DataSourceTierArabic)
  43. return element.FindResource("tierArabicTemplate") as DataTemplate;
  44. else if (item is DataSourceTierRoman)
  45. return element.FindResource("tierRomanTemplate") as DataTemplate;
  46. else
  47. return element.FindResource("sourceTemplate") as DataTemplate;
  48. }
  49. }
  50. /// <summary>
  51. /// Holds information about a data source (that is, an "extra" property data file).
  52. /// </summary>
  53. class DataSourceInfo : INotifyPropertyChanged
  54. {
  55. /// <summary>The name of the property.</summary>
  56. public string Name { get { return PropertyId.FileId + (PropertyId.ColumnId == null ? "" : ("/" + PropertyId.ColumnId)); } }
  57. /// <summary>Name of the data file's author.</summary>
  58. public string Author { get { return PropertyId.Author; } }
  59. /// <summary>A short description of the property.</summary>
  60. public string Description { get; private set; }
  61. public event PropertyChangedEventHandler PropertyChanged;
  62. protected DataSourceInfo() { }
  63. private ExtraPropertyId _propertyId;
  64. public DataSourceInfo(ExtraPropertyInfo propertyInfo)
  65. {
  66. if (propertyInfo == null)
  67. throw new ArgumentNullException();
  68. _propertyId = propertyInfo.PropertyId;
  69. Description = Ut.StringForCurrentLanguage(propertyInfo.Descriptions);
  70. }
  71. /// <summary>
  72. /// Updates those properties that are allowed to change without treating the data source as a different source.
  73. /// There can be several versions of the same data source which may differ in the property description etc. This
  74. /// method is called to ensure such values are inherited from the latest version of the file.
  75. /// </summary>
  76. public void UpdateFrom(ExtraPropertyInfo propertyInfo)
  77. {
  78. if (propertyInfo == null)
  79. throw new ArgumentNullException();
  80. if (Description != Ut.StringForCurrentLanguage(propertyInfo.Descriptions))
  81. {
  82. Description = Ut.StringForCurrentLanguage(propertyInfo.Descriptions);
  83. if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("Description"));
  84. }
  85. }
  86. /// <summary>Returns an "extra" property identifier matching this data file.</summary>
  87. public virtual ExtraPropertyId PropertyId
  88. {
  89. get { return _propertyId; }
  90. }
  91. }
  92. /// <summary>Represents a "tier" data source value using arabic numerals.</summary>
  93. sealed class DataSourceTierArabic : DataSourceInfo
  94. {
  95. public override ExtraPropertyId PropertyId
  96. {
  97. get { return ExtraPropertyId.TierArabic; }
  98. }
  99. }
  100. /// <summary>Represents a "tier" data source value using roman numerals.</summary>
  101. sealed class DataSourceTierRoman : DataSourceInfo
  102. {
  103. public override ExtraPropertyId PropertyId
  104. {
  105. get { return ExtraPropertyId.TierRoman; }
  106. }
  107. }
  108. }