KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > jasperreports > engine > export > JRGraphics2DExporter


1 /*
2  * ============================================================================
3  * GNU Lesser General Public License
4  * ============================================================================
5  *
6  * JasperReports - Free Java report-generating library.
7  * Copyright (C) 2001-2006 JasperSoft Corporation http://www.jaspersoft.com
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
22  *
23  * JasperSoft Corporation
24  * 303 Second Street, Suite 450 North
25  * San Francisco, CA 94107
26  * http://www.jaspersoft.com
27  */

28
29 /*
30  * Contributors:
31  * Eugene D - eugenedruy@users.sourceforge.net
32  * Adrian Jackson - iapetus@users.sourceforge.net
33  * David Taylor - exodussystems@users.sourceforge.net
34  * Lars Kristensen - llk@users.sourceforge.net
35  */

36 package net.sf.jasperreports.engine.export;
37
38 import java.awt.BasicStroke JavaDoc;
39 import java.awt.Color JavaDoc;
40 import java.awt.Graphics2D JavaDoc;
41 import java.awt.Rectangle JavaDoc;
42 import java.awt.RenderingHints JavaDoc;
43 import java.awt.Shape JavaDoc;
44 import java.awt.Stroke JavaDoc;
45 import java.awt.geom.AffineTransform JavaDoc;
46 import java.awt.geom.Dimension2D JavaDoc;
47 import java.util.Collection JavaDoc;
48 import java.util.Iterator JavaDoc;
49
50 import net.sf.jasperreports.engine.JRAbstractExporter;
51 import net.sf.jasperreports.engine.JRAlignment;
52 import net.sf.jasperreports.engine.JRBox;
53 import net.sf.jasperreports.engine.JRElement;
54 import net.sf.jasperreports.engine.JRException;
55 import net.sf.jasperreports.engine.JRExporterParameter;
56 import net.sf.jasperreports.engine.JRGraphicElement;
57 import net.sf.jasperreports.engine.JRImage;
58 import net.sf.jasperreports.engine.JRLine;
59 import net.sf.jasperreports.engine.JRPrintElement;
60 import net.sf.jasperreports.engine.JRPrintEllipse;
61 import net.sf.jasperreports.engine.JRPrintFrame;
62 import net.sf.jasperreports.engine.JRPrintImage;
63 import net.sf.jasperreports.engine.JRPrintLine;
64 import net.sf.jasperreports.engine.JRPrintPage;
65 import net.sf.jasperreports.engine.JRPrintRectangle;
66 import net.sf.jasperreports.engine.JRPrintText;
67 import net.sf.jasperreports.engine.JRRenderable;
68 import net.sf.jasperreports.engine.JRTextElement;
69 import net.sf.jasperreports.engine.util.JRGraphEnvInitializer;
70 import net.sf.jasperreports.engine.util.JRProperties;
71 import net.sf.jasperreports.engine.util.JRStyledText;
72
73
74 /**
75  * Exports a JasperReports document to a <tt>Graphics2D</tt> object. Since all font measurement and layout
76  * calculation during report filling is done using AWT, this is considered the perfect exporter, unlike the others,
77  * which are only approximations of the initial document.
78  * <p>
79  * As its name indicates, this exporter is special because it does not produce files or does not send character
80  * or binary data to an output stream.
81  * @author Teodor Danciu (teodord@users.sourceforge.net)
82  * @version $Id: JRGraphics2DExporter.java 1469 2006-11-08 16:37:14 +0200 (Wed, 08 Nov 2006) teodord $
83  */

