KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > it > businesslogic > ireport > TextReportElement


1 /*
2  * Copyright (C) 2005 - 2006 JasperSoft Corporation. All rights reserved.
3  * http://www.jaspersoft.com.
4  *
5  * Unless you have purchased a commercial license agreement from JasperSoft,
6  * the following license terms apply:
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 as published by
10  * the Free Software Foundation.
11  *
12  * This program is distributed WITHOUT ANY WARRANTY; and without the
13  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14  * See the GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, see http://www.gnu.org/licenses/gpl.txt
18  * or write to:
19  *
20  * Free Software Foundation, Inc.,
21  * 59 Temple Place - Suite 330,
22  * Boston, MA USA 02111-1307
23  *
24  *
25  *
26  *
27  * TextReportElement.java
28  *
29  * Created on 28 febbraio 2003, 19.20
30  *
31  */

32
33 package it.businesslogic.ireport;
34 import it.businesslogic.ireport.gui.*;
35 import it.businesslogic.ireport.util.*;
36 import java.awt.*;
37 import java.awt.image.*;
38 import java.awt.geom.*;
39 import java.awt.font.*;
40 import java.util.*;
41 import java.util.List JavaDoc;
42 import java.text.AttributedCharacterIterator JavaDoc;
43 import java.text.AttributedString JavaDoc;
44
45
46
47 public abstract class TextReportElement extends ReportElement implements BoxElement {
48    static public Rotation ROTATION_NONE;
49    static public Rotation ROTATION_LEFT;
50    static public Rotation ROTATION_RIGHT;
51    static public Rotation ROTATION_UPSIDEDOWN;
52   
53    static
54    {
55      ROTATION_NONE = new Rotation("None", 0);
56      ROTATION_LEFT = new Rotation("Left", 1);
57      ROTATION_RIGHT = new Rotation("Right", 2);
58      ROTATION_UPSIDEDOWN = new Rotation("UpsideDown", 3);
59    }
60
61    private String JavaDoc text="";
62    
63    static public String JavaDoc ALIGN = "ALIGN";
64    static public String JavaDoc VERTICAL_ALIGN = "VERTICAL_ALIGN";
65    static public String JavaDoc IS_STYLED_TEXT = "IS_STYLED_TEXT";
66    static public String JavaDoc LINE_SPACING = "LINE_SPACING";
67    static public String JavaDoc ROTATE = "ROTATE";
68
69    
70    static public String JavaDoc DEFAULT_ALIGN = "Left";
71    static public String JavaDoc DEFAULT_VERTICAL_ALIGN = "Top";
72    static public boolean DEFAULT_IS_STYLED_TEXT = false;
73    static public String JavaDoc DEFAULT_LINE_SPACING = "single";
74    static public String JavaDoc DEFAULT_ROTATE = "None";
75    
76    //private String reportFont="";
77
//private String fontName="";
78
//private String PDFFontName="";
79
//private int fontSize=10;
80
//private String TTFFont="";
81
//private boolean bold=false;
82
//private boolean underline=false;
83
//private boolean italic=false;
84
//private boolean strikeTrought=false;
85
//private String lineSpacing="";
86
//private String align="";
87
//private String verticalAlign="";
88
//private boolean pdfEmbedded=false;
89
//private String pdfEncoding="";
90

91    private java.awt.Font JavaDoc font=null;
92
93
94    //private String rotate = "None";
95
//private boolean isStyledText = false;
96

97     private Box box = null;
98     private IReportFont iReportFont;
99
100    
101    public TextReportElement(int x, int y, int width, int height)
102    {
103       super(x,y,width,height);
104       
105       //see net.sf.jasperreports.engine.base.JRBaseFont
106
//setGraphicElementPen("Thin");
107
//this.bgcolor = Color.WHITE;
108
//this.fgcolor = Color.BLACK;
109
this.text = "Static text\nsecond line";
110       //this.verticalAlign = "Top";
111
//this.align = "Left";
112
//this.lineSpacing = "Single";
113
box = new Box();
114       //set font
115
transparentDefault = "Transparent";
116       setIReportFont(new IReportFont());
117
118    }
119    
120    public void setIReportFont(IReportFont newIreportFont){
121     
122     if(newIreportFont == null){
123         this.iReportFont = new IReportFont();
124     }
125         else
126         {
127             this.iReportFont = (IReportFont)newIreportFont.clone();
128         }
129     this.font = iReportFont.getJavaAWTFont();
130    }
131
132    /** Return a instance of IReportFont. */
133    public IReportFont getIReportFont(){
134         if (iReportFont == null) iReportFont = new IReportFont();
135     return iReportFont;
136    }
137    
138    public void drawObject(Graphics2D g,double zoom_factor, int x_shift_origin, int y_shift_origin)
139    {
140       //System.out.println(new java.util.Date() + " Print text " + this.getText() + " " + x_shift_origin +" " +y_shift_origin);
141

142       position.x -= 10;
143       position.y -= 10;
144       x_shift_origin -= 10;
145       y_shift_origin -= 10;
146
147       
148       this.zoom_factor = zoom_factor;
149       
150       g.setColor( getBgcolor() );
151       
152       if (!getTransparent().equalsIgnoreCase("Transparent"))
153       {
154          g.fillRect(getZoomedDim(position.x)-x_shift_origin,
155                     getZoomedDim(position.y)-y_shift_origin,
156                     getZoomedDim(width),
157                     getZoomedDim(height));
158       }
159       g.setColor( getFgcolor() );
160       
161       // Set font to default font....
162
//Font oldFont = g.getFont();
163

164       if (font == null)
165       {
166          int style = 0;
167          if (isBold()) style |= Font.BOLD;
168          if (isItalic()) style |= Font.ITALIC;
169          if (style == 0) style = Font.PLAIN;
170          font = new Font( this.getFontName(), style , getZoomedDim( this.getFontSize() ));
171       }
172            
173         // Code for rotation by gt (taked by jasperreports...
174

175         int gfx_x = getZoomedDim(position.x + getBox().getLeftPadding() )-x_shift_origin;
176         int gfx_y = getZoomedDim(position.y + getBox().getTopPadding())-y_shift_origin;
177         int gfx_width = getZoomedDim(width - getBox().getLeftPadding() - getBox().getRightPadding());
178         int gfx_height = getZoomedDim(height - getBox().getTopPadding() - getBox().getBottomPadding());
179
180       
181         /*
182       Java2DUtil.setClip(g,
183       gfx_x,
184       gfx_y,
185       gfx_width,
186       gfx_height);
187       */

188       
189       
190         double angle = 0;
191         double angle_restore = 0;
192
193         // Apply the transformation "rotation"
194
// - Do nothing when rotate = "none"
195
AffineTransform transform = null;
196          AffineTransform savedAT = g.getTransform();
197            
198          if(getRotate().equals(ROTATION_LEFT.getName()))
199          {
200            transform = g.getTransform();
201            transform.rotate(-Math.PI / 2, gfx_x, gfx_y);
202            transform.translate(-gfx_height , -gfx_height);
203            gfx_y = gfx_y + gfx_height;
204            gfx_height = getZoomedDim(width - getBox().getLeftPadding() - getBox().getRightPadding());
205            gfx_width = getZoomedDim(height - getBox().getTopPadding() - getBox().getBottomPadding());
206  
207          }
208          else if(getRotate().equals(ROTATION_RIGHT.getName()))
209          {
210            transform = g.getTransform();
211
212            transform.rotate(Math.PI / 2, gfx_x, gfx_y);
213            transform.translate(-gfx_width, -gfx_width);
214            gfx_x = gfx_x + gfx_width;
215            gfx_width = getZoomedDim(height);
216            gfx_height = getZoomedDim(width - getBox().getLeftPadding() - getBox().getRightPadding());
217            gfx_width = getZoomedDim(height - getBox().getTopPadding() - getBox().getBottomPadding());
218          }
219          else if(getRotate().equals(ROTATION_UPSIDEDOWN.getName()))
220          {
221            transform = g.getTransform();
222
223            transform.rotate(Math.PI, gfx_x, gfx_y);
224            transform.translate(-gfx_width, -gfx_height);
225            //gfx_x = gfx_x + gfx_width;
226
//gfx_y = gfx_y + gfx_height;
227
//gfx_width = getZoomedDim(height);
228
//gfx_height =
229
//gfx_width = getZoomedDim(height - getBox().getTopPadding() - getBox().getBottomPadding());
230

231          }
232                 
233          if (this.getText() != null && this.getText().length() > 0)
234          {
235              drawText(g, transform, gfx_x, gfx_y, gfx_height, gfx_width);
236          }
237           
238         
239       
240       //g.setClip(null);
241
//g.setClip(orgClipBounds);
242

243       // ----- Java2DUtil.resetClip(g);
244

245     
246         
247       position.x += 10;
248       position.y += 10;
249       x_shift_origin += 10;
250       y_shift_origin += 10;
251       
252       drawBorder( g, zoom_factor, x_shift_origin,y_shift_origin);
253       drawBorder( g, zoom_factor, x_shift_origin,y_shift_origin,getBox());
254       //drawGraphicsElement(g,this.getGraphicElementPen(), zoom_factor, x_shift_origin,y_shift_origin, 0);
255
}
256    
257    /** Getter for property align.
258     * @return Value of property align.
259     *
260     */

261    public java.lang.String JavaDoc getAlign()
262    {
263        if (getPropertyValue(ALIGN) == null)
264        {
265             // Look for a fgcolor in the stylesheet...
266
if (getStyle() != null)
267             {
268                return getStyle().getAttributeString( getStyle().ATTRIBUTE_hAlign, DEFAULT_ALIGN, true);
269             }
270         }
271         return getStringValue(ALIGN, DEFAULT_ALIGN );
272    }
273    
274    /** Setter for property align.
275     * @param align New value of property align.
276     *
277     */

278    public void setAlign(java.lang.String JavaDoc align)
279    {
280       this.setPropertyValue(ALIGN, align);
281    }
282    
283    /** Getter for property bold.
284     * @return Value of property bold.
285     *
286     */

287    public boolean isBold()
288    {
289        if (getIReportFont().getPropertyValue( IReportFont.IS_BOLD) == null)
290        {
291             // Look for a fgcolor in the stylesheet...
292
if (getStyle() != null)
293             {
294                return getStyle().getAttributeBoolean( getStyle().ATTRIBUTE_isBold, IReportFont.DEFAULT_IS_BOLD, true);
295             }
296         }
297         return getIReportFont().getBooleanValue( IReportFont.IS_BOLD, IReportFont.DEFAULT_IS_BOLD );
298    }
299    
300    /** Setter for property bold.
301     * @param bold New value of property bold.
302     *
303     */

304    public void setBold(boolean bold)
305    {
306       getIReportFont().setPropertyValue( IReportFont.IS_BOLD, ""+bold);
307    }
308    
309    /** Getter for property font.
310     * @return Value of property font.
311     *
312     */

313    public java.awt.Font JavaDoc getFont()
314    {
315       return font;
316    }
317    
318    /** Setter for property font.
319     * @param font New value of property font.
320     *
321     */

322    public void setFont(java.awt.Font JavaDoc font)
323    {
324       this.font = font;
325    }
326    
327    /** Getter for property fontName.
328     * @return Value of property fontName.
329     *
330     */

331    public java.lang.String JavaDoc getFontName()
332    {
333       if (getIReportFont().getPropertyValue( IReportFont.FONT_NAME) == null)
334        {
335             // Look for a fgcolor in the stylesheet...
336
if (getStyle() != null)
337             {
338                return getStyle().getAttributeString( getStyle().ATTRIBUTE_fontName, IReportFont.DEFAULT_FONT_NAME, true);
339             }
340         }
341         return getIReportFont().getStringValue( IReportFont.FONT_NAME, IReportFont.DEFAULT_FONT_NAME );
342    }
343    
344    /** Setter for property fontName.
345     * @param fontName New value of property fontName.
346     *
347     */

348    public void setFontName(java.lang.String JavaDoc fontName)
349    {
350       getIReportFont().setPropertyValue( IReportFont.FONT_NAME, fontName);
351    }
352    
353    /** Getter for property fontSize.
354     * @return Value of property fontSize.
355     *
356     */

357    public int getFontSize()
358    {
359        if (getIReportFont().getPropertyValue( IReportFont.FONT_SIZE) == null)
360        {
361             // Look for a fgcolor in the stylesheet...
362
if (getStyle() != null)
363             {
364                return getStyle().getAttributeInteger( getStyle().ATTRIBUTE_fontSize, IReportFont.DEFAULT_FONT_SIZE, true);
365             }
366         }
367         return getIReportFont().getIntValue( IReportFont.FONT_SIZE, IReportFont.DEFAULT_FONT_SIZE );
368    }
369    
370    /** Setter for property fontSize.
371     * @param fontSize New value of property fontSize.
372     *
373     */

374    public void setFontSize(int fontSize)
375    {
376       getIReportFont().setPropertyValue( IReportFont.FONT_SIZE, ""+fontSize);
377    }
378    
379    /** Setter for property fontSize.
380     * @param fontSize New value of property fontSize.
381     *
382     */

383    public void modifyFontSize(int delta)
384    {
385        int newFonsSize = getFontSize() + delta;
386        if ( newFonsSize >= 5)
387         this.setFontSize(newFonsSize );
388    }
389    
390    /** Getter for property italic.
391     * @return Value of property italic.
392     *
393     */

394    public boolean isItalic()
395    {
396       if (getIReportFont().getPropertyValue( IReportFont.IS_ITALIC) == null)
397        {
398             // Look for a fgcolor in the stylesheet...
399
if (getStyle() != null)
400             {
401                return getStyle().getAttributeBoolean( getStyle().ATTRIBUTE_isItalic, IReportFont.DEFAULT_IS_ITALIC, true);
402             }
403         }
404         return getIReportFont().getBooleanValue( IReportFont.IS_ITALIC, IReportFont.DEFAULT_IS_ITALIC );
405    }
406    
407    /** Setter for property italic.
408     * @param italic New value of property italic.
409     *
410     */

411    public void setItalic(boolean italic)
412    {
413       getIReportFont().setPropertyValue( IReportFont.IS_ITALIC, ""+italic);
414    }
415    
416    /** Getter for property lineSpacing.
417     * @return Value of property lineSpacing.
418     *
419     */

420    public java.lang.String JavaDoc getLineSpacing()
421    {
422        if (getPropertyValue( LINE_SPACING) == null)
423        {
424             // Look for a fgcolor in the stylesheet...
425
if (getStyle() != null)
426             {
427                return getStyle().getAttributeString( getStyle().ATTRIBUTE_lineSpacing, DEFAULT_LINE_SPACING, true);
428             }
429         }
430         return getStringValue( LINE_SPACING, DEFAULT_LINE_SPACING );
431    }
432    
433    /** Setter for property lineSpacing.
434     * @param lineSpacing New value of property lineSpacing.
435     *
436     */

437    public void setLineSpacing(java.lang.String JavaDoc lineSpacing)
438    {
439       setPropertyValue( LINE_SPACING, lineSpacing);
440    }
441    
442    /** Getter for property pdfEmbedded.
443     * @return Value of property pdfEmbedded.
444     *
445     */

446    public boolean isPdfEmbedded()
447    {
448       if (getIReportFont().getPropertyValue( IReportFont.IS_PDF_EMBEDDED) == null)
449        {
450             // Look for a fgcolor in the stylesheet...
451
if (getStyle() != null)
452             {
453                return getStyle().getAttributeBoolean( getStyle().ATTRIBUTE_isPdfEmbedded, IReportFont.DEFAULT_IS_PDF_EMBEDDED, true);
454             }
455         }
456         return getIReportFont().getBooleanValue( IReportFont.IS_PDF_EMBEDDED, IReportFont.DEFAULT_IS_PDF_EMBEDDED );
457    }
458    
459    /** Setter for property pdfEmbedded.
460     * @param pdfEmbedded New value of property pdfEmbedded.
461     *
462     */

463    public void setPdfEmbedded(boolean pdfEmbedded)
464    {
465       getIReportFont().setPropertyValue( IReportFont.IS_PDF_EMBEDDED, ""+pdfEmbedded);
466    }
467    
468    /** Getter for property pdfEncoding.
469     * @return Value of property pdfEncoding.
470     *
471     */

472    public java.lang.String JavaDoc getPdfEncoding()
473    {
474       if (getIReportFont().getPropertyValue( IReportFont.PDF_ENCODING) == null)
475        {
476             // Look for a fgcolor in the stylesheet...
477
if (getStyle() != null)
478             {
479                return getStyle().getAttributeString( getStyle().ATTRIBUTE_pdfEncoding, IReportFont.DEFAULT_PDF_ENCODING, true);
480             }
481         }
482         return getIReportFont().getStringValue( IReportFont.PDF_ENCODING, IReportFont.DEFAULT_PDF_ENCODING );
483    }
484    
485    /** Setter for property pdfEncoding.
486     * @param pdfEncoding New value of property pdfEncoding.
487     *
488     */

489    public void setPdfEncoding(java.lang.String JavaDoc pdfEncoding)
490    {
491       getIReportFont().setPropertyValue( IReportFont.PDF_ENCODING, pdfEncoding);
492    }
493    
494    /** Getter for property PDFFontName.
495     * @return Value of property PDFFontName.
496     *
497     */

498    public java.lang.String JavaDoc getPDFFontName()
499    {
500        if (getIReportFont().getPropertyValue( IReportFont.PDF_FONT_NAME) == null)
501        {
502             // Look for a fgcolor in the stylesheet...
503
if (getStyle() != null)
504             {
505                return getStyle().getAttributeString( getStyle().ATTRIBUTE_pdfFontName, IReportFont.DEFAULT_PDF_FONT_NAME, true);
506             }
507         }
508         return getIReportFont().getStringValue( IReportFont.PDF_FONT_NAME, IReportFont.DEFAULT_PDF_FONT_NAME );
509    }
510    
511    /** Setter for property PDFFontName.
512     * @param PDFFontName New value of property PDFFontName.
513     *
514     */

515    public void setPDFFontName(java.lang.String JavaDoc PDFFontName)
516    {
517       getIReportFont().setPropertyValue( IReportFont.PDF_FONT_NAME, PDFFontName);
518    }
519    
520    /** Getter for property reportFont.
521     * @return Value of property reportFont.
522     *
523     */

524    public java.lang.String JavaDoc getReportFont()
525    {
526       return getIReportFont().getStringValue( IReportFont.REPORT_FONT, IReportFont.DEFAULT_REPORT_FONT);
527    }
528    
529    /** Setter for property reportFont.
530     * @param reportFont New value of property reportFont.
531     *
532     */

533    public void setReportFont(java.lang.String JavaDoc reportFont)
534    {
535       getIReportFont().setPropertyValue( IReportFont.REPORT_FONT, reportFont);
536    }
537    
538    /** Getter for property strikeTrought.
539     * @return Value of property strikeTrought.
540     *
541     */

542    public boolean isStrikeTrought()
543    {
544       if (getIReportFont().getPropertyValue( IReportFont.IS_STRIKETROUGHT) == null)
545        {
546             // Look for a fgcolor in the stylesheet...
547
if (getStyle() != null)
548             {
549                return getStyle().getAttributeBoolean( getStyle().ATTRIBUTE_isStrikeThrough, IReportFont.DEFAULT_IS_STRIKETROUGHT, true);
550             }
551         }
552         return getIReportFont().getBooleanValue( IReportFont.IS_STRIKETROUGHT, IReportFont.DEFAULT_IS_STRIKETROUGHT );
553    }
554    
555    /** Setter for property strikeTrought.
556     * @param strikeTrought New value of property strikeTrought.
557     *
558     */

559    public void setStrikeTrought(boolean strikeTrought)
560    {
561       getIReportFont().setPropertyValue( IReportFont.IS_STRIKETROUGHT, ""+strikeTrought);
562    }
563    
564    /** Getter for property text.
565     * @return Value of property text.
566     *
567     */

568    public java.lang.String JavaDoc getText()
569    {
570       return text;
571    }
572    
573    /** Setter for property text.
574     * @param text New value of property text.
575     *
576     */

577    public void setText(java.lang.String JavaDoc text)
578    {
579       this.text = text;
580    }
581    
582    /** Getter for property TTFFont.
583     * @return Value of property TTFFont.
584     *
585     */

586    public java.lang.String JavaDoc getTTFFont()
587    {
588       if (getIReportFont().getPropertyValue( IReportFont.TTF_FONT_NAME) == null)
589        {
590             // Look for a fgcolor in the stylesheet...
591
if (getStyle() != null)
592             {
593                return getStyle().getAttributeString( getStyle().ATTRIBUTE_pdfFontName, IReportFont.DEFAULT_TTF_FONT_NAME, true);
594             }
595         }
596         return getIReportFont().getStringValue( IReportFont.TTF_FONT_NAME, IReportFont.DEFAULT_TTF_FONT_NAME );
597    }
598    
599    /** Setter for property TTFFont.
600     * @param TTFFont New value of property TTFFont.
601     *
602     */

603    public void setTTFFont(java.lang.String JavaDoc TTFFont)
604    {
605       getIReportFont().setPropertyValue( IReportFont.TTF_FONT_NAME, TTFFont );
606    }
607    
608    /** Getter for property underline.
609     * @return Value of property underline.
610     *
611     */

612    public boolean isUnderline()
613    {
614        if (getIReportFont().getPropertyValue( IReportFont.IS_UNDERLINE) == null)
615        {
616             // Look for a fgcolor in the stylesheet...
617
if (getStyle() != null)
618             {
619                return getStyle().getAttributeBoolean( getStyle().ATTRIBUTE_isUnderline, IReportFont.DEFAULT_IS_UNDERLINE, true);
620             }
621         }
622         return getIReportFont().getBooleanValue( IReportFont.IS_UNDERLINE, IReportFont.DEFAULT_IS_UNDERLINE);
623    }
624    
625    /** Setter for property underline.
626     * @param underline New value of property underline.
627     *
628     */

629    public void setUnderline(boolean underline)
630    {
631       getIReportFont().setPropertyValue( IReportFont.IS_UNDERLINE, ""+underline );
632    }
633    
634    /** Getter for property verticalAlign.
635     * @return Value of property verticalAlign.
636     *
637     */

638    public java.lang.String JavaDoc getVerticalAlign()
639    {
640       if (getPropertyValue( VERTICAL_ALIGN) == null)
641        {
642             // Look for a fgcolor in the stylesheet...
643
if (getStyle() != null)
644             {
645                return getStyle().getAttributeString( getStyle().ATTRIBUTE_vAlign, DEFAULT_VERTICAL_ALIGN, true);
646             }
647         }
648         return getStringValue( VERTICAL_ALIGN, DEFAULT_VERTICAL_ALIGN );
649    }
650    
651    /** Setter for property verticalAlign.
652     * @param verticalAlign New value of property verticalAlign.
653     *
654     */

655    public void setVerticalAlign(java.lang.String JavaDoc verticalAlign)
656    {
657       setPropertyValue( VERTICAL_ALIGN, verticalAlign );
658    }
659    
660    /** Getter for property Rotate.
661     * Rotate can be one of the values: "none", "left", "right"
662     * @return Value of property Rotate.
663     */

664    public java.lang.String JavaDoc getRotate()
665    {
666        if (getPropertyValue( ROTATE) == null)
667        {
668             // Look for a fgcolor in the stylesheet...
669
if (getStyle() != null)
670             {
671                return getStyle().getAttributeString( getStyle().ATTRIBUTE_rotation, DEFAULT_ROTATE, true);
672             }
673         }
674         return getStringValue( ROTATE, DEFAULT_ROTATE );
675    }
676    
677    /** Setter for property Rotate.
678     * @param Rotate New value of property Rotate.
679     *
680     */

681    public void setRotate(java.lang.String JavaDoc rotate)
682    {
683       setPropertyValue( ROTATE, rotate );
684    }
685    
686    public int getTextHeight(java.awt.FontMetrics JavaDoc fm)
687    {
688       //
689
return fm.getAscent() + (this.getLineCount()-1)*(fm.getHeight());
690    }
691    
692    public int getLineCount()
693    {
694       // For any \n, return a line...
695
String JavaDoc text = this.getText();
696       text = text.replace('\r', ' ');
697       int line=1;
698       while (text.indexOf('\n')>0)
699       {
700          line++;
701          text = text.substring( text.indexOf('\n') + 1);
702       }
703       return line;
704    }
705  
706    public void copyBaseReportElement(ReportElement destination, ReportElement source)
707    {
708       super.copyBaseReportElement(destination, source);
709       
710       if (destination instanceof TextReportElement &&
711       source instanceof TextReportElement )
712       {
713           TextReportElement tre = (TextReportElement)source;
714           TextReportElement tred = (TextReportElement)destination;
715           source.clone(tred);
716           tre.getIReportFont().clone( tred.getIReportFont() );
717           /*
718           ((TextReportElement)destination).setAlign(new String( ((TextReportElement)source).getAlign() ));
719          ((TextReportElement)destination).setReportFont(((TextReportElement)source).getReportFont());
720          ((TextReportElement)destination).setFontName(new String( ((TextReportElement)source).getFontName() ));
721          ((TextReportElement)destination).setPDFFontName(new String( ((TextReportElement)source).getPDFFontName() ));
722          ((TextReportElement)destination).setFontSize( ((TextReportElement)source).getFontSize() );
723          ((TextReportElement)destination).setTTFFont(new String( ((TextReportElement)source).getTTFFont() ));
724          ((TextReportElement)destination).setBold( ((TextReportElement)source).isBold() );
725          ((TextReportElement)destination).setItalic( ((TextReportElement)source).isItalic() );
726          ((TextReportElement)destination).setUnderline( ((TextReportElement)source).isUnderline() );
727          ((TextReportElement)destination).setStrikeTrought( ((TextReportElement)source).isStrikeTrought() );
728          ((TextReportElement)destination).setLineSpacing(new String( ((TextReportElement)source).getLineSpacing() ));
729          ((TextReportElement)destination).setVerticalAlign(new String( ((TextReportElement)source).getVerticalAlign() ));
730          ((TextReportElement)destination).setPdfEmbedded( ((TextReportElement)source).isPdfEmbedded());
731          ((TextReportElement)destination).setPdfEncoding(new String( ((TextReportElement)source).getPdfEncoding() ));
732          */

733          ((TextReportElement)destination).setText(new String JavaDoc( ((TextReportElement)source).getText() ));
734          ((TextReportElement)destination).setBox( ((TextReportElement)source).getBox().cloneMe() );
735       }
736    }
737
738
739    private class TextReportElementLayout {
740        private TextLayout layout;
741        private float x;
742        private float y;
743
744
745        private TextReportElementLayout(TextLayout layout, float x, float y) {
746            this.layout = layout;
747            this.x = x;
748            this.y = y;
749        }
750
751
752        void drawWithOffset(Graphics2D g2, float yOffset) {
753            layout.draw(g2, x, y + yOffset);
754        }
755    }
756
757
758    static List JavaDoc getRotations() {
759        return Rotation.rotations;
760    }
761
762    /** Getter for property isStyledText.
763     * @return Value of property isStyledText.
764     *
765     */

766    public boolean isIsStyledText() {
767        if (getPropertyValue( IS_STYLED_TEXT) == null)
768        {
769             // Look for a fgcolor in the stylesheet...
770
if (getStyle() != null)
771             {
772                return getStyle().getAttributeBoolean( getStyle().ATTRIBUTE_isStyledText, DEFAULT_IS_STYLED_TEXT, true);
773             }
774         }
775         return getBooleanValue( IS_STYLED_TEXT, DEFAULT_IS_STYLED_TEXT );
776    }
777
778    /** Setter for property isStyledText.
779     * @param isStyledText New value of property isStyledText.
780     *
781     */

782    public void setIsStyledText(boolean isStyledText) {
783       setPropertyValue( IS_STYLED_TEXT, "" + isStyledText );
784    }
785    
786    static class Rotation {
787        static private ArrayList rotations;
788        private String JavaDoc name;
789        private int number;
790
791
792        Rotation(String JavaDoc name, int number) {
793            this.name = name;
794            this.number = number;
795            rotations = new ArrayList();
796            rotations.add(this);
797        }
798
799
800        public int getNumber() {
801          return number;
802        }
803
804
805        public String JavaDoc getName() {
806            return name;
807        }
808
809
810        public String JavaDoc toString() {
811            return getName();
812        }
813    }
814    
815
816     public Box getBox() {
817         return box;
818     }
819
820     public void setBox(Box box) {
821         this.box = box;
822     }
823     
824      public void setStyle(Style style) {
825      
826         super.setStyle(style);
827         
828         if (style != null)
829         {
830             //this.setAlign( style.getAttributeString( style.ATTRIBUTE_hAlign, getAlign(), true));
831
//this.setFontName( style.getAttributeString( style.ATTRIBUTE_fontName, getFontName(), true));
832
//this.setPDFFontName( style.getAttributeString( style.ATTRIBUTE_pdfFontName, getPDFFontName(), true));
833
//this.setFontSize( Integer.parseInt( style.getAttributeString( style.ATTRIBUTE_fontSize, getFontSize()+"", true)));
834
//this.setTTFFont( style.getAttributeString( style.ATTRIBUTE_pdfFontName, getTTFFont(), true));
835
//this.setBold( style.getAttributeBoolean( style.ATTRIBUTE_isBold, isBold(), true));
836
//this.setItalic(style.getAttributeBoolean( style.ATTRIBUTE_isItalic, isItalic(), true));
837
//this.setUnderline(style.getAttributeBoolean( style.ATTRIBUTE_isUnderline, isUnderline(), true));
838
//this.setStrikeTrought(style.getAttributeBoolean( style.ATTRIBUTE_isStrikeThrough, isStrikeTrought(), true));
839
//this.setLineSpacing( style.getAttributeString( style.ATTRIBUTE_lineSpacing, getLineSpacing(), true));
840
//this.setVerticalAlign( style.getAttributeString( style.ATTRIBUTE_vAlign, getVerticalAlign(), true));
841
//this.setPdfEmbedded( style.getAttributeBoolean( style.ATTRIBUTE_isPdfEmbedded, isPdfEmbedded(), true));
842
//this.setPdfEncoding( style.getAttributeString( style.ATTRIBUTE_pdfEncoding, getPdfEncoding(), true));
843
//this.setRotate(style.getAttributeString( style.ATTRIBUTE_rotation, getRotate(), true) );
844

845             // BOX
846
if (style.getAttributeString(style.ATTRIBUTE_border, null, true) != null)
847                 this.getBox().setBorder( style.getAttributeString(style.ATTRIBUTE_border, null, true) );
848             if (style.getAttributeColor(style.ATTRIBUTE_borderColor, null, true) != null)
849                 this.getBox().setBorderColor( style.getAttributeColor(style.ATTRIBUTE_borderColor, null, true));
850             if (style.getAttributeString(style.ATTRIBUTE_padding, null, true) != null)
851                 this.getBox().setPadding( Integer.parseInt( style.getAttributeString(style.ATTRIBUTE_padding, null, true) ));
852             
853             if (style.getAttributeString(style.ATTRIBUTE_topBorder, null, true) != null)
854                 this.getBox().setTopBorder( style.getAttributeString(style.ATTRIBUTE_topBorder, null, true) );
855             if (style.getAttributeColor(style.ATTRIBUTE_topBorderColor, null, true) != null)
856                 this.getBox().setTopBorderColor( style.getAttributeColor(style.ATTRIBUTE_topBorderColor, null, true));
857             if (style.getAttributeString(style.ATTRIBUTE_topPadding, null, true) != null)
858                 this.getBox().setTopPadding( Integer.parseInt( style.getAttributeString(style.ATTRIBUTE_topPadding, null, true) ));
859             
860             if (style.getAttributeString(style.ATTRIBUTE_leftBorder, null, true) != null)
861                 this.getBox().setLeftBorder( style.getAttributeString(style.ATTRIBUTE_leftBorder, null, true) );
862             if (style.getAttributeColor(style.ATTRIBUTE_leftBorderColor, null, true) != null)
863                 this.getBox().setLeftBorderColor( style.getAttributeColor(style.ATTRIBUTE_leftBorderColor, null, true));
864             if (style.getAttributeString(style.ATTRIBUTE_leftPadding, null, true) != null)
865                 this.getBox().setLeftPadding( Integer.parseInt( style.getAttributeString(style.ATTRIBUTE_leftPadding, null, true) ));
866             
867             if (style.getAttributeString(style.ATTRIBUTE_rightBorder, null, true) != null)
868                 this.getBox().setRightBorder( style.getAttributeString(style.ATTRIBUTE_rightBorder, null, true) );
869             if (style.getAttributeColor(style.ATTRIBUTE_rightBorderColor, null, true) != null)
870                 this.getBox().setRightBorderColor( style.getAttributeColor(style.ATTRIBUTE_rightBorderColor, null, true));
871             if (style.getAttributeString(style.ATTRIBUTE_rightPadding, null, true) != null)
872                 this.getBox().setRightPadding( Integer.parseInt( style.getAttributeString(style.ATTRIBUTE_rightPadding, null, true) ));
873             
874             if (style.getAttributeString(style.ATTRIBUTE_bottomBorder, null, true) != null)
875                 this.getBox().setBottomBorder( style.getAttributeString(style.ATTRIBUTE_bottomBorder, null, true) );
876             if (style.getAttributeColor(style.ATTRIBUTE_bottomBorderColor, null, true) != null)
877                 this.getBox().setBottomBorderColor( style.getAttributeColor(style.ATTRIBUTE_bottomBorderColor, null, true));
878             if (style.getAttributeString(style.ATTRIBUTE_bottomPadding, null, true) != null)
879                 this.getBox().setBottomPadding( Integer.parseInt( style.getAttributeString(style.ATTRIBUTE_bottomPadding, null, true) ));
880             
881         }
882      }
883      
884      
885      private void drawText(Graphics2D g, AffineTransform transform, int gfx_x, int gfx_y, int gfx_height, int gfx_width)
886      {
887           int zoomedFieldHeight = gfx_height;
888          String JavaDoc allText = Misc.treatNewLineChars(this.getText());
889          float formatWidth = (float)gfx_width;
890          float verticalOffset = 0f;
891          TextReportElementLayout textReportElementLayout;
892          
893          
894          ArrayList textLayouts;
895         float x, y;
896          
897          
898          FontRenderContext fontRenderContext = g.getFontRenderContext();
899          java.util.Map JavaDoc fontAttributes = font.getAttributes();
900          fontAttributes.put(TextAttribute.SIZE, new Float JavaDoc(getZoomedDim( this.getFontSize() )) );
901          fontAttributes.put(TextAttribute.FAMILY, this.getFontName() );
902          if (this.isBold())
903          {
904             fontAttributes.put(TextAttribute.WEIGHT, TextAttribute.WEIGHT_BOLD);
905          }
906          if (this.isItalic())
907          {
908             fontAttributes.put(TextAttribute.POSTURE, TextAttribute.POSTURE_OBLIQUE);
909          }
910          if (this.isUnderline())
911          {
912             fontAttributes.put(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON);
913          }
914          if (this.isStrikeTrought())
915          {
916             fontAttributes.put(TextAttribute.STRIKETHROUGH, TextAttribute.STRIKETHROUGH_ON);
917          }
918          
919          float lineSpacing = 1f;
920          if (this.getLineSpacing().equals("Single")) lineSpacing = 1f;
921          else if (this.getLineSpacing().equals("1_1_2")) lineSpacing = 1.5f;
922          else if (this.getLineSpacing().equals("Double")) lineSpacing = 2f;
923          
924          AttributedString JavaDoc atext;
925          AttributedCharacterIterator JavaDoc paragraph;
926          int paragraphStart;
927          int paragraphEnd;
928          LineBreakMeasurer lineMeasurer;
929          TextLayout layout = null;
930          
931          
932          String JavaDoc paragr_text = "";
933          boolean isMaxHeightReached = false;
934          
935          StringTokenizer tkzer = new StringTokenizer(allText, "\n");
936          
937          float drawPosY = 0;
938          float drawPosX = 0;
939         
940          paragr_text = "";
941          isMaxHeightReached = false;
942          
943          tkzer = new StringTokenizer(allText, "\n");
944
945
946          textLayouts = new ArrayList();
947         
948          // Calculate the layouts. (But don't draw yet because we don't know yet
949
// the offset which is needed if we align the text "middle" or "bottom")
950
while(tkzer.hasMoreTokens() && !isMaxHeightReached)
951          {
952             paragr_text = tkzer.nextToken();
953             
954             atext = new AttributedString JavaDoc(paragr_text, fontAttributes);
955             paragraph = atext.getIterator();
956             paragraphStart = paragraph.getBeginIndex();
957             paragraphEnd = paragraph.getEndIndex();
958             lineMeasurer = new LineBreakMeasurer(paragraph, fontRenderContext);
959             lineMeasurer.setPosition(paragraphStart);
960             
961             layout = null;
962             while (lineMeasurer.getPosition() < paragraphEnd && !isMaxHeightReached)
963             {
964                layout = lineMeasurer.nextLayout(formatWidth);
965                
966                drawPosY += layout.getLeading() + lineSpacing * layout.getAscent();
967                
968                if (drawPosY + layout.getDescent() <= zoomedFieldHeight+1)
969                {
970                   if (this.getAlign().equals("Justify"))
971                   {
972                      if (layout.isLeftToRight())
973                      {
974                         drawPosX = 0;
975                      }
976                      else
977                      {
978                         drawPosX = formatWidth - layout.getAdvance();
979                      }
980                      if (lineMeasurer.getPosition() < paragraphEnd)
981                      {
982                         layout = layout.getJustifiedLayout(formatWidth);
983                      }
984                   }
985                   else if (this.getAlign().equals("Right"))
986                   {
987                      if (layout.isLeftToRight())
988                      {
989                         drawPosX = formatWidth - layout.getAdvance();
990                      }
991                      else
992                      {
993                         drawPosX = formatWidth;
994                      }
995                   }
996                   else if (this.getAlign().equals("Center"))
997                   {
998                      drawPosX = (formatWidth - layout.getAdvance()) / 2;
999                   }
1000                  else //if (this.getAlign().equals("Left"))
1001
{
1002                     if (layout.isLeftToRight())
1003                     {
1004                        drawPosX = 0;
1005                     }
1006                     else
1007                     {
1008                        drawPosX = formatWidth - layout.getAdvance();
1009                     }
1010                  }
1011                
1012                  x = drawPosX + gfx_x; //getZoomedDim(position.x)-x_shift_origin;
1013
y = drawPosY + gfx_y; //getZoomedDim(position.y)-y_shift_origin;
1014
textReportElementLayout = new TextReportElementLayout(layout, x, y);
1015                  textLayouts.add(textReportElementLayout);
1016
1017
1018                  drawPosY += layout.getDescent();
1019               }
1020               else
1021               {
1022                  drawPosY -= layout.getLeading() + lineSpacing * layout.getAscent();
1023                  isMaxHeightReached = true;
1024               }
1025            }
1026         }
1027
1028
1029         // Calculate the offset when aligning the text.
1030
float textHeight = drawPosY;
1031         if (this.getVerticalAlign().equals("Top"))
1032         {
1033             verticalOffset = 0f;
1034         }
1035         else if (this.getVerticalAlign().equals("Middle"))
1036         {
1037            verticalOffset = ((float)zoomedFieldHeight- textHeight) / 2f;
1038         }
1039         else if (this.getVerticalAlign().equals("Bottom"))
1040         {
1041            verticalOffset = (float)zoomedFieldHeight - (float)textHeight;
1042         }
1043
1044         // Put affine transformatio here!!!
1045

1046         AffineTransform savedAT = g.getTransform();
1047         if (transform != null) g.setTransform( transform);
1048      
1049         // Now draw the text according to the calculated layouts.
1050
for(Iterator JavaDoc i=textLayouts.iterator(); i.hasNext();)
1051         {
1052           textReportElementLayout = (TextReportElementLayout) i.next();
1053           textReportElementLayout.drawWithOffset(g, verticalOffset);
1054         }
1055         
1056         if(transform != null)
1057         {
1058            g.setTransform(savedAT);
1059         }
1060         
1061         
1062     }
1063}
1064
Popular Tags