1 /*
2  * JasperReports - Free Java Reporting Library.
3  * Copyright (C) 2001 - 2019 TIBCO Software Inc. All rights reserved.
4  * http://www.jaspersoft.com
5  *
6  * Unless you have purchased a commercial license agreement from Jaspersoft,
7  * the following license terms apply:
8  *
9  * This program is part of JasperReports.
10  *
11  * JasperReports is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License as published by
13  * the Free Software Foundation, either version 3 of the License, or
14  * (at your option) any later version.
15  *
16  * JasperReports is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU Lesser General Public License for more details.
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * along with JasperReports. If not, see <http://www.gnu.org/licenses/>.
23  */

24 package net.sf.jasperreports.engine.base;
25
26 import java.awt.Color;
27 import java.io.IOException;
28 import java.io.ObjectInputStream;
29
30 import net.sf.jasperreports.engine.JRCommonText;
31 import net.sf.jasperreports.engine.JRConstants;
32 import net.sf.jasperreports.engine.JRFont;
33 import net.sf.jasperreports.engine.JRLineBox;
34 import net.sf.jasperreports.engine.JRParagraph;
35 import net.sf.jasperreports.engine.JRTextElement;
36 import net.sf.jasperreports.engine.type.HorizontalTextAlignEnum;
37 import net.sf.jasperreports.engine.type.LineSpacingEnum;
38 import net.sf.jasperreports.engine.type.ModeEnum;
39 import net.sf.jasperreports.engine.type.RotationEnum;
40 import net.sf.jasperreports.engine.type.VerticalTextAlignEnum;
41
42
43 /**
44  * This class provides functionality common to text elements. It provides implementation for the methods described
45  * in <tt>JRTextElement</tt>.
46  * @author Teodor Danciu (teodord@users.sourceforge.net)
47  */

