XAF 顯示 UnInplace Report(設置自定義條件顯示報表,不是根據選擇ListView記錄條件顯示報表)...

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; }
        }

    }
}

转载于:https://www.cnblogs.com/Tonyyang/archive/2011/08/24/2152276.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值