using System;
using System.Globalization;
using System.Windows.Data;
namespace Nova.Studio
{
/// <summary>
/// String converter that trims whitespace and limits strings to a few lines (for use by data bound to XAML).
/// </summary>
[ValueConversion(typeof(string), typeof(string))]
public class StringConverterTrim : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
// Truncate messages to N lines of text maximum with an ellipsis on line N+1
const int maxLines = 3;
string text = value.ToString();
int start = 0;
for (int i = 0; i < maxLines; ++i)
{
start = text.IndexOf('\n', start);
if (start == -1) break;
++start;
}
// Truncate message if necessary, and trim leading and trailing whitespace
return ((start < 0 || text.IndexOf('\n', start) == -1) ? text.Trim() : (text.Substring(0, start).Trim() + "\n..."));
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
}