Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
SettingsListKey.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;
5 using System.Collections.Generic;
6 using System.ComponentModel;
7 using System.Globalization;
8 using System.Linq;
9 
10 using SiliconStudio.Core.IO;
11 
12 namespace SiliconStudio.Presentation.Settings
13 {
14  /// <summary>
15  /// This class represents a <see cref="SettingsKey"/> containing a list of values of the given type <see cref="T"/>.
16  /// </summary>
17  /// <typeparam name="T">The type of items in the list contained in this key.</typeparam>
18  public class SettingsListKey<T> : SettingsKey
19  {
20  /// <summary>
21  /// Initializes a new instance of the <see cref="SettingsListKey{T}"/> class.
22  /// </summary>
23  /// <param name="name">The name of the settings key. Must be unique amongst an application.</param>
24  /// <param name="defaultValue">The default values for this settings key.</param>
25  public SettingsListKey(UFile name, IEnumerable<T> defaultValue)
26  : base(name, defaultValue.ToList())
27  {
28  if (defaultValue == null) throw new ArgumentNullException("defaultValue");
29  }
30 
31  /// <inheritdoc/>
32  public override Type Type { get { return typeof(IList<T>); } }
33 
34  /// <summary>
35  /// Gets the default values of this settings key.
36  /// </summary>
37  public IEnumerable<T> DefaultValue { get { return (IEnumerable<T>)DefaultObjectValue; } }
38 
39  /// <summary>
40  /// Gets the list of values of this settings key in the current profile.
41  /// </summary>
42  /// <param name="searchInParentProfile">If true, the settings service will look in the parent profile of the given profile if the settings key is not defined into it.</param>
43  /// <param name="profile">The profile in which to look for the value. If <c>null</c>, it will look in the <see cref="SettingsService.CurrentProfile"/>.</param>
44  /// <param name="createInCurrentProfile">If true, the list will be created in the current profile, from the value of its parent profile.</param>
45  /// <returns>The value of this settings key.</returns>
46  /// <exception cref="KeyNotFoundException">No value can be found in the given profile matching this settings key.</exception>
47  public IList<T> GetList(bool searchInParentProfile = true, SettingsProfile profile = null, bool createInCurrentProfile = false)
48  {
49  profile = profile ?? SettingsService.CurrentProfile;
50  object value;
51  if (profile.GetValue(Name, out value, searchInParentProfile, createInCurrentProfile))
52  {
53  var list = value as IList<T>;
54  if (list != null)
55  {
56  return list;
57  }
58  }
59 
60  throw new KeyNotFoundException("Settings key not found");
61  }
62 
63  /// <summary>
64  /// Tries to gets the list of values of this settings key in the current profile.
65  /// </summary>
66  /// <param name="list">The resulting value, if found</param>
67  /// <param name="searchInParentProfile">If true, the settings service will look in the parent profile of the given profile if the settings key is not defined into it.</param>
68  /// <param name="profile">The profile in which to look for the value. If <c>null</c>, it will look in the <see cref="SettingsService.CurrentProfile"/>.</param>
69  /// <returns><c>true</c> if the list was found, <c>false</c> otherwise.</returns>
70  public bool TryGetList(out IList<T> list, bool searchInParentProfile = true, SettingsProfile profile = null)
71  {
72  profile = profile ?? SettingsService.CurrentProfile;
73  object value;
74  if (profile.GetValue(Name, out value, searchInParentProfile, false))
75  {
76  list = value as IList<T>;
77  return list != null;
78  }
79  list = null;
80  return false;
81  }
82 
83  /// <inheritdoc/>
84  internal override object ConvertValue(object value)
85  {
86  var result = new List<T>();
87  try
88  {
89  var list = value as IList;
90  if (list == null)
91  throw new Exception();
92 
93  foreach (object item in list)
94  {
95  var converter = TypeDescriptor.GetConverter(typeof(T));
96  T newValue;
97  if (converter.CanConvertFrom(item != null ? item.GetType() : typeof(object)))
98  {
99  newValue = (T)converter.ConvertFrom(item);
100  }
101  else
102  {
103  newValue = (T)Convert.ChangeType(item, typeof(T));
104  }
105  result.Add(newValue);
106  }
107  }
108  catch (Exception)
109  {
110  result = new List<T>(DefaultValue);
111  return result;
112  }
113  return result;
114  }
115  }
116 }
bool TryGetList(out IList< T > list, bool searchInParentProfile=true, SettingsProfile profile=null)
Tries to gets the list of values of this settings key in the current profile.
This class represents a collection of values for all registered SettingsKey. It may also contains val...
This class represents property to store in the settings that is identified by a key.
Definition: SettingsKey.cs:37
SettingsListKey(UFile name, IEnumerable< T > defaultValue)
Initializes a new instance of the SettingsListKey{T} class.
HRESULT Convert(_In_ const Image &srcImage, _In_ DXGI_FORMAT format, _In_ DWORD filter, _In_ float threshold, _Out_ ScratchImage &image)
Defines a normalized file path. See UPath for details. This class cannot be inherited.
Definition: UFile.cs:13
IList< T > GetList(bool searchInParentProfile=true, SettingsProfile profile=null, bool createInCurrentProfile=false)
Gets the list of values of this settings key in the current profile.