84 public class JRGraphics2DExporter extends JRAbstractExporter
85 {
86
87     private static final int ELEMENT_RECTANGLE_PADDING = 3;
88     private static final float DEFAULT_ZOOM = 1f;
89
90     /**
91      * Property that provides a default value for the
92      * {@link net.sf.jasperreports.engine.export.JRGraphics2DExporterParameter#MINIMIZE_PRINTER_JOB_SIZE JRGraphics2DExporterParameter.MINIMIZE_PRINTER_JOB_SIZE}
93      * Graphics2D exporter parameter.
94      *
95      * @see net.sf.jasperreports.engine.export.JRGraphics2DExporterParameter#MINIMIZE_PRINTER_JOB_SIZE
96      */

97     public static final String JavaDoc MINIMIZE_PRINTER_JOB_SIZE = JRProperties.PROPERTY_PREFIX + "export.graphics2d.min.job.size";
98
99     /**
100      *
101      */

102     protected Graphics2D JavaDoc grx = null;
103     protected JRExportProgressMonitor progressMonitor = null;
104     protected float zoom = DEFAULT_ZOOM;
105
106     protected TextRenderer textRenderer = null;
107
108     /**
109      *
110      */

111     public JRGraphics2DExporter() throws JRException
112     {
113         JRGraphEnvInitializer.initializeGraphEnv();
114     }
115     
116
117     /**
118      *
119      */

120     public void exportReport() throws JRException
121     {
122         progressMonitor = (JRExportProgressMonitor)parameters.get(JRExporterParameter.PROGRESS_MONITOR);
123         
124         /* */
125         setOffset();
126
127         try
128         {
129             /* */
130             setExportContext();
131     
132             /* */
133             setInput();
134     
135             /* */
136             setPageRange();
137     
138             /* */
139             setTextRenderer();
140             
141             grx = (Graphics2D JavaDoc)parameters.get(JRGraphics2DExporterParameter.GRAPHICS_2D);
142             if (grx == null)
143             {
144                 throw new JRException("No output specified for the exporter. java.awt.Graphics2D object expected.");
145             }
146             
147             Float JavaDoc zoomRatio = (Float JavaDoc)parameters.get(JRGraphics2DExporterParameter.ZOOM_RATIO);
148             if (zoomRatio != null)
149             {
150                 zoom = zoomRatio.floatValue();
151                 if (zoom <= 0)
152                 {
153                     throw new JRException("Invalid zoom ratio : " + zoom);
154                 }
155             }
156             else
157             {
158                 zoom = DEFAULT_ZOOM;
159             }
160     
161             exportReportToGraphics2D();
162         }
163         finally
164         {
165             resetExportContext();
166         }
167     }
168         
169
170     protected void setTextRenderer()
171     {
172         boolean isMinimizePrinterJobSize = true;
173         Boolean JavaDoc isMinimizePrinterJobSizeParam = (Boolean JavaDoc) parameters.get(JRGraphics2DExporterParameter.MINIMIZE_PRINTER_JOB_SIZE);
174         if (isMinimizePrinterJobSizeParam == null)
175         {
176             isMinimizePrinterJobSize = JRProperties.getBooleanProperty(MINIMIZE_PRINTER_JOB_SIZE);
177         }
178         else
179         {
180             isMinimizePrinterJobSize = isMinimizePrinterJobSizeParam.booleanValue();
181         }
182         
183         textRenderer = new TextRenderer(isMinimizePrinterJobSize);
184     }
185
186     
187     /**
188      *
189      */

190     public void exportReportToGraphics2D() throws JRException
191     {
192         grx.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
193         //grx.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF);
194
grx.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
195         grx.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
196
197         AffineTransform JavaDoc atrans = new AffineTransform JavaDoc();
198         atrans.translate(globalOffsetX, globalOffsetY);
199         atrans.scale(zoom, zoom);
200         grx.transform(atrans);
201
202         java.util.List JavaDoc pages = jasperPrint.getPages();
203         if (pages != null)
204         {
205             Shape JavaDoc oldClipShape = grx.getClip();
206     
207             grx.clip(new Rectangle JavaDoc(0, 0, jasperPrint.getPageWidth(), jasperPrint.getPageHeight()));
208     
209             try
210             {
211                 JRPrintPage page = (JRPrintPage)pages.get(startPageIndex);
212                 exportPage(page);
213             }
214             finally
215             {
216                 grx.setClip(oldClipShape);
217             }
218         }
219     }
220     
221
222     /**
223      *
224      */

225     protected void exportPage(JRPrintPage page) throws JRException
226     {
227         grx.setColor(Color.white);
228         grx.fillRect(
229             0,
230             0,
231             jasperPrint.getPageWidth(),
232             jasperPrint.getPageHeight()
233             );
234
235         grx.setColor(Color.black);
236         grx.setStroke(new BasicStroke JavaDoc(1));
237
238         /* */
239         Collection JavaDoc elements = page.getElements();
240         exportElements(elements);
241         
242         if (progressMonitor != null)
243         {
244             progressMonitor.afterPageExport();
245         }
246     }
247
248
249     protected void exportElements(Collection JavaDoc elements) throws JRException
250     {
251         if (elements != null && elements.size() > 0)
252         {
253             Shape JavaDoc clipArea = grx.getClip();
254             for(Iterator JavaDoc it = elements.iterator(); it.hasNext();)
255             {
256                 JRPrintElement element = (JRPrintElement)it.next();
257                 
258                 if (!clipArea.intersects(
259                         element.getX() + getOffsetX() - ELEMENT_RECTANGLE_PADDING,
260                         element.getY() + getOffsetY() - ELEMENT_RECTANGLE_PADDING,
261                         element.getWidth() + 2 * ELEMENT_RECTANGLE_PADDING,
262                         element.getHeight() + 2 * ELEMENT_RECTANGLE_PADDING))
263                 {
264                     continue;
265                 }
266                 
267                 if (element instanceof JRPrintLine)
268                 {
269                     exportLine((JRPrintLine)element);
270                 }
271                 else if (element instanceof JRPrintRectangle)
272                 {
273                     exportRectangle((JRPrintRectangle)element);
274                 }
275                 else if (element instanceof JRPrintEllipse)
276                 {
277                     exportEllipse((JRPrintEllipse)element);
278                 }
279                 else if (element instanceof JRPrintImage)
280                 {
281                     exportImage((JRPrintImage)element);
282                 }
283                 else if (element instanceof JRPrintText)
284                 {
285                     exportText((JRPrintText)element);
286                 }
287                 else if (element instanceof JRPrintFrame)
288                 {
289                     exportFrame((JRPrintFrame) element);
290                 }
291             }
292         }
293     }
294
295
296     /**
297      *
298      */

299     protected void exportLine(JRPrintLine line)
300     {
301         grx.setColor(line.getForecolor());
302         
303         Stroke JavaDoc stroke = getStroke(line.getPen());
304
305         if (stroke != null)
306         {
307             grx.setStroke(stroke);
308             
309             grx.translate(.5, .5);
310             
311             if (line.getDirection() == JRLine.DIRECTION_TOP_DOWN)
312             {
313                 grx.drawLine(
314                     line.getX() + getOffsetX(),
315                     line.getY() + getOffsetY(),
316                     line.getX() + getOffsetX() + line.getWidth() - 1,
317                     line.getY() + getOffsetY() + line.getHeight() - 1
318                     );
319             }
320             else
321             {
322                 grx.drawLine(
323                     line.getX() + getOffsetX(),
324                     line.getY() + getOffsetY() + line.getHeight() - 1,
325                     line.getX() + getOffsetX() + line.getWidth() - 1,
326                     line.getY() + getOffsetY()
327                     );
328             }
329             
330             grx.translate(-.5, -.5);
331         }
332     }
333
334
335     /**
336      *
337      */

338     protected void exportRectangle(JRPrintRectangle rectangle)
339     {
340         if (rectangle.getMode() == JRElement.MODE_OPAQUE)
341         {
342             grx.setColor(rectangle.getBackcolor());
343             if (rectangle.getRadius() > 0)
344             {
345                 grx.fillRoundRect(
346                         rectangle.getX() + getOffsetX(),
347                         rectangle.getY() + getOffsetY(),
348                         rectangle.getWidth(),
349                         rectangle.getHeight(),
350                         2 * rectangle.getRadius(),
351                         2 * rectangle.getRadius()
352                         );
353             }
354             else
355             {
356                 grx.fillRect(
357                         rectangle.getX() + getOffsetX(),
358                         rectangle.getY() + getOffsetY(),
359                         rectangle.getWidth(),
360                         rectangle.getHeight()
361                         );
362             }
363         }
364
365         grx.setColor(rectangle.getForecolor());
366
367         byte pen = rectangle.getPen();
368         Stroke JavaDoc stroke = getStroke(pen);
369
370         if (stroke != null)
371         {
372             double cornerOffset = getBorderCornerOffset(pen);
373             int sizeAdjust = getRectangleSizeAdjust(pen);
374             
375             AffineTransform JavaDoc transform = grx.getTransform();
376             
377             grx.translate(rectangle.getX() + getOffsetX() + cornerOffset, rectangle.getY() + getOffsetY() + cornerOffset);
378             if (pen == JRGraphicElement.PEN_THIN)
379             {
380                 grx.scale((rectangle.getWidth() - .5) / rectangle.getWidth(), (rectangle.getHeight() - .5) / rectangle.getHeight());
381             }
382             
383             grx.setStroke(stroke);
384             
385             if (rectangle.getRadius() > 0)
386             {
387                 grx.drawRoundRect(
388                         0,
389                         0,
390                         rectangle.getWidth() - sizeAdjust,
391                         rectangle.getHeight() - sizeAdjust,
392                         2 * rectangle.getRadius(),
393                         2 * rectangle.getRadius()
394                         );
395             }
396             else
397             {
398                 grx.drawRect(
399                         0,
400                         0,
401                         rectangle.getWidth() - sizeAdjust,
402                         rectangle.getHeight() - sizeAdjust
403                         );
404             }
405
406             grx.setTransform(transform);
407         }
408     }
409
410     
411     private static int getRectangleSizeAdjust(byte pen)
412     {
413         switch (pen)
414         {
415             case JRGraphicElement.PEN_1_POINT:
416             case JRGraphicElement.PEN_DOTTED:
417                 return 1;
418             default:
419                 return 0;
420         }
421     }
422     
423     
424     /**
425      *
426      */

427     protected void exportEllipse(JRPrintEllipse ellipse)
428     {
429         if (ellipse.getMode() == JRElement.MODE_OPAQUE)
430         {
431             grx.setColor(ellipse.getBackcolor());
432             grx.fillOval(
433                 ellipse.getX() + getOffsetX(),
434                 ellipse.getY() + getOffsetY(),
435                 ellipse.getWidth(),
436                 ellipse.getHeight()
437                 );
438         }
439
440         grx.setColor(ellipse.getForecolor());
441
442         byte pen = ellipse.getPen();
443         Stroke JavaDoc stroke = getStroke(pen);
444
445         if (stroke != null)
446         {
447             double cornerOffset = getBorderCornerOffset(pen);
448             int sizeAdjust = getRectangleSizeAdjust(pen);
449             
450             AffineTransform JavaDoc transform = grx.getTransform();
451             
452             grx.translate(ellipse.getX() + getOffsetX() + cornerOffset, ellipse.getY() + getOffsetY() + cornerOffset);
453             if (pen == JRGraphicElement.PEN_THIN)
454             {
455                 grx.scale((ellipse.getWidth() - .5) / ellipse.getWidth(), (ellipse.getHeight() - .5) / ellipse.getHeight());
456             }
457             
458             grx.setStroke(stroke);
459             
460             grx.drawOval(
461                 0,
462                 0,
463                 ellipse.getWidth() - sizeAdjust,
464                 ellipse.getHeight() - sizeAdjust
465                 );
466             
467             grx.setTransform(transform);
468         }
469     }
470
471
472     /**
473      *
474      */

475     protected void exportImage(JRPrintImage printImage) throws JRException
476     {
477         if (printImage.getMode() == JRElement.MODE_OPAQUE)
478         {
479             grx.setColor(printImage.getBackcolor());
480
481             grx.fillRect(
482                 printImage.getX() + getOffsetX(),
483                 printImage.getY() + getOffsetY(),
484                 printImage.getWidth(),
485                 printImage.getHeight()
486                 );
487         }
488
489         int topPadding = printImage.getTopPadding();
490         int leftPadding = printImage.getLeftPadding();
491         int bottomPadding = printImage.getBottomPadding();
492         int rightPadding = printImage.getRightPadding();
493         
494         int availableImageWidth = printImage.getWidth() - leftPadding - rightPadding;
495         availableImageWidth = (availableImageWidth < 0)?0:availableImageWidth;
496
497         int availableImageHeight = printImage.getHeight() - topPadding - bottomPadding;
498         availableImageHeight = (availableImageHeight < 0)?0:availableImageHeight;
499         
500         JRRenderable renderer = printImage.getRenderer();
501         
502         if (
503             availableImageWidth > 0
504             && availableImageHeight > 0
505             && renderer != null
506             )
507         {
508             int normalWidth = availableImageWidth;
509             int normalHeight = availableImageHeight;
510
511             Dimension2D JavaDoc dimension = renderer.getDimension();
512             if (dimension != null)
513             {
514                 normalWidth = (int)dimension.getWidth();
515                 normalHeight = (int)dimension.getHeight();
516             }
517     
518             float xalignFactor = 0f;
519             switch (printImage.getHorizontalAlignment())
520             {
521                 case JRAlignment.HORIZONTAL_ALIGN_RIGHT :
522                 {
523                     xalignFactor = 1f;
524                     break;
525                 }
526                 case JRAlignment.HORIZONTAL_ALIGN_CENTER :
527                 {
528                     xalignFactor = 0.5f;
529                     break;
530                 }
531                 case JRAlignment.HORIZONTAL_ALIGN_LEFT :
532                 default :
533                 {
534                     xalignFactor = 0f;
535                     break;
536                 }
537             }
538
539             float yalignFactor = 0f;
540             switch (printImage.getVerticalAlignment())
541             {
542                 case JRAlignment.VERTICAL_ALIGN_BOTTOM :
543                 {
544                     yalignFactor = 1f;
545                     break;
546                 }
547                 case JRAlignment.VERTICAL_ALIGN_MIDDLE :
548                 {
549                     yalignFactor = 0.5f;
550                     break;
551                 }
552                 case JRAlignment.VERTICAL_ALIGN_TOP :
553                 default :
554                 {
555                     yalignFactor = 0f;
556                     break;
557                 }
558             }
559
560             switch (printImage.getScaleImage())// FIXME maybe put this in JRFiller
561
{
562                 case JRImage.SCALE_IMAGE_CLIP :
563                 {
564                     int xoffset = (int)(xalignFactor * (availableImageWidth - normalWidth));
565                     int yoffset = (int)(yalignFactor * (availableImageHeight - normalHeight));
566
567                     Shape JavaDoc oldClipShape = grx.getClip();
568
569                     grx.clip(
570                         new Rectangle JavaDoc(
571                             printImage.getX() + leftPadding + getOffsetX(),
572                             printImage.getY() + topPadding + getOffsetY(),
573                             availableImageWidth,
574                             availableImageHeight
575                             )
576                         );
577                     
578                     try
579                     {
580                         renderer.render(
581                             grx,
582                             new Rectangle JavaDoc(
583                                 printImage.getX() + leftPadding + getOffsetX() + xoffset,
584                                 printImage.getY() + topPadding + getOffsetY() + yoffset,
585                                 normalWidth,
586                                 normalHeight
587                                 )
588                             );
589                     }
590                     finally
591                     {
592                         grx.setClip(oldClipShape);
593                     }
594     
595                     break;
596                 }
597                 case JRImage.SCALE_IMAGE_FILL_FRAME :
598                 {
599                     renderer.render(
600                         grx,
601                         new Rectangle JavaDoc(
602                             printImage.getX() + leftPadding + getOffsetX(),
603                             printImage.getY() + topPadding + getOffsetY(),
604                             availableImageWidth,
605                             availableImageHeight
606                             )
607                         );
608     
609                     break;
610                 }
611                 case JRImage.SCALE_IMAGE_RETAIN_SHAPE :
612                 default :
613                 {
614                     if (printImage.getHeight() > 0)
615                     {
616                         double ratio = (double)normalWidth / (double)normalHeight;
617                         
618                         if( ratio > (double)availableImageWidth / (double)availableImageHeight )
619                         {
620                             normalWidth = availableImageWidth;
621                             normalHeight = (int)(availableImageWidth / ratio);
622                         }
623                         else
624                         {
625                             normalWidth = (int)(availableImageHeight * ratio);
626                             normalHeight = availableImageHeight;
627                         }
628
629                         int xoffset = (int)(xalignFactor * (availableImageWidth - normalWidth));
630                         int yoffset = (int)(yalignFactor * (availableImageHeight - normalHeight));
631
632                         renderer.render(
633                             grx,
634                             new Rectangle JavaDoc(
635                                 printImage.getX() + leftPadding + getOffsetX() + xoffset,
636                                 printImage.getY() + topPadding + getOffsetY() + yoffset,
637                                 normalWidth,
638                                 normalHeight
639                                 )
640                             );
641                     }
642                     
643                     break;
644                 }
645             }
646         }
647
648         if (
649             printImage.getTopBorder() == JRGraphicElement.PEN_NONE &&
650             printImage.getLeftBorder() == JRGraphicElement.PEN_NONE &&
651             printImage.getBottomBorder() == JRGraphicElement.PEN_NONE &&
652             printImage.getRightBorder() == JRGraphicElement.PEN_NONE
653             )
654         {
655             if (printImage.getPen() != JRGraphicElement.PEN_NONE)
656             {
657                 exportBox(getBox(printImage), printImage);
658             }
659         }
660         else
661         {
662             exportBox(printImage, printImage);
663         }
664     }
665     
666
667     /**
668      *
669      */

670     protected void exportText(JRPrintText text)
671     {
672         JRStyledText styledText = getStyledText(text, false);
673         
674         if (styledText == null)
675         {
676             return;
677         }
678
679         String JavaDoc allText = styledText.getText();
680         
681         int x = text.getX() + getOffsetX();
682         int y = text.getY() + getOffsetY();
683         int width = text.getWidth();
684         int height = text.getHeight();
685         int topPadding = text.getTopPadding();
686         int leftPadding = text.getLeftPadding();
687         int bottomPadding = text.getBottomPadding();
688         int rightPadding = text.getRightPadding();
689         
690         double angle = 0;
691         
692         switch (text.getRotation())
693         {
694             case JRTextElement.ROTATION_LEFT :
695             {
696                 y = text.getY() + getOffsetY() + text.getHeight();
697                 width = text.getHeight();
698                 height = text.getWidth();
699                 int tmpPadding = topPadding;
700                 topPadding = leftPadding;
701                 leftPadding = bottomPadding;
702                 bottomPadding = rightPadding;
703                 rightPadding = tmpPadding;
704                 angle = - Math.PI / 2;
705                 break;
706             }
707             case JRTextElement.ROTATION_RIGHT :
708             {
709                 x = text.getX() + getOffsetX() + text.getWidth();
710                 width = text.getHeight();
711                 height = text.getWidth();
712                 int tmpPadding = topPadding;
713                 topPadding = rightPadding;
714                 rightPadding = bottomPadding;
715                 bottomPadding = leftPadding;
716                 leftPadding = tmpPadding;
717                 angle = Math.PI / 2;
718                 break;
719             }
720             case JRTextElement.ROTATION_UPSIDE_DOWN :
721             {
722                 int tmpPadding = topPadding;
723                 x = text.getX() + getOffsetX() + text.getWidth();
724                 y = text.getY() + getOffsetY() + text.getHeight();
725                 topPadding = bottomPadding;
726                 bottomPadding = tmpPadding;
727                 tmpPadding = leftPadding;
728                 leftPadding = rightPadding;
729                 rightPadding = tmpPadding;
730                 angle = Math.PI;
731                 break;
732             }
733             case JRTextElement.ROTATION_NONE :
734             default :
735             {
736             }
737         }
738         
739         grx.rotate(angle, x, y);
740
741         if (text.getMode() == JRElement.MODE_OPAQUE)
742         {
743             grx.setColor(text.getBackcolor());
744             grx.fillRect(x, y, width, height);
745         }
746         else
747         {
748             /*
749             grx.setColor(text.getForecolor());
750             grx.setStroke(new BasicStroke(1));
751             grx.drawRect(x, y, width, height);
752             */

753         }
754
755         if (allText.length() > 0)
756         {
757             grx.setColor(text.getForecolor());
758
759             /* */
760             textRenderer.render(
761                 grx,
762                 x,
763                 y,
764                 width,
765                 height,
766                 topPadding,
767                 leftPadding,
768                 bottomPadding,
769                 rightPadding,
770                 text.getTextHeight(),
771                 text.getHorizontalAlignment(),
772                 text.getVerticalAlignment(),
773                 text.getLineSpacingFactor(),
774                 text.getLeadingOffset(),
775                 text.getFontSize(),
776                 text.isStyledText(),
777                 styledText,
778                 allText
779                 );
780             
781         }
782         
783         grx.rotate(-angle, x, y);
784
785         /* */
786         exportBox(
787             text,
788             text
789             );
790     }
791
792
793     /**
794      *
795      */

796     protected void exportBox(JRBox box, JRPrintElement element)
797     {
798         Stroke JavaDoc topStroke = null;
799         Stroke JavaDoc leftStroke = null;
800         Stroke JavaDoc bottomStroke = null;
801         Stroke JavaDoc rightStroke = null;
802         
803         if (box != null)
804         {
805             topStroke = getBorderStroke(box.getTopBorder());
806             leftStroke = getBorderStroke(box.getLeftBorder());
807             bottomStroke = getBorderStroke(box.getBottomBorder());
808             rightStroke = getBorderStroke(box.getRightBorder());
809         }
810
811         if (topStroke != null)
812         {
813             double cornerOffset = getBorderCornerOffset(box.getTopBorder());
814             
815             grx.setStroke(topStroke);
816             grx.setColor(box.getTopBorderColor() == null ? element.getForecolor() : box.getTopBorderColor());
817     
818             grx.translate(0, cornerOffset);
819             grx.drawLine(
820                 element.getX() + getOffsetX(),
821                 element.getY() + getOffsetY(),
822                 element.getX() + getOffsetX() + element.getWidth(),
823                 element.getY() + getOffsetY()
824                 );
825             grx.translate(0, -cornerOffset);
826         }
827
828         if (leftStroke != null)
829         {
830             double cornerOffset = getBorderCornerOffset(box.getLeftBorder());
831             
832             grx.setStroke(leftStroke);
833             grx.setColor(box.getLeftBorderColor() == null ? element.getForecolor() : box.getLeftBorderColor());
834     
835             grx.translate(cornerOffset, 0);
836             grx.drawLine(
837                 element.getX() + getOffsetX(),
838                 element.getY() + getOffsetY(),
839                 element.getX() + getOffsetX(),
840                 element.getY() + getOffsetY() + element.getHeight()
841                 );
842             grx.translate(-cornerOffset, 0);
843         }
844
845         if (bottomStroke != null)
846         {
847             double cornerOffset = getBorderCornerOffset(box.getBottomBorder());
848             
849             grx.setStroke(bottomStroke);
850             grx.setColor(box.getBottomBorderColor() == null ? element.getForecolor() : box.getBottomBorderColor());
851     
852             grx.translate(0, -cornerOffset);
853             grx.drawLine(
854                 element.getX() + getOffsetX(),
855                 element.getY() + getOffsetY() + element.getHeight(),
856                 element.getX() + getOffsetX() + element.getWidth(),
857                 element.getY() + getOffsetY() + element.getHeight()
858                 );
859             grx.translate(0, cornerOffset);
860         }
861
862         if (rightStroke != null)
863         {
864             double cornerOffset = getBorderCornerOffset(box.getRightBorder());
865             
866             grx.setStroke(rightStroke);
867             grx.setColor(box.getRightBorderColor() == null ? element.getForecolor() : box.getRightBorderColor());
868     
869             grx.translate(-cornerOffset, 0);
870             grx.drawLine(
871                 element.getX() + getOffsetX() + element.getWidth(),
872                 element.getY() + getOffsetY(),
873                 element.getX() + getOffsetX() + element.getWidth(),
874                 element.getY() + getOffsetY() + element.getHeight()
875                 );
876             grx.translate(cornerOffset, 0);
877         }
878     }
879
880     /**
881      *
882      */

883     private static final double THIN_CORNER_OFFSET = 0.25d;
884     private static final double ONE_POINT_CORNER_OFFSET = 0.5d;
885     
886     private static final Stroke JavaDoc STROKE_THIN = new BasicStroke JavaDoc(0.5f);
887     private static final Stroke JavaDoc STROKE_1_POINT = new BasicStroke JavaDoc(1f);
888     private static final Stroke JavaDoc STROKE_2_POINT = new BasicStroke JavaDoc(2f);
889     private static final Stroke JavaDoc STROKE_4_POINT = new BasicStroke JavaDoc(4f);
890     private static final Stroke JavaDoc STROKE_DOTTED =
891         new BasicStroke JavaDoc(
892             1f,
893             BasicStroke.CAP_SQUARE,
894             BasicStroke.JOIN_MITER,
895             10f,
896             new float[]{5f, 3f},
897             0f
898             );
899     
900     private static final Stroke JavaDoc BORDER_STROKE_THIN = new BasicStroke JavaDoc(0.5f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER);
901     private static final Stroke JavaDoc BORDER_STROKE_1_POINT = new BasicStroke JavaDoc(1f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER);
902     private static final Stroke JavaDoc BORDER_STROKE_DOTTED =
903         new BasicStroke JavaDoc(
904             1f,
905             BasicStroke.CAP_BUTT,
906             BasicStroke.JOIN_MITER,
907             10f,
908             new float[]{5f, 3f},
909             0f
910             );
911
912     /**
913      *
914      */

915     private static Stroke JavaDoc getStroke(byte pen)
916     {
917         switch (pen)
918         {
919             case JRGraphicElement.PEN_DOTTED :
920             {
921                 return STROKE_DOTTED;
922             }
923             case JRGraphicElement.PEN_4_POINT :
924             {
925                 return STROKE_4_POINT;
926             }
927             case JRGraphicElement.PEN_2_POINT :
928             {
929                 return STROKE_2_POINT;
930             }
931             case JRGraphicElement.PEN_NONE :
932             {
933                 return null;
934             }
935             case JRGraphicElement.PEN_THIN :
936             {
937                 return STROKE_THIN;
938             }
939             case JRGraphicElement.PEN_1_POINT :
940             default :
941             {
942                 return STROKE_1_POINT;
943             }
944         }
945     }
946
947     private static Stroke JavaDoc getBorderStroke(byte pen)
948     {
949         switch (pen)
950         {
951             case JRGraphicElement.PEN_DOTTED :
952             {
953                 return BORDER_STROKE_DOTTED;
954             }
955             case JRGraphicElement.PEN_4_POINT :
956             {
957                 return STROKE_4_POINT;
958             }
959             case JRGraphicElement.PEN_2_POINT :
960             {
961                 return STROKE_2_POINT;
962             }
963             case JRGraphicElement.PEN_NONE :
964             {
965                 return null;
966             }
967             case JRGraphicElement.PEN_THIN :
968             {
969                 return BORDER_STROKE_THIN;
970             }
971             case JRGraphicElement.PEN_1_POINT :
972             default :
973             {
974                 return BORDER_STROKE_1_POINT;
975             }
976         }
977     }
978     
979
980     private static double getBorderCornerOffset(byte pen)
981     {
982         switch (pen)
983         {
984             case JRGraphicElement.PEN_THIN :
985             {
986                 return THIN_CORNER_OFFSET;
987             }
988             case JRGraphicElement.PEN_1_POINT :
989             case JRGraphicElement.PEN_DOTTED :
990             {
991                 return ONE_POINT_CORNER_OFFSET;
992             }
993             default :
994             {
995                 return 0;
996             }
997         }
998     }
999     
1000    protected void exportFrame(JRPrintFrame frame) throws JRException
1001    {
1002        if (frame.getMode() == JRElement.MODE_OPAQUE)
1003        {
1004            int x = frame.getX() + getOffsetX();
1005            int y = frame.getY() + getOffsetY();
1006            
1007            grx.setColor(frame.getBackcolor());
1008            grx.fillRect(x, y, frame.getWidth(), frame.getHeight());
1009        }
1010        
1011        setFrameElementsOffset(frame, false);
1012        try
1013        {
1014            exportElements(frame.getElements());
1015        }
1016        finally
1017        {
1018            restoreElementOffsets();
1019        }
1020        
1021        exportBox(frame, frame);
1022    }
1023
1024}
1025
Popular Tags