KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fop > fo > pagination > SimplePageMaster


1 /*
2  * $Id: SimplePageMaster.java,v 1.15.2.9 2003/04/11 00:24:41 pietsch Exp $
3  * ============================================================================
4  * The Apache Software License, Version 1.1
5  * ============================================================================
6  *
7  * Copyright (C) 1999-2003 The Apache Software Foundation. All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without modifica-
10  * tion, are permitted provided that the following conditions are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright notice,
13  * this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright notice,
16  * this list of conditions and the following disclaimer in the documentation
17  * and/or other materials provided with the distribution.
18  *
19  * 3. The end-user documentation included with the redistribution, if any, must
20  * include the following acknowledgment: "This product includes software
21  * developed by the Apache Software Foundation (http://www.apache.org/)."
22  * Alternately, this acknowledgment may appear in the software itself, if
23  * and wherever such third-party acknowledgments normally appear.
24  *
25  * 4. The names "FOP" and "Apache Software Foundation" must not be used to
26  * endorse or promote products derived from this software without prior
27  * written permission. For written permission, please contact
28  * apache@apache.org.
29  *
30  * 5. Products derived from this software may not be called "Apache", nor may
31  * "Apache" appear in their name, without prior written permission of the
32  * Apache Software Foundation.
33  *
34  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
35  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
36  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
37  * APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
38  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
39  * DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
40  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
41  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
42  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
43  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
44  * ============================================================================
45  *
46  * This software consists of voluntary contributions made by many individuals
47  * on behalf of the Apache Software Foundation and was originally created by
48  * James Tauber <jtauber@jtauber.com>. For more information on the Apache
49  * Software Foundation, please see <http://www.apache.org/>.
50  */

51 package org.apache.fop.fo.pagination;
52
53 // FOP
54
import org.apache.fop.fo.*;
55 import org.apache.fop.layout.PageMaster;
56 import org.apache.fop.layout.BodyRegionArea;
57 import org.apache.fop.layout.MarginProps;
58 import org.apache.fop.apps.FOPException;
59 import org.apache.fop.datatypes.Length;
60
61 import java.util.HashMap JavaDoc;
62 import java.util.Iterator JavaDoc;
63
64 /**
65  * Class modeling the fo:simple-page-master object.
66  *
67  * @see <a HREF="@XSLFO-STD@#fo_simple-page-master"
68  * target="_xslfostd">@XSLFO-STDID@
69  * &para;6.4.12</a>
70  */

