/Xpand/Xpand.ExpressApp/Xpand.ExpressApp.Web/SystemModule/LayoutStyleController.cs

https://bitbucket.org/expand/expand · C# · 238 lines · 204 code · 34 blank · 0 comment · 56 complexity · 459e2a367dbee46455a10666a7296981 MD5 · raw file

  1. using System;
  2. using System.ComponentModel;
  3. using System.Drawing;
  4. using System.Web.UI;
  5. using System.Web.UI.WebControls;
  6. using DevExpress.ExpressApp;
  7. using DevExpress.ExpressApp.Model;
  8. using DevExpress.ExpressApp.Web;
  9. using DevExpress.ExpressApp.Web.Editors;
  10. using DevExpress.ExpressApp.Web.Editors.ASPx;
  11. using DevExpress.ExpressApp.Web.Layout;
  12. using DevExpress.Utils;
  13. using Xpand.ExpressApp.Web.Layout;
  14. using Xpand.Utils.Helpers;
  15. using System.Linq;
  16. using Xpand.Persistent.Base.General;
  17. namespace Xpand.ExpressApp.Web.SystemModule {
  18. public interface ILayoutStyle : IModelNode {
  19. IModelLayoutStyle ContainerPanel { get; }
  20. IModelLayoutStyle ContainerCell { get; }
  21. IModelLayoutStyle Control { get; }
  22. IModelLayoutStyle Caption { get; }
  23. }
  24. public interface IModelLayoutViewItemStyle {
  25. ILayoutStyle LayoutStyle { get; }
  26. }
  27. public interface IModelLayoutStyle : IModelNode {
  28. [Category("eXpand.Layout")]
  29. FontStyle FontStyle { get; set; }
  30. [Category("eXpand.Layout")]
  31. Color FontColor { get; set; }
  32. [Category("eXpand.Layout")]
  33. Color BackColor { get; set; }
  34. [Category("eXpand.Layout")]
  35. string CssClass { get; set; }
  36. [Category("eXpand.Layout")]
  37. string Style { get; set; }
  38. }
  39. public class LayoutStyleController : ViewController<ObjectView>, IModelExtender {
  40. LayoutStyleProvider _layoutStyleProvider;
  41. protected override void OnActivated() {
  42. base.OnActivated();
  43. _layoutStyleProvider = new LayoutStyleProvider();
  44. View.LayoutManager.LayoutCreated += LayoutManager_LayoutCreated;
  45. var layoutManager = View.LayoutManager as IWebLayoutManager;
  46. if (layoutManager != null) { ((IWebLayoutManager)View.LayoutManager).Instantiated += OnInstantiated; }
  47. foreach (var item in View.GetItems<WebPropertyEditor>()) {
  48. item.ControlCreated += ItemOnControlCreated;
  49. }
  50. var listView = View as ListView;
  51. if (listView != null) {
  52. var asPxGridListEditor = listView.Editor as ASPxGridListEditor;
  53. if (asPxGridListEditor != null) {
  54. asPxGridListEditor.CustomCreateCellControl += AsPxGridListEditorOnCustomCreateCellControl;
  55. }
  56. }
  57. }
  58. void AsPxGridListEditorOnCustomCreateCellControl(object sender, CustomCreateCellControlEventArgs customCreateCellControlEventArgs) {
  59. ApplyStyle(customCreateCellControlEventArgs.PropertyEditor);
  60. }
  61. void LayoutManager_LayoutCreated(object sender, EventArgs e) {
  62. View.LayoutManager.LayoutCreated -= LayoutManager_LayoutCreated;
  63. var modelLayoutStyle = ((IModelLayoutStyle)View.Model);
  64. if (modelLayoutStyle != null) {
  65. _layoutStyleProvider.ApplyStyle(modelLayoutStyle, (WebControl)View.LayoutManager.Container);
  66. }
  67. }
  68. void ItemOnControlCreated(object sender, EventArgs eventArgs) {
  69. var webPropertyEditor = ((WebPropertyEditor)sender);
  70. webPropertyEditor.ControlCreated -= ItemOnControlCreated;
  71. ApplyStyle(webPropertyEditor);
  72. }
  73. void ApplyStyle(WebPropertyEditor webPropertyEditor) {
  74. var modelLayoutViewItem = ModelLayoutViewItem(webPropertyEditor);
  75. if (modelLayoutViewItem != null) {
  76. var containerCell = ContainerCell(webPropertyEditor);
  77. if (containerCell != null) {
  78. _layoutStyleProvider.ApplyContainerCellStyle(containerCell, modelLayoutViewItem.LayoutStyle);
  79. _layoutStyleProvider.ApplyControlStyle((WebControl)containerCell.Controls[0], modelLayoutViewItem.LayoutStyle);
  80. }
  81. }
  82. }
  83. IModelLayoutViewItemStyle ModelLayoutViewItem(WebPropertyEditor webPropertyEditor) {
  84. Guard.ArgumentNotNull(webPropertyEditor, "webPropertyEditor");
  85. if (View == null) return (IModelLayoutViewItemStyle)webPropertyEditor.Model;
  86. var modelDetailView = View.Model as IModelDetailView;
  87. return modelDetailView == null ? (IModelLayoutViewItemStyle)webPropertyEditor.Model
  88. : modelDetailView.Layout.ViewItems(webPropertyEditor.Model).Cast<IModelLayoutViewItemStyle>().FirstOrDefault();
  89. }
  90. protected override void OnDeactivated() {
  91. base.OnDeactivated();
  92. var layoutManager = View.LayoutManager as IWebLayoutManager;
  93. if (layoutManager != null)
  94. ((IWebLayoutManager)View.LayoutManager).Instantiated -= OnInstantiated;
  95. }
  96. TableCell ContainerCell(WebPropertyEditor item) {
  97. var tableEx = ((TableEx)item.Control);
  98. if (tableEx == null) return null;
  99. return ((TableRow)tableEx.Controls[0]).Cells[1];
  100. }
  101. void OnInstantiated(object sender, TemplateInstantiatedEventArgs templateInstantiatedEventArgs) {
  102. var layoutItemTemplateContainer = templateInstantiatedEventArgs.Container as LayoutItemTemplateContainer;
  103. if (layoutItemTemplateContainer != null) {
  104. _layoutStyleProvider.ApplyCaptionControlStyle(layoutItemTemplateContainer);
  105. _layoutStyleProvider.ApplyContainerControlStyle(layoutItemTemplateContainer);
  106. }
  107. var layoutGroupTemplateContainer = templateInstantiatedEventArgs.Container as LayoutGroupTemplateContainer;
  108. if (layoutGroupTemplateContainer != null) {
  109. _layoutStyleProvider.ApplyGroupControlStyle(layoutGroupTemplateContainer);
  110. }
  111. }
  112. public void ExtendModelInterfaces(ModelInterfaceExtenders extenders) {
  113. extenders.Add<IModelLayoutViewItem, IModelLayoutViewItemStyle>();
  114. extenders.Add<IModelColumn, IModelLayoutViewItemStyle>();
  115. extenders.Add<IModelDetailView, IModelLayoutStyle>();
  116. extenders.Add<IModelLayoutGroup, IModelLayoutStyle>();
  117. }
  118. }
  119. public class LayoutStyleProvider {
  120. static readonly string _backColorPropertyName;
  121. static readonly string _fontColorPropertyName;
  122. static readonly string _fontStylePropertyName;
  123. static readonly string _stylePropertyName;
  124. static readonly string _cssClassPropertyName;
  125. static LayoutStyleProvider() {
  126. _backColorPropertyName = ReflectionExtensions.GetPropertyName<IModelLayoutStyle>(style => style.BackColor);
  127. _fontColorPropertyName = ReflectionExtensions.GetPropertyName<IModelLayoutStyle>(style => style.FontColor);
  128. _fontStylePropertyName = ReflectionExtensions.GetPropertyName<IModelLayoutStyle>(style => style.FontStyle);
  129. _stylePropertyName = ReflectionExtensions.GetPropertyName<IModelLayoutStyle>(style => style.Style);
  130. _cssClassPropertyName = ReflectionExtensions.GetPropertyName<IModelLayoutStyle>(style => style.CssClass);
  131. }
  132. public void ApplyContainerControlStyle(LayoutItemTemplateContainer layoutItemTemplateContainer) {
  133. var containerControl = layoutItemTemplateContainer.Controls.OfType<Panel>().FirstOrDefault();
  134. if (containerControl != null && layoutItemTemplateContainer.LayoutItemControl != containerControl) {
  135. var layoutStyle = ((IModelLayoutViewItemStyle)layoutItemTemplateContainer.Model).LayoutStyle;
  136. if (layoutStyle != null)
  137. ApplyStyle(layoutStyle.ContainerPanel, containerControl);
  138. }
  139. }
  140. public void ApplyCaptionControlStyle(LayoutItemTemplateContainer layoutItemTemplateContainer) {
  141. var layoutStyle = ((IModelLayoutViewItemStyle)layoutItemTemplateContainer.Model).LayoutStyle;
  142. if (layoutItemTemplateContainer.CaptionControl != null && layoutStyle != null)
  143. ApplyStyle(layoutStyle.Caption, layoutItemTemplateContainer.CaptionControl);
  144. }
  145. public void ApplyContainerCellStyle(WebControl webControl, ILayoutStyle layoutStyle) {
  146. if (webControl != null && layoutStyle != null)
  147. ApplyStyle(layoutStyle.ContainerCell, webControl);
  148. }
  149. T GetValue<T>(IModelLayoutStyle modelLayoutStyle, string name) {
  150. var value = modelLayoutStyle.GetValue<T>(name);
  151. return !IsDefault(value) ? value : default(T);
  152. }
  153. bool IsDefault<T>(T value) {
  154. return Equals(default(T), value);
  155. }
  156. public void ApplyStyle(IModelLayoutStyle layoutStyle, WebControl webControl) {
  157. ApplyColors(layoutStyle, webControl);
  158. var fontStyle = GetValue<FontStyle>(layoutStyle, _fontStylePropertyName);
  159. if (fontStyle != FontStyle.Regular)
  160. webControl.Style.Add(HtmlTextWriterStyle.FontStyle, fontStyle.ToString());
  161. ApplyCssClassAndInlineStyle(layoutStyle, webControl);
  162. }
  163. void ApplyCssClassAndInlineStyle(IModelLayoutStyle layoutStyle, WebControl webControl) {
  164. var value = GetValue<string>(layoutStyle, _cssClassPropertyName);
  165. if (!string.IsNullOrEmpty(value))
  166. webControl.CssClass = value;
  167. value = GetValue<string>(layoutStyle, _stylePropertyName);
  168. if (!string.IsNullOrEmpty(value))
  169. webControl.Style.Value += ";" + value;
  170. }
  171. void ApplyColors(IModelLayoutStyle layoutStyle, WebControl webControl) {
  172. var color = GetValue<Color>(layoutStyle, _backColorPropertyName);
  173. if (color != Color.Empty) {
  174. webControl.Style.Add(HtmlTextWriterStyle.BackgroundColor, ColorTranslator.ToHtml(color));
  175. }
  176. color = GetValue<Color>(layoutStyle, _fontColorPropertyName);
  177. if (color != Color.Empty) {
  178. webControl.Style.Add(HtmlTextWriterStyle.Color, ColorTranslator.ToHtml(color));
  179. }
  180. }
  181. public void ApplyGroupControlStyle(LayoutGroupTemplateContainer layoutGroupTemplateContainer) {
  182. var layoutStyle = ((IModelLayoutStyle)layoutGroupTemplateContainer.Model);
  183. var containerControl = layoutGroupTemplateContainer.Controls.OfType<WebControl>().FirstOrDefault();
  184. if (containerControl != null) {
  185. var groupTemplateContainer = LayoutGroupTemplateContainer(containerControl, 0);
  186. if (groupTemplateContainer != null) {
  187. ApplyStyle((IModelLayoutStyle)groupTemplateContainer.Model, (WebControl)groupTemplateContainer.Parent);
  188. groupTemplateContainer = LayoutGroupTemplateContainer(containerControl, 1);
  189. if (groupTemplateContainer != null)
  190. ApplyStyle((IModelLayoutStyle)groupTemplateContainer.Model, (WebControl)groupTemplateContainer.Parent);
  191. }
  192. ApplyStyle(layoutStyle, containerControl);
  193. }
  194. }
  195. LayoutGroupTemplateContainer LayoutGroupTemplateContainer(WebControl containerControl, int cellIndex) {
  196. var tableRow = containerControl.Controls[0] as TableRow;
  197. return tableRow != null ? tableRow.Cells[cellIndex].Controls[0] as LayoutGroupTemplateContainer : null;
  198. }
  199. public void ApplyControlStyle(WebControl webControl, ILayoutStyle layoutStyle) {
  200. if (webControl != null && layoutStyle != null)
  201. ApplyStyle(layoutStyle.Control, webControl);
  202. }
  203. }
  204. }