Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
TemplateProviderBase.cs
Go to the documentation of this file.
1 // Copyright (c) 2014 Silicon Studio Corp. (http://siliconstudio.co.jp)
2 // This file is distributed under GPL v3. See LICENSE.md for details.
3 using System;
4 using System.Collections.Generic;
5 using System.Windows;
6 using System.Windows.Markup;
7 
8 namespace SiliconStudio.Presentation.View
9 {
10  /// <summary>
11  /// An abstract implementation of the <see cref="ITemplateProvider"/> interface.
12  /// </summary>
13  [ContentProperty("Template")]
15  {
16  /// <summary>
17  /// Initializes a new instance of the <see cref="TemplateProviderBase"/> class.
18  /// </summary>
20  {
21  OverriddenProviderNames = new List<string>();
22  OverrideRule = OverrideRule.Some;
23  }
24 
25  /// <inheritdoc/>
26  public abstract string Name { get; }
27 
28  /// <inheritdoc/>
29  public DataTemplate Template { get; set; }
30 
31  /// <inheritdoc/>
32  public OverrideRule OverrideRule { get; set; }
33 
34  /// <inheritdoc/>
35  public List<string> OverriddenProviderNames { get; private set; }
36 
37  /// <inheritdoc/>
38  public abstract bool Match(object obj);
39 
40  public int CompareTo(ITemplateProvider other)
41  {
42  if (other == null) throw new ArgumentNullException("other");
43 
44  // Both overrides all: undeterminated.
45  if (OverrideRule == OverrideRule.All && other.OverrideRule == OverrideRule.All)
46  return 0;
47 
48  // This overrides all: this is first.
49  if (OverrideRule == OverrideRule.All)
50  return -1;
51 
52  // Other overrides all: other is first.
53  if (other.OverrideRule == OverrideRule.All)
54  return 1;
55 
56  // Both overrides none: undeterminated.
57  if (OverrideRule == OverrideRule.None && other.OverrideRule == OverrideRule.None)
58  return 0;
59 
60  // This overrides none: other is first.
61  if (OverrideRule == OverrideRule.None)
62  return 1;
63 
64  // Other overrides none: this is first.
65  if (other.OverrideRule == OverrideRule.None)
66  return -1;
67 
68  // Both overrides most: undeterminated.
69  if (OverrideRule == OverrideRule.Most && other.OverrideRule == OverrideRule.Most)
70  return 0;
71 
72  // From this point, at least one have the "Some" rule and at most one have the "Most" rule.
73  bool thisOverrides = OverriddenProviderNames.Contains(other.Name);
74  bool otherOverrides = other.OverriddenProviderNames.Contains(Name);
75 
76  // Both overrides each other: undeterminated
77  if (thisOverrides && otherOverrides)
78  return 0;
79 
80  // None overrides the other...
81  if (!thisOverrides && !otherOverrides)
82  {
83  // ...but this overrides most: this is first.
84  if (OverrideRule == OverrideRule.Most)
85  return -1;
86 
87  // ...but other overrides most: other is first.
88  if (other.OverrideRule == OverrideRule.Most)
89  return 1;
90  }
91  // Result: whichever overrides the other is first.
92  return thisOverrides ? -1 : 1;
93  }
94  }
95 }
OverrideRule
This enum describes how an ITemplateProvider should override other providers that matches the same ob...
An abstract implementation of the ITemplateProvider interface.
OverrideRule OverrideRule
Gets or sets the rule to use when this provider can potentially override other providers that matches...
An interface for a class that can provide a template for a given object that matches some prerequisit...
TemplateProviderBase()
Initializes a new instance of the TemplateProviderBase class.