using System;
using System.Collections.Generic;
using System.Text;
using DevExpress.ExpressApp;
using DevExpress.ExpressApp.Actions;
using DevExpress.ExpressApp.Reports;
using DevExpress.Data.Filtering;
using System.Collections;
using DevExpress.Persistent.Base;
using DevExpress.Utils;
using DevExpress.ExpressApp.Utils;
namespace Wise.Module.SystemModule.Report
{
public class MyInplaceReportCacheController : WindowController
{
private LightDictionary<Type, List<IReportData>> reportsCache = new LightDictionary<Type, List<IReportData>>();
private Type reportDataType;
protected virtual IObjectSpace CreateObjectSpace()
{
DevExpress.ExpressApp.Utils.Guard.ArgumentNotNull(Application, "Application");
return Application.CreateObjectSpace();
}
protected LightDictionary<Type, List<IReportData>> ReportsCache
{
get { return reportsCache; }
}
public MyInplaceReportCacheController()
{
TargetWindowType = WindowType.Main;
}
public void ClearCache()
{
reportsCache.Clear();
}
public virtual List<IReportData> GetReportDataList(Type targetObjectType)
{
if (ReportDataType == null)
{
return new List<IReportData>();
}
List<IReportData> cachedReports;
if (reportsCache.TryGetValue(targetObjectType, out cachedReports))
{
return cachedReports;
}
else
{
using (IObjectSpace objectSpace = CreateObjectSpace())
{
List<string> targetObjectTypeNames = new List<string>();
Type currentType = targetObjectType;
while ((currentType != typeof(Object)) && (currentType != null))
{
if (XafTypesInfo.Instance.FindTypeInfo(currentType).IsPersistent)
{
targetObjectTypeNames.Add(currentType.FullName);
}
currentType = currentType.BaseType;
}
List<IReportData> result = new List<IReportData>();
if (targetObjectTypeNames.Count > 0)
{
IList reports = objectSpace.CreateCollection(
ReportDataType,
new GroupOperator(new InOperator("DataTypeName", targetObjectTypeNames), new BinaryOperator(ReportData.IsInplaceReportMember, false)));
foreach (IReportData reportData in reports)
{
result.Add(reportData);
}
}
reportsCache.Add(targetObjectType, result);
return result;
}
}
}
public Type ReportDataType
{
get
{
if (reportDataType == null)
{
reportDataType = ReportsModule.GetCurrentReportDataType(Application.Modules);
}
return reportDataType;
}
set { reportDataType = value; }
}
public static List<IReportData> GetReportDataList(Frame frame, Type targetObjectType)
{
if (frame != null)
{
Frame cachFrame = frame;
if (frame.Application != null && frame.Application.MainWindow != null)
{
cachFrame = frame.Application.MainWindow;
}
MyInplaceReportCacheController cacheController = cachFrame.GetController<MyInplaceReportCacheController>();
if (cacheController != null)
{
return cacheController.GetReportDataList(targetObjectType);
}
}
return new List<IReportData>();
}
[Obsolete("Use 'GetReportDataList(Frame frame, Type targetObjectType)' instead.")]
public static List<IReportData> GetReportDataList(XafApplication xafApplication, Type targetObjectType)
{
if (xafApplication != null)
{
return GetReportDataList(xafApplication.MainWindow, targetObjectType);
}
return new List<IReportData>();
}
}
/// <summary>
/// UnInplaceReport
/// </summary>
public class MyPrintSelectionBaseController : ObjectViewController
{
public const string ActiveKeyObjectHasKeyMember = "ObjectHasKeyMember";
public const string ActiveKeyDisableActionWhenThereAreChanges = "DisableActionWhenThereAreChanges";
public const string ActiveKeyInplaceReportsAreEnabledInModule = "reportsModule.EnableInplaceReports";
public const string ActiveKeyViewSupportsSelection = "ViewSupportsSelection";
public const string ActiveKeyDisableForLookupListView = "DisableForLookupListView";
public const string ActiveKeyControlsCreated = "ControlsCreated";
private SingleChoiceAction showInReportAction;
private void showInReportAction_Execute(object sender, SingleChoiceActionExecuteEventArgs e)
{
//if (e.SelectedObjects.Count == 0)
//{
// return;
//}
ShowInReport(e, (IReportData)e.SelectedChoiceActionItem.Data);
}
private void ObjectSpace_ModifiedChanged(object sender, EventArgs e)
{
UpdateActionState();
}
private void View_SelectionTypeChanged(object sender, EventArgs e)
{
if (View != null)
{
Active[ActiveKeyViewSupportsSelection] = (View.SelectionType != SelectionType.None);
}
}
private void View_ControlsCreated(object sender, EventArgs e)
{
View.ControlsCreated -= new EventHandler(View_ControlsCreated);
Initialize();
}
protected void Initialize()
{
showInReportAction.Active[ActiveKeyControlsCreated] = true;
List<IReportData> reportDataList = MyInplaceReportCacheController.GetReportDataList(Frame, ((ObjectView)View).ObjectTypeInfo.Type);
List<ChoiceActionItem> items = new List<ChoiceActionItem>();
foreach (IReportData reportData in reportDataList)
{
ChoiceActionItem item = new ChoiceActionItem(reportData.ReportName, reportData);
item.ImageName = "Action_Report_Object_UnInplace_Preview";
items.Add(item);
}
items.Sort(SortByCaption);
showInReportAction.Items.Clear();
showInReportAction.Items.AddRange(items);
View.ObjectSpace.ModifiedChanged += new EventHandler(ObjectSpace_ModifiedChanged);
UpdateActionState();
}
protected virtual void UpdateActionState()
{
ShowInReportAction.Enabled[PrintSelectionBaseController.ActiveKeyDisableActionWhenThereAreChanges] = !View.ObjectSpace.IsModified;
}
protected virtual void ShowInReport(SingleChoiceActionExecuteEventArgs e, IReportData reportData)
{
ArrayList keys = new ArrayList();
foreach (object selectedObject in e.SelectedObjects)
{
keys.Add(ObjectSpace.GetKeyValue(selectedObject));
}
//Frame.GetController<ReportServiceController>().ShowPreview(reportData, new InOperator(((ObjectView)View).ObjectTypeInfo.KeyMember.Name, keys));
Frame.GetController<ReportServiceController>().ShowPreview(reportData);//, new InOperator(((ObjectView)View).ObjectTypeInfo.KeyMember.Name, keys));
}
private int SortByCaption(ChoiceActionItem left, ChoiceActionItem right)
{
return Comparer<string>.Default.Compare(left.Caption, right.Caption);
}
protected override void OnViewChanging(View view)
{
base.OnViewChanging(view);
if (View != null)
{
View.SelectionTypeChanged -= new EventHandler(View_SelectionTypeChanged);
}
if (Application != null)
{
ReportsModule reportsModule = ReportsModule.FindReportsModule(Application.Modules);
if (reportsModule == null)
{
Active["ReportsModule in Application.Modules"] = false;
}
else
{
Active[ActiveKeyInplaceReportsAreEnabledInModule] = reportsModule.EnableInplaceReports;
}
}
Active[ActiveKeyViewSupportsSelection] = (view is ISelectionContext) && (((ISelectionContext)view).SelectionType != SelectionType.None);
if ((view is ObjectView) && (((ObjectView)view).ObjectTypeInfo != null))
{
Active[ActiveKeyObjectHasKeyMember] = (((ObjectView)view).ObjectTypeInfo.KeyMember != null);
}
if (view != null)
{
view.SelectionTypeChanged += new EventHandler(View_SelectionTypeChanged);
}
}
protected override void OnActivated()
{
base.OnActivated();
showInReportAction.Active[ActiveKeyControlsCreated] = false;
if (View.IsControlCreated)
{
Initialize();
}
else
{
View.ControlsCreated += new EventHandler(View_ControlsCreated);
}
}
protected override void OnDeactivated()
{
View.ObjectSpace.ModifiedChanged -= new EventHandler(ObjectSpace_ModifiedChanged);
View.ControlsCreated -= new EventHandler(View_ControlsCreated);
base.OnDeactivated();
}
protected override void OnFrameAssigned()
{
base.OnFrameAssigned();
if ((Frame != null) && ((Frame.Context == TemplateContext.LookupWindow) || (Frame.Context == TemplateContext.LookupControl)))
{
this.Active.SetItemValue(ActiveKeyDisableForLookupListView, false);
}
}
public MyPrintSelectionBaseController()
{
TypeOfView = typeof(ObjectView);
showInReportAction = new SingleChoiceAction(this, "MyShowInReport", PredefinedCategory.Reports);
showInReportAction.Caption = "My Show in Report";
showInReportAction.ToolTip = "Show selected records in a report";
showInReportAction.Execute += new SingleChoiceActionExecuteEventHandler(showInReportAction_Execute);
showInReportAction.ItemType = SingleChoiceActionItemType.ItemIsOperation;
showInReportAction.SelectionDependencyType = SelectionDependencyType.RequireMultipleObjects;
showInReportAction.ImageName = "Action_Report_Object_UnInplace_Preview";
}
public SingleChoiceAction ShowInReportAction
{
get { return showInReportAction; }
}
}
}
using System.Collections.Generic;
using System.Text;
using DevExpress.ExpressApp;
using DevExpress.ExpressApp.Actions;
using DevExpress.ExpressApp.Reports;
using DevExpress.Data.Filtering;
using System.Collections;
using DevExpress.Persistent.Base;
using DevExpress.Utils;
using DevExpress.ExpressApp.Utils;
namespace Wise.Module.SystemModule.Report
{
public class MyInplaceReportCacheController : WindowController
{
private LightDictionary<Type, List<IReportData>> reportsCache = new LightDictionary<Type, List<IReportData>>();
private Type reportDataType;
protected virtual IObjectSpace CreateObjectSpace()
{
DevExpress.ExpressApp.Utils.Guard.ArgumentNotNull(Application, "Application");
return Application.CreateObjectSpace();
}
protected LightDictionary<Type, List<IReportData>> ReportsCache
{
get { return reportsCache; }
}
public MyInplaceReportCacheController()
{
TargetWindowType = WindowType.Main;
}
public void ClearCache()
{
reportsCache.Clear();
}
public virtual List<IReportData> GetReportDataList(Type targetObjectType)
{
if (ReportDataType == null)
{
return new List<IReportData>();
}
List<IReportData> cachedReports;
if (reportsCache.TryGetValue(targetObjectType, out cachedReports))
{
return cachedReports;
}
else
{
using (IObjectSpace objectSpace = CreateObjectSpace())
{
List<string> targetObjectTypeNames = new List<string>();
Type currentType = targetObjectType;
while ((currentType != typeof(Object)) && (currentType != null))
{
if (XafTypesInfo.Instance.FindTypeInfo(currentType).IsPersistent)
{
targetObjectTypeNames.Add(currentType.FullName);
}
currentType = currentType.BaseType;
}
List<IReportData> result = new List<IReportData>();
if (targetObjectTypeNames.Count > 0)
{
IList reports = objectSpace.CreateCollection(
ReportDataType,
new GroupOperator(new InOperator("DataTypeName", targetObjectTypeNames), new BinaryOperator(ReportData.IsInplaceReportMember, false)));
foreach (IReportData reportData in reports)
{
result.Add(reportData);
}
}
reportsCache.Add(targetObjectType, result);
return result;
}
}
}
public Type ReportDataType
{
get
{
if (reportDataType == null)
{
reportDataType = ReportsModule.GetCurrentReportDataType(Application.Modules);
}
return reportDataType;
}
set { reportDataType = value; }
}
public static List<IReportData> GetReportDataList(Frame frame, Type targetObjectType)
{
if (frame != null)
{
Frame cachFrame = frame;
if (frame.Application != null && frame.Application.MainWindow != null)
{
cachFrame = frame.Application.MainWindow;
}
MyInplaceReportCacheController cacheController = cachFrame.GetController<MyInplaceReportCacheController>();
if (cacheController != null)
{
return cacheController.GetReportDataList(targetObjectType);
}
}
return new List<IReportData>();
}
[Obsolete("Use 'GetReportDataList(Frame frame, Type targetObjectType)' instead.")]
public static List<IReportData> GetReportDataList(XafApplication xafApplication, Type targetObjectType)
{
if (xafApplication != null)
{
return GetReportDataList(xafApplication.MainWindow, targetObjectType);
}
return new List<IReportData>();
}
}
/// <summary>
/// UnInplaceReport
/// </summary>
public class MyPrintSelectionBaseController : ObjectViewController
{
public const string ActiveKeyObjectHasKeyMember = "ObjectHasKeyMember";
public const string ActiveKeyDisableActionWhenThereAreChanges = "DisableActionWhenThereAreChanges";
public const string ActiveKeyInplaceReportsAreEnabledInModule = "reportsModule.EnableInplaceReports";
public const string ActiveKeyViewSupportsSelection = "ViewSupportsSelection";
public const string ActiveKeyDisableForLookupListView = "DisableForLookupListView";
public const string ActiveKeyControlsCreated = "ControlsCreated";
private SingleChoiceAction showInReportAction;
private void showInReportAction_Execute(object sender, SingleChoiceActionExecuteEventArgs e)
{
//if (e.SelectedObjects.Count == 0)
//{
// return;
//}
ShowInReport(e, (IReportData)e.SelectedChoiceActionItem.Data);
}
private void ObjectSpace_ModifiedChanged(object sender, EventArgs e)
{
UpdateActionState();
}
private void View_SelectionTypeChanged(object sender, EventArgs e)
{
if (View != null)
{
Active[ActiveKeyViewSupportsSelection] = (View.SelectionType != SelectionType.None);
}
}
private void View_ControlsCreated(object sender, EventArgs e)
{
View.ControlsCreated -= new EventHandler(View_ControlsCreated);
Initialize();
}
protected void Initialize()
{
showInReportAction.Active[ActiveKeyControlsCreated] = true;
List<IReportData> reportDataList = MyInplaceReportCacheController.GetReportDataList(Frame, ((ObjectView)View).ObjectTypeInfo.Type);
List<ChoiceActionItem> items = new List<ChoiceActionItem>();
foreach (IReportData reportData in reportDataList)
{
ChoiceActionItem item = new ChoiceActionItem(reportData.ReportName, reportData);
item.ImageName = "Action_Report_Object_UnInplace_Preview";
items.Add(item);
}
items.Sort(SortByCaption);
showInReportAction.Items.Clear();
showInReportAction.Items.AddRange(items);
View.ObjectSpace.ModifiedChanged += new EventHandler(ObjectSpace_ModifiedChanged);
UpdateActionState();
}
protected virtual void UpdateActionState()
{
ShowInReportAction.Enabled[PrintSelectionBaseController.ActiveKeyDisableActionWhenThereAreChanges] = !View.ObjectSpace.IsModified;
}
protected virtual void ShowInReport(SingleChoiceActionExecuteEventArgs e, IReportData reportData)
{
ArrayList keys = new ArrayList();
foreach (object selectedObject in e.SelectedObjects)
{
keys.Add(ObjectSpace.GetKeyValue(selectedObject));
}
//Frame.GetController<ReportServiceController>().ShowPreview(reportData, new InOperator(((ObjectView)View).ObjectTypeInfo.KeyMember.Name, keys));
Frame.GetController<ReportServiceController>().ShowPreview(reportData);//, new InOperator(((ObjectView)View).ObjectTypeInfo.KeyMember.Name, keys));
}
private int SortByCaption(ChoiceActionItem left, ChoiceActionItem right)
{
return Comparer<string>.Default.Compare(left.Caption, right.Caption);
}
protected override void OnViewChanging(View view)
{
base.OnViewChanging(view);
if (View != null)
{
View.SelectionTypeChanged -= new EventHandler(View_SelectionTypeChanged);
}
if (Application != null)
{
ReportsModule reportsModule = ReportsModule.FindReportsModule(Application.Modules);
if (reportsModule == null)
{
Active["ReportsModule in Application.Modules"] = false;
}
else
{
Active[ActiveKeyInplaceReportsAreEnabledInModule] = reportsModule.EnableInplaceReports;
}
}
Active[ActiveKeyViewSupportsSelection] = (view is ISelectionContext) && (((ISelectionContext)view).SelectionType != SelectionType.None);
if ((view is ObjectView) && (((ObjectView)view).ObjectTypeInfo != null))
{
Active[ActiveKeyObjectHasKeyMember] = (((ObjectView)view).ObjectTypeInfo.KeyMember != null);
}
if (view != null)
{
view.SelectionTypeChanged += new EventHandler(View_SelectionTypeChanged);
}
}
protected override void OnActivated()
{
base.OnActivated();
showInReportAction.Active[ActiveKeyControlsCreated] = false;
if (View.IsControlCreated)
{
Initialize();
}
else
{
View.ControlsCreated += new EventHandler(View_ControlsCreated);
}
}
protected override void OnDeactivated()
{
View.ObjectSpace.ModifiedChanged -= new EventHandler(ObjectSpace_ModifiedChanged);
View.ControlsCreated -= new EventHandler(View_ControlsCreated);
base.OnDeactivated();
}
protected override void OnFrameAssigned()
{
base.OnFrameAssigned();
if ((Frame != null) && ((Frame.Context == TemplateContext.LookupWindow) || (Frame.Context == TemplateContext.LookupControl)))
{
this.Active.SetItemValue(ActiveKeyDisableForLookupListView, false);
}
}
public MyPrintSelectionBaseController()
{
TypeOfView = typeof(ObjectView);
showInReportAction = new SingleChoiceAction(this, "MyShowInReport", PredefinedCategory.Reports);
showInReportAction.Caption = "My Show in Report";
showInReportAction.ToolTip = "Show selected records in a report";
showInReportAction.Execute += new SingleChoiceActionExecuteEventHandler(showInReportAction_Execute);
showInReportAction.ItemType = SingleChoiceActionItemType.ItemIsOperation;
showInReportAction.SelectionDependencyType = SelectionDependencyType.RequireMultipleObjects;
showInReportAction.ImageName = "Action_Report_Object_UnInplace_Preview";
}
public SingleChoiceAction ShowInReportAction
{
get { return showInReportAction; }
}
}
}
欢迎转载,转载请注明出处:http://www.cnblogs.com/Tonyyang/