using System;
using System.ComponentModel;
using FastReport.Utils;
using System.Drawing.Design;
namespace FastReport.Data
{
///
/// Represents a report parameter that is used to pass user data to a report.
///
///
/// See for details about using parameters.
///
public class Parameter : Base, IParent
{
#region Fields
private Type dataType;
private object value;
private string expression;
private string description;
private ParameterCollection parameters;
#endregion
#region Properties
///
/// Gets or sets the name of parameter.
///
[MergableProperty(false)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
[Category("Design")]
[DisplayName("(Name)")]
public new string Name
{
get { return (this as Base).Name; }
set
{
if (value != "" && Report != null)
{
foreach (Parameter p in Report.Parameters)
{
if (p != null && p != this && p.Name == value)
{
return;
}
}
}
(this as Base).Name = value;
}
}
///
/// Gets or sets the type of parameter.
///
[TypeConverter(typeof(FastReport.TypeConverters.DataTypeConverter))]
[Category("Data")]
[Editor("FastReport.TypeEditors.DataTypeEditor, FastReport", typeof(UITypeEditor))]
public Type DataType
{
get { return dataType; }
set { dataType = value; }
}
///
/// Gets or sets the value of parameter.
///
///
/// You may specify the static value in this property. Note: if the
/// property is not empty, it will be calculated and its value will be returned.
///
[Browsable(false)]
public virtual object Value
{
get
{
if (!String.IsNullOrEmpty(Expression) && Report != null && Report.IsRunning)
value = Report.Calc(Expression);
return value;
}
set
{
this.value = value;
if (value != null)
dataType = value.GetType();
}
}
///
/// Gets or sets value of the parameter as a string.
///
[Browsable(false)]
public string AsString
{
get
{
object value = Value;
return value == null ? "" : value.ToString();
}
set
{
Expression = "";
this.value = Convert.ChangeType(value, DataType);
}
}
///
/// Gets or sets an expression of the parameter.
///
///
/// This expression will be calculated each time you access a parameter's Value.
///
[Category("Data")]
[Editor("FastReport.TypeEditors.ExpressionEditor, FastReport", typeof(UITypeEditor))]
public string Expression
{
get { return expression; }
set { expression = value; }
}
///
/// Gets or sets the description of a parameter.
///
public string Description
{
get { return description; }
set { description = value; }
}
///
/// Gets a collection of nested parameters.
///
///
/// Parameters can have child (nested) parameters. To get or set a nested
/// parameter's value, use the method.
///
[Browsable(false)]
public ParameterCollection Parameters
{
get { return parameters; }
}
///
/// Gets the full name of the parameter. This is useful to get the nested parameter's full name.
///
[Browsable(false)]
public string FullName
{
get
{
string result = Name;
Parameter parent = Parent as Parameter;
while (parent != null)
{
result = parent.Name + "." + result;
parent = parent.Parent as Parameter;
}
return result;
}
}
///
/// This property is not relevant to this class.
///
[Browsable(false)]
public new Restrictions Restrictions
{
get { return base.Restrictions; }
set { base.Restrictions = value; }
}
#endregion
#region Public Methods
///
public override void Assign(Base source)
{
BaseAssign(source);
}
///
public override void Serialize(FRWriter writer)
{
base.Serialize(writer);
writer.WriteValue("DataType", DataType);
if (!String.IsNullOrEmpty(Expression))
writer.WriteStr("Expression", Expression);
if (!String.IsNullOrEmpty(Description))
writer.WriteStr("Description", Description);
}
///
public override string[] GetExpressions()
{
return new string[] { Expression };
}
internal Parameter Add(string name)
{
if (Parameters.FindByName(name) != null)
throw new Exception("Parameter " + name + " already exists.");
Parameter variable = new Parameter(name);
Parameters.Add(variable);
return variable;
}
#endregion
#region IParent Members
///
public virtual bool CanContain(Base child)
{
return child is Parameter;
}
///
public void GetChildObjects(ObjectCollection list)
{
foreach (Parameter v in Parameters)
{
list.Add(v);
}
}
///
public void AddChild(Base child)
{
Parameters.Add(child as Parameter);
}
///
public void RemoveChild(Base child)
{
Parameters.Remove(child as Parameter);
}
///
public int GetChildOrder(Base child)
{
return Parameters.IndexOf(child as Parameter);
}
///
public void SetChildOrder(Base child, int order)
{
int oldOrder = child.ZOrder;
if (oldOrder != -1 && order != -1 && oldOrder != order)
{
if (order > Parameters.Count)
order = Parameters.Count;
if (oldOrder <= order)
order--;
Parameters.Remove(child as Parameter);
Parameters.Insert(order, child as Parameter);
}
}
///
public void UpdateLayout(float dx, float dy)
{
// do nothing
}
#endregion
///
/// Initializes a new instance of the class with default settings.
///
public Parameter() : this("")
{
}
///
/// Initializes a new instance of the class with specified name.
///
public Parameter(string name)
{
Name = name;
DataType = typeof(string);
expression = "";
description = "";
parameters = new ParameterCollection(this);
SetFlags(Flags.CanCopy | Flags.CanEdit, false);
}
}
}