📄 reportsettings.cs
字号:
//
// SharpDevelop ReportEditor
//
// Copyright (C) 2005 Peter Forstmeier
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
// Peter Forstmeier (Peter.Forstmeier@t-online.de)
using System;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Printing;
using System.Globalization;
using System.Reflection;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Serialization;
using SharpReportCore;
namespace SharpReportCore{
/// <summary>
/// This class stores all the basic settings of an Report
/// </summary>
///
public class ReportSettings : BaseSettings,SharpReportCore.IStoreable,
IBaseRenderer,IDisposable{
private string connectionString;
private string commandText;
private System.Data.CommandType commandType;
private Font defaultFont = new Font("Microsoft Sans Serif",
10,
FontStyle.Regular,
GraphicsUnit.Point);
private GlobalEnums.enmReportType reportType;
private GlobalEnums.enmPushPullModel dataModel;
private AbstractParametersCollection reportParametersCollection;
private ColumnCollection availableFields;
private ColumnCollection groupingsCollection;
private ColumnCollection sortingCollection;
#region Constructor's
public ReportSettings(System.Drawing.Printing.PageSettings defaultPageSettings)
:this(defaultPageSettings,"",""){
}
public ReportSettings(System.Drawing.Printing.PageSettings pageSettings,
string reportName,
string fileName):base(pageSettings,reportName,fileName){
BaseValues();
}
#endregion
#region privates
void BaseValues() {
connectionString = String.Empty;
this.availableFields = new ColumnCollection();
sortingCollection = new ColumnCollection();
groupingsCollection = new ColumnCollection();
reportParametersCollection = new AbstractParametersCollection();
this.reportType = GlobalEnums.enmReportType.FormSheet;
this.dataModel = GlobalEnums.enmPushPullModel.FormSheet;
}
/// <summary>
/// Set the values for all Columns that inherit
/// from <see cref="AbstractColumn"></see> like for sorting etc
/// </summary>
/// <param name="reader">See XMLFormReader</param>
/// <param name="item">AbstractColumn</param>
/// <param name="ctrlElem">Element witch contains the values</param>
private void BuildAbstractColumn (XmlFormReader reader,
XmlElement ctrlElem,
AbstractColumn item) {
XmlNodeList nodeList = ctrlElem.ChildNodes;
foreach (XmlNode node in nodeList) {
XmlElement elem = node as XmlElement;
if (elem != null) {
if (elem.HasAttribute("value")) {
reader.SetValue (item,elem.Name,elem.GetAttribute("value"));
}
}
}
}
/// <summary>
/// This Class fills an Reportparameter
/// </summary>
/// <param name="reader">See XMLFormReader</param>
/// <param name="parElement">XmlElement ReportParameter</param>
/// <param name="item"><see cref="ReportParameter"</param>
private void BuildReportParameter(XmlFormReader reader,
XmlElement parElement,
SharpReportCore.AbstractParameter item) {
XmlNodeList nodeList = parElement.ChildNodes;
foreach (XmlNode node in nodeList) {
XmlElement elem = node as XmlElement;
if (elem != null) {
if (elem.HasAttribute("value")) {
reader.SetValue ((SqlParameter)item,elem.Name,elem.GetAttribute("value"));
}
}
}
}
#endregion
#region RestoreItems
private void CheckForCollection ( XmlFormReader xmlReader,XmlElement xmlCol) {
if (xmlCol.ChildNodes.Count == 0) {
return;
}
switch ((GlobalEnums.enmParamCollectionName)GlobalEnums.StringToEnum(typeof(GlobalEnums.enmParamCollectionName),xmlCol.Name)) {
case GlobalEnums.enmParamCollectionName.AvailableColumns: {
XmlNodeList nodeList = xmlCol.ChildNodes;
this.availableFields.Clear();
foreach (XmlNode node in nodeList) {
XmlElement elem = node as XmlElement;
if (elem != null) {
AbstractColumn abstr = new AbstractColumn();
BuildAbstractColumn (xmlReader,elem,abstr);
this.availableFields.Add(abstr);
}
}
break;
}
case GlobalEnums.enmParamCollectionName.Sortings:{
XmlNodeList nodeList = xmlCol.ChildNodes;
this.sortingCollection.Clear();
foreach (XmlNode node in nodeList) {
XmlElement elem = node as XmlElement;
if (elem != null) {
SortColumn sc = new SortColumn();
BuildAbstractColumn (xmlReader,elem,sc);
sortingCollection.Add(sc);
}
}
break;
}
case GlobalEnums.enmParamCollectionName.Groupings:{
XmlNodeList nodeList = xmlCol.ChildNodes;
this.groupingsCollection.Clear();
foreach (XmlNode node in nodeList) {
XmlElement elem = node as XmlElement;
if (elem != null) {
GroupColumn gc = new GroupColumn();
BuildAbstractColumn (xmlReader,elem,gc);
groupingsCollection.Add(gc);
}
}
break;
}
case GlobalEnums.enmParamCollectionName.SqlParams:{
XmlNodeList nodeList = xmlCol.ChildNodes;
this.reportParametersCollection.Clear();
foreach( XmlNode node in nodeList) {
XmlElement elem = node as XmlElement;
if (elem != null) {
SqlParameter parameter = new SqlParameter();
BuildReportParameter (xmlReader,
elem,
parameter);
reportParametersCollection.Add(parameter);
}
}
break;
}
}
}
public void SetSettings(XmlElement xmlSettings) {
XmlNodeList nodeList = xmlSettings.ChildNodes;
XmlFormReader xmlFormReader = new XmlFormReader();
base.InitDone = false;
foreach (XmlNode node in nodeList) {
XmlElement elem = node as XmlElement;
if (elem != null) {
CheckForCollection (xmlFormReader,elem);
if (elem.Name == "PageSettings") {
base.PageSettings = (PageSettings)XmlFormReader.StringToTypedValue(elem.GetAttribute("value"),
typeof(PageSettings),
CultureInfo.InvariantCulture);
}
else if (elem.HasAttribute("value")) {
xmlFormReader.SetValue (this,elem.Name,elem.GetAttribute("value"));
}
}
}
// base.InitDone = true;
}
#endregion
#region SharpReport.DelegatesInterfaces.IStoreable interface implementation
private void SectionItemToXml (XmlElement xmlSection) {
Type type = this.GetType();
PropertyInfo [] prop = type.GetProperties();
XmlAttribute att = xmlSection.OwnerDocument.CreateAttribute ("name");
att.InnerText = type.FullName;
xmlSection.Attributes.Append(att);
XmlElement xmlProperty;
foreach (PropertyInfo p in prop) {
AttributeCollection attributes = TypeDescriptor.GetProperties(this)[p.Name].Attributes;
XmlIgnoreAttribute xmlIgnoreAttribute = (XmlIgnoreAttribute)attributes[typeof(XmlIgnoreAttribute)];
DefaultValueAttribute defaultValue = (DefaultValueAttribute)attributes[typeof(DefaultValueAttribute)];
if (xmlIgnoreAttribute == null){
if (p.CanWrite) {
if (defaultValue == null) {
xmlProperty = SaveItem (xmlSection,p);
xmlSection.AppendChild(xmlProperty);
} else {
if (defaultValue.Value.Equals(p.GetValue(this,null)) == false) {
xmlProperty = SaveItem (xmlSection,p);
xmlSection.AppendChild(xmlProperty);
}
}
}
}
}
}
private XmlElement SaveItem (XmlElement section,PropertyInfo prop) {
XmlElement xmlProperty;
XmlAttribute attPropValue;
xmlProperty = section.OwnerDocument.CreateElement (prop.Name);
if (prop.PropertyType == typeof(Font)) {
XmlFormReader.BuildFontElement (this.DefaultFont,xmlProperty);
}else if (prop.PropertyType == typeof(Margins)) {
XmlAttribute a = xmlProperty.OwnerDocument.CreateAttribute ("value");
string str = XmlFormReader.TypedValueToString (this.DefaultMargins,
CultureInfo.InvariantCulture);
a.InnerText = str ;
xmlProperty.Attributes.Append(a);
}
else {
attPropValue = section.OwnerDocument.CreateAttribute ("value");
attPropValue.InnerText = Convert.ToString(prop.GetValue(this,null));
xmlProperty.Attributes.Append(attPropValue);
}
return xmlProperty;
}
private void SaveCollectionItems (XmlElement xmlSaveTo,AbstractColumn column,PropertyInfo [] prop) {
XmlElement xmlProperty = null;
XmlAttribute attPropValue;
foreach (PropertyInfo p in prop) {
if (p.CanWrite) {
xmlProperty = xmlSaveTo.OwnerDocument.CreateElement (p.Name);
attPropValue = xmlSaveTo.OwnerDocument.CreateAttribute ("value");
attPropValue.InnerText = Convert.ToString(p.GetValue(column,null));
xmlProperty.Attributes.Append(attPropValue);
xmlSaveTo.AppendChild(xmlProperty);
}
}
}
private void SqlParamsToXml (XmlElement xmlParam) {
XmlElement xmlElem = null;
try {
foreach (SqlParameter rPar in this.reportParametersCollection) {
Type type = rPar.GetType();
PropertyInfo [] prop = type.GetProperties();
xmlElem = xmlParam.OwnerDocument.CreateElement ("params");
XmlElement xmlProperty = null;
XmlAttribute attPropValue;
foreach (PropertyInfo p in prop) {
if (p.CanWrite) {
xmlProperty = xmlParam.OwnerDocument.CreateElement(p.Name);
attPropValue = xmlParam.OwnerDocument.CreateAttribute ("value");
attPropValue.InnerText = Convert.ToString(p.GetValue(rPar,null));
xmlProperty.Attributes.Append(attPropValue);
xmlElem.AppendChild(xmlProperty);
}
}
xmlParam.AppendChild(xmlElem);
}
} catch (Exception) {
throw;
}
}
private void SortColumnsToXml(XmlElement xmlSection) {
try {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -