File: LogicalPageBox.java

package info (click to toggle)
liblayout 0.2.10-5
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 5,332 kB
  • sloc: java: 51,125; xml: 138; makefile: 17
file content (406 lines) | stat: -rw-r--r-- 12,391 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
/**
 * ===========================================
 * LibLayout : a free Java layouting library
 * ===========================================
 *
 * Project Info:  http://reporting.pentaho.org/liblayout/
 *
 * (C) Copyright 2006-2007, by Pentaho Corporation and Contributors.
 *
 * This library is free software; you can redistribute it and/or modify it under the terms
 * of the GNU Lesser General Public License as published by the Free Software Foundation;
 * either version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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 Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License along with this
 * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
 * Boston, MA 02111-1307, USA.
 *
 * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
 * in the United States and other countries.]
 *
 * ------------
 * $Id: LogicalPageBox.java 2755 2007-04-10 19:27:09Z taqua $
 * ------------
 * (C) Copyright 2006-2007, by Pentaho Corporation.
 */
package org.jfree.layouting.renderer.model.page;

import java.util.Arrays;

import org.jfree.layouting.renderer.model.BlockRenderBox;
import org.jfree.layouting.renderer.model.EmptyBoxDefinition;
import org.jfree.layouting.renderer.model.NormalFlowRenderBox;
import org.jfree.layouting.renderer.model.PageAreaRenderBox;
import org.jfree.layouting.renderer.model.RenderBox;
import org.jfree.layouting.renderer.model.RenderNode;
import org.jfree.layouting.renderer.model.NodeLayoutProperties;
import org.jfree.layouting.layouter.context.LayoutContext;
import org.jfree.layouting.output.OutputProcessorMetaData;
import org.jfree.layouting.namespace.Namespaces;

/**
 * The logical page box does not have a layout at all. It has collection of
 * flows, one for each <b>logical</b> page area.
 * <p/>
 * Each logical page has a header (for the repeating group headers, not to be
 * mistaken as PageHeader!), content and footer area. The footer area contains
 * the foot-notes at the very bottom, the repeatable footer area and finally the
 * flow:bottom area.
 * <p/>
 * The flow- and repeatable areas behave like stacks, and are filled from the
 * content area (so for headers, new content goes after any previous content,
 * and for footers, new content goes before any previous content.)
 * <p/>
 * The footNotes section is always filled in the normal-order, so new content
 * gets added at the bottom of that area.
 * <p/>
 * The logical page also holds the absolutely and static positioned elements.
 * These elements may overlap the repeating headers, but will never overlap the
 * physical page header or footer.
 * <p/>
 * The logical page is also the container for all physical pages. (The sizes of
 * the physical pages influence the available space on the logical pages.)
 * <p/>
 * Layout notes: The logical page, as it is implemented now, consists of three
 * areas. The header and footer areas are plain banded areas and correspond to
 * the @top and @bottom page-areas. These areas are layouted zero-based. The
 * header's y=0 corresponds to the beginning of the page, while the footer's y=0
 * depends on the total height of the footer and must fullfill the following
 * constraint: Page_bottom_edge = (footer_y + footer_height).
 * <p/>
 * The content window is positioned relative to the content-flow and is placed
 * between the header and the footer. The mapping of the content into the header
 * is defined by the pageOffset. The content-window's y=0 corresponds to the
 * normal-flow's y=pageOffset.
 * <p/>
 * Repeatable headers and footers are *not* inserted into the content-flow.
 *
 * @author Thomas Morgner
 */
public class LogicalPageBox extends BlockRenderBox
{
  //private ArrayList subFlows;
  private PageGrid pageGrid;

  private long[] pageWidths;
  private long[] pageHeights;
  private long[] horizontalBreaks;
  private long[] verticalBreaks;
  private long pageWidth;
  private long pageHeight;

  //private long offset;
  private Object contentAreaId;
  private PageAreaRenderBox footerArea;
  private PageAreaRenderBox headerArea;

  private long pageOffset;
  private boolean normalFlowActive;