71 public class SimplePageMaster extends FObj {
72     // Fallback values for "auto" page size: 8x11in
73
private static final int FALLBACK_PAGE_HEIGHT = 792000;
74     private static final int FALLBACK_PAGE_WIDTH = 576000;
75
76     public static class Maker extends FObj.Maker {
77         public FObj make(FObj parent, PropertyList propertyList,
78                         String JavaDoc systemId, int line, int column)
79             throws FOPException {
80             return new SimplePageMaster(parent, propertyList,
81                                         systemId, line, column);
82         }
83
84     }
85
86     public static FObj.Maker maker() {
87         return new SimplePageMaster.Maker();
88     }
89
90     /**
91      * Page regions (regionClass, Region)
92      */

93     private HashMap JavaDoc _regions;
94
95
96     LayoutMasterSet layoutMasterSet;
97     PageMaster pageMaster;
98     String JavaDoc masterName;
99
100     // before and after data as required by start and end
101
boolean beforePrecedence = false;
102     int beforeExtent, afterExtent, startExtent, endExtent;
103     boolean afterPrecedence = false;
104
105     protected SimplePageMaster(FObj parent, PropertyList propertyList,
106                                String JavaDoc systemId, int line, int column)
107         throws FOPException {
108         super(parent, propertyList, systemId, line, column);
109
110         if (parent.getName().equals("fo:layout-master-set")) {
111             this.layoutMasterSet = (LayoutMasterSet)parent;
112             masterName = this.properties.get("master-name").getString();
113             if (masterName == null) {
114                 log.warn("simple-page-master does not have "
115                                        + "a master-name and so is being ignored");
116             } else {
117                 this.layoutMasterSet.addSimplePageMaster(this);
118             }
119         } else {
120             throw new FOPException("fo:simple-page-master must be child "
121                                    + "of fo:layout-master-set, not "
122                                    + parent.getName(), systemId, line, column);
123         }
124         _regions = new HashMap JavaDoc();
125
126     }
127
128     public String JavaDoc getName() {
129         return "fo:simple-page-master";
130     }
131
132     protected void end() {
133         Length pageWidthLen = this.properties.get("page-width").getLength();
134         int pageWidth = pageWidthLen.isAuto() ? FALLBACK_PAGE_WIDTH : pageWidthLen.mvalue();
135         Length pageHeightLen = this.properties.get("page-height").getLength();
136         int pageHeight = pageHeightLen.isAuto() ? FALLBACK_PAGE_HEIGHT : pageHeightLen.mvalue();
137         // this.properties.get("reference-orientation");
138
// this.properties.get("writing-mode");
139

140         // Common Margin Properties-Block
141
MarginProps mProps = propMgr.getMarginProps();
142
143         int contentRectangleXPosition = mProps.marginLeft;
144         int contentRectangleYPosition = pageHeight - mProps.marginTop;
145         int contentRectangleWidth = pageWidth - mProps.marginLeft
146                                     - mProps.marginRight;
147         int contentRectangleHeight = pageHeight - mProps.marginTop
148                                      - mProps.marginBottom;
149         this.pageMaster = new PageMaster(pageWidth, pageHeight);
150         Region body = getRegion(RegionBody.REGION_CLASS);
151         RegionBefore before = (RegionBefore)getRegion(RegionBefore.REGION_CLASS);
152         RegionAfter after = (RegionAfter)getRegion(RegionAfter.REGION_CLASS);
153         RegionStart start = (RegionStart)getRegion(RegionStart.REGION_CLASS);
154         RegionEnd end = (RegionEnd)getRegion(RegionEnd.REGION_CLASS);
155         if (before != null) {
156             beforePrecedence = before.getPrecedence();
157             beforeExtent = before.getExtent();
158         }
159         if (after != null) {
160             afterPrecedence = after.getPrecedence();
161             afterExtent = after.getExtent();
162         }
163         if (start != null)
164             startExtent = start.getExtent();
165         if (end != null)
166             endExtent = end.getExtent();
167
168         if (body != null)
169             this.pageMaster.addBody((BodyRegionArea)body.makeRegionArea(
170                     contentRectangleXPosition,
171                     contentRectangleYPosition,
172                     contentRectangleWidth,
173                     contentRectangleHeight));
174         else
175             log.error("simple-page-master must have a region of class "
176                                    + RegionBody.REGION_CLASS);
177
178         if (before != null)
179             this.pageMaster.addBefore(before.makeRegionArea(contentRectangleXPosition,
180                           contentRectangleYPosition, contentRectangleWidth,
181                           contentRectangleHeight, startExtent, endExtent));
182
183         if (after != null)
184             this.pageMaster.addAfter(after.makeRegionArea(contentRectangleXPosition,
185                           contentRectangleYPosition, contentRectangleWidth,
186                           contentRectangleHeight, startExtent, endExtent));
187
188         if (start != null)
189             this.pageMaster.addStart(start.makeRegionArea(contentRectangleXPosition,
190                     contentRectangleYPosition, contentRectangleWidth,
191                     contentRectangleHeight, beforePrecedence,
192                     afterPrecedence, beforeExtent, afterExtent));
193
194         if (end != null)
195             this.pageMaster.addEnd(end.makeRegionArea(contentRectangleXPosition,
196                     contentRectangleYPosition, contentRectangleWidth,
197                     contentRectangleHeight, beforePrecedence,
198                     afterPrecedence, beforeExtent, afterExtent));
199     }
200
201     public PageMaster getPageMaster() {
202         return this.pageMaster;
203     }
204
205     public PageMaster getNextPageMaster() {
206         return this.pageMaster;
207     }
208
209     public String JavaDoc getMasterName() {
210         return masterName;
211     }
212
213
214     protected void addRegion(Region region) throws FOPException {
215         if (_regions.containsKey(region.getRegionClass())) {
216             throw new FOPException("Only one region of class "
217                                    + region.getRegionClass()
218                                    + " allowed within a simple-page-master.",
219                                    systemId, line, column);
220         } else {
221             _regions.put(region.getRegionClass(), region);
222         }
223     }
224
225     protected Region getRegion(String JavaDoc regionClass) {
226         return (Region)_regions.get(regionClass);
227     }
228
229     protected HashMap JavaDoc getRegions() {
230         return _regions;
231     }
232
233     protected boolean regionNameExists(String JavaDoc regionName) {
234         for (Iterator JavaDoc i = _regions.values().iterator(); i.hasNext(); ) {
235             Region r = (Region)i.next();
236             if (r.getRegionName().equals(regionName)) {
237                 return true;
238             }
239         }
240         return false;
241     }
242
243 }
244
Popular Tags