Source Code Cross Referenced for JRPreviewBuilder.java in  » Report » jasperreports-2.0.1 » net » sf » jasperreports » view » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Report » jasperreports 2.0.1 » net.sf.jasperreports.view 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


0001:        /*
0002:         * ============================================================================
0003:         * GNU Lesser General Public License
0004:         * ============================================================================
0005:         *
0006:         * JasperReports - Free Java report-generating library.
0007:         * Copyright (C) 2001-2006 JasperSoft Corporation http://www.jaspersoft.com
0008:         * 
0009:         * This library is free software; you can redistribute it and/or
0010:         * modify it under the terms of the GNU Lesser General Public
0011:         * License as published by the Free Software Foundation; either
0012:         * version 2.1 of the License, or (at your option) any later version.
0013:         * 
0014:         * This library is distributed in the hope that it will be useful,
0015:         * but WITHOUT ANY WARRANTY; without even the implied warranty of
0016:         * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0017:         * Lesser General Public License for more details.
0018:         * 
0019:         * You should have received a copy of the GNU Lesser General Public
0020:         * License along with this library; if not, write to the Free Software
0021:         * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307, USA.
0022:         * 
0023:         * JasperSoft Corporation
0024:         * 303 Second Street, Suite 450 North
0025:         * San Francisco, CA 94107
0026:         * http://www.jaspersoft.com
0027:         */
0028:
0029:        /*
0030:         * Contributors:
0031:         * Ryan Johnson - delscovich@users.sourceforge.net
0032:         * Carlton Moore - cmoore79@users.sourceforge.net
0033:         */
0034:        package net.sf.jasperreports.view;
0035:
0036:        import java.awt.Color;
0037:        import java.awt.Image;
0038:        import java.awt.font.TextAttribute;
0039:        import java.util.ArrayList;
0040:        import java.util.Arrays;
0041:        import java.util.HashMap;
0042:        import java.util.HashSet;
0043:        import java.util.Iterator;
0044:        import java.util.List;
0045:        import java.util.Map;
0046:        import java.util.Set;
0047:
0048:        import net.sf.jasperreports.crosstabs.JRCellContents;
0049:        import net.sf.jasperreports.crosstabs.JRCrosstab;
0050:        import net.sf.jasperreports.crosstabs.JRCrosstabCell;
0051:        import net.sf.jasperreports.crosstabs.JRCrosstabColumnGroup;
0052:        import net.sf.jasperreports.crosstabs.JRCrosstabRowGroup;
0053:        import net.sf.jasperreports.crosstabs.fill.calculation.BucketDefinition;
0054:        import net.sf.jasperreports.engine.JRBand;
0055:        import net.sf.jasperreports.engine.JRBox;
0056:        import net.sf.jasperreports.engine.JRChart;
0057:        import net.sf.jasperreports.engine.JRChild;
0058:        import net.sf.jasperreports.engine.JRDefaultStyleProvider;
0059:        import net.sf.jasperreports.engine.JRElement;
0060:        import net.sf.jasperreports.engine.JRElementGroup;
0061:        import net.sf.jasperreports.engine.JREllipse;
0062:        import net.sf.jasperreports.engine.JRException;
0063:        import net.sf.jasperreports.engine.JRExpression;
0064:        import net.sf.jasperreports.engine.JRExpressionChunk;
0065:        import net.sf.jasperreports.engine.JRFrame;
0066:        import net.sf.jasperreports.engine.JRGraphicElement;
0067:        import net.sf.jasperreports.engine.JRGroup;
0068:        import net.sf.jasperreports.engine.JRImage;
0069:        import net.sf.jasperreports.engine.JRImageRenderer;
0070:        import net.sf.jasperreports.engine.JRLine;
0071:        import net.sf.jasperreports.engine.JRPrintLine;
0072:        import net.sf.jasperreports.engine.JRPrintPage;
0073:        import net.sf.jasperreports.engine.JRPrintText;
0074:        import net.sf.jasperreports.engine.JRRectangle;
0075:        import net.sf.jasperreports.engine.JRRenderable;
0076:        import net.sf.jasperreports.engine.JRReport;
0077:        import net.sf.jasperreports.engine.JRReportTemplate;
0078:        import net.sf.jasperreports.engine.JRRuntimeException;
0079:        import net.sf.jasperreports.engine.JRStaticText;
0080:        import net.sf.jasperreports.engine.JRStyle;
0081:        import net.sf.jasperreports.engine.JRStyleContainer;
0082:        import net.sf.jasperreports.engine.JRStyleSetter;
0083:        import net.sf.jasperreports.engine.JRTemplate;
0084:        import net.sf.jasperreports.engine.JRTemplateReference;
0085:        import net.sf.jasperreports.engine.JRTextElement;
0086:        import net.sf.jasperreports.engine.JRTextField;
0087:        import net.sf.jasperreports.engine.JasperPrint;
0088:        import net.sf.jasperreports.engine.base.JRBaseObjectFactory;
0089:        import net.sf.jasperreports.engine.base.JRBasePrintElement;
0090:        import net.sf.jasperreports.engine.base.JRBasePrintEllipse;
0091:        import net.sf.jasperreports.engine.base.JRBasePrintFrame;
0092:        import net.sf.jasperreports.engine.base.JRBasePrintGraphicElement;
0093:        import net.sf.jasperreports.engine.base.JRBasePrintImage;
0094:        import net.sf.jasperreports.engine.base.JRBasePrintLine;
0095:        import net.sf.jasperreports.engine.base.JRBasePrintPage;
0096:        import net.sf.jasperreports.engine.base.JRBasePrintRectangle;
0097:        import net.sf.jasperreports.engine.base.JRBasePrintText;
0098:        import net.sf.jasperreports.engine.design.JRDesignFrame;
0099:        import net.sf.jasperreports.engine.fill.TextMeasurer;
0100:        import net.sf.jasperreports.engine.util.JRFontUtil;
0101:        import net.sf.jasperreports.engine.util.JRImageLoader;
0102:        import net.sf.jasperreports.engine.util.JRLoader;
0103:        import net.sf.jasperreports.engine.util.JRStyledText;
0104:        import net.sf.jasperreports.engine.util.JRStyledTextParser;
0105:        import net.sf.jasperreports.engine.xml.JRXmlTemplateLoader;
0106:
0107:        import org.apache.commons.collections.SequencedHashMap;
0108:        import org.apache.commons.logging.Log;
0109:        import org.apache.commons.logging.LogFactory;
0110:        import org.xml.sax.SAXException;
0111:
0112:        /**
0113:         * @author Sanda Zaharia (shertage@users.sourceforge.net)
0114:         * @version $Id: JRPreviewBuilder.java 1825 2007-08-24 13:51:39Z teodord $
0115:         */
0116:        public class JRPreviewBuilder {
0117:
0118:            protected final static Log log = LogFactory
0119:                    .getLog(JRPreviewBuilder.class);
0120:
0121:            private JasperPrint jasperPrint;
0122:            private JRPrintPage page;
0123:            int pageWidth;
0124:            private int offsetX;
0125:            private int offsetY;
0126:            private int upColumns;
0127:            private int downColumns;
0128:
0129:            /**
0130:             * List containing ContourElement objects, containers for contour line coordinates and dimensions 
0131:             */
0132:            private List contourElementsList = new ArrayList();
0133:
0134:            /**
0135:             * List containing int[4] arrays, representing band separator line coordinates and dimensions 
0136:             */
0137:            private List bandSeparatorList = new ArrayList();
0138:
0139:            /**
0140:             * List containing page elements in a given order 
0141:             */
0142:            private List pageElements = new ArrayList();
0143:
0144:            private StyleFactory styleFactory;
0145:            protected Map stylesMap;
0146:            protected JRDefaultStyleProvider defaultStyleProvider;
0147:            private JRStyledTextParser styledTextParser = new JRStyledTextParser();
0148:
0149:            private boolean isFirstBand = true;
0150:
0151:            private boolean hasContour;
0152:
0153:            /**
0154:             *
0155:             */
0156:            public JRPreviewBuilder(JRReport report) throws JRException {
0157:                createJasperPrint(report);
0158:            }
0159:
0160:            /**
0161:             * This method reads a JRReport object and creates a simmilar JasperPrint object
0162:             * containing the design representation information
0163:             * @param report the JRReport object
0164:             * @throws JRException
0165:             */
0166:            private void createJasperPrint(JRReport report) throws JRException {
0167:                jasperPrint = new JasperPrint();
0168:                defaultStyleProvider = jasperPrint.getDefaultStyleProvider();
0169:                page = new JRBasePrintPage();
0170:                pageWidth = report.getPageWidth();
0171:
0172:                jasperPrint.setDefaultFont(report.getDefaultFont());
0173:                jasperPrint.setFormatFactoryClass(report
0174:                        .getFormatFactoryClass());
0175:                //TODO: locale and timezone settings jasperprint object
0176:                //		jasperPrint.setLocaleCode(JRDataUtils.getLocaleCode(Locale.getDefault()));
0177:                //		jasperPrint.setTimeZoneId(JRDataUtils.getTimeZoneId(TimeZone.getDefault()));
0178:                jasperPrint.setName(report.getName());
0179:                jasperPrint.setOrientation(report.getOrientation());
0180:                jasperPrint.setPageWidth(pageWidth);
0181:                offsetY = report.getTopMargin();
0182:                offsetX = report.getLeftMargin();
0183:
0184:                setStyles(report);
0185:
0186:                JRBand band = report.getBackground();
0187:                if (band != null)
0188:                    setElements(band.getChildren(), report.getPageHeight(), 0,
0189:                            0, 0, 0, false);
0190:
0191:                Color color = new Color(170, 170, 255);
0192:
0193:                addBandElements(report.getTitle(), color);
0194:                addBandElements(report.getPageHeader(), color);
0195:                upColumns = offsetY;
0196:                addBandElements(report.getColumnHeader(), color);
0197:
0198:                JRGroup[] groups = report.getGroups();
0199:                if (groups != null) {
0200:                    for (int i = 0; i < groups.length; i++) {
0201:                        addBandElements(groups[i].getGroupHeader(), color);
0202:                    }
0203:                }
0204:
0205:                addBandElements(report.getDetail(), color);
0206:
0207:                if (groups != null) {
0208:                    for (int i = 0; i < groups.length; i++) {
0209:                        addBandElements(groups[i].getGroupFooter(), color);
0210:                    }
0211:                }
0212:
0213:                addBandElements(report.getColumnFooter(), color);
0214:                downColumns = offsetY;
0215:                addBandElements(report.getPageFooter(), color);
0216:                addBandElements(report.getLastPageFooter(), color);
0217:                addBandElements(report.getSummary(), color);
0218:                addBandElements(report.getNoData(), color);
0219:                jasperPrint.setPageHeight(offsetY + report.getBottomMargin());
0220:
0221:                // contour lines
0222:                for (int i = 0; i < contourElementsList.size(); i++) {
0223:                    pageElements
0224:                            .add(getContourShape((ContourElement) contourElementsList
0225:                                    .get(i)));
0226:                }
0227:
0228:                //band dotted delimitation
0229:                for (int i = 0; i < bandSeparatorList.size(); i++) {
0230:                    pageElements.add(0,
0231:                            getContourLine((ContourElement) bandSeparatorList
0232:                                    .get(i)));
0233:                }
0234:
0235:                // column dotted delimitation 
0236:                int leftColumnPos = report.getLeftMargin();
0237:                for (int i = 0; i < report.getColumnCount(); i++) {
0238:                    pageElements.add(0, getContourLine(new ContourElement(
0239:                            leftColumnPos, upColumns, 0, downColumns
0240:                                    - upColumns, color,
0241:                            JRGraphicElement.PEN_DOTTED)));
0242:                    leftColumnPos += report.getColumnWidth();
0243:                    pageElements.add(0, getContourLine(new ContourElement(
0244:                            leftColumnPos, upColumns, 0, downColumns
0245:                                    - upColumns, color,
0246:                            JRGraphicElement.PEN_DOTTED)));
0247:                    leftColumnPos += report.getColumnSpacing();
0248:                }
0249:                // page dotted contour line
0250:                pageElements.add(0, getContourLine(new ContourElement(0, report
0251:                        .getTopMargin(), pageWidth, 0, color,
0252:                        JRGraphicElement.PEN_DOTTED)));
0253:                pageElements.add(0, getContourLine(new ContourElement(0,
0254:                        offsetY, pageWidth, 0, color,
0255:                        JRGraphicElement.PEN_DOTTED)));
0256:                pageElements.add(0, getContourLine(new ContourElement(offsetX,
0257:                        0, 0, jasperPrint.getPageHeight(), color,
0258:                        JRGraphicElement.PEN_DOTTED)));
0259:                pageElements.add(0, getContourLine(new ContourElement(pageWidth
0260:                        - report.getRightMargin(), 0, 0, jasperPrint
0261:                        .getPageHeight(), color, JRGraphicElement.PEN_DOTTED)));
0262:
0263:                page.setElements(pageElements);
0264:                jasperPrint.addPage(page);
0265:            }
0266:
0267:            protected void setStyles(JRReport report) throws JRException {
0268:                styleFactory = new StyleFactory();
0269:                stylesMap = new SequencedHashMap();
0270:
0271:                loadReportStyles(report);
0272:
0273:                for (Iterator it = stylesMap.values().iterator(); it.hasNext();) {
0274:                    JRStyle style = (JRStyle) it.next();
0275:                    jasperPrint.addStyle(style);
0276:                }
0277:
0278:                JRStyle reportDefault = report.getDefaultStyle();
0279:                JRStyle printDefault = null;
0280:                if (reportDefault == null) {
0281:                    //search for the last default style
0282:                    for (Iterator it = stylesMap.values().iterator(); it
0283:                            .hasNext();) {
0284:                        JRStyle style = (JRStyle) it.next();
0285:                        if (style.isDefault()) {
0286:                            printDefault = style;
0287:                        }
0288:                    }
0289:                } else {
0290:                    printDefault = reportDefault;
0291:                }
0292:
0293:                if (printDefault != null) {
0294:                    jasperPrint.setDefaultStyle(printDefault);
0295:                }
0296:            }
0297:
0298:            protected void loadReportStyles(JRReport report) {
0299:                JRReportTemplate[] templates = report.getTemplates();
0300:                if (templates != null) {
0301:                    Set loadedLocations = new HashSet();
0302:                    for (int i = 0; i < templates.length; i++) {
0303:                        loadReportTemplateStyles(templates[i], loadedLocations);
0304:                    }
0305:                }
0306:
0307:                collectStyles(report.getStyles());
0308:            }
0309:
0310:            protected void loadReportTemplateStyles(JRReportTemplate template,
0311:                    Set loadedLocations) {
0312:                JRExpression sourceExpression = template.getSourceExpression();
0313:                if (sourceExpression != null) {
0314:                    String location = getSimpleExpressionString(sourceExpression);
0315:                    if (location == null) {
0316:                        log
0317:                                .warn("Template source expression "
0318:                                        + sourceExpression.getText()
0319:                                        + "cannot be evaluated; some styles might remain unresolved.");
0320:                    } else {
0321:                        loadTemplateStyles(location, loadedLocations);
0322:                    }
0323:                }
0324:            }
0325:
0326:            protected void loadTemplateStyles(String location,
0327:                    Set loadedLocations) {
0328:                if (!loadedLocations.add(location)) {
0329:                    throw new JRRuntimeException(
0330:                            "Circular dependency found for template at location "
0331:                                    + location);
0332:                }
0333:
0334:                JRTemplate template;
0335:                try {
0336:                    template = JRXmlTemplateLoader.load(location);
0337:                } catch (Exception e) {
0338:                    log.warn("Could not load template from location "
0339:                            + location
0340:                            + "; some styles might remain unresolved.");
0341:                    return;
0342:                }
0343:
0344:                JRTemplateReference[] includedTemplates = template
0345:                        .getIncludedTemplates();
0346:                if (includedTemplates != null) {
0347:                    for (int i = 0; i < includedTemplates.length; i++) {
0348:                        JRTemplateReference reference = includedTemplates[i];
0349:                        loadTemplateStyles(reference.getLocation(),
0350:                                loadedLocations);
0351:                    }
0352:                }
0353:
0354:                collectStyles(template.getStyles());
0355:            }
0356:
0357:            protected void collectStyles(JRStyle[] styles) {
0358:                if (styles != null) {
0359:                    for (int i = 0; i < styles.length; i++) {
0360:                        JRStyle style = styles[i];
0361:                        JRStyle copy = styleFactory.getStyle(style);
0362:                        stylesMap.put(copy.getName(), copy);
0363:                    }
0364:                }
0365:            }
0366:
0367:            /**
0368:             * This method reads elements or element groups from a JRDesignElements list (usually a band elements
0369:             * list), and transfers all their relevant properties to the correspondents JRPrintElements
0370:             * @param list list of JRDesignElement
0371:             * @param bandHeight the band height 
0372:             */
0373:            private void setElements(List list, int bandHeight, int parentX,
0374:                    int parentY, int parentWidth, int parentHeight,
0375:                    boolean isFrameChild) {
0376:                Iterator it = list.iterator();
0377:                while (it.hasNext()) {
0378:                    Object element = it.next();
0379:                    JRBasePrintElement baseElement = null;
0380:
0381:                    //element could be either a JRFrame, JRDesignElementGroup or a JRDesignElement instance
0382:                    if (element instanceof  JRFrame) {
0383:                        //it extends both JRElement and JRElementGroup
0384:                        JRFrame frameElement = (JRFrame) element;
0385:                        baseElement = getFrameElement(frameElement);
0386:                        setBaseElement(baseElement, (JRElement) element,
0387:                                bandHeight, parentX, parentY, parentWidth,
0388:                                parentHeight, isFrameChild);
0389:                        if (baseElement.getHeight() > 0)
0390:                            pageElements.add(baseElement);
0391:
0392:                        JRBasePrintFrame basePrintFrameElement = (JRBasePrintFrame) baseElement;
0393:
0394:                        // one add contour lines only if the element has no its own border line
0395:                        // if at least one border line is defined, the element gets no contour line
0396:                        if (basePrintFrameElement.getBorder() == JRGraphicElement.PEN_NONE
0397:                                && basePrintFrameElement.getTopBorder() == JRGraphicElement.PEN_NONE
0398:                                && basePrintFrameElement.getLeftBorder() == JRGraphicElement.PEN_NONE
0399:                                && basePrintFrameElement.getRightBorder() == JRGraphicElement.PEN_NONE
0400:                                && basePrintFrameElement.getBottomBorder() == JRGraphicElement.PEN_NONE)
0401:                            addContourElement(baseElement);
0402:
0403:                        // setting frame children elements
0404:                        List frameChildren = Arrays.asList(frameElement
0405:                                .getElements());
0406:                        List availableElements = new ArrayList();
0407:                        Iterator frameIt = frameChildren.iterator();
0408:                        while (frameIt.hasNext()) {
0409:                            JRElement elem = (JRElement) frameIt.next();
0410:                            // consider only elements
0411:                            if (elem.getY() < frameElement.getHeight()
0412:                                    && elem.getX() < frameElement.getWidth()) {
0413:                                availableElements.add(elem);
0414:                            }
0415:                        }
0416:                        setElements(availableElements, bandHeight,
0417:                                basePrintFrameElement.getX(),
0418:                                basePrintFrameElement.getY(),
0419:                                basePrintFrameElement.getWidth(),
0420:                                basePrintFrameElement.getHeight(), true);
0421:                    } else if (element instanceof  JRElementGroup) {
0422:                        List children = ((JRElementGroup) element)
0423:                                .getChildren();
0424:                        setElements(children, bandHeight, parentX, parentY,
0425:                                parentWidth, parentHeight, isFrameChild);
0426:                    } else {
0427:                        hasContour = false;
0428:
0429:                        if (element instanceof  JRImage) {
0430:                            baseElement = getImageElement((JRImage) element);
0431:                        } else if (element instanceof  JRGraphicElement) {
0432:                            baseElement = getGraphicElement((JRGraphicElement) element);
0433:                        } else if (element instanceof  JRTextElement) {
0434:                            baseElement = getTextElement((JRTextElement) element);
0435:                        } else if (element instanceof  JRChart) {
0436:                            baseElement = getChartElement((JRChart) element);
0437:                        } else if (element instanceof  JRCrosstab) {
0438:                            setElements(getCrosstabList((JRCrosstab) element),
0439:                                    bandHeight, parentX, parentY, parentWidth,
0440:                                    parentHeight, isFrameChild);
0441:                            continue;
0442:                        } else {
0443:                            baseElement = new JRBasePrintElement(
0444:                                    defaultStyleProvider);
0445:                        }
0446:
0447:                        setBaseElement(baseElement, (JRElement) element,
0448:                                bandHeight, parentX, parentY, parentWidth,
0449:                                parentHeight, isFrameChild);
0450:
0451:                        if (element instanceof  JRTextElement) {
0452:                            JRBasePrintText basePrintTextElement = (JRBasePrintText) baseElement;
0453:                            measureTextElement(basePrintTextElement,
0454:                                    (JRTextElement) element);
0455:                        }
0456:
0457:                        // one add elements in this list before setting the print page elements,
0458:                        // because dotted design lines should be painted first
0459:                        pageElements.add(baseElement);
0460:
0461:                        if (!hasContour) {
0462:                            // preparing for drawing additional contour lines
0463:                            addContourElement(baseElement);
0464:                        }
0465:                        hasContour = false;
0466:                    }
0467:                }
0468:            }
0469:
0470:            /**
0471:             * The method is a caller for the main setElements() method
0472:             * @param band the band where elements are picked up from
0473:             */
0474:            private void addBandElements(JRBand band, Color color) {
0475:                if (band != null) {
0476:                    if (isFirstBand) {
0477:                        bandSeparatorList.add(new ContourElement(0, offsetY,
0478:                                pageWidth, 0, color,
0479:                                JRGraphicElement.PEN_DOTTED));
0480:                    }
0481:                    setElements(band.getChildren(), band.getHeight(), 0, 0, 0,
0482:                            0, false);
0483:                    offsetY += band.getHeight();
0484:                    bandSeparatorList.add(new ContourElement(0, offsetY,
0485:                            pageWidth, 0, color, JRGraphicElement.PEN_DOTTED));
0486:                    isFirstBand = false;
0487:                }
0488:            }
0489:
0490:            /**
0491:             * 
0492:             * @param exp
0493:             * @return
0494:             */
0495:            private String getExpressionText(JRExpression exp) {
0496:                return exp == null ? null : exp.getText();
0497:            }
0498:
0499:            /**
0500:             * Returns a styled text associated with a textElement
0501:             * @param textElement
0502:             * @return
0503:             */
0504:            private JRStyledText getStyledText(JRTextElement textElement,
0505:                    JRPrintText printText) {
0506:                JRStyledText styledText = null;
0507:
0508:                String text = null;
0509:                if (textElement instanceof  JRStaticText) {
0510:                    text = ((JRStaticText) textElement).getText();
0511:                } else if (textElement instanceof  JRTextField) {
0512:                    JRExpression textExpression = ((JRTextField) textElement)
0513:                            .getExpression();
0514:                    if (textExpression != null) {
0515:                        text = textExpression.getText();
0516:                    }
0517:                }
0518:
0519:                if (text == null) {
0520:                    text = "";
0521:                }
0522:
0523:                Map attributes = new HashMap();
0524:                JRFontUtil.setAttributes(attributes, printText);
0525:                attributes.put(TextAttribute.FOREGROUND, printText
0526:                        .getForecolor());
0527:
0528:                if (textElement instanceof  JRStaticText
0529:                        && textElement.isStyledText()) {
0530:                    try {
0531:                        styledText = styledTextParser.parse(attributes, text);
0532:                    } catch (SAXException e) {
0533:                        //ignore if invalid styled text and treat like normal text
0534:                    }
0535:                }
0536:
0537:                if (styledText == null) {
0538:                    styledText = new JRStyledText();
0539:                    styledText.append(text);
0540:                    styledText.addRun(new JRStyledText.Run(attributes, 0, text
0541:                            .length()));
0542:                }
0543:
0544:                return styledText;
0545:            }
0546:
0547:            protected String getSimpleExpressionString(JRExpression expression) {
0548:                String value = null;
0549:                if (expression != null) {
0550:                    JRExpressionChunk[] chunks = expression.getChunks();
0551:                    if (chunks != null
0552:                            && chunks.length == 1
0553:                            && chunks[0].getType() == JRExpressionChunk.TYPE_TEXT) {
0554:                        String chunk = chunks[0].getText().trim();
0555:                        int chunkLength = chunk.length();
0556:                        if (chunk.charAt(0) == '"'
0557:                                && chunk.charAt(chunkLength - 1) == '"') {
0558:                            value = chunk.substring(1, chunkLength - 1);
0559:                        }
0560:                    }
0561:                }
0562:                return value;
0563:            }
0564:
0565:            /**
0566:             * 
0567:             * @param imageElement
0568:             * @return
0569:             */
0570:            private JRRenderable getRenderer(JRImage imageElement) {
0571:                JRRenderable imageRenderer = null;
0572:                Image awtImage = null;
0573:
0574:                String location = getSimpleExpressionString(imageElement
0575:                        .getExpression());
0576:                if (location != null) {
0577:                    try {
0578:                        awtImage = JRImageLoader.loadImage(JRLoader
0579:                                .loadBytesFromLocation(location));
0580:                        if (awtImage == null) {
0581:                            awtImage = JRImageLoader
0582:                                    .getImage(JRImageLoader.NO_IMAGE);
0583:                            imageElement
0584:                                    .setScaleImage(JRImage.SCALE_IMAGE_CLIP);
0585:                            imageElement
0586:                                    .setStretchType(JRElement.STRETCH_TYPE_NO_STRETCH);
0587:                        }
0588:                        imageRenderer = JRImageRenderer.getInstance(awtImage,
0589:                                imageElement.getOnErrorType());
0590:                        return imageRenderer;
0591:                    } catch (JRException e) {
0592:                        e.printStackTrace();
0593:                    }
0594:                }
0595:
0596:                try {
0597:                    awtImage = JRImageLoader.getImage(JRImageLoader.NO_IMAGE);
0598:                    imageRenderer = JRImageRenderer.getInstance(awtImage,
0599:                            imageElement.getOnErrorType());
0600:                    imageElement.setScaleImage(JRImage.SCALE_IMAGE_CLIP);
0601:                    imageElement
0602:                            .setStretchType(JRElement.STRETCH_TYPE_NO_STRETCH);
0603:                } catch (JRException e) {
0604:                    e.printStackTrace();
0605:                }
0606:
0607:                return imageRenderer;
0608:            }
0609:
0610:            /**
0611:             * 
0612:             * @param imageElement image representing a chart element
0613:             * @return
0614:             */
0615:            private JRRenderable getRenderer(JRChart imageElement) {
0616:                JRRenderable imageRenderer = null;
0617:                Image awtImage = null;
0618:
0619:                try {
0620:                    awtImage = JRImageLoader
0621:                            .getImage(JRImageLoader.CHART_IMAGE);
0622:                    if (awtImage == null) {
0623:                        awtImage = JRImageLoader
0624:                                .getImage(JRImageLoader.NO_IMAGE);
0625:                    }
0626:                    imageRenderer = JRImageRenderer.getInstance(awtImage,
0627:                            JRImage.ON_ERROR_TYPE_ICON);
0628:                    imageElement
0629:                            .setStretchType(JRElement.STRETCH_TYPE_NO_STRETCH);
0630:                } catch (JRException e) {
0631:                    e.printStackTrace();
0632:                }
0633:
0634:                return imageRenderer;
0635:            }
0636:
0637:            /**
0638:             * Return a top-down dotted line having the specified coordinates and dimensions
0639:             * @param x the x coordinate of the starting point
0640:             * @param y the y coordinate of the starting point
0641:             * @param width the line width
0642:             * @param height the line height
0643:             * @param color the line color
0644:             * @param pen the line style
0645:             * @return JRPrintLine
0646:             */
0647:            private JRPrintLine getContourLine(ContourElement contour) {
0648:                JRPrintLine line = new JRBasePrintLine(defaultStyleProvider);
0649:                line.setX(contour.getContourX());
0650:                line.setY(contour.getContourY());
0651:                line.setWidth(contour.getContourWidth());
0652:                line.setHeight(contour.getContourHeight());
0653:                line.setPen(contour.getContourPen());
0654:                line.setForecolor(contour.getContourColor());
0655:                line.setDirection(JRLine.DIRECTION_TOP_DOWN);
0656:                return line;
0657:            }
0658:
0659:            /**
0660:             * Return a top-down dotted line having the specified coordinates and dimensions
0661:             * @param x the x coordinate of the starting point
0662:             * @param y the y coordinate of the starting point
0663:             * @param width the line width
0664:             * @param height the line height
0665:             * @param color the line color
0666:             * @param pen the line style
0667:             * @return JRPrintLine
0668:             */
0669:            private JRBasePrintElement getContourShape(ContourElement contour) {
0670:                JRBasePrintRectangle rectangle = new JRBasePrintRectangle(
0671:                        defaultStyleProvider);
0672:                rectangle.setX(contour.getContourX());
0673:                rectangle.setY(contour.getContourY());
0674:                rectangle.setWidth(contour.getContourWidth());
0675:                rectangle.setHeight(contour.getContourHeight());
0676:                rectangle.setPen(JRGraphicElement.PEN_THIN);
0677:                rectangle.setForecolor(contour.getContourColor());
0678:                rectangle.setMode(JRElement.MODE_TRANSPARENT);
0679:                return rectangle;
0680:            }
0681:
0682:            /**
0683:             * 
0684:             * @param imageElement
0685:             * @return
0686:             */
0687:            private JRBasePrintElement getImageElement(JRImage imageElement) {
0688:                JRBasePrintImage basePrintImageElement = new JRBasePrintImage(
0689:                        defaultStyleProvider);
0690:
0691:                basePrintImageElement
0692:                        .setAnchorName(getExpressionText(imageElement
0693:                                .getAnchorNameExpression()));
0694:                basePrintImageElement.setBookmarkLevel(imageElement
0695:                        .getBookmarkLevel());
0696:                basePrintImageElement.setBorder(imageElement.getOwnBorder());
0697:                basePrintImageElement.setBorderColor(imageElement
0698:                        .getOwnBorderColor());
0699:                basePrintImageElement.setBottomBorder(imageElement
0700:                        .getOwnBottomBorder());
0701:                basePrintImageElement.setBottomBorderColor(imageElement
0702:                        .getOwnBottomBorderColor());
0703:                basePrintImageElement.setBottomPadding(imageElement
0704:                        .getOwnBottomPadding());
0705:                basePrintImageElement.setFill(imageElement.getOwnFill());
0706:                basePrintImageElement.setHorizontalAlignment(imageElement
0707:                        .getOwnHorizontalAlignment());
0708:                basePrintImageElement.setLazy(imageElement.isLazy());
0709:                basePrintImageElement.setLeftBorder(imageElement
0710:                        .getOwnLeftBorder());
0711:                basePrintImageElement.setLeftBorderColor(imageElement
0712:                        .getOwnLeftBorderColor());
0713:                basePrintImageElement.setLeftPadding(imageElement
0714:                        .getOwnLeftPadding());
0715:                basePrintImageElement.setLinkType(imageElement.getLinkType());
0716:                basePrintImageElement.setOnErrorType(imageElement
0717:                        .getOnErrorType());
0718:                basePrintImageElement.setPadding(imageElement.getOwnPadding());
0719:                basePrintImageElement.setPen(imageElement.getOwnPen());
0720:                basePrintImageElement.setRightBorder(imageElement
0721:                        .getOwnRightBorder());
0722:                basePrintImageElement.setRightBorderColor(imageElement
0723:                        .getOwnRightBorderColor());
0724:                basePrintImageElement.setRightPadding(imageElement
0725:                        .getOwnRightPadding());
0726:                basePrintImageElement.setTopBorder(imageElement
0727:                        .getOwnTopBorder());
0728:                basePrintImageElement.setTopBorderColor(imageElement
0729:                        .getOwnTopBorderColor());
0730:                basePrintImageElement.setTopPadding(imageElement
0731:                        .getOwnTopPadding());
0732:                basePrintImageElement.setVerticalAlignment(imageElement
0733:                        .getOwnVerticalAlignment());
0734:                basePrintImageElement.setRenderer(getRenderer(imageElement));
0735:                basePrintImageElement.setScaleImage(imageElement
0736:                        .getOwnScaleImage());
0737:
0738:                hasContour = (basePrintImageElement.getBorder() != JRGraphicElement.PEN_NONE || (basePrintImageElement
0739:                        .getTopBorder() != JRGraphicElement.PEN_NONE
0740:                        && basePrintImageElement.getLeftBorder() != JRGraphicElement.PEN_NONE
0741:                        && basePrintImageElement.getRightBorder() != JRGraphicElement.PEN_NONE && basePrintImageElement
0742:                        .getBottomBorder() != JRGraphicElement.PEN_NONE));
0743:                return basePrintImageElement;
0744:            }
0745:
0746:            /**
0747:             * 
0748:             * @param imageElement
0749:             * @return
0750:             */
0751:            private JRBasePrintElement getChartElement(JRChart imageElement) {
0752:                JRBasePrintImage basePrintImageElement = new JRBasePrintImage(
0753:                        defaultStyleProvider);
0754:
0755:                basePrintImageElement
0756:                        .setAnchorName(getExpressionText(imageElement
0757:                                .getAnchorNameExpression()));
0758:                basePrintImageElement.setBookmarkLevel(imageElement
0759:                        .getBookmarkLevel());
0760:                basePrintImageElement.setBorder(imageElement.getOwnBorder());
0761:                basePrintImageElement.setBorderColor(imageElement
0762:                        .getOwnBorderColor());
0763:                basePrintImageElement.setBottomBorder(imageElement
0764:                        .getOwnBottomBorder());
0765:                basePrintImageElement.setBottomBorderColor(imageElement
0766:                        .getOwnBottomBorderColor());
0767:                basePrintImageElement.setBottomPadding(imageElement
0768:                        .getOwnBottomPadding());
0769:                basePrintImageElement.setLeftBorder(imageElement
0770:                        .getOwnLeftBorder());
0771:                basePrintImageElement.setLeftBorderColor(imageElement
0772:                        .getOwnLeftBorderColor());
0773:                basePrintImageElement.setLeftPadding(imageElement
0774:                        .getOwnLeftPadding());
0775:                basePrintImageElement.setLinkType(imageElement.getLinkType());
0776:                basePrintImageElement
0777:                        .setOnErrorType(JRImage.ON_ERROR_TYPE_ICON);
0778:                basePrintImageElement.setPadding(imageElement.getPadding());
0779:                basePrintImageElement.setPen(JRGraphicElement.PEN_THIN);
0780:                basePrintImageElement.setRightBorder(imageElement
0781:                        .getOwnRightBorder());
0782:                basePrintImageElement.setRightBorderColor(imageElement
0783:                        .getOwnRightBorderColor());
0784:                basePrintImageElement.setRightPadding(imageElement
0785:                        .getOwnRightPadding());
0786:                basePrintImageElement.setTopBorder(imageElement
0787:                        .getOwnTopBorder());
0788:                basePrintImageElement.setTopBorderColor(imageElement
0789:                        .getOwnTopBorderColor());
0790:                basePrintImageElement.setTopPadding(imageElement
0791:                        .getOwnTopPadding());
0792:                basePrintImageElement.setRenderer(getRenderer(imageElement));
0793:                basePrintImageElement.setScaleImage(JRImage.SCALE_IMAGE_CLIP);
0794:                hasContour = (basePrintImageElement.getBorder() != JRGraphicElement.PEN_NONE || (basePrintImageElement
0795:                        .getTopBorder() != JRGraphicElement.PEN_NONE
0796:                        && basePrintImageElement.getLeftBorder() != JRGraphicElement.PEN_NONE
0797:                        && basePrintImageElement.getRightBorder() != JRGraphicElement.PEN_NONE && basePrintImageElement
0798:                        .getBottomBorder() != JRGraphicElement.PEN_NONE));
0799:                return basePrintImageElement;
0800:            }
0801:
0802:            /**
0803:             * 
0804:             * @param element
0805:             * @return
0806:             */
0807:            private JRBasePrintElement getGraphicElement(
0808:                    JRGraphicElement element) {
0809:                JRBasePrintElement baseElement = null;
0810:                if (element instanceof  JREllipse) {
0811:                    baseElement = new JRBasePrintEllipse(defaultStyleProvider);
0812:                } else if (element instanceof  JRRectangle) {
0813:                    baseElement = new JRBasePrintRectangle(defaultStyleProvider);
0814:                    ((JRBasePrintRectangle) baseElement)
0815:                            .setRadius(((JRRectangle) element).getOwnRadius());
0816:                } else if (element instanceof  JRLine) {
0817:                    baseElement = new JRBasePrintLine(defaultStyleProvider);
0818:                    ((JRBasePrintLine) baseElement)
0819:                            .setDirection(((JRLine) element).getDirection());
0820:                }
0821:                JRBasePrintGraphicElement basePrintGraphicElement = (JRBasePrintGraphicElement) baseElement;
0822:                JRGraphicElement graphicElement = (JRGraphicElement) element;
0823:                basePrintGraphicElement.setFill(graphicElement.getOwnFill());
0824:                basePrintGraphicElement.setPen(graphicElement.getOwnPen());
0825:
0826:                hasContour = basePrintGraphicElement.getPen() != JRGraphicElement.PEN_NONE;
0827:                return basePrintGraphicElement;
0828:            }
0829:
0830:            /**
0831:             * 
0832:             * @param frameElement
0833:             * @return
0834:             */
0835:            private JRBasePrintElement getFrameElement(JRFrame frameElement) {
0836:                JRBasePrintFrame basePrintFrameElement = new JRBasePrintFrame(
0837:                        defaultStyleProvider);
0838:                basePrintFrameElement.setBorder(frameElement.getOwnBorder());
0839:                basePrintFrameElement.setBorderColor(frameElement
0840:                        .getOwnBorderColor());
0841:                basePrintFrameElement.setBottomBorder(frameElement
0842:                        .getOwnBottomBorder());
0843:                basePrintFrameElement.setBottomBorderColor(frameElement
0844:                        .getOwnBottomBorderColor());
0845:                basePrintFrameElement.setBottomPadding(frameElement
0846:                        .getOwnBottomPadding());
0847:                basePrintFrameElement.setLeftBorder(frameElement
0848:                        .getOwnLeftBorder());
0849:                basePrintFrameElement.setLeftBorderColor(frameElement
0850:                        .getOwnLeftBorderColor());
0851:                basePrintFrameElement.setLeftPadding(frameElement
0852:                        .getOwnLeftPadding());
0853:                basePrintFrameElement.setPadding(frameElement.getOwnPadding());
0854:                basePrintFrameElement.setRightBorder(frameElement
0855:                        .getOwnRightBorder());
0856:                basePrintFrameElement.setRightBorderColor(frameElement
0857:                        .getOwnRightBorderColor());
0858:                basePrintFrameElement.setRightPadding(frameElement
0859:                        .getOwnRightPadding());
0860:                basePrintFrameElement.setTopBorder(frameElement
0861:                        .getOwnTopBorder());
0862:                basePrintFrameElement.setTopBorderColor(frameElement
0863:                        .getOwnTopBorderColor());
0864:                basePrintFrameElement.setTopPadding(frameElement
0865:                        .getOwnTopPadding());
0866:                return basePrintFrameElement;
0867:            }
0868:
0869:            /**
0870:             * 
0871:             * @param textElement
0872:             * @return
0873:             */
0874:            private JRBasePrintElement getTextElement(JRTextElement textElement) {
0875:                JRBasePrintText basePrintTextElement = new JRBasePrintText(
0876:                        defaultStyleProvider);
0877:
0878:                basePrintTextElement.setBorder(textElement.getOwnBorder());
0879:                basePrintTextElement.setBorderColor(textElement
0880:                        .getOwnBorderColor());
0881:                basePrintTextElement.setBottomBorder(textElement
0882:                        .getOwnBottomBorder());
0883:                basePrintTextElement.setBottomBorderColor(textElement
0884:                        .getOwnBottomBorderColor());
0885:                basePrintTextElement.setBottomPadding(textElement
0886:                        .getOwnBottomPadding());
0887:                basePrintTextElement.setLeftBorder(textElement
0888:                        .getOwnLeftBorder());
0889:                basePrintTextElement.setLeftBorderColor(textElement
0890:                        .getOwnLeftBorderColor());
0891:                basePrintTextElement.setLeftPadding(textElement
0892:                        .getOwnLeftPadding());
0893:                basePrintTextElement.setPadding(textElement.getOwnPadding());
0894:                basePrintTextElement.setRightBorder(textElement
0895:                        .getOwnRightBorder());
0896:                basePrintTextElement.setRightBorderColor(textElement
0897:                        .getOwnRightBorderColor());
0898:                basePrintTextElement.setRightPadding(textElement
0899:                        .getOwnRightPadding());
0900:                basePrintTextElement
0901:                        .setTopBorder(textElement.getOwnTopBorder());
0902:                basePrintTextElement.setTopBorderColor(textElement
0903:                        .getOwnTopBorderColor());
0904:                basePrintTextElement.setTopPadding(textElement
0905:                        .getOwnTopPadding());
0906:                basePrintTextElement.setBold(textElement.isOwnBold());
0907:                basePrintTextElement.setFontName(textElement.getOwnFontName());
0908:                basePrintTextElement.setFontSize(textElement.getOwnFontSize());
0909:                basePrintTextElement.setHorizontalAlignment(textElement
0910:                        .getOwnHorizontalAlignment());
0911:                basePrintTextElement.setItalic(textElement.isOwnItalic());
0912:                basePrintTextElement.setLineSpacing(textElement
0913:                        .getOwnLineSpacing());
0914:                basePrintTextElement.setPdfEmbedded(textElement
0915:                        .isOwnPdfEmbedded());
0916:                basePrintTextElement.setPdfEncoding(textElement
0917:                        .getOwnPdfEncoding());
0918:                basePrintTextElement.setPdfFontName(textElement
0919:                        .getOwnPdfFontName());
0920:                basePrintTextElement.setReportFont(textElement.getReportFont());
0921:                basePrintTextElement.setRotation(textElement.getOwnRotation());
0922:                basePrintTextElement.setStrikeThrough(textElement
0923:                        .isOwnStrikeThrough());
0924:                basePrintTextElement.setStyledText(textElement
0925:                        .isOwnStyledText());
0926:                basePrintTextElement.setUnderline(textElement.isOwnUnderline());
0927:                basePrintTextElement.setVerticalAlignment(textElement
0928:                        .getOwnVerticalAlignment());
0929:
0930:                if (textElement instanceof  JRStaticText) {
0931:                    basePrintTextElement.setText(((JRStaticText) textElement)
0932:                            .getText());
0933:                }
0934:                if (textElement instanceof  JRTextField) {
0935:                    JRTextField textFieldElement = (JRTextField) textElement;
0936:                    basePrintTextElement
0937:                            .setAnchorName(getExpressionText(textFieldElement
0938:                                    .getAnchorNameExpression()));
0939:                    basePrintTextElement.setBookmarkLevel(textFieldElement
0940:                            .getBookmarkLevel());
0941:                    basePrintTextElement.setLinkType(textFieldElement
0942:                            .getLinkType());
0943:                    basePrintTextElement.setPattern(textFieldElement
0944:                            .getOwnPattern());
0945:                    basePrintTextElement
0946:                            .setText(getExpressionText(textFieldElement
0947:                                    .getExpression()));
0948:                }
0949:
0950:                hasContour = (basePrintTextElement.getBorder() != JRGraphicElement.PEN_NONE || (basePrintTextElement
0951:                        .getTopBorder() != JRGraphicElement.PEN_NONE
0952:                        && basePrintTextElement.getLeftBorder() != JRGraphicElement.PEN_NONE
0953:                        && basePrintTextElement.getRightBorder() != JRGraphicElement.PEN_NONE && basePrintTextElement
0954:                        .getBottomBorder() != JRGraphicElement.PEN_NONE));
0955:                return basePrintTextElement;
0956:            }
0957:
0958:            /**
0959:             * 
0960:             * @param basePrintTextElement
0961:             * @param textElement
0962:             */
0963:            private void measureTextElement(
0964:                    JRBasePrintText basePrintTextElement,
0965:                    JRTextElement textElement) {
0966:                TextMeasurer textMeasurer = new TextMeasurer(textElement
0967:                        .getWidth(), textElement.getHeight(),
0968:                        basePrintTextElement, basePrintTextElement
0969:                                .getRotation(), basePrintTextElement
0970:                                .getLineSpacing(), basePrintTextElement
0971:                                .isStyledText(), basePrintTextElement
0972:                                .getFontSize());
0973:                JRStyledText styledText = getStyledText(textElement,
0974:                        basePrintTextElement);
0975:                textMeasurer.measure(styledText, styledText.getText(), 0, 0);
0976:                basePrintTextElement
0977:                        .setTextHeight(textMeasurer.getTextHeight() < basePrintTextElement
0978:                                .getHeight() ? textMeasurer.getTextHeight()
0979:                                : basePrintTextElement.getHeight());
0980:                basePrintTextElement.setLeadingOffset(textMeasurer
0981:                        .getLeadingOffset());
0982:                basePrintTextElement.setLineSpacingFactor(textMeasurer
0983:                        .getLineSpacingFactor());
0984:
0985:            }
0986:
0987:            /**
0988:             * 
0989:             * @param baseElement
0990:             * @param designElement
0991:             * @param bandHeight
0992:             */
0993:            private void setBaseElement(JRBasePrintElement baseElement,
0994:                    JRElement designElement, int bandHeight, int parentX,
0995:                    int parentY, int parentWidth, int parentHeight,
0996:                    boolean isFrameChild) {
0997:                baseElement.setBackcolor(designElement.getOwnBackcolor());
0998:                baseElement.setForecolor(designElement.getOwnForecolor());
0999:                baseElement.setKey(designElement.getKey());
1000:                baseElement.setMode(designElement.getOwnMode());
1001:                baseElement.setStyle(resolveStyle(designElement));
1002:
1003:                if (isFrameChild) {
1004:                    setBaseElementGeometry(baseElement, designElement, parentX,
1005:                            parentY, parentWidth, parentHeight);
1006:                } else {
1007:                    setBaseElementGeometry(baseElement, designElement, offsetX,
1008:                            offsetY, pageWidth, bandHeight);
1009:                }
1010:            }
1011:
1012:            protected JRStyle resolveStyle(JRStyleContainer originalContainer) {
1013:                JRStyle originalStyle = originalContainer.getStyle();
1014:                String nameReference = originalContainer
1015:                        .getStyleNameReference();
1016:                JRStyle style;
1017:                if (originalStyle != null) {
1018:                    style = styleFactory.getStyle(originalStyle);
1019:                } else if (nameReference != null) {
1020:                    style = (JRStyle) stylesMap.get(nameReference);
1021:                    if (style == null) {
1022:                        log.warn("Style " + nameReference
1023:                                + " could not be resolved.");
1024:                    }
1025:                } else {
1026:                    style = null;
1027:                }
1028:                return style;
1029:            }
1030:
1031:            private void setBaseElementGeometry(JRBasePrintElement baseElement,
1032:                    JRElement element, int parentX, int parentY,
1033:                    int parentWidth, int parentHeight) {
1034:                baseElement.setX(element.getX() + parentX);
1035:                baseElement.setY(element.getY() + parentY);
1036:
1037:                baseElement.setWidth(element.getWidth());
1038:                // truncating boundary elements, if necessary
1039:                if (element.getY() + element.getHeight() > parentHeight) {
1040:                    baseElement.setHeight(parentHeight - element.getY());
1041:                } else {
1042:                    baseElement.setHeight(element.getHeight());
1043:                }
1044:
1045:                if (parentWidth > 0
1046:                        && element.getX() + element.getWidth() > parentWidth) {
1047:                    baseElement.setWidth(parentWidth - element.getX());
1048:                } else {
1049:                    baseElement.setWidth(element.getWidth());
1050:                }
1051:
1052:            }
1053:
1054:            /**
1055:             * 
1056:             * @param baseElement
1057:             */
1058:            private void addContourElement(JRBasePrintElement baseElement) {
1059:                Color contourColor = baseElement.getForecolor() == null ? Color.black
1060:                        : baseElement.getForecolor();
1061:                ContourElement contourElement = new ContourElement(baseElement
1062:                        .getX(), baseElement.getY(), baseElement.getWidth(),
1063:                        baseElement.getHeight(), contourColor,
1064:                        JRGraphicElement.PEN_THIN);
1065:                contourElementsList.add(contourElement);
1066:            }
1067:
1068:            /**
1069:             * The method picks up elements from within a crosstab and creates a list
1070:             * of frames containing all valid cells info; each cell is represented as a frame
1071:             * @param crosstab the crosstab element
1072:             * @return List the list of available design elements (frames)
1073:             */
1074:            protected List getCrosstabList(JRCrosstab crosstab) {
1075:                List crosstabElements = new ArrayList();
1076:
1077:                JRCrosstabRowGroup[] rowGroups = crosstab.getRowGroups();
1078:                int rowHeadersXOffset = 0;
1079:                for (int i = 0; i < rowGroups.length; i++) {
1080:                    rowHeadersXOffset += rowGroups[i].getWidth();
1081:                }
1082:
1083:                JRCrosstabColumnGroup[] columnGroups = crosstab
1084:                        .getColumnGroups();
1085:                int colHeadersYOffset = 0;
1086:                for (int i = 0; i < columnGroups.length; i++) {
1087:                    colHeadersYOffset += columnGroups[i].getHeight();
1088:                }
1089:
1090:                JRCellContents headerCell = crosstab.getHeaderCell();
1091:                if (headerCell != null) {
1092:                    if (headerCell.getWidth() != 0
1093:                            && headerCell.getHeight() != 0) {
1094:                        crosstabElements.add(getCrosstabCellFrame(headerCell,
1095:                                crosstab.getX(), crosstab.getY(), false, false,
1096:                                false));
1097:
1098:                    }
1099:
1100:                }
1101:
1102:                addCrosstabColumnHeaders(crosstab, rowHeadersXOffset,
1103:                        crosstabElements);
1104:                addCrosstabRows(crosstab, rowHeadersXOffset, colHeadersYOffset,
1105:                        crosstabElements);
1106:
1107:                if (crosstab.getRunDirection() == JRCrosstab.RUN_DIRECTION_RTL) {
1108:                    mirrorElements(crosstabElements, crosstab.getX(), crosstab
1109:                            .getWidth());
1110:                }
1111:
1112:                return crosstabElements;
1113:            }
1114:
1115:            protected void mirrorElements(List elements, int x, int width) {
1116:                for (Iterator it = elements.iterator(); it.hasNext();) {
1117:                    JRElement element = (JRElement) it.next();
1118:                    int mirrorX = 2 * x + width - element.getX()
1119:                            - element.getWidth();
1120:                    element.setX(mirrorX);
1121:                }
1122:            }
1123:
1124:            /**
1125:             * 
1126:             * @param cell
1127:             * @param x
1128:             * @param y
1129:             * @param left
1130:             * @param top
1131:             */
1132:            protected JRFrame getCrosstabCellFrame(JRCellContents cell, int x,
1133:                    int y, boolean left, boolean right, boolean top) {
1134:                JRDesignFrame frame = new JRDesignFrame(cell
1135:                        .getDefaultStyleProvider());
1136:                frame.setX(x);
1137:                frame.setY(y);
1138:                frame.setWidth(cell.getWidth());
1139:                frame.setHeight(cell.getHeight());
1140:
1141:                frame.setMode(cell.getMode());
1142:                frame.setBackcolor(cell.getBackcolor());
1143:                frame.setStyle(resolveStyle(cell));
1144:
1145:                JRBox box = cell.getBox();
1146:                if (box != null) {
1147:                    frame.setBox(box);
1148:
1149:                    boolean copyLeft = left
1150:                            && box.getLeftBorder() == JRGraphicElement.PEN_NONE
1151:                            && box.getRightBorder() != JRGraphicElement.PEN_NONE;
1152:                    boolean copyRight = right
1153:                            && box.getRightBorder() == JRGraphicElement.PEN_NONE
1154:                            && box.getLeftBorder() != JRGraphicElement.PEN_NONE;
1155:                    boolean copyTop = top
1156:                            && box.getTopBorder() == JRGraphicElement.PEN_NONE
1157:                            && box.getBottomBorder() != JRGraphicElement.PEN_NONE;
1158:
1159:                    if (copyLeft) {
1160:                        frame.setLeftBorder(box.getRightBorder());
1161:                        frame.setLeftBorderColor(box.getRightBorderColor());
1162:                    }
1163:
1164:                    if (copyRight) {
1165:                        frame.setRightBorder(box.getLeftBorder());
1166:                        frame.setRightBorderColor(box.getLeftBorderColor());
1167:                    }
1168:
1169:                    if (copyTop) {
1170:                        frame.setTopBorder(box.getBottomBorder());
1171:                        frame.setTopBorderColor(box.getBottomBorderColor());
1172:                    }
1173:                }
1174:
1175:                List children = cell.getChildren();
1176:                if (children != null) {
1177:                    for (Iterator it = children.iterator(); it.hasNext();) {
1178:                        JRChild child = (JRChild) it.next();
1179:                        if (child instanceof  JRElement) {
1180:                            frame.addElement((JRElement) child);
1181:                        } else if (child instanceof  JRElementGroup) {
1182:                            frame.addElementGroup((JRElementGroup) child);
1183:                        }
1184:                    }
1185:                }
1186:
1187:                return frame;
1188:            }
1189:
1190:            /**
1191:             * 
1192:             * @param crosstab
1193:             * @param rowHeadersXOffset
1194:             * @param crosstabElements
1195:             */
1196:            protected void addCrosstabColumnHeaders(JRCrosstab crosstab,
1197:                    int rowHeadersXOffset, List crosstabElements) {
1198:                JRCrosstabColumnGroup[] groups = crosstab.getColumnGroups();
1199:                int crosstabX = crosstab.getX();
1200:                int crosstabY = crosstab.getY();
1201:
1202:                for (int i = 0, x = 0, y = 0; i < groups.length; i++) {
1203:                    JRCrosstabColumnGroup group = groups[i];
1204:
1205:                    if (group.getTotalPosition() == BucketDefinition.TOTAL_POSITION_START) {
1206:                        JRCellContents totalHeader = group.getTotalHeader();
1207:                        if (totalHeader.getWidth() != 0
1208:                                && totalHeader.getHeight() != 0) {
1209:                            boolean firstOnRow = x == 0
1210:                                    && crosstab.getHeaderCell() == null;
1211:                            crosstabElements
1212:                                    .add(getCrosstabCellFrame(
1213:                                            totalHeader,
1214:                                            crosstabX + rowHeadersXOffset + x,
1215:                                            crosstabY + y,
1216:                                            firstOnRow
1217:                                                    && crosstab
1218:                                                            .getRunDirection() == JRCrosstab.RUN_DIRECTION_LTR,
1219:                                            firstOnRow
1220:                                                    && crosstab
1221:                                                            .getRunDirection() == JRCrosstab.RUN_DIRECTION_RTL,
1222:                                            false));
1223:
1224:                            x += totalHeader.getWidth();
1225:                        }
1226:                    }
1227:
1228:                    JRCellContents header = group.getHeader();
1229:                    if (header.getWidth() != 0 && header.getHeight() != 0) {
1230:                        boolean firstOnRow = x == 0
1231:                                && crosstab.getHeaderCell() == null;
1232:                        crosstabElements
1233:                                .add(getCrosstabCellFrame(
1234:                                        header,
1235:                                        crosstabX + rowHeadersXOffset + x,
1236:                                        crosstabY + y,
1237:                                        firstOnRow
1238:                                                && crosstab.getRunDirection() == JRCrosstab.RUN_DIRECTION_LTR,
1239:                                        firstOnRow
1240:                                                && crosstab.getRunDirection() == JRCrosstab.RUN_DIRECTION_RTL,
1241:                                        false));
1242:                    }
1243:
1244:                    if (group.getTotalPosition() == BucketDefinition.TOTAL_POSITION_END) {
1245:                        JRCellContents totalHeader = group.getTotalHeader();
1246:                        if (totalHeader.getWidth() != 0
1247:                                && totalHeader.getHeight() != 0)
1248:                            crosstabElements.add(getCrosstabCellFrame(
1249:                                    totalHeader, crosstabX + rowHeadersXOffset
1250:                                            + x + header.getWidth(), crosstabY
1251:                                            + y, false, false, false));
1252:
1253:                    }
1254:
1255:                    y += group.getHeight();
1256:                }
1257:            }
1258:
1259:            /**
1260:             * 
1261:             * @param crosstab
1262:             * @param rowHeadersXOffset
1263:             * @param colHeadersYOffset
1264:             * @param crosstabElements
1265:             */
1266:            protected void addCrosstabRows(JRCrosstab crosstab,
1267:                    int rowHeadersXOffset, int colHeadersYOffset,
1268:                    List crosstabElements) {
1269:                JRCrosstabRowGroup[] groups = crosstab.getRowGroups();
1270:                int crosstabX = crosstab.getX();
1271:                int crosstabY = crosstab.getY();
1272:
1273:                for (int i = 0, x = 0, y = 0; i < groups.length; i++) {
1274:                    JRCrosstabRowGroup group = groups[i];
1275:
1276:                    if (group.getTotalPosition() == BucketDefinition.TOTAL_POSITION_START) {
1277:                        JRCellContents totalHeader = group.getTotalHeader();
1278:                        if (totalHeader.getWidth() != 0
1279:                                && totalHeader.getHeight() != 0) {
1280:                            crosstabElements
1281:                                    .add(getCrosstabCellFrame(
1282:                                            totalHeader,
1283:                                            crosstabX + x,
1284:                                            crosstabY + colHeadersYOffset + y,
1285:                                            false,
1286:                                            false,
1287:                                            y == 0
1288:                                                    && crosstab.getHeaderCell() == null));
1289:
1290:                            addCrosstabDataCellsRow(crosstab,
1291:                                    rowHeadersXOffset, colHeadersYOffset + y,
1292:                                    i, crosstabElements);
1293:                            y += totalHeader.getHeight();
1294:                        }
1295:                    }
1296:
1297:                    JRCellContents header = group.getHeader();
1298:                    if (header.getWidth() != 0 && header.getHeight() != 0)
1299:                        crosstabElements.add(getCrosstabCellFrame(header,
1300:                                crosstabX + x, crosstabY + colHeadersYOffset
1301:                                        + y, false, false, y == 0
1302:                                        && crosstab.getHeaderCell() == null));
1303:
1304:                    if (i == groups.length - 1) {
1305:                        addCrosstabDataCellsRow(crosstab, rowHeadersXOffset,
1306:                                colHeadersYOffset + y, groups.length,
1307:                                crosstabElements);
1308:                    }
1309:
1310:                    if (group.getTotalPosition() == BucketDefinition.TOTAL_POSITION_END) {
1311:                        JRCellContents totalHeader = group.getTotalHeader();
1312:                        if (totalHeader.getWidth() != 0
1313:                                && totalHeader.getHeight() != 0) {
1314:                            crosstabElements.add(getCrosstabCellFrame(
1315:                                    totalHeader, crosstabX + x, crosstabY
1316:                                            + colHeadersYOffset + y
1317:                                            + header.getHeight(), false, false,
1318:                                    false));
1319:
1320:                            addCrosstabDataCellsRow(crosstab,
1321:                                    rowHeadersXOffset, colHeadersYOffset + y
1322:                                            + header.getHeight(), i,
1323:                                    crosstabElements);
1324:                        }
1325:                    }
1326:
1327:                    x += group.getWidth();
1328:                }
1329:            }
1330:
1331:            /**
1332:             * 
1333:             * @param crosstab
1334:             * @param rowOffsetX
1335:             * @param rowOffsetY
1336:             * @param rowIndex
1337:             * @param crosstabElements
1338:             */
1339:            protected void addCrosstabDataCellsRow(JRCrosstab crosstab,
1340:                    int rowOffsetX, int rowOffsetY, int rowIndex,
1341:                    List crosstabElements) {
1342:
1343:                JRCrosstabColumnGroup[] colGroups = crosstab.getColumnGroups();
1344:                JRCrosstabCell[][] cells = crosstab.getCells();
1345:                int crosstabX = crosstab.getX() + rowOffsetX;
1346:                int crosstabY = crosstab.getY() + rowOffsetY;
1347:
1348:                for (int i = 0, x = 0; i < colGroups.length; i++) {
1349:                    JRCrosstabColumnGroup group = colGroups[i];
1350:
1351:                    if (group.getTotalPosition() == BucketDefinition.TOTAL_POSITION_START) {
1352:                        JRCellContents cell = cells[rowIndex][i].getContents();
1353:                        if (cell.getWidth() != 0 && cell.getHeight() != 0) {
1354:                            crosstabElements.add(getCrosstabCellFrame(cell,
1355:                                    crosstabX + x, crosstabY, false, false,
1356:                                    false));
1357:                            x += cells[rowIndex][i].getContents().getWidth();
1358:                        }
1359:                    }
1360:
1361:                    if (i == colGroups.length - 1) {
1362:                        JRCellContents cell = cells[rowIndex][colGroups.length]
1363:                                .getContents();
1364:                        if (cell.getWidth() != 0 && cell.getHeight() != 0)
1365:                            crosstabElements.add(getCrosstabCellFrame(cell,
1366:                                    crosstabX + x, crosstabY, false, false,
1367:                                    false));
1368:                    }
1369:
1370:                    if (group.getTotalPosition() == BucketDefinition.TOTAL_POSITION_END) {
1371:                        JRCellContents cell = cells[rowIndex][i].getContents();
1372:                        if (cell.getWidth() != 0 && cell.getHeight() != 0)
1373:                            crosstabElements.add(getCrosstabCellFrame(cell,
1374:                                    crosstabX + x
1375:                                            + group.getHeader().getWidth(),
1376:                                    crosstabY, false, false, false));
1377:                    }
1378:                }
1379:            }
1380:
1381:            /**
1382:             * 
1383:             */
1384:            public JasperPrint getJasperPrint() {
1385:                return this .jasperPrint;
1386:            }
1387:
1388:            /**
1389:             * 
1390:             * @author Sanda Zaharia (shertage@users.sourceforge.net)
1391:             *
1392:             */
1393:            protected class ContourElement {
1394:                private int contourX;
1395:                private int contourY;
1396:                private int contourWidth;
1397:                private int contourHeight;
1398:                private byte contourPen;
1399:                private Color contourColor;
1400:
1401:                public ContourElement() {
1402:
1403:                }
1404:
1405:                public ContourElement(int x, int y, int width, int height,
1406:                        Color color, byte pen) {
1407:                    this .contourX = x;
1408:                    this .contourY = y;
1409:                    this .contourWidth = width;
1410:                    this .contourHeight = height;
1411:                    this .contourColor = color;
1412:                    this .contourPen = pen;
1413:                }
1414:
1415:                /**
1416:                 * @return the contourColor
1417:                 */
1418:                public Color getContourColor() {
1419:                    return contourColor;
1420:                }
1421:
1422:                /**
1423:                 * @param contourColor the contourColor to set
1424:                 */
1425:                public void setContourColor(Color contourColor) {
1426:                    this .contourColor = contourColor;
1427:                }
1428:
1429:                /**
1430:                 * @return the contourHeight
1431:                 */
1432:                public int getContourHeight() {
1433:                    return contourHeight;
1434:                }
1435:
1436:                /**
1437:                 * @param contourHeight the contourHeight to set
1438:                 */
1439:                public void setContourHeight(int contourHeight) {
1440:                    this .contourHeight = contourHeight;
1441:                }
1442:
1443:                /**
1444:                 * @return the contourPen
1445:                 */
1446:                public byte getContourPen() {
1447:                    return contourPen;
1448:                }
1449:
1450:                /**
1451:                 * @param contourPen the contourPen to set
1452:                 */
1453:                public void setContourPen(byte contourPen) {
1454:                    this .contourPen = contourPen;
1455:                }
1456:
1457:                /**
1458:                 * @return the contourWidth
1459:                 */
1460:                public int getContourWidth() {
1461:                    return contourWidth;
1462:                }
1463:
1464:                /**
1465:                 * @param contourWidth the contourWidth to set
1466:                 */
1467:                public void setContourWidth(int contourWidth) {
1468:                    this .contourWidth = contourWidth;
1469:                }
1470:
1471:                /**
1472:                 * @return the contourX
1473:                 */
1474:                public int getContourX() {
1475:                    return contourX;
1476:                }
1477:
1478:                /**
1479:                 * @param contourX the contourX to set
1480:                 */
1481:                public void setContourX(int contourX) {
1482:                    this .contourX = contourX;
1483:                }
1484:
1485:                /**
1486:                 * @return the contourY
1487:                 */
1488:                public int getContourY() {
1489:                    return contourY;
1490:                }
1491:
1492:                /**
1493:                 * @param contourY the contourY to set
1494:                 */
1495:                public void setContourY(int contourY) {
1496:                    this .contourY = contourY;
1497:                }
1498:            }
1499:
1500:            protected class StyleFactory extends JRBaseObjectFactory {
1501:                public StyleFactory() {
1502:                    super (defaultStyleProvider);
1503:                }
1504:
1505:                public JRExpression getExpression(JRExpression expression,
1506:                        boolean assignNotUsedId) {
1507:                    return expression;
1508:                }
1509:
1510:                protected void handleStyleNameReference(JRStyleSetter setter,
1511:                        String nameReference) {
1512:                    JRStyle style = (JRStyle) stylesMap.get(nameReference);
1513:                    if (style == null) {
1514:                        log.warn("Style " + nameReference
1515:                                + " could not be resolved.");
1516:                    } else {
1517:                        setter.setStyle(style);
1518:                    }
1519:                }
1520:            }
1521:
1522:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.