using System;
using System.Windows.Input;
using System.Windows.Threading;
using System.Windows;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using MarketData.MarketDataModel;
using System.Windows.Data;
using MarketData.Utils;
namespace TradeBlotter.UIUtils
{
public class RMultipleValueConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
double doubleValue = (double)value;
if (double.IsNaN(doubleValue)) return Constants.CONST_DASHES;
return Utility.FormatNumber(doubleValue,2)+"R";
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return null;
}
}
public class DoubleValueConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
double doubleValue = (double)value;
if (double.IsNaN(doubleValue)) return Constants.CONST_DASHES;
if (null != parameter) return Utility.FormatNumber(doubleValue, int.Parse(parameter.ToString()));
return Utility.FormatNumber(doubleValue, 4);
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return null;
}
}
public class IntValueConverter:IValueConverter
{
public object Convert(object value,Type targetType,object parameter,System.Globalization.CultureInfo culture)
{
int intValue=(int)value;
if(int.MinValue.Equals(intValue))return Constants.CONST_DASHES;
if(null!=parameter) return Utility.FormatNumber(intValue,int.Parse(parameter.ToString()),true);
return Utility.FormatNumber(intValue,2,true);
}
public object ConvertBack(object value,Type targetType,object parameter,System.Globalization.CultureInfo culture)
{
return null;
}
}
public class BoolValueConverter:IValueConverter
{
public object Convert(object value,Type targetType,object parameter,System.Globalization.CultureInfo culture)
{
bool boolValue=(bool)value;
if(null!=parameter)
{
if("0".Equals(parameter.ToString())) return boolValue?"T":"F";
if("1".Equals(parameter.ToString())) return boolValue?"Y":"N";
if("2".Equals(parameter.ToString())) return boolValue?"Yes":"No";
if("3".Equals(parameter.ToString())) return boolValue?"True":"False";
}
return boolValue.ToString();
}
public object ConvertBack(object value,Type targetType,object parameter,System.Globalization.CultureInfo culture)
{
return null;
}
}
public class DateValueConverter:IValueConverter
{
public object Convert(object value,Type targetType,object parameter,System.Globalization.CultureInfo culture)
{
DateTime dateValue=(DateTime)value;
if(Utility.IsEpoch(dateValue))return Constants.CONST_DASHES;
return Utility.DateTimeToStringMMSDDSYYYY(dateValue);
}
public object ConvertBack(object value,Type targetType,object parameter,System.Globalization.CultureInfo culture)
{
return null;
}
}
// ********************************************************************
public class SubStringConverter : IValueConverter
{
/// the zero-based starting character position
public int StartIndex { get; set; }
/// The number of characters in the substring
public int Length { get; set; }
/// shows "..." if value was truncated after StartIndex
public bool ShowEllipse { get; set; }
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
string valueString = value as string;
if (string.IsNullOrWhiteSpace(valueString) == false)
{
if (Length > 0 && Length < (valueString.Length + StartIndex))
{
if (ShowEllipse)
return valueString.Substring(StartIndex, Length - 3) + "...";
else
return valueString.Substring(StartIndex, Length);
}
else if (StartIndex < valueString.Length)
return valueString.Substring(StartIndex);
else
return ""; //because startIndex must be past the length of the string
}
else
{
return value;
}
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
public class TextMarkerOffsets
{
private static int MAX_HITS=12;
private Dictionary tradesBySymbolAndDate=new Dictionary();
private double defaultOffset=0.00;
private double increment=-18;
private int hits;
public TextMarkerOffsets()
{
hits=0;
}
public TextMarkerOffsets(double defaultOffset)
{
this.defaultOffset=defaultOffset;
hits=0;
}
public TextMarkerOffsets(double defaultOffset,double increment)
{
this.defaultOffset=defaultOffset;
this.increment=increment;
this.hits=0;
}
public double GetOffset(PortfolioTrade portfolioTrade)
{
String key;
double offset=defaultOffset;
if (portfolioTrade.IsOpen)
{
key = portfolioTrade.Symbol + portfolioTrade.TradeDate.ToShortDateString();
}
else
{
key = portfolioTrade.Symbol + portfolioTrade.SellDate.ToShortDateString();
}
if (tradesBySymbolAndDate.ContainsKey(key))
{
if(hits>MAX_HITS)increment=5;
offset=tradesBySymbolAndDate[key]+increment;
tradesBySymbolAndDate[key]=offset;
hits++;
}
else
{
offset=defaultOffset;
tradesBySymbolAndDate.Add(key,defaultOffset);
}
return offset;
}
}
public class MenuItemSorter : IComparer
{
public int Compare(System.Windows.Controls.MenuItem v1,System.Windows.Controls.MenuItem v2)
{
return v1.Header.ToString().CompareTo(v2.Header.ToString());
}
}
public static class UIServices
{
///
/// A value indicating whether the UI is currently busy
///
//private static bool isBusy;
///
/// Sets the busystate as busy.
///
public static void ClearProperty(int fromSeconds, EventHandler dispatchEventHandler)
{
new DispatcherTimer(TimeSpan.FromSeconds(fromSeconds), DispatcherPriority.ApplicationIdle, dispatchEventHandler, Application.Current.Dispatcher);
}
public static void SortMenuItems(ObservableCollection menuCollection)
{
List items=new List();
foreach(System.Windows.Controls.MenuItem item in menuCollection)items.Add(item);
items.Sort(new MenuItemSorter());
menuCollection.Clear();
foreach(System.Windows.Controls.MenuItem item in items)menuCollection.Add(item);
}
}
}