1 /*
2  * JasperReports - Free Java Reporting Library.
3  * Copyright (C) 2001 - 2019 TIBCO Software Inc. All rights reserved.
4  * http://www.jaspersoft.com
5  *
6  * Unless you have purchased a commercial license agreement from Jaspersoft,
7  * the following license terms apply:
8  *
9  * This program is part of JasperReports.
10  *
11  * JasperReports is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License as published by
13  * the Free Software Foundation, either version 3 of the License, or
14  * (at your option) any later version.
15  *
16  * JasperReports is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU Lesser General Public License for more details.
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * along with JasperReports. If not, see <http://www.gnu.org/licenses/>.
23  */

24 package net.sf.jasperreports.engine.fill;
25
26 import java.util.ArrayList;
27 import java.util.Collection;
28 import java.util.List;
29
30 import org.apache.commons.logging.Log;
31 import org.apache.commons.logging.LogFactory;
32
33 import net.sf.jasperreports.engine.JRPrintElement;
34 import net.sf.jasperreports.engine.JRPrintElementContainer;
35 import net.sf.jasperreports.engine.JRPrintPage;
36 import net.sf.jasperreports.engine.JRPropertiesUtil;
37 import net.sf.jasperreports.engine.PrintElementId;
38 import net.sf.jasperreports.engine.base.JRVirtualPrintPage;
39 import net.sf.jasperreports.engine.base.VirtualizableElementList;
40 import net.sf.jasperreports.engine.util.VirtualizableElementCounter;
41
42 /**
43  * @author Lucian Chirita (lucianc@users.sourceforge.net)
44  */

45 public class VirtualizableFrame implements JRPrintElementContainer, OffsetElementsContainer
46 {
47
48     private static final Log log = LogFactory.getLog(VirtualizableFrame.class);
49     
50     public static final String PROPERTY_FRAME_VIRTUALIZATION_ENABLED = 
51             JRPropertiesUtil.PROPERTY_PREFIX + "frame.virtualization.enabled";
52
53     private JRTemplatePrintFrame frame;
54     private List<Object> elements;
55     private int contentsWidth;
56     private JRVirtualizationContext virtualizationContext;
57     private JRVirtualPrintPage page;
58     private int virtualizationPageElementSize;
59     private int deepSize;
60
61     public VirtualizableFrame(JRTemplatePrintFrame frame, 
62             JRVirtualizationContext virtualizationContext, JRPrintPage page)
63     {
64         this.frame = frame;
65         this.elements = new ArrayList<>();
66         
67         boolean virtualizationEnabled = virtualizationContext != null && page instanceof JRVirtualPrintPage
68                 && JRPropertiesUtil.getInstance(virtualizationContext.getJasperReportsContext()).getBooleanProperty(
69                         PROPERTY_FRAME_VIRTUALIZATION_ENABLED, true);    
70         if (virtualizationEnabled)
71         {
72             this.virtualizationContext = virtualizationContext;
73             this.page = (JRVirtualPrintPage) page;
74             this.virtualizationPageElementSize = virtualizationContext.getPageElementSize();
75             this.deepSize = 0;
76         }
77         else
78         {
79             this.virtualizationPageElementSize = 0;
80         }
81     }
82     
83     @Override
84     public void addElement(JRPrintElement element)
85     {
86         deepSize += VirtualizableElementCounter.count(element);
87         
88         this.elements.add(element);        
89     }
90
91     @Override
92     public void addOffsetElements(Collection<? extends JRPrintElement> elements, int offsetX, int offsetY)
93     {
94         if (elements == null || elements.isEmpty())
95         {
96             // nothing to do
97             return;
98         }
99         
100         deepSize += VirtualizableElementCounter.count(elements);
101         
102         OffsetElements offsetElements = new OffsetElements(elements, offsetX, offsetY);
103         this.elements.add(offsetElements);        
104     }
105     
106     public void fill()
107     {
108         if (virtualizationPageElementSize > 0 && deepSize > virtualizationPageElementSize)
109         {
110             PrintElementId frameID = PrintElementId.forElement(frame);
111             if (log.isDebugEnabled())
112             {
113                 log.debug("creating virtualized list for frame " + frame.getUUID() + " (" + frameID + ")");
114             }
115             
116             JRVirtualizationContext framesContext = virtualizationContext.getFramesContext();
117             VirtualizableElementList virtualizableList = 
118                     new VirtualizableElementList(framesContext, page);
119             frame.setElementsList(virtualizableList);
120             framesContext.cacheVirtualizableList(frameID, virtualizableList);
121         }
122         
123         //TODO lucian optimize case of a single subreport with zero offsets (e.g. a table)
124         for (OffsetElementsIterator it = new OffsetElementsIterator(elements); it.hasNext();)
125         {
126             JRPrintElement element = it.next();
127             frame.addElement(element);
128         }
129     }
130
131     @Override
132     public List<JRPrintElement> getElements()
133     {
134         throw new UnsupportedOperationException();
135     }
136
137     @Override
138     public int getHeight()
139     {
140         return frame.getHeight();
141     }
142
143     @Override
144     public void setHeight(int height)
145     {
146         frame.setHeight(height);
147     }
148
149     @Override
150     public void setContentsWidth(int width)
151     {
152         this.contentsWidth = width;
153     }
154     
155     public int getContentsWidth()
156     {
157         return contentsWidth;
158     }
159     
160 }
161