48 public abstract class JRBaseTextElement extends JRBaseElement implements JRTextElement
49 {
50
51
52     /**
53      *
54      */

55     private static final long serialVersionUID = JRConstants.SERIAL_VERSION_UID;
56
57
58     /**
59      *
60      */

61     protected HorizontalTextAlignEnum horizontalTextAlign;
62     protected VerticalTextAlignEnum verticalTextAlign;
63     protected RotationEnum rotationValue;
64     
65     protected String markup;
66
67     /**
68      *
69      */

70     protected JRLineBox lineBox;
71     protected JRParagraph paragraph;
72
73     protected String fontName;
74     protected Boolean isBold;
75     protected Boolean isItalic;
76     protected Boolean isUnderline;
77     protected Boolean isStrikeThrough;
78     protected Float fontsize;
79     protected String pdfFontName;
80     protected String pdfEncoding;
81     protected Boolean isPdfEmbedded;
82
83     
84     /**
85      * Initializes properties that are specific to text elements. Common properties are initialized by its
86      * parent constructor.
87      * @param textElement an element whose properties are copied to this element. Usually it is a
88      * {@link net.sf.jasperreports.engine.design.JRDesignTextElement} that must be transformed into an
89      * <tt>JRBaseTextElement</tt> at compile time.
90      * @param factory a factory used in the compile process
91      */

92     protected JRBaseTextElement(JRTextElement textElement, JRBaseObjectFactory factory)
93     {
94         super(textElement, factory);
95
96         horizontalTextAlign = textElement.getOwnHorizontalTextAlign();
97         verticalTextAlign = textElement.getOwnVerticalTextAlign();
98         rotationValue = textElement.getOwnRotationValue();
99         markup = textElement.getOwnMarkup();
100
101         lineBox = textElement.getLineBox().clone(this);
102         paragraph = textElement.getParagraph().clone(this);
103
104         fontName = textElement.getOwnFontName();
105         isBold = textElement.isOwnBold();
106         isItalic = textElement.isOwnItalic();
107         isUnderline = textElement.isOwnUnderline();
108         isStrikeThrough = textElement.isOwnStrikeThrough();
109         fontsize = textElement.getOwnFontsize();
110         pdfFontName = textElement.getOwnPdfFontName();
111         pdfEncoding = textElement.getOwnPdfEncoding();
112         isPdfEmbedded = textElement.isOwnPdfEmbedded();
113     }
114
115
116     @Override
117     public HorizontalTextAlignEnum getHorizontalTextAlign()
118     {
119         return getStyleResolver().getHorizontalTextAlign(this);
120     }
121         
122     @Override
123     public HorizontalTextAlignEnum getOwnHorizontalTextAlign()
124     {
125         return horizontalTextAlign;
126     }
127         
128     @Override
129     public void setHorizontalTextAlign(HorizontalTextAlignEnum horizontalTextAlign)
130     {
131         Object old = this.horizontalTextAlign;
132         this.horizontalTextAlign = horizontalTextAlign;
133         getEventSupport().firePropertyChange(JRBaseStyle.PROPERTY_HORIZONTAL_TEXT_ALIGNMENT, old, this.horizontalTextAlign);
134     }
135
136     @Override
137     public VerticalTextAlignEnum getVerticalTextAlign()
138     {
139         return getStyleResolver().getVerticalTextAlign(this);
140     }
141         
142     @Override
143     public VerticalTextAlignEnum getOwnVerticalTextAlign()
144     {
145         return verticalTextAlign;
146     }
147         
148     @Override
149     public void setVerticalTextAlign(VerticalTextAlignEnum verticalTextAlign)
150     {
151         Object old = this.verticalTextAlign;
152         this.verticalTextAlign = verticalTextAlign;
153         getEventSupport().firePropertyChange(JRBaseStyle.PROPERTY_VERTICAL_TEXT_ALIGNMENT, old, this.verticalTextAlign);
154     }
155
156     @Override
157     public RotationEnum getRotationValue()
158     {
159         return getStyleResolver().getRotationValue(this);
160     }
161
162     @Override
163     public RotationEnum getOwnRotationValue()
164     {
165         return this.rotationValue;
166     }
167
168     @Override
169     public void setRotation(RotationEnum rotationValue)
170     {
171         Object old = this.rotationValue;
172         this.rotationValue = rotationValue;
173         getEventSupport().firePropertyChange(JRBaseStyle.PROPERTY_ROTATION, old, this.rotationValue);
174     }
175
176     @Override
177     public String getMarkup()
178     {
179         return getStyleResolver().getMarkup(this);
180     }
181         
182     @Override
183     public String getOwnMarkup()
184     {
185         return markup;
186     }
187
188     @Override
189     public void setMarkup(String markup)
190     {
191         Object old = this.markup;
192         this.markup = markup;
193         getEventSupport().firePropertyChange(JRBaseStyle.PROPERTY_MARKUP, old, this.markup);
194     }
195
196     @Override
197     public JRLineBox getLineBox()
198     {
199         return lineBox;
200     }
201
202     @Override
203     public JRParagraph getParagraph()
204     {
205         return paragraph;
206     }
207
208     /**
209      * @deprecated
210      */

211     public JRFont getFont()
212     {
213         return this;
214     }
215
216     @Override
217     public ModeEnum getModeValue()
218     {
219         return getStyleResolver().getMode(this, ModeEnum.TRANSPARENT);
220     }
221
222     @Override
223     public String getFontName()
224     {
225         return getStyleResolver().getFontName(this);
226     }
227
228     @Override
229     public String getOwnFontName()
230     {
231         return fontName;
232     }
233
234     @Override
235     public void setFontName(String fontName)
236     {
237         Object old = this.fontName;
238         this.fontName = fontName;
239         getEventSupport().firePropertyChange(JRBaseStyle.PROPERTY_FONT_NAME, old, this.fontName);
240     }
241
242
243     @Override
244     public boolean isBold()
245     {
246         return getStyleResolver().isBold(this);
247     }
248
249     @Override
250     public Boolean isOwnBold()
251     {
252         return isBold;
253     }
254
255     /**
256      * @deprecated Replaced by {@link #setBold(Boolean)}.
257      */

258     @Override
259     public void setBold(boolean isBold)
260     {
261         setBold((Boolean)isBold);
262     }
263
264     /**
265      * Alternative setBold method which allows also to reset
266      * the "own" isBold property.
267      */

268     @Override
269     public void setBold(Boolean isBold)
270     {
271         Object old = this.isBold;
272         this.isBold = isBold;
273         getEventSupport().firePropertyChange(JRBaseStyle.PROPERTY_BOLD, old, this.isBold);
274     }
275
276
277     @Override
278     public boolean isItalic()
279     {
280         return getStyleResolver().isItalic(this);
281     }
282
283     @Override
284     public Boolean isOwnItalic()
285     {
286         return isItalic;
287     }
288
289     /**
290      * @deprecated Replaced by {@link #setItalic(Boolean)}.
291      */

292     @Override
293     public void setItalic(boolean isItalic)
294     {
295         setItalic((Boolean)isItalic);
296     }
297
298     /**
299      * Alternative setItalic method which allows also to reset
300      * the "own" isItalic property.
301      */

302     @Override
303     public void setItalic(Boolean isItalic)
304     {
305         Object old = this.isItalic;
306         this.isItalic = isItalic;
307         getEventSupport().firePropertyChange(JRBaseStyle.PROPERTY_ITALIC, old, this.isItalic);
308     }
309
310     @Override
311     public boolean isUnderline()
312     {
313         return getStyleResolver().isUnderline(this);
314     }
315
316     @Override
317     public Boolean isOwnUnderline()
318     {
319         return isUnderline;
320     }
321
322     /**
323      * @deprecated Replaced by {@link #setUnderline(Boolean)}.
324      */

325     @Override
326     public void setUnderline(boolean isUnderline)
327     {
328         setUnderline((Boolean)isUnderline);
329     }
330
331     /**
332      * Alternative setUnderline method which allows also to reset
333      * the "own" isUnderline property.
334      */

335     @Override
336     public void setUnderline(Boolean isUnderline)
337     {
338         Object old = this.isUnderline;
339         this.isUnderline = isUnderline;
340         getEventSupport().firePropertyChange(JRBaseStyle.PROPERTY_UNDERLINE, old, this.isUnderline);
341     }
342
343     @Override
344     public boolean isStrikeThrough()
345     {
346         return getStyleResolver().isStrikeThrough(this);
347     }
348
349     @Override
350     public Boolean isOwnStrikeThrough()
351     {
352         return isStrikeThrough;
353     }
354
355     /**
356      * @deprecated Replaced by {@link #setStrikeThrough(Boolean)}.
357      */

358     @Override
359     public void setStrikeThrough(boolean isStrikeThrough)
360     {
361         setStrikeThrough((Boolean)isStrikeThrough);
362     }
363
364     /**
365      * Alternative setStrikeThrough method which allows also to reset
366      * the "own" isStrikeThrough property.
367      */

368     @Override
369     public void setStrikeThrough(Boolean isStrikeThrough)
370     {
371         Object old = this.isStrikeThrough;
372         this.isStrikeThrough = isStrikeThrough;
373         getEventSupport().firePropertyChange(JRBaseStyle.PROPERTY_STRIKE_THROUGH, old, this.isStrikeThrough);
374     }
375
376     @Override
377     public float getFontsize()
378     {
379         return getStyleResolver().getFontsize(this);
380     }
381
382     @Override
383     public Float getOwnFontsize()
384     {
385         return fontsize;
386     }
387
388     /**
389      * Method which allows also to reset the "own" size property.
390      */

391     @Override
392     public void setFontSize(Float fontSize)
393     {
394         Object old = this.fontsize;
395         this.fontsize = fontSize;
396         getEventSupport().firePropertyChange(JRBaseStyle.PROPERTY_FONT_SIZE, old, this.fontsize);
397     }
398
399     @Override
400     public String getPdfFontName()
401     {
402         return getStyleResolver().getPdfFontName(this);
403     }
404
405     @Override
406     public String getOwnPdfFontName()
407     {
408         return pdfFontName;
409     }
410
411     @Override
412     public void setPdfFontName(String pdfFontName)
413     {
414         Object old = this.pdfFontName;
415         this.pdfFontName = pdfFontName;
416         getEventSupport().firePropertyChange(JRBaseStyle.PROPERTY_PDF_FONT_NAME, old, this.pdfFontName);
417     }
418
419
420     @Override
421     public String getPdfEncoding()
422     {
423         return getStyleResolver().getPdfEncoding(this);
424     }
425
426     @Override
427     public String getOwnPdfEncoding()
428     {
429         return pdfEncoding;
430     }
431
432     @Override
433     public void setPdfEncoding(String pdfEncoding)
434     {
435         Object old = this.pdfEncoding;
436         this.pdfEncoding = pdfEncoding;
437         getEventSupport().firePropertyChange(JRBaseStyle.PROPERTY_PDF_ENCODING, old, this.pdfEncoding);
438     }
439
440
441     @Override
442     public boolean isPdfEmbedded()
443     {
444         return getStyleResolver().isPdfEmbedded(this);
445     }
446
447     @Override
448     public Boolean isOwnPdfEmbedded()
449     {
450         return isPdfEmbedded;
451     }
452
453     /**
454      * @deprecated Replaced by {@link #setPdfEmbedded(Boolean)}.
455      */

456     @Override
457     public void setPdfEmbedded(boolean isPdfEmbedded)
458     {
459         setPdfEmbedded((Boolean)isPdfEmbedded);
460     }
461
462     /**
463      * Alternative setPdfEmbedded method which allows also to reset
464      * the "own" isPdfEmbedded property.
465      */

466     @Override
467     public void setPdfEmbedded(Boolean isPdfEmbedded)
468     {
469         Object old = this.isPdfEmbedded;
470         this.isPdfEmbedded = isPdfEmbedded;
471         getEventSupport().firePropertyChange(JRBaseStyle.PROPERTY_PDF_EMBEDDED, old, this.isPdfEmbedded);
472     }
473
474     @Override
475     public Color getDefaultLineColor() 
476     {
477         return getForecolor();
478     }
479
480     @Override
481     public Object clone() 
482     {
483         JRBaseTextElement clone = (JRBaseTextElement)super.clone();
484         
485         clone.lineBox = lineBox.clone(clone);
486         clone.paragraph = paragraph.clone(clone);
487         
488         return clone;
489     }
490     
491
492     /*
493      * These fields are only for serialization backward compatibility.
494      */

495     private int PSEUDO_SERIAL_VERSION_UID = JRConstants.PSEUDO_SERIAL_VERSION_UID; //NOPMD
496     /**
497      * @deprecated
498      */

499     private Byte horizontalAlignment;
500     /**
501      * @deprecated
502      */

503     private Byte verticalAlignment;
504     /**
505      * @deprecated
506      */

507     private net.sf.jasperreports.engine.type.HorizontalAlignEnum horizontalAlignmentValue;
508     /**
509      * @deprecated
510      */

511     private net.sf.jasperreports.engine.type.VerticalAlignEnum verticalAlignmentValue;
512     /**
513      * @deprecated
514      */

515     private Byte rotation;    
516     /**
517      * @deprecated
518      */

519     private Byte lineSpacing;
520     /**
521      * @deprecated
522      */

523     private LineSpacingEnum lineSpacingValue;
524     /**
525      * @deprecated
526      */

527     private Boolean isStyledText;
528     /**
529      * @deprecated
530      */

531     private Integer fontSize;
532     
533     @SuppressWarnings("deprecation")
534     private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException
535     {
536         in.defaultReadObject();
537
538         if (PSEUDO_SERIAL_VERSION_UID < JRConstants.PSEUDO_SERIAL_VERSION_UID_3_7_2)
539         {
540             horizontalAlignmentValue = net.sf.jasperreports.engine.type.HorizontalAlignEnum.getByValue(horizontalAlignment);
541             verticalAlignmentValue = net.sf.jasperreports.engine.type.VerticalAlignEnum.getByValue(verticalAlignment);
542             rotationValue = RotationEnum.getByValue(rotation);
543             lineSpacingValue = LineSpacingEnum.getByValue(lineSpacing);
544
545             horizontalAlignment = null;
546             verticalAlignment = null;
547             rotation = null;
548             lineSpacing = null;
549         }
550
551         if (isStyledText != null)
552         {
553             markup = isStyledText ? JRCommonText.MARKUP_STYLED_TEXT : JRCommonText.MARKUP_NONE;
554             isStyledText = null;
555         }
556
557         if (paragraph == null)
558         {
559             paragraph = new JRBaseParagraph(this);
560             paragraph.setLineSpacing(lineSpacingValue);
561             lineSpacingValue = null;
562         }
563
564         if (PSEUDO_SERIAL_VERSION_UID < JRConstants.PSEUDO_SERIAL_VERSION_UID_5_5_2)
565         {
566             fontsize = fontSize == null ? null : fontSize.floatValue();
567
568             fontSize = null;
569         }
570
571         if (PSEUDO_SERIAL_VERSION_UID < JRConstants.PSEUDO_SERIAL_VERSION_UID_6_0_2)
572         {
573             horizontalTextAlign = net.sf.jasperreports.engine.type.HorizontalAlignEnum.getHorizontalTextAlignEnum(horizontalAlignmentValue);
574             verticalTextAlign = net.sf.jasperreports.engine.type.VerticalAlignEnum.getVerticalTextAlignEnum(verticalAlignmentValue);
575
576             horizontalAlignmentValue = null;
577             verticalAlignmentValue = null;
578         }
579     }
580 }
581