0001: /*
0002: * Copyright (C) 2005 - 2008 JasperSoft Corporation. All rights reserved.
0003: * http://www.jaspersoft.com.
0004: *
0005: * Unless you have purchased a commercial license agreement from JasperSoft,
0006: * the following license terms apply:
0007: *
0008: * This program is free software; you can redistribute it and/or modify
0009: * it under the terms of the GNU General Public License version 2 as published by
0010: * the Free Software Foundation.
0011: *
0012: * This program is distributed WITHOUT ANY WARRANTY; and without the
0013: * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
0014: * See the GNU General Public License for more details.
0015: *
0016: * You should have received a copy of the GNU General Public License
0017: * along with this program; if not, see http://www.gnu.org/licenses/gpl.txt
0018: * or write to:
0019: *
0020: * Free Software Foundation, Inc.,
0021: * 59 Temple Place - Suite 330,
0022: * Boston, MA USA 02111-1307
0023: *
0024: *
0025: *
0026: *
0027: * TextReportElement.java
0028: *
0029: * Created on 28 febbraio 2003, 19.20
0030: *
0031: */
0032:
0033: package it.businesslogic.ireport;
0034:
0035: import it.businesslogic.ireport.gui.*;
0036: import it.businesslogic.ireport.util.*;
0037: import java.awt.*;
0038: import java.awt.image.*;
0039: import java.awt.geom.*;
0040: import java.awt.font.*;
0041: import java.util.*;
0042: import java.util.List;
0043: import java.text.AttributedCharacterIterator;
0044: import java.text.AttributedString;
0045:
0046: public abstract class TextReportElement extends ReportElement implements
0047: BoxElement {
0048: static public Rotation ROTATION_NONE;
0049: static public Rotation ROTATION_LEFT;
0050: static public Rotation ROTATION_RIGHT;
0051: static public Rotation ROTATION_UPSIDEDOWN;
0052:
0053: static {
0054: ROTATION_NONE = new Rotation("None", 0);
0055: ROTATION_LEFT = new Rotation("Left", 1);
0056: ROTATION_RIGHT = new Rotation("Right", 2);
0057: ROTATION_UPSIDEDOWN = new Rotation("UpsideDown", 3);
0058: }
0059:
0060: private String text = "";
0061:
0062: static public String ALIGN = "ALIGN";
0063: static public String VERTICAL_ALIGN = "VERTICAL_ALIGN";
0064: static public String IS_STYLED_TEXT = "IS_STYLED_TEXT";
0065: static public String LINE_SPACING = "LINE_SPACING";
0066: static public String ROTATE = "ROTATE";
0067: static public String MARKUP = "MARKUP";
0068:
0069: static public String DEFAULT_ALIGN = "Left";
0070: static public String DEFAULT_VERTICAL_ALIGN = "Top";
0071: static public boolean DEFAULT_IS_STYLED_TEXT = false;
0072: static public String DEFAULT_LINE_SPACING = "Single";
0073: static public String DEFAULT_ROTATE = "None";
0074: static public String DEFAULT_MARKUP = null;
0075:
0076: //private String reportFont="";
0077: //private String fontName="";
0078: //private String PDFFontName="";
0079: //private int fontSize=10;
0080: //private String TTFFont="";
0081: //private boolean bold=false;
0082: //private boolean underline=false;
0083: //private boolean italic=false;
0084: //private boolean strikeTrought=false;
0085: //private String lineSpacing="";
0086: //private String align="";
0087: //private String verticalAlign="";
0088: //private boolean pdfEmbedded=false;
0089: //private String pdfEncoding="";
0090:
0091: private java.awt.Font font = null;
0092:
0093: //private String rotate = "None";
0094: //private boolean isStyledText = false;
0095:
0096: private Box box = null;
0097: private IReportFont iReportFont;
0098:
0099: public TextReportElement(int x, int y, int width, int height) {
0100: super (x, y, width, height);
0101:
0102: //see net.sf.jasperreports.engine.base.JRBaseFont
0103: //setGraphicElementPen("Thin");
0104: //this.bgcolor = Color.WHITE;
0105: //this.fgcolor = Color.BLACK;
0106: this .text = "Static text\nsecond line";
0107: //this.verticalAlign = "Top";
0108: //this.align = "Left";
0109: //this.lineSpacing = "Single";
0110: box = new Box();
0111: //set font
0112: transparentDefault = "Transparent";
0113: setIReportFont(new IReportFont());
0114:
0115: }
0116:
0117: public void setIReportFont(IReportFont newIreportFont) {
0118:
0119: if (newIreportFont == null) {
0120: this .iReportFont = new IReportFont();
0121: } else {
0122: this .iReportFont = (IReportFont) newIreportFont.clone();
0123: }
0124: this .font = iReportFont.getJavaAWTFont();
0125: }
0126:
0127: /** Return a instance of IReportFont. */
0128: public IReportFont getIReportFont() {
0129: if (iReportFont == null)
0130: iReportFont = new IReportFont();
0131: return iReportFont;
0132: }
0133:
0134: public void drawObject(Graphics2D g, double zoom_factor,
0135: int x_shift_origin, int y_shift_origin) {
0136: //System.out.println(new java.util.Date() + " Print text " + this.getText() + " " + x_shift_origin +" " +y_shift_origin);
0137:
0138: position.x -= 10;
0139: position.y -= 10;
0140: x_shift_origin -= 10;
0141: y_shift_origin -= 10;
0142:
0143: this .zoom_factor = zoom_factor;
0144:
0145: g.setColor(getBgcolor());
0146:
0147: if (!getTransparent().equalsIgnoreCase("Transparent")) {
0148: g.fillRect(getZoomedDim(position.x) - x_shift_origin,
0149: getZoomedDim(position.y) - y_shift_origin,
0150: getZoomedDim(width), getZoomedDim(height));
0151: }
0152: g.setColor(getFgcolor());
0153:
0154: // Set font to default font....
0155: //Font oldFont = g.getFont();
0156:
0157: if (font == null) {
0158: int style = 0;
0159: if (isBold())
0160: style |= Font.BOLD;
0161: if (isItalic())
0162: style |= Font.ITALIC;
0163: if (style == 0)
0164: style = Font.PLAIN;
0165: font = new Font(this .getFontName(), style,
0166: getZoomedDim(this .getFontSize()));
0167: }
0168:
0169: // Code for rotation by gt (taked by jasperreports...
0170:
0171: int gfx_x = getZoomedDim(position.x + getBox().getLeftPadding())
0172: - x_shift_origin;
0173: int gfx_y = getZoomedDim(position.y + getBox().getTopPadding())
0174: - y_shift_origin;
0175: int gfx_width = getZoomedDim(width - getBox().getLeftPadding()
0176: - getBox().getRightPadding());
0177: int gfx_height = getZoomedDim(height - getBox().getTopPadding()
0178: - getBox().getBottomPadding());
0179:
0180: /*
0181: Java2DUtil.setClip(g,
0182: gfx_x,
0183: gfx_y,
0184: gfx_width,
0185: gfx_height);
0186: */
0187:
0188: double angle = 0;
0189: double angle_restore = 0;
0190:
0191: // Apply the transformation "rotation"
0192: // - Do nothing when rotate = "none"
0193: AffineTransform transform = null;
0194: AffineTransform savedAT = g.getTransform();
0195:
0196: if (getRotate().equals(ROTATION_LEFT.getName())) {
0197: transform = g.getTransform();
0198: transform.rotate(-Math.PI / 2, gfx_x, gfx_y);
0199: transform.translate(-gfx_height, -gfx_height);
0200: gfx_y = gfx_y + gfx_height;
0201: gfx_height = getZoomedDim(width - getBox().getLeftPadding()
0202: - getBox().getRightPadding());
0203: gfx_width = getZoomedDim(height - getBox().getTopPadding()
0204: - getBox().getBottomPadding());
0205:
0206: } else if (getRotate().equals(ROTATION_RIGHT.getName())) {
0207: transform = g.getTransform();
0208:
0209: transform.rotate(Math.PI / 2, gfx_x, gfx_y);
0210: transform.translate(-gfx_width, -gfx_width);
0211: gfx_x = gfx_x + gfx_width;
0212: gfx_width = getZoomedDim(height);
0213: gfx_height = getZoomedDim(width - getBox().getLeftPadding()
0214: - getBox().getRightPadding());
0215: gfx_width = getZoomedDim(height - getBox().getTopPadding()
0216: - getBox().getBottomPadding());
0217: } else if (getRotate().equals(ROTATION_UPSIDEDOWN.getName())) {
0218: transform = g.getTransform();
0219:
0220: transform.rotate(Math.PI, gfx_x, gfx_y);
0221: transform.translate(-gfx_width, -gfx_height);
0222: //gfx_x = gfx_x + gfx_width;
0223: //gfx_y = gfx_y + gfx_height;
0224: //gfx_width = getZoomedDim(height);
0225: //gfx_height =
0226: //gfx_width = getZoomedDim(height - getBox().getTopPadding() - getBox().getBottomPadding());
0227:
0228: }
0229:
0230: if (this .getText() != null && this .getText().length() > 0) {
0231: drawText(g, transform, gfx_x, gfx_y, gfx_height, gfx_width);
0232: }
0233:
0234: //g.setClip(null);
0235: //g.setClip(orgClipBounds);
0236:
0237: // ----- Java2DUtil.resetClip(g);
0238:
0239: position.x += 10;
0240: position.y += 10;
0241: x_shift_origin += 10;
0242: y_shift_origin += 10;
0243:
0244: drawBorder(g, zoom_factor, x_shift_origin, y_shift_origin);
0245:
0246: Box tmpBox = box == null ? new Box() : box;
0247: if (getStyle() != null && getStyle().getBox() != null) {
0248: tmpBox = box.derive(getStyle().getBox());
0249: }
0250: drawBorder(g, zoom_factor, x_shift_origin, y_shift_origin,
0251: tmpBox);
0252: //drawGraphicsElement(g,this.getGraphicElementPen(), zoom_factor, x_shift_origin,y_shift_origin, 0);
0253: }
0254:
0255: /** Getter for property align.
0256: * @return Value of property align.
0257: *
0258: */
0259: public java.lang.String getAlign() {
0260: if (getPropertyValue(ALIGN) == null) {
0261: // Look for a fgcolor in the stylesheet...
0262: if (getStyle() != null) {
0263: return getStyle().getAttributeString(
0264: getStyle().ATTRIBUTE_hAlign, DEFAULT_ALIGN,
0265: true);
0266: }
0267: }
0268: return getStringValue(ALIGN, DEFAULT_ALIGN);
0269: }
0270:
0271: /** Setter for property align.
0272: * @param align New value of property align.
0273: *
0274: */
0275: public void setAlign(java.lang.String align) {
0276: this .setPropertyValue(ALIGN, align);
0277: }
0278:
0279: /** Getter for property bold.
0280: * @return Value of property bold.
0281: *
0282: */
0283: public boolean isBold() {
0284: if (getIReportFont().getPropertyValue(IReportFont.IS_BOLD) == null) {
0285: // Look for a fgcolor in the stylesheet...
0286: if (getStyle() != null) {
0287: return getStyle().getAttributeBoolean(
0288: getStyle().ATTRIBUTE_isBold,
0289: IReportFont.DEFAULT_IS_BOLD, true);
0290: }
0291: }
0292: return getIReportFont().getBooleanValue(IReportFont.IS_BOLD,
0293: IReportFont.DEFAULT_IS_BOLD);
0294: }
0295:
0296: /** Setter for property bold.
0297: * @param bold New value of property bold.
0298: *
0299: */
0300: public void setBold(boolean bold) {
0301: getIReportFont().setPropertyValue(IReportFont.IS_BOLD,
0302: "" + bold);
0303: }
0304:
0305: /** Getter for property font.
0306: * @return Value of property font.
0307: *
0308: */
0309: public java.awt.Font getFont() {
0310: return font;
0311: }
0312:
0313: /** Setter for property font.
0314: * @param font New value of property font.
0315: *
0316: */
0317: public void setFont(java.awt.Font font) {
0318: this .font = font;
0319: }
0320:
0321: /** Getter for property fontName.
0322: * @return Value of property fontName.
0323: *
0324: */
0325: public java.lang.String getFontName() {
0326: if (getIReportFont().getPropertyValue(IReportFont.FONT_NAME) == null) {
0327: // Look for a fgcolor in the stylesheet...
0328: if (getStyle() != null) {
0329: return getStyle().getAttributeString(
0330: getStyle().ATTRIBUTE_fontName,
0331: IReportFont.DEFAULT_FONT_NAME, true);
0332: }
0333: }
0334: return getIReportFont().getStringValue(IReportFont.FONT_NAME,
0335: IReportFont.DEFAULT_FONT_NAME);
0336: }
0337:
0338: /** Setter for property fontName.
0339: * @param fontName New value of property fontName.
0340: *
0341: */
0342: public void setFontName(java.lang.String fontName) {
0343: getIReportFont().setPropertyValue(IReportFont.FONT_NAME,
0344: fontName);
0345: }
0346:
0347: /** Getter for property fontSize.
0348: * @return Value of property fontSize.
0349: *
0350: */
0351: public int getFontSize() {
0352: if (getIReportFont().getPropertyValue(IReportFont.FONT_SIZE) == null) {
0353: // Look for a fgcolor in the stylesheet...
0354: if (getStyle() != null) {
0355: return getStyle().getAttributeInteger(
0356: getStyle().ATTRIBUTE_fontSize,
0357: IReportFont.DEFAULT_FONT_SIZE, true);
0358: }
0359: }
0360: return getIReportFont().getIntValue(IReportFont.FONT_SIZE,
0361: IReportFont.DEFAULT_FONT_SIZE);
0362: }
0363:
0364: /** Setter for property fontSize.
0365: * @param fontSize New value of property fontSize.
0366: *
0367: */
0368: public void setFontSize(int fontSize) {
0369: getIReportFont().setPropertyValue(IReportFont.FONT_SIZE,
0370: "" + fontSize);
0371: }
0372:
0373: /** Setter for property fontSize.
0374: * @param fontSize New value of property fontSize.
0375: *
0376: */
0377: public void modifyFontSize(int delta) {
0378: int newFonsSize = getFontSize() + delta;
0379: if (newFonsSize >= 5)
0380: this .setFontSize(newFonsSize);
0381: }
0382:
0383: /** Getter for property italic.
0384: * @return Value of property italic.
0385: *
0386: */
0387: public boolean isItalic() {
0388: if (getIReportFont().getPropertyValue(IReportFont.IS_ITALIC) == null) {
0389: // Look for a fgcolor in the stylesheet...
0390: if (getStyle() != null) {
0391: return getStyle().getAttributeBoolean(
0392: getStyle().ATTRIBUTE_isItalic,
0393: IReportFont.DEFAULT_IS_ITALIC, true);
0394: }
0395: }
0396: return getIReportFont().getBooleanValue(IReportFont.IS_ITALIC,
0397: IReportFont.DEFAULT_IS_ITALIC);
0398: }
0399:
0400: /** Setter for property italic.
0401: * @param italic New value of property italic.
0402: *
0403: */
0404: public void setItalic(boolean italic) {
0405: getIReportFont().setPropertyValue(IReportFont.IS_ITALIC,
0406: "" + italic);
0407: }
0408:
0409: /** Getter for property lineSpacing.
0410: * @return Value of property lineSpacing.
0411: *
0412: */
0413: public java.lang.String getLineSpacing() {
0414: if (getPropertyValue(LINE_SPACING) == null) {
0415: // Look for a fgcolor in the stylesheet...
0416: if (getStyle() != null) {
0417: return getStyle().getAttributeString(
0418: getStyle().ATTRIBUTE_lineSpacing,
0419: DEFAULT_LINE_SPACING, true);
0420: }
0421: }
0422: return getStringValue(LINE_SPACING, DEFAULT_LINE_SPACING);
0423: }
0424:
0425: /** Setter for property lineSpacing.
0426: * @param lineSpacing New value of property lineSpacing.
0427: *
0428: */
0429: public void setLineSpacing(java.lang.String lineSpacing) {
0430: setPropertyValue(LINE_SPACING, lineSpacing);
0431: }
0432:
0433: /** Getter for property pdfEmbedded.
0434: * @return Value of property pdfEmbedded.
0435: *
0436: */
0437: public boolean isPdfEmbedded() {
0438: if (getIReportFont().getPropertyValue(
0439: IReportFont.IS_PDF_EMBEDDED) == null) {
0440: // Look for a fgcolor in the stylesheet...
0441: if (getStyle() != null) {
0442: return getStyle().getAttributeBoolean(
0443: getStyle().ATTRIBUTE_isPdfEmbedded,
0444: IReportFont.DEFAULT_IS_PDF_EMBEDDED, true);
0445: }
0446: }
0447: return getIReportFont().getBooleanValue(
0448: IReportFont.IS_PDF_EMBEDDED,
0449: IReportFont.DEFAULT_IS_PDF_EMBEDDED);
0450: }
0451:
0452: /** Setter for property pdfEmbedded.
0453: * @param pdfEmbedded New value of property pdfEmbedded.
0454: *
0455: */
0456: public void setPdfEmbedded(boolean pdfEmbedded) {
0457: getIReportFont().setPropertyValue(IReportFont.IS_PDF_EMBEDDED,
0458: "" + pdfEmbedded);
0459: }
0460:
0461: /** Getter for property pdfEncoding.
0462: * @return Value of property pdfEncoding.
0463: *
0464: */
0465: public java.lang.String getPdfEncoding() {
0466: if (getIReportFont().getPropertyValue(IReportFont.PDF_ENCODING) == null) {
0467: // Look for a fgcolor in the stylesheet...
0468: if (getStyle() != null) {
0469: return getStyle().getAttributeString(
0470: getStyle().ATTRIBUTE_pdfEncoding,
0471: IReportFont.DEFAULT_PDF_ENCODING, true);
0472: }
0473: }
0474: return getIReportFont().getStringValue(
0475: IReportFont.PDF_ENCODING,
0476: IReportFont.DEFAULT_PDF_ENCODING);
0477: }
0478:
0479: /** Setter for property pdfEncoding.
0480: * @param pdfEncoding New value of property pdfEncoding.
0481: *
0482: */
0483: public void setPdfEncoding(java.lang.String pdfEncoding) {
0484: getIReportFont().setPropertyValue(IReportFont.PDF_ENCODING,
0485: pdfEncoding);
0486: }
0487:
0488: /** Getter for property PDFFontName.
0489: * @return Value of property PDFFontName.
0490: *
0491: */
0492: public java.lang.String getPDFFontName() {
0493: if (getIReportFont()
0494: .getPropertyValue(IReportFont.PDF_FONT_NAME) == null) {
0495: // Look for a fgcolor in the stylesheet...
0496: if (getStyle() != null) {
0497: return getStyle().getAttributeString(
0498: getStyle().ATTRIBUTE_pdfFontName,
0499: IReportFont.DEFAULT_PDF_FONT_NAME, true);
0500: }
0501: }
0502: return getIReportFont().getStringValue(
0503: IReportFont.PDF_FONT_NAME,
0504: IReportFont.DEFAULT_PDF_FONT_NAME);
0505: }
0506:
0507: /** Setter for property PDFFontName.
0508: * @param PDFFontName New value of property PDFFontName.
0509: *
0510: */
0511: public void setPDFFontName(java.lang.String PDFFontName) {
0512: getIReportFont().setPropertyValue(IReportFont.PDF_FONT_NAME,
0513: PDFFontName);
0514: }
0515:
0516: /** Getter for property reportFont.
0517: * @return Value of property reportFont.
0518: *
0519: */
0520: public java.lang.String getReportFont() {
0521: return getIReportFont().getStringValue(IReportFont.REPORT_FONT,
0522: IReportFont.DEFAULT_REPORT_FONT);
0523: }
0524:
0525: /** Setter for property reportFont.
0526: * @param reportFont New value of property reportFont.
0527: *
0528: */
0529: public void setReportFont(java.lang.String reportFont) {
0530: getIReportFont().setPropertyValue(IReportFont.REPORT_FONT,
0531: reportFont);
0532: }
0533:
0534: /** Getter for property strikeTrought.
0535: * @return Value of property strikeTrought.
0536: *
0537: */
0538: public boolean isStrikeTrought() {
0539: if (getIReportFont().getPropertyValue(
0540: IReportFont.IS_STRIKETROUGHT) == null) {
0541: // Look for a fgcolor in the stylesheet...
0542: if (getStyle() != null) {
0543: return getStyle().getAttributeBoolean(
0544: getStyle().ATTRIBUTE_isStrikeThrough,
0545: IReportFont.DEFAULT_IS_STRIKETROUGHT, true);
0546: }
0547: }
0548: return getIReportFont().getBooleanValue(
0549: IReportFont.IS_STRIKETROUGHT,
0550: IReportFont.DEFAULT_IS_STRIKETROUGHT);
0551: }
0552:
0553: /** Setter for property strikeTrought.
0554: * @param strikeTrought New value of property strikeTrought.
0555: *
0556: */
0557: public void setStrikeTrought(boolean strikeTrought) {
0558: getIReportFont().setPropertyValue(IReportFont.IS_STRIKETROUGHT,
0559: "" + strikeTrought);
0560: }
0561:
0562: /** Getter for property text.
0563: * @return Value of property text.
0564: *
0565: */
0566: public java.lang.String getText() {
0567: return text;
0568: }
0569:
0570: /** Setter for property text.
0571: * @param text New value of property text.
0572: *
0573: */
0574: public void setText(java.lang.String text) {
0575: this .text = text;
0576: }
0577:
0578: /** Getter for property TTFFont.
0579: * @return Value of property TTFFont.
0580: *
0581: */
0582: public java.lang.String getTTFFont() {
0583: if (getIReportFont()
0584: .getPropertyValue(IReportFont.TTF_FONT_NAME) == null) {
0585: // Look for a fgcolor in the stylesheet...
0586: if (getStyle() != null) {
0587: return getStyle().getAttributeString(
0588: getStyle().ATTRIBUTE_pdfFontName,
0589: IReportFont.DEFAULT_TTF_FONT_NAME, true);
0590: }
0591: }
0592: return getIReportFont().getStringValue(
0593: IReportFont.TTF_FONT_NAME,
0594: IReportFont.DEFAULT_TTF_FONT_NAME);
0595: }
0596:
0597: /** Setter for property TTFFont.
0598: * @param TTFFont New value of property TTFFont.
0599: *
0600: */
0601: public void setTTFFont(java.lang.String TTFFont) {
0602: getIReportFont().setPropertyValue(IReportFont.TTF_FONT_NAME,
0603: TTFFont);
0604: }
0605:
0606: /** Getter for property underline.
0607: * @return Value of property underline.
0608: *
0609: */
0610: public boolean isUnderline() {
0611: if (getIReportFont().getPropertyValue(IReportFont.IS_UNDERLINE) == null) {
0612: // Look for a fgcolor in the stylesheet...
0613: if (getStyle() != null) {
0614: return getStyle().getAttributeBoolean(
0615: getStyle().ATTRIBUTE_isUnderline,
0616: IReportFont.DEFAULT_IS_UNDERLINE, true);
0617: }
0618: }
0619: return getIReportFont().getBooleanValue(
0620: IReportFont.IS_UNDERLINE,
0621: IReportFont.DEFAULT_IS_UNDERLINE);
0622: }
0623:
0624: /** Setter for property underline.
0625: * @param underline New value of property underline.
0626: *
0627: */
0628: public void setUnderline(boolean underline) {
0629: getIReportFont().setPropertyValue(IReportFont.IS_UNDERLINE,
0630: "" + underline);
0631: }
0632:
0633: /** Getter for property verticalAlign.
0634: * @return Value of property verticalAlign.
0635: *
0636: */
0637: public java.lang.String getVerticalAlign() {
0638: if (getPropertyValue(VERTICAL_ALIGN) == null) {
0639: // Look for a fgcolor in the stylesheet...
0640: if (getStyle() != null) {
0641: return getStyle().getAttributeString(
0642: getStyle().ATTRIBUTE_vAlign,
0643: DEFAULT_VERTICAL_ALIGN, true);
0644: }
0645: }
0646: return getStringValue(VERTICAL_ALIGN, DEFAULT_VERTICAL_ALIGN);
0647: }
0648:
0649: /** Setter for property verticalAlign.
0650: * @param verticalAlign New value of property verticalAlign.
0651: *
0652: */
0653: public void setVerticalAlign(java.lang.String verticalAlign) {
0654: setPropertyValue(VERTICAL_ALIGN, verticalAlign);
0655: }
0656:
0657: /** Getter for property Rotate.
0658: * Rotate can be one of the values: "none", "left", "right"
0659: * @return Value of property Rotate.
0660: */
0661: public java.lang.String getRotate() {
0662: if (getPropertyValue(ROTATE) == null) {
0663: // Look for a fgcolor in the stylesheet...
0664: if (getStyle() != null) {
0665: return getStyle().getAttributeString(
0666: getStyle().ATTRIBUTE_rotation, DEFAULT_ROTATE,
0667: true);
0668: }
0669: }
0670: return getStringValue(ROTATE, DEFAULT_ROTATE);
0671: }
0672:
0673: /** Setter for property Rotate.
0674: * @param Rotate New value of property Rotate.
0675: *
0676: */
0677: public void setRotate(java.lang.String rotate) {
0678: setPropertyValue(ROTATE, rotate);
0679: }
0680:
0681: public int getTextHeight(java.awt.FontMetrics fm) {
0682: //
0683: return fm.getAscent() + (this .getLineCount() - 1)
0684: * (fm.getHeight());
0685: }
0686:
0687: public int getLineCount() {
0688: // For any \n, return a line...
0689: String text = this .getText();
0690: text = text.replace('\r', ' ');
0691: int line = 1;
0692: while (text.indexOf('\n') > 0) {
0693: line++;
0694: text = text.substring(text.indexOf('\n') + 1);
0695: }
0696: return line;
0697: }
0698:
0699: public void copyBaseReportElement(ReportElement destination,
0700: ReportElement source) {
0701: super .copyBaseReportElement(destination, source);
0702:
0703: if (destination instanceof TextReportElement
0704: && source instanceof TextReportElement) {
0705: TextReportElement tre = (TextReportElement) source;
0706: TextReportElement tred = (TextReportElement) destination;
0707: source.clone(tred);
0708: tre.getIReportFont().clone(tred.getIReportFont());
0709: /*
0710: ((TextReportElement)destination).setAlign(new String( ((TextReportElement)source).getAlign() ));
0711: ((TextReportElement)destination).setReportFont(((TextReportElement)source).getReportFont());
0712: ((TextReportElement)destination).setFontName(new String( ((TextReportElement)source).getFontName() ));
0713: ((TextReportElement)destination).setPDFFontName(new String( ((TextReportElement)source).getPDFFontName() ));
0714: ((TextReportElement)destination).setFontSize( ((TextReportElement)source).getFontSize() );
0715: ((TextReportElement)destination).setTTFFont(new String( ((TextReportElement)source).getTTFFont() ));
0716: ((TextReportElement)destination).setBold( ((TextReportElement)source).isBold() );
0717: ((TextReportElement)destination).setItalic( ((TextReportElement)source).isItalic() );
0718: ((TextReportElement)destination).setUnderline( ((TextReportElement)source).isUnderline() );
0719: ((TextReportElement)destination).setStrikeTrought( ((TextReportElement)source).isStrikeTrought() );
0720: ((TextReportElement)destination).setLineSpacing(new String( ((TextReportElement)source).getLineSpacing() ));
0721: ((TextReportElement)destination).setVerticalAlign(new String( ((TextReportElement)source).getVerticalAlign() ));
0722: ((TextReportElement)destination).setPdfEmbedded( ((TextReportElement)source).isPdfEmbedded());
0723: ((TextReportElement)destination).setPdfEncoding(new String( ((TextReportElement)source).getPdfEncoding() ));
0724: */
0725: ((TextReportElement) destination).setText(new String(
0726: ((TextReportElement) source).getText()));
0727: ((TextReportElement) destination)
0728: .setBox(((TextReportElement) source).getBox()
0729: .cloneMe());
0730: }
0731: }
0732:
0733: private class TextReportElementLayout {
0734: private TextLayout layout;
0735: private float x;
0736: private float y;
0737:
0738: private TextReportElementLayout(TextLayout layout, float x,
0739: float y) {
0740: this .layout = layout;
0741: this .x = x;
0742: this .y = y;
0743: }
0744:
0745: void drawWithOffset(Graphics2D g2, float yOffset) {
0746: layout.draw(g2, x, y + yOffset);
0747: }
0748: }
0749:
0750: static List getRotations() {
0751: return Rotation.rotations;
0752: }
0753:
0754: /** Getter for property isStyledText.
0755: * @return Value of property isStyledText.
0756: *
0757: */
0758: public boolean isIsStyledText() {
0759: if (getPropertyValue(IS_STYLED_TEXT) == null) {
0760: // Look for a fgcolor in the stylesheet...
0761: if (getStyle() != null) {
0762: return getStyle().getAttributeBoolean(
0763: getStyle().ATTRIBUTE_isStyledText,
0764: DEFAULT_IS_STYLED_TEXT, true);
0765: }
0766: }
0767: return getBooleanValue(IS_STYLED_TEXT, DEFAULT_IS_STYLED_TEXT);
0768: }
0769:
0770: /** Setter for property isStyledText.
0771: * @param isStyledText New value of property isStyledText.
0772: *
0773: */
0774: public void setIsStyledText(boolean isStyledText) {
0775: setPropertyValue(IS_STYLED_TEXT, "" + isStyledText);
0776: }
0777:
0778: static class Rotation {
0779: static private ArrayList rotations;
0780: private String name;
0781: private int number;
0782:
0783: Rotation(String name, int number) {
0784: this .name = name;
0785: this .number = number;
0786: rotations = new ArrayList();
0787: rotations.add(this );
0788: }
0789:
0790: public int getNumber() {
0791: return number;
0792: }
0793:
0794: public String getName() {
0795: return name;
0796: }
0797:
0798: public String toString() {
0799: return getName();
0800: }
0801: }
0802:
0803: public Box getBox() {
0804: return box;
0805: }
0806:
0807: public void setBox(Box box) {
0808: this .box = box;
0809: }
0810:
0811: public void setStyle(Style style) {
0812:
0813: super .setStyle(style);
0814:
0815: if (style != null) {
0816: //this.setAlign( style.getAttributeString( style.ATTRIBUTE_hAlign, getAlign(), true));
0817: //this.setFontName( style.getAttributeString( style.ATTRIBUTE_fontName, getFontName(), true));
0818: //this.setPDFFontName( style.getAttributeString( style.ATTRIBUTE_pdfFontName, getPDFFontName(), true));
0819: //this.setFontSize( Integer.parseInt( style.getAttributeString( style.ATTRIBUTE_fontSize, getFontSize()+"", true)));
0820: //this.setTTFFont( style.getAttributeString( style.ATTRIBUTE_pdfFontName, getTTFFont(), true));
0821: //this.setBold( style.getAttributeBoolean( style.ATTRIBUTE_isBold, isBold(), true));
0822: //this.setItalic(style.getAttributeBoolean( style.ATTRIBUTE_isItalic, isItalic(), true));
0823: //this.setUnderline(style.getAttributeBoolean( style.ATTRIBUTE_isUnderline, isUnderline(), true));
0824: //this.setStrikeTrought(style.getAttributeBoolean( style.ATTRIBUTE_isStrikeThrough, isStrikeTrought(), true));
0825: //this.setLineSpacing( style.getAttributeString( style.ATTRIBUTE_lineSpacing, getLineSpacing(), true));
0826: //this.setVerticalAlign( style.getAttributeString( style.ATTRIBUTE_vAlign, getVerticalAlign(), true));
0827: //this.setPdfEmbedded( style.getAttributeBoolean( style.ATTRIBUTE_isPdfEmbedded, isPdfEmbedded(), true));
0828: //this.setPdfEncoding( style.getAttributeString( style.ATTRIBUTE_pdfEncoding, getPdfEncoding(), true));
0829: //this.setRotate(style.getAttributeString( style.ATTRIBUTE_rotation, getRotate(), true) );
0830:
0831: // BOX
0832:
0833: if (style.getBox() != null) {
0834: setBox(style.getBox().derive(getBox()));
0835: getBox().setPadding(style.getBox().getPadding());
0836: getBox()
0837: .setLeftPadding(style.getBox().getLeftPadding());
0838: getBox().setRightPadding(
0839: style.getBox().getRightPadding());
0840: getBox().setTopPadding(style.getBox().getTopPadding());
0841: getBox().setBottomPadding(
0842: style.getBox().getBottomPadding());
0843: }
0844:
0845: /*
0846: if (style.getAttributeString(style.ATTRIBUTE_border, null, true) != null)
0847: this.getBox().setBorder( style.getAttributeString(style.ATTRIBUTE_border, null, true) );
0848: if (style.getAttributeColor(style.ATTRIBUTE_borderColor, null, true) != null)
0849: this.getBox().setBorderColor( style.getAttributeColor(style.ATTRIBUTE_borderColor, null, true));
0850: if (style.getAttributeString(style.ATTRIBUTE_padding, null, true) != null)
0851: this.getBox().setPadding( Integer.parseInt( style.getAttributeString(style.ATTRIBUTE_padding, null, true) ));
0852:
0853: if (style.getAttributeString(style.ATTRIBUTE_topBorder, null, true) != null)
0854: this.getBox().setTopBorder( style.getAttributeString(style.ATTRIBUTE_topBorder, null, true) );
0855: if (style.getAttributeColor(style.ATTRIBUTE_topBorderColor, null, true) != null)
0856: this.getBox().setTopBorderColor( style.getAttributeColor(style.ATTRIBUTE_topBorderColor, null, true));
0857: if (style.getAttributeString(style.ATTRIBUTE_topPadding, null, true) != null)
0858: this.getBox().setTopPadding( Integer.parseInt( style.getAttributeString(style.ATTRIBUTE_topPadding, null, true) ));
0859:
0860: if (style.getAttributeString(style.ATTRIBUTE_leftBorder, null, true) != null)
0861: this.getBox().setLeftBorder( style.getAttributeString(style.ATTRIBUTE_leftBorder, null, true) );
0862: if (style.getAttributeColor(style.ATTRIBUTE_leftBorderColor, null, true) != null)
0863: this.getBox().setLeftBorderColor( style.getAttributeColor(style.ATTRIBUTE_leftBorderColor, null, true));
0864: if (style.getAttributeString(style.ATTRIBUTE_leftPadding, null, true) != null)
0865: this.getBox().setLeftPadding( Integer.parseInt( style.getAttributeString(style.ATTRIBUTE_leftPadding, null, true) ));
0866:
0867: if (style.getAttributeString(style.ATTRIBUTE_rightBorder, null, true) != null)
0868: this.getBox().setRightBorder( style.getAttributeString(style.ATTRIBUTE_rightBorder, null, true) );
0869: if (style.getAttributeColor(style.ATTRIBUTE_rightBorderColor, null, true) != null)
0870: this.getBox().setRightBorderColor( style.getAttributeColor(style.ATTRIBUTE_rightBorderColor, null, true));
0871: if (style.getAttributeString(style.ATTRIBUTE_rightPadding, null, true) != null)
0872: this.getBox().setRightPadding( Integer.parseInt( style.getAttributeString(style.ATTRIBUTE_rightPadding, null, true) ));
0873:
0874: if (style.getAttributeString(style.ATTRIBUTE_bottomBorder, null, true) != null)
0875: this.getBox().setBottomBorder( style.getAttributeString(style.ATTRIBUTE_bottomBorder, null, true) );
0876: if (style.getAttributeColor(style.ATTRIBUTE_bottomBorderColor, null, true) != null)
0877: this.getBox().setBottomBorderColor( style.getAttributeColor(style.ATTRIBUTE_bottomBorderColor, null, true));
0878: if (style.getAttributeString(style.ATTRIBUTE_bottomPadding, null, true) != null)
0879: this.getBox().setBottomPadding( Integer.parseInt( style.getAttributeString(style.ATTRIBUTE_bottomPadding, null, true) ));
0880: */
0881: }
0882: }
0883:
0884: private void drawText(Graphics2D g, AffineTransform transform,
0885: int gfx_x, int gfx_y, int gfx_height, int gfx_width) {
0886: int zoomedFieldHeight = gfx_height;
0887: String allText = Misc.treatNewLineChars(this .getText());
0888: float formatWidth = (float) gfx_width;
0889: float verticalOffset = 0f;
0890: TextReportElementLayout textReportElementLayout;
0891:
0892: ArrayList textLayouts;
0893: float x, y;
0894:
0895: FontRenderContext fontRenderContext = g.getFontRenderContext();
0896: java.util.Map fontAttributes = font.getAttributes();
0897: fontAttributes.put(TextAttribute.SIZE, new Float(
0898: getZoomedDim(this .getFontSize())));
0899: fontAttributes.put(TextAttribute.FAMILY, this .getFontName());
0900: if (this .isBold()) {
0901: fontAttributes.put(TextAttribute.WEIGHT,
0902: TextAttribute.WEIGHT_BOLD);
0903: }
0904: if (this .isItalic()) {
0905: fontAttributes.put(TextAttribute.POSTURE,
0906: TextAttribute.POSTURE_OBLIQUE);
0907: }
0908: if (this .isUnderline()) {
0909: fontAttributes.put(TextAttribute.UNDERLINE,
0910: TextAttribute.UNDERLINE_ON);
0911: }
0912: if (this .isStrikeTrought()) {
0913: fontAttributes.put(TextAttribute.STRIKETHROUGH,
0914: TextAttribute.STRIKETHROUGH_ON);
0915: }
0916:
0917: float lineSpacing = 1f;
0918: if (this .getLineSpacing().equals("Single"))
0919: lineSpacing = 1f;
0920: else if (this .getLineSpacing().equals("1_1_2"))
0921: lineSpacing = 1.5f;
0922: else if (this .getLineSpacing().equals("Double"))
0923: lineSpacing = 2f;
0924:
0925: AttributedString atext;
0926: AttributedCharacterIterator paragraph;
0927: int paragraphStart;
0928: int paragraphEnd;
0929: LineBreakMeasurer lineMeasurer;
0930: TextLayout layout = null;
0931:
0932: String paragr_text = "";
0933: boolean isMaxHeightReached = false;
0934:
0935: StringTokenizer tkzer = new StringTokenizer(allText, "\n");
0936:
0937: float drawPosY = 0;
0938: float drawPosX = 0;
0939:
0940: paragr_text = "";
0941: isMaxHeightReached = false;
0942:
0943: tkzer = new StringTokenizer(allText, "\n");
0944:
0945: textLayouts = new ArrayList();
0946:
0947: // Calculate the layouts. (But don't draw yet because we don't know yet
0948: // the offset which is needed if we align the text "middle" or "bottom")
0949: while (tkzer.hasMoreTokens() && !isMaxHeightReached) {
0950: paragr_text = tkzer.nextToken();
0951:
0952: atext = new AttributedString(paragr_text, fontAttributes);
0953: paragraph = atext.getIterator();
0954: paragraphStart = paragraph.getBeginIndex();
0955: paragraphEnd = paragraph.getEndIndex();
0956: lineMeasurer = new LineBreakMeasurer(paragraph,
0957: fontRenderContext);
0958: lineMeasurer.setPosition(paragraphStart);
0959:
0960: layout = null;
0961: while (lineMeasurer.getPosition() < paragraphEnd
0962: && !isMaxHeightReached) {
0963: layout = lineMeasurer.nextLayout(formatWidth);
0964:
0965: drawPosY += layout.getLeading() + lineSpacing
0966: * layout.getAscent();
0967:
0968: if (drawPosY + layout.getDescent() <= zoomedFieldHeight + 1) {
0969: if (this .getAlign().equals("Justify")) {
0970: if (layout.isLeftToRight()) {
0971: drawPosX = 0;
0972: } else {
0973: drawPosX = formatWidth
0974: - layout.getAdvance();
0975: }
0976: if (lineMeasurer.getPosition() < paragraphEnd) {
0977: layout = layout
0978: .getJustifiedLayout(formatWidth);
0979: }
0980: } else if (this .getAlign().equals("Right")) {
0981: //if (layout.isLeftToRight())
0982: //{
0983: drawPosX = formatWidth - layout.getAdvance();
0984: //}
0985: //else
0986: //{
0987: // drawPosX = formatWidth;
0988: //}
0989: } else if (this .getAlign().equals("Center")) {
0990: drawPosX = (formatWidth - layout.getAdvance()) / 2;
0991: } else //if (this.getAlign().equals("Left"))
0992: {
0993: //if (layout.isLeftToRight())
0994: //{
0995: drawPosX = 0;
0996: //}
0997: //else
0998: //{
0999: // drawPosX = formatWidth - layout.getAdvance();
1000: //}
1001: }
1002:
1003: x = drawPosX + gfx_x; //getZoomedDim(position.x)-x_shift_origin;
1004: y = drawPosY + gfx_y; //getZoomedDim(position.y)-y_shift_origin;
1005: textReportElementLayout = new TextReportElementLayout(
1006: layout, x, y);
1007: textLayouts.add(textReportElementLayout);
1008:
1009: drawPosY += layout.getDescent();
1010: } else {
1011: drawPosY -= layout.getLeading() + lineSpacing
1012: * layout.getAscent();
1013: isMaxHeightReached = true;
1014: }
1015: }
1016: }
1017:
1018: // Calculate the offset when aligning the text.
1019: float textHeight = drawPosY;
1020: if (this .getVerticalAlign().equals("Top")) {
1021: verticalOffset = 0f;
1022: } else if (this .getVerticalAlign().equals("Middle")) {
1023: verticalOffset = ((float) zoomedFieldHeight - textHeight) / 2f;
1024: } else if (this .getVerticalAlign().equals("Bottom")) {
1025: verticalOffset = (float) zoomedFieldHeight
1026: - (float) textHeight;
1027: }
1028:
1029: // Put affine transformatio here!!!
1030:
1031: AffineTransform savedAT = g.getTransform();
1032: if (transform != null)
1033: g.setTransform(transform);
1034:
1035: // Now draw the text according to the calculated layouts.
1036: for (Iterator i = textLayouts.iterator(); i.hasNext();) {
1037: textReportElementLayout = (TextReportElementLayout) i
1038: .next();
1039: textReportElementLayout.drawWithOffset(g, verticalOffset);
1040: }
1041:
1042: if (transform != null) {
1043: g.setTransform(savedAT);
1044: }
1045:
1046: }
1047:
1048: /** Getter for property isStyledText.
1049: * @return Value of property isStyledText.
1050: *
1051: */
1052: public String getMarkup() {
1053: if (getPropertyValue(MARKUP) == null) {
1054: // Look for a fgcolor in the stylesheet...
1055: if (getStyle() != null) {
1056: return getStyle().getAttributeString(
1057: getStyle().ATTRIBUTE_markup, DEFAULT_MARKUP,
1058: true);
1059: }
1060: }
1061: return getStringValue(MARKUP, DEFAULT_MARKUP);
1062: }
1063:
1064: /** Setter for property isStyledText.
1065: * @param isStyledText New value of property isStyledText.
1066: *
1067: */
1068: public void setMarkup(String markup) {
1069: setPropertyValue(MARKUP, markup);
1070: }
1071: }
|