using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Collections.ObjectModel;
using System.Xml.Linq;
namespace RepWPF
{
using System;
//////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////
/// <summary>
/// Contains extension methods for the XElement class
/// </summary>
public static class ExtendedXElement
{
//--------------------------------------------------------------------------------
public static bool ContainsElement(this XElement root, string name)
{
return (root.Elements(name) != null);
}
//--------------------------------------------------------------------------------
public static bool ContainsAttribute(this XElement root, string name)
{
return (root.Attributes(name) != null);
}
//--------------------------------------------------------------------------------
public static string GetValue(this XElement root, string name, string defaultValue)
{
return (string)root.Elements(name).FirstOrDefault() ?? defaultValue;
}
//--------------------------------------------------------------------------------
public static double GetValue(this XElement root, string name, double defaultValue)
{
string strValue = (string)root.Elements(name).FirstOrDefault() ?? defaultValue.ToString();
double value;
if (!double.TryParse(strValue, out value))
{
throw new Exception(string.Format("Element {0}: Value retrieved was not a valid double", name));
}
return value;
}
//--------------------------------------------------------------------------------
public static decimal GetValue(this XElement root, string name, decimal defaultValue)
{
string strValue = (string)root.Elements(name).FirstOrDefault() ?? defaultValue.ToString();
decimal value;
if (!decimal.TryParse(strValue, out value))
{
throw new Exception(string.Format("Element {0}: Value retrieved was not a valid decimal", name));
}
return value;
}
//--------------------------------------------------------------------------------
public static Int32 GetValue(this XElement root, string name, Int32 defaultValue)
{
string strValue = (string)root.Elements(name).FirstOrDefault() ?? defaultValue.ToString();
Int32 value;
if (!Int32.TryParse(strValue, out value))
{
throw new Exception(string.Format("Element {0}: Value retrieved was not a valid 32-bit integer", name));
}
return value;
}
//--------------------------------------------------------------------------------
public static bool GetValue(this XElement root, string name, bool defaultValue)
{
string strValue = (string)root.Elements(name).FirstOrDefault() ?? defaultValue.ToString();
bool value;
if (!bool.TryParse(strValue, out value))
{
throw new Exception(string.Format("Element {0}: Value retrieved was not a valid boolean", name));
}
return value;
}
//--------------------------------------------------------------------------------
public static DateTime GetValue(this XElement root, string name, DateTime defaultValue)
{
string strValue = (string)root.Elements(name).FirstOrDefault() ?? defaultValue.ToString();
DateTime value;
if (!DateTime.TryParse(strValue, out value))
{
throw new Exception(string.Format("Element {0}: Value retrieved was not a valid DateTime", name));
}
return value;
}
//--------------------------------------------------------------------------------
public static string GetAttribute(this XElement root, string name, string defaultValue)
{
return (string)root.Attributes(name).FirstOrDefault() ?? defaultValue;
}
//--------------------------------------------------------------------------------
public static double GetAttribute(this XElement root, string name, double defaultValue)
{
string strValue = (string)root.Attributes(name).FirstOrDefault() ?? defaultValue.ToString();
double value;
if (!double.TryParse(strValue, out value))
{
throw new Exception(string.Format("Attribute {0}: Value retrieved was not a valid double", name));
}
return value;
}
//--------------------------------------------------------------------------------
public static decimal GetAttribute(this XElement root, string name, decimal defaultValue)
{
string strValue = (string)root.Attributes(name).FirstOrDefault() ?? defaultValue.ToString();
decimal value;
if (!decimal.TryParse(strValue, out value))
{
throw new Exception(string.Format("Attribute {0}: Value retrieved was not a valid decimal", name));
}
return value;
}
//--------------------------------------------------------------------------------
public static Int32 GetAttribute(this XElement root, string name, Int32 defaultValue)
{
string strValue = (string)root.Attributes(name).FirstOrDefault() ?? defaultValue.ToString();
Int32 value;
if (!Int32.TryParse(strValue, out value))
{
throw new Exception(string.Format("Attribute {0}: Value retrieved was not a valid 32-bit integer", name));
}
return value;
}
//--------------------------------------------------------------------------------
public static bool GetAttribute(this XElement root, string name, bool defaultValue)
{
string strValue = (string)root.Attributes(name).FirstOrDefault() ?? defaultValue.ToString();
bool value;
if (!bool.TryParse(strValue, out value))
{
throw new Exception(string.Format("Attribute {0}: Value retrieved was not a valid boolean", name));
}
return value;
}
//--------------------------------------------------------------------------------
public static DateTime GetAttribute(this XElement root, string name, DateTime defaultValue)
{
string strValue = (string)root.Attributes(name).FirstOrDefault() ?? defaultValue.ToString();
DateTime value;
if (!DateTime.TryParse(strValue, out value))
{
throw new Exception(string.Format("Attribute {0}: Value retrieved was not a valid DateTime", name));
}
return value;
}
}
//////////////////////////////////////////////////////////////////////////////////////
public class DBItemBase : INotifyPropertyChanged
{
#region INotifyPropertyChanged code
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string name)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(name));
}
}
#endregion INotifyPropertyChanged code
}
//////////////////////////////////////////////////////////////////////////////////////
public class DBSeriesDataItem : DBItemBase
{
protected decimal m_value;
protected string m_name;
protected DateTime m_metricDate;
//--------------------------------------------------------------------------------
public decimal Value
{
get { return m_value; }
set { m_value = value; OnPropertyChanged("Value"); }
}
//--------------------------------------------------------------------------------
public string Name
{
get { return m_name; }
set { m_name = value; OnPropertyChanged("Name"); }
}
//--------------------------------------------------------------------------------
public XElement XElement
{
get
{
XElement value = new XElement("SeriesData");
return value;
}
set
{
if (value != null)
{
//this.Value = Convert.ToDecimal(value.GetValue ("Value", "0"));
string strValue = value.GetValue("Value", "");
if (!string.IsNullOrEmpty(strValue))
{
decimal numValue;
if (decimal.TryParse(strValue, out numValue))
{
this.Value = numValue;
}
}
this.Name = value.GetValue("Name", "UNK").Trim();
}
}
}
// indicates that the data represented by this data item is fabricated because
// it was filled in by the code in order to complete the desired data
protected bool m_fabricatedData = false;
public bool IsFabricatedData
{
get { return m_fabricatedData; }
set { m_fabricatedData = value; }
}
//--------------------------------------------------------------------------------
public DBSeriesDataItem(XElement element)
{
this.XElement = element;
}
//--------------------------------------------------------------------------------
public DBSeriesDataItem(string name, decimal value)
{
this.Name = name;
this.Value = value;
}
//--------------------------------------------------------------------------------
/// <summary>
/// This constructor builds a data item with a name but no value
/// </summary>
/// <param name="name"></param>
public DBSeriesDataItem(string name)
{
this.Name = name;
this.IsFabricatedData = true;
}
}
#region Sample data
//////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////
public class Expense : INotifyPropertyChanged
{
private string m_name;
private double m_value;
private double m_goal;
//--------------------------------------------------------------------------------
public string Name
{
get { return m_name; }
set
{
m_name = value;
OnPropertyChanged("Name");
}
}
//--------------------------------------------------------------------------------
public double Value
{
get { return m_value; }
set
{
m_value = value;
OnPropertyChanged("Value");
}
}
//--------------------------------------------------------------------------------
public double Goal
{
get { return m_goal; }
set
{
m_goal = value;
OnPropertyChanged("Goal");
}
}
//--------------------------------------------------------------------------------
public Expense(string name, double value)
{
this.Name = name;
this.Value = value;
this.Goal = 35;
}
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string name)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(name));
}
}
}
//////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////
public class Expenses : ObservableCollection<DBSeriesDataItem>
{
public Expenses()
{
MakeExpense("Food", 50.0M);
MakeExpense("Rent", 173.0M);
MakeExpense("Car", 150.50M);
MakeExpense("Water", 23.0M);
MakeExpense("Phone", 46.0M);
}
public Expenses(decimal[] values)
{
if (values.Length >= 1)
{
MakeExpense("Food", values[0]);
}
if (values.Length >= 2)
{
MakeExpense("Rent", values[1]);
}
if (values.Length >= 3)
{
MakeExpense("Car", values[2]);
}
if (values.Length >= 4)
{
MakeExpense("Water", values[3]);
}
if (values.Length >= 5)
{
MakeExpense("Phone", values[4]);
}
}
private void MakeExpense(string name, decimal value)
{
DBSeriesDataItem exp = new DBSeriesDataItem(name, value);
this.Add(exp);
}
public decimal GetMaxValue()
{
decimal value = 0;
foreach (DBSeriesDataItem item in this)
{
value = Math.Max(value, item.Value);
}
return value;
}
}
#endregion Sample data
}