-
Notifications
You must be signed in to change notification settings - Fork 365
/
FolderPathTypeConverter.cs
72 lines (62 loc) · 2.93 KB
/
FolderPathTypeConverter.cs
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
//===============================================================================================================
// System : Sandcastle Help File Builder Utilities
// File : FolderPathTypeConverter.cs
// Author : Eric Woodruff (Eric@EWoodruff.us)
// Updated : 04/13/2021
// Note : Copyright 2006-2021, Eric Woodruff, All rights reserved
//
// This file contains a type converter used to convert FolderPath objects to and from strings
//
// This code is published under the Microsoft Public License (Ms-PL). A copy of the license should be
// distributed with the code and can be found at the project website: https://GitHub.com/EWSoftware/SHFB. This
// notice, the author's name, and all copyright notices must remain intact in all applications, documentation,
// and source files.
//
// Date Who Comments
// ==============================================================================================================
// 12/29/2006 EFW Created the code
//===============================================================================================================
using System;
using System.ComponentModel;
using System.Globalization;
namespace SandcastleBuilder.Utils.Design
{
/// <summary>
/// This type converter is used to convert a FolderPath object to and from a string so that it can be edited
/// in a <c>System.Windows.Forms.PropertyGrid</c>.
/// </summary>
public sealed class FolderPathTypeConverter : ExpandableObjectConverter
{
/// <inheritdoc />
public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value,
Type destinationType)
{
FolderPath folderPath = value as FolderPath;
if(destinationType == typeof(string) && folderPath != null)
return folderPath.PersistablePath;
return base.ConvertTo(context, culture, value, destinationType);
}
/// <inheritdoc />
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
{
if(sourceType == typeof(string))
return true;
return base.CanConvertFrom(context, sourceType);
}
/// <inheritdoc />
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
{
IBasePathProvider provider;
if(value is string s)
{
// Get the base path provider from the existing instance so that the new path is kept relative to it
if(context == null)
provider = null;
else
provider = ((FolderPath)context.PropertyDescriptor.GetValue(context.Instance)).BasePathProvider;
return new FolderPath(s, provider);
}
return base.ConvertFrom(context, culture, value);
}
}
}