aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Tango.Editors/IConfigurableExtensions.cs
blob: e7b44c55f49864d3efa3c5e87ded428031d7f542 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
using Tango.Editors;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Threading.Tasks;

/// <exclude/>
/// <summary>
/// A collection of <see cref="IConfigurable"/> extension methods.
/// </summary>
public static class IConfigurableExtensions
{
    /// <summary>
    /// Applies the specified configuration file with an optional animation if supported by the configurable type.
    /// </summary>
    /// <param name="filePath">The file path to where a configuration has been saved.</param>
    /// <param name="animation">The animation.</param>
    public static void SetConfiguration(this IConfigurable configurable, String filePath, ConfigurationAnimation animation)
    {
        IConfiguration config = null;

        using (FileStream fs = new FileStream(filePath, FileMode.Open))
        {
            BinaryFormatter f = new BinaryFormatter();
            config = f.Deserialize(fs) as IConfiguration;
        }

        if (config != null)
        {
            configurable.SetConfiguration(config, animation);
        }
        else
        {
            throw new ArgumentException("The specified file path does not contain any saved configuration.");
        }
    }

    /// <summary>
    /// Applies the specified configuration stream with an optional animation if supported by the configurable type.
    /// </summary>
    /// <param name="stream">The stream where a configuration has been saved.</param>
    /// <param name="animation">The animation.</param>
    public static void SetConfiguration(this IConfigurable configurable, Stream stream, ConfigurationAnimation animation)
    {
        IConfiguration config = null;

        BinaryFormatter f = new BinaryFormatter();
        config = f.Deserialize(stream) as IConfiguration;

        if (config != null)
        {
            configurable.SetConfiguration(config, animation);
        }
        else
        {
            throw new ArgumentException("The specified stream does not contain any saved configuration.");
        }
    }

    /// <summary>
    /// Applies the specified configuration with no animation.
    /// </summary>
    /// <param name="configurable">The configurable.</param>
    /// <param name="configuration">The configuration.</param>
    public static void SetConfiguration(this IConfigurable configurable, IConfiguration configuration)
    {
        configurable.SetConfiguration(configuration, new ConfigurationAnimation());
    }

    /// <summary>
    /// Clones this configurable.
    /// </summary>
    /// <param name="configurable">The configurable instance.</param>
    /// <returns></returns>
    public static IConfigurable Clone(this IConfigurable configurable)
    {
        IConfigurable cloned = Activator.CreateInstance(configurable.GetType()) as IConfigurable;
        cloned.SetConfiguration(configurable.GetConfiguration());

        try
        {
            configurable.GetType().GetProperty("ID").SetValue(cloned, Guid.NewGuid().ToString());
        }
        catch { }

        return cloned;
    }
}