  public LogicalPageBox(final PageGrid pageGrid)
  {
    super(EmptyBoxDefinition.getInstance());

    if (pageGrid == null)
    {
      throw new NullPointerException("PageGrid must not be null");
    }

    //this.subFlows = new ArrayList();
    final NormalFlowRenderBox contentArea =
        new NormalFlowRenderBox(EmptyBoxDefinition.getInstance());
    this.contentAreaId = contentArea.getInstanceId();
    this.headerArea = new PageAreaRenderBox(EmptyBoxDefinition.getInstance());
    this.headerArea.setParent(this);
    this.footerArea = new PageAreaRenderBox(EmptyBoxDefinition.getInstance());
    this.footerArea.setParent(this);

    updatePageArea(pageGrid);

    addChild(contentArea);

    setMajorAxis(VERTICAL_AXIS);
    setMinorAxis(HORIZONTAL_AXIS);

    final NodeLayoutProperties nodeLayoutProperties = getNodeLayoutProperties();
    nodeLayoutProperties.setNamespace(Namespaces.LIBLAYOUT_NAMESPACE);
    nodeLayoutProperties.setTagName("logical-page");
  }

  public void appyStyle(final LayoutContext context, final OutputProcessorMetaData metaData)
  {
    super.appyStyle(context, metaData);
    final NodeLayoutProperties nodeLayoutProperties = getNodeLayoutProperties();
    nodeLayoutProperties.setNamespace(Namespaces.LIBLAYOUT_NAMESPACE);
    nodeLayoutProperties.setTagName("logical-page");
  }

  public void updatePageArea(final PageGrid pageGrid)
  {
    if (pageGrid == null)
    {
      throw new NullPointerException();
    }

    this.pageGrid = pageGrid;
    this.pageHeights = new long[pageGrid.getColumnCount()];
    this.pageWidths = new long[pageGrid.getRowCount()];
    this.horizontalBreaks = new long[pageGrid.getColumnCount()];
    this.verticalBreaks = new long[pageGrid.getRowCount()];

    Arrays.fill(pageHeights, Long.MAX_VALUE);
    Arrays.fill(pageWidths, Long.MAX_VALUE);

    // todo: This is invalid right now. The page grids content area must be used

    for (int row = 0; row < pageGrid.getRowCount(); row++)
    {
      for (int col = 0; col < pageGrid.getColumnCount(); col++)
      {
        final PhysicalPageBox box = pageGrid.getPage(row, col);
        pageHeights[row] = Math.min(pageHeights[row], box.getImageableHeight());
        pageWidths[col] = Math.min(pageWidths[col], box.getImageableWidth());
      }
    }

    pageHeight = 0;
    for (int i = 0; i < pageHeights.length; i++)
    {
      pageHeight += pageHeights[i];
      verticalBreaks[i] = pageHeight;
    }

    pageWidth = 0;
    for (int i = 0; i < pageWidths.length; i++)
    {
      pageWidth += pageWidths[i];
      horizontalBreaks[i] = pageWidth;
    }
  }

  public NormalFlowRenderBox getContentArea()
  {
    return (NormalFlowRenderBox) findNodeById(contentAreaId);
  }

  public PageAreaRenderBox getFooterArea()
  {
    return footerArea;
  }

  public PageAreaRenderBox getHeaderArea()
  {
    return headerArea;
  }
//
//  public void addAbsoluteFlow(NormalFlowRenderBox flow)
//  {
//    subFlows.add(flow);
//  }
//
//  public NormalFlowRenderBox[] getAbsoluteFlows()
//  {
//    return (NormalFlowRenderBox[])
//        subFlows.toArray(new NormalFlowRenderBox[subFlows.size()]);
//  }
//
//  public NormalFlowRenderBox getAbsoluteFlow(int i)
//  {
//    return (NormalFlowRenderBox) subFlows.get(i);
//  }
//
//  public int getAbsoluteFlowCount()
//  {
//    return subFlows.size();
//  }

  public LogicalPageBox getLogicalPage()
  {
    return this;
  }

  public NormalFlowRenderBox getNormalFlow()
  {
    return getContentArea();
  }

  public PageGrid getPageGrid()
  {
    return pageGrid;
  }

  public RenderBox getInsertationPoint()
  {
    return getContentArea().getInsertationPoint();
  }

  public long[] getPhysicalBreaks(final int axis)
  {
    if (axis == HORIZONTAL_AXIS)
    {
      return (long[]) horizontalBreaks.clone();
    }
    return (long[]) verticalBreaks.clone();
  }

  public boolean isOverflow()
  {
    return false;
  }

  /**
   * Derive creates a disconnected node that shares all the properties of the
   * original node. The derived node will no longer have any parent, silbling,
   * child or any other relationships with other nodes.
   *
   * @return
   */
  public RenderNode deriveFrozen(final boolean deepDerive)
  {
    final LogicalPageBox box = (LogicalPageBox) super.deriveFrozen(deepDerive);
    box.headerArea = (PageAreaRenderBox) headerArea.deriveFrozen(deepDerive);
    box.footerArea = (PageAreaRenderBox) footerArea.deriveFrozen(deepDerive);
//
//    box.subFlows.clear();
//
//    for (int i = 0; i < subFlows.size(); i++)
//    {
//      NormalFlowRenderBox flowRenderBox = (NormalFlowRenderBox) subFlows.get(i);
//      box.subFlows.add(flowRenderBox.deriveFrozen(deepDerive));
//    }

    return box;
  }

  /**
   * Derive creates a disconnected node that shares all the properties of the
   * original node. The derived node will no longer have any parent, silbling,
   * child or any other relationships with other nodes.
   *
   * @return
   */
  public RenderNode derive(final boolean deepDerive)
  {
    final LogicalPageBox box = (LogicalPageBox) super.derive(deepDerive);
    box.headerArea = (PageAreaRenderBox) headerArea.derive(deepDerive);
    box.footerArea = (PageAreaRenderBox) footerArea.derive(deepDerive);
//    box.subFlows.clear();
//
//    for (int i = 0; i < subFlows.size(); i++)
//    {
//      NormalFlowRenderBox flowRenderBox = (NormalFlowRenderBox) subFlows.get(i);
//      box.subFlows.add(flowRenderBox.derive(deepDerive));
//    }

    return box;
  }

  /**
   * Derives an hibernation copy. The resulting object should get stripped of
   * all unnecessary caching information and all objects, which will be
   * regenerated when the layouting restarts. Size does matter here.
   *
   * @return
   */
  public RenderNode hibernate()
  {
    final LogicalPageBox box = (LogicalPageBox) super.hibernate();
    box.headerArea = (PageAreaRenderBox) headerArea.hibernate();
    box.footerArea = (PageAreaRenderBox) footerArea.hibernate();

//    box.subFlows.clear();
//
//    for (int i = 0; i < subFlows.size(); i++)
//    {
//      NormalFlowRenderBox flowRenderBox = (NormalFlowRenderBox) subFlows.get(i);
//      box.subFlows.add(flowRenderBox.hibernate());
//    }

    return box;
  }

  /**
   * Clones this node. Be aware that cloning can get you into deep trouble, as
   * the relations this node has may no longer be valid.
   *
   * @return
   */
  public Object clone()
  {
    try
    {
      final LogicalPageBox o = (LogicalPageBox) super.clone();
      o.pageHeights = (long[]) pageHeights.clone();
      o.pageWidths = (long[]) pageWidths.clone();
      o.pageGrid = (PageGrid) pageGrid.clone();
//      o.subFlows = (ArrayList) subFlows.clone();
      return o;
    }
    catch (CloneNotSupportedException e)
    {
      throw new IllegalStateException("Cloning *must* be supported.");
    }
  }

  public boolean isNormalFlowActive()
  {
    return normalFlowActive;
  }

  public long getPageHeight()
  {
    return pageHeight;
  }

  public long getPageWidth()
  {
    return pageWidth;
  }

  public long getPageOffset()
  {
    return pageOffset;
  }

  public void setPageOffset(final long pageOffset)
  {
    this.pageOffset = pageOffset;
  }

  public void setNormalFlowActive(final boolean normalFlowActive)
  {
    this.normalFlowActive = normalFlowActive;
  }

  public void insertFirst(final RenderNode node)
  {
    insertBefore(getFirstChild(), node);
  }

  public void insertLast(final RenderNode node)
  {
    insertAfter(getLastChild(), node);
  }

  public RenderNode findNodeById(final Object instanceId)
  {
    // quick check
    final RenderNode footerNode = footerArea.findNodeById(instanceId);
    if (footerNode != null)
    {
      return footerNode;
    }

    final RenderNode headerNode = headerArea.findNodeById(instanceId);
    if (headerNode != null)
    {
      return headerNode;
    }
    // then depth-first for ordinary content.
    return super.findNodeById(instanceId);
  }
}