0001: /*
0002: * ============================================================================
0003: * GNU Lesser General Public License
0004: * ============================================================================
0005: *
0006: * JasperReports - Free Java report-generating library.
0007: * Copyright (C) 2001-2006 JasperSoft Corporation http://www.jaspersoft.com
0008: *
0009: * This library is free software; you can redistribute it and/or modify it
0010: * under the terms of the GNU Lesser General Public License as published by the
0011: * Free Software Foundation; either version 2.1 of the License, or (at your
0012: * option) any later version.
0013: *
0014: * This library is distributed in the hope that it will be useful, but WITHOUT
0015: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0016: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
0017: * for more details.
0018: *
0019: * You should have received a copy of the GNU Lesser General Public License
0020: * along with this library; if not, write to the Free Software Foundation,
0021: * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
0022: *
0023: * JasperSoft Corporation
0024: * 303 Second Street, Suite 450 North
0025: * San Francisco, CA 94107
0026: * http://www.jaspersoft.com
0027: */
0028:
0029: /*
0030: * Contributor: Manuel Paul <mpaul@ratundtat.com>,
0031: * Rat & Tat Beratungsgesellschaft mbH,
0032: * Muehlenkamp 6c,
0033: * 22303 Hamburg,
0034: * Germany.
0035: */
0036: package net.sf.jasperreports.engine.export;
0037:
0038: import java.awt.Color;
0039: import java.awt.Graphics2D;
0040: import java.awt.Rectangle;
0041: import java.awt.geom.Dimension2D;
0042: import java.awt.image.BufferedImage;
0043: import java.io.IOException;
0044: import java.io.OutputStream;
0045: import java.net.URL;
0046: import java.util.HashMap;
0047: import java.util.Map;
0048:
0049: import jxl.CellView;
0050: import jxl.JXLException;
0051: import jxl.SheetSettings;
0052: import jxl.Workbook;
0053: import jxl.biff.DisplayFormat;
0054: import jxl.format.Alignment;
0055: import jxl.format.BoldStyle;
0056: import jxl.format.Border;
0057: import jxl.format.BorderLineStyle;
0058: import jxl.format.Colour;
0059: import jxl.format.Orientation;
0060: import jxl.format.PageOrientation;
0061: import jxl.format.PaperSize;
0062: import jxl.format.Pattern;
0063: import jxl.format.RGB;
0064: import jxl.format.UnderlineStyle;
0065: import jxl.format.VerticalAlignment;
0066: import jxl.write.Blank;
0067: import jxl.write.DateTime;
0068: import jxl.write.Label;
0069: import jxl.write.Number;
0070: import jxl.write.WritableCellFormat;
0071: import jxl.write.WritableFont;
0072: import jxl.write.WritableHyperlink;
0073: import jxl.write.WritableImage;
0074: import jxl.write.WritableSheet;
0075: import jxl.write.WritableWorkbook;
0076: import jxl.write.WriteException;
0077: import jxl.write.biff.CellValue;
0078: import jxl.write.biff.RowsExceededException;
0079: import net.sf.jasperreports.engine.JRAlignment;
0080: import net.sf.jasperreports.engine.JRBox;
0081: import net.sf.jasperreports.engine.JRElement;
0082: import net.sf.jasperreports.engine.JRException;
0083: import net.sf.jasperreports.engine.JRFont;
0084: import net.sf.jasperreports.engine.JRGraphicElement;
0085: import net.sf.jasperreports.engine.JRHyperlink;
0086: import net.sf.jasperreports.engine.JRImage;
0087: import net.sf.jasperreports.engine.JRImageRenderer;
0088: import net.sf.jasperreports.engine.JRPrintElement;
0089: import net.sf.jasperreports.engine.JRPrintFrame;
0090: import net.sf.jasperreports.engine.JRPrintImage;
0091: import net.sf.jasperreports.engine.JRPrintLine;
0092: import net.sf.jasperreports.engine.JRPrintText;
0093: import net.sf.jasperreports.engine.JRRenderable;
0094: import net.sf.jasperreports.engine.JRReport;
0095: import net.sf.jasperreports.engine.JRTextElement;
0096: import net.sf.jasperreports.engine.JasperPrint;
0097: import net.sf.jasperreports.engine.export.data.BooleanTextValue;
0098: import net.sf.jasperreports.engine.export.data.DateTextValue;
0099: import net.sf.jasperreports.engine.export.data.NumberTextValue;
0100: import net.sf.jasperreports.engine.export.data.StringTextValue;
0101: import net.sf.jasperreports.engine.export.data.TextValue;
0102: import net.sf.jasperreports.engine.export.data.TextValueHandler;
0103: import net.sf.jasperreports.engine.util.JRImageLoader;
0104: import net.sf.jasperreports.engine.util.JRStyledText;
0105:
0106: import org.apache.commons.collections.ReferenceMap;
0107: import org.apache.commons.logging.Log;
0108: import org.apache.commons.logging.LogFactory;
0109:
0110: /**
0111: * @author Manuel Paul (mpaul@ratundtat.com)
0112: * @version $Id: JExcelApiExporter.java 1824 2007-08-23 14:19:12Z teodord $
0113: */
0114: public class JExcelApiExporter extends JRXlsAbstractExporter {
0115:
0116: private static final Log log = LogFactory
0117: .getLog(JExcelApiExporter.class);
0118:
0119: protected static final Colour WHITE = Colour.WHITE;
0120: protected static final Colour BLACK = Colour.BLACK;
0121:
0122: protected static final String EMPTY_SHEET_NAME = "Sheet1";
0123:
0124: private static Map colorsCache = new ReferenceMap();
0125:
0126: private static Colour[] FIXED_COLOURS = new Colour[] { WHITE,
0127: BLACK, Colour.PALETTE_BLACK, Colour.DEFAULT_BACKGROUND,
0128: Colour.DEFAULT_BACKGROUND1, Colour.AUTOMATIC,
0129: Colour.UNKNOWN };
0130:
0131: private Map loadedCellStyles = new HashMap();
0132:
0133: private WritableWorkbook workbook = null;
0134:
0135: private WritableSheet sheet = null;
0136:
0137: private Pattern backgroundMode = Pattern.SOLID;
0138:
0139: private Map numberFormats;
0140: private Map dateFormats;
0141:
0142: protected Map formatPatternsMap = null;
0143:
0144: protected boolean createCustomPalette;
0145: protected Map workbookColours = new HashMap();
0146: protected Map usedColours = new HashMap();
0147:
0148: public JExcelApiExporter() {
0149: numberFormats = new HashMap();
0150: dateFormats = new HashMap();
0151: }
0152:
0153: protected void setParameters() {
0154: super .setParameters();
0155:
0156: formatPatternsMap = (Map) getParameter(JRXlsExporterParameter.FORMAT_PATTERNS_MAP);
0157:
0158: createCustomPalette = getBooleanParameter(
0159: JExcelApiExporterParameter.CREATE_CUSTOM_PALETTE,
0160: JExcelApiExporterParameter.PROPERTY_CREATE_CUSTOM_PALETTE,
0161: false);
0162:
0163: if (createCustomPalette) {
0164: initCustomPalette();
0165: }
0166: }
0167:
0168: protected void initCustomPalette() {
0169: //mark "fixed" colours as always used
0170: for (int i = 0; i < FIXED_COLOURS.length; i++) {
0171: Colour colour = FIXED_COLOURS[i];
0172: setColourUsed(colour);
0173: }
0174: }
0175:
0176: protected void setColourUsed(Colour colour) {
0177: usedColours.put(colour, colour.getDefaultRGB());
0178: }
0179:
0180: protected void setColourUsed(Colour colour, Color reportColour) {
0181: if (log.isDebugEnabled()) {
0182: log.debug("Modifying palette colour " + colour.getValue()
0183: + " to " + reportColour);
0184: }
0185:
0186: int red = reportColour.getRed();
0187: int green = reportColour.getGreen();
0188: int blue = reportColour.getBlue();
0189:
0190: workbook.setColourRGB(colour, red, green, blue);
0191:
0192: RGB customRGB = new RGB(red, green, blue);
0193: usedColours.put(colour, customRGB);
0194: }
0195:
0196: protected void setBackground() {
0197: if (isWhitePageBackground) {
0198: this .backgroundMode = Pattern.SOLID;
0199: } else {
0200: this .backgroundMode = Pattern.NONE;
0201: }
0202: }
0203:
0204: protected void openWorkbook(OutputStream os) throws JRException {
0205: try {
0206: workbook = Workbook.createWorkbook(os);
0207: } catch (IOException e) {
0208: throw new JRException("Error generating XLS report : "
0209: + jasperPrint.getName(), e);
0210: }
0211: }
0212:
0213: protected void createSheet(String name) {
0214: sheet = workbook.createSheet(name, Integer.MAX_VALUE);
0215: setSheetSettings(sheet);
0216: }
0217:
0218: protected void closeWorkbook(OutputStream os) throws JRException {
0219: if (sheet == null)//empty document
0220: {
0221: //creating an empty sheet so that write() doesn't fail
0222: workbook.createSheet(EMPTY_SHEET_NAME, Integer.MAX_VALUE);
0223: }
0224:
0225: try {
0226: workbook.write();
0227: workbook.close();
0228: } catch (IOException e) {
0229: throw new JRException("Error generating XLS report : "
0230: + jasperPrint.getName(), e);
0231: } catch (WriteException e) {
0232: throw new JRException("Error generating XLS report : "
0233: + jasperPrint.getName(), e);
0234: }
0235: }
0236:
0237: protected void setColumnWidth(short index, short width) {
0238: CellView cv = new CellView();
0239: cv.setSize(width);
0240: sheet.setColumnView(index, cv);
0241: }
0242:
0243: protected void setRowHeight(int y, int lastRowHeight)
0244: throws JRException {
0245: try {
0246: sheet.setRowView(y, (lastRowHeight * 20)); // twips
0247: } catch (RowsExceededException e) {
0248: throw new JRException("Error generating XLS report : "
0249: + jasperPrint.getName(), e);
0250: }
0251: }
0252:
0253: protected void setCell(int x, int y) {
0254: }
0255:
0256: protected void addBlankCell(JRExporterGridCell gridCell,
0257: int colIndex, int rowIndex) throws JRException {
0258: try {
0259: Colour forecolor = BLACK;
0260: if (gridCell.getForecolor() != null) {
0261: forecolor = getWorkbookColour(gridCell.getForecolor());
0262: }
0263:
0264: Pattern mode = backgroundMode;
0265: Colour backcolor = WHITE;
0266: if (gridCell.getCellBackcolor() != null) {
0267: mode = Pattern.SOLID;
0268: backcolor = getWorkbookColour(gridCell
0269: .getCellBackcolor());
0270: }
0271:
0272: WritableFont cellFont = getLoadedFont(getDefaultFont(),
0273: forecolor.getValue());
0274: WritableCellFormat cellStyle = getLoadedCellStyle(mode,
0275: backcolor, Alignment.LEFT.getValue(),
0276: VerticalAlignment.TOP.getValue(),
0277: Orientation.HORIZONTAL.getValue(), cellFont,
0278: gridCell);
0279:
0280: sheet.addCell(new Blank(colIndex, rowIndex, cellStyle));
0281: } catch (RowsExceededException e) {
0282: throw new JRException("Error generating XLS report : "
0283: + jasperPrint.getName(), e);
0284: } catch (WriteException e) {
0285: throw new JRException("Error generating XLS report : "
0286: + jasperPrint.getName(), e);
0287: }
0288: }
0289:
0290: protected void exportLine(JRPrintLine line,
0291: JRExporterGridCell gridCell, int x, int y)
0292: throws JRException {
0293: addMergeRegion(gridCell, x, y);
0294:
0295: Colour forecolor2 = getWorkbookColour(line.getForecolor());
0296: WritableFont cellFont2 = this .getLoadedFont(getDefaultFont(),
0297: forecolor2.getValue());
0298: WritableCellFormat cellStyle2 = this .getLoadedCellStyle(
0299: Pattern.SOLID, forecolor2, Alignment.LEFT.getValue(),
0300: VerticalAlignment.TOP.getValue(),
0301: Orientation.HORIZONTAL.getValue(), cellFont2, gridCell);
0302:
0303: Blank cell2 = new Blank(x, y, cellStyle2);
0304:
0305: try {
0306: sheet.addCell(cell2);
0307: } catch (Exception e) {
0308: throw new JRException("Can't add cell.", e);
0309: }
0310: }
0311:
0312: protected void exportRectangle(JRPrintElement element,
0313: JRExporterGridCell gridCell, int x, int y)
0314: throws JRException {
0315: addMergeRegion(gridCell, x, y);
0316:
0317: Colour forecolor = getWorkbookColour(element.getForecolor());
0318: Colour backcolor = WHITE;
0319: Pattern mode = this .backgroundMode;
0320:
0321: if (gridCell.getCellBackcolor() != null) {
0322: mode = Pattern.SOLID;
0323: backcolor = getWorkbookColour(gridCell.getCellBackcolor());
0324: }
0325:
0326: WritableFont cellFont2 = this .getLoadedFont(getDefaultFont(),
0327: forecolor.getValue());
0328: WritableCellFormat cellStyle2 = this .getLoadedCellStyle(mode,
0329: backcolor, Alignment.LEFT.getValue(),
0330: VerticalAlignment.TOP.getValue(),
0331: Orientation.HORIZONTAL.getValue(), cellFont2, gridCell);
0332: Blank cell2 = new Blank(x, y, cellStyle2);
0333:
0334: try {
0335: sheet.addCell(cell2);
0336: } catch (Exception e) {
0337: throw new JRException("Can't add cell.", e);
0338: }
0339: }
0340:
0341: protected void exportText(JRPrintText text,
0342: JRExporterGridCell gridCell, int x, int y)
0343: throws JRException {
0344: addMergeRegion(gridCell, x, y);
0345:
0346: JRStyledText styledText = getStyledText(text);
0347:
0348: if (styledText != null) {
0349: Colour forecolor = getWorkbookColour(text.getForecolor());
0350: WritableFont cellFont = this .getLoadedFont(text, forecolor
0351: .getValue());
0352:
0353: TextAlignHolder alignment = getTextAlignHolder(text);
0354: int horizontalAlignment = getHorizontalAlignment(alignment);
0355: int verticalAlignment = getVerticalAlignment(alignment);
0356: int rotation = getRotation(alignment);
0357:
0358: Pattern mode = this .backgroundMode;
0359: Colour backcolor = WHITE;
0360:
0361: if (gridCell.getCellBackcolor() != null) {
0362: mode = Pattern.SOLID;
0363: backcolor = getWorkbookColour(gridCell
0364: .getCellBackcolor());
0365: }
0366:
0367: StyleInfo baseStyle = new StyleInfo(mode, backcolor,
0368: horizontalAlignment, verticalAlignment, rotation,
0369: cellFont, gridCell.getBox());
0370:
0371: String textStr = styledText.getText();
0372:
0373: String href = null;
0374: JRHyperlinkProducer customHandler = getCustomHandler(text);
0375: if (customHandler == null) {
0376: switch (text.getHyperlinkType()) {
0377: case JRHyperlink.HYPERLINK_TYPE_REFERENCE: {
0378: href = text.getHyperlinkReference();
0379: break;
0380: }
0381: case JRHyperlink.HYPERLINK_TYPE_LOCAL_ANCHOR:
0382: case JRHyperlink.HYPERLINK_TYPE_LOCAL_PAGE:
0383: case JRHyperlink.HYPERLINK_TYPE_REMOTE_ANCHOR:
0384: case JRHyperlink.HYPERLINK_TYPE_REMOTE_PAGE:
0385: case JRHyperlink.HYPERLINK_TYPE_NONE:
0386: default: {
0387: }
0388: }
0389: } else {
0390: href = customHandler.getHyperlink(text);
0391: }
0392:
0393: try {
0394: if (href == null) {
0395: addCell(x, y, text, textStr, baseStyle);
0396: } else {
0397: URL url = new URL(href);
0398: WritableHyperlink hyperlink = new WritableHyperlink(
0399: x, y, x, y, url, textStr);
0400: sheet.addHyperlink(hyperlink);
0401: }
0402: } catch (Exception e) {
0403: throw new JRException("Can't add cell.", e);
0404: }
0405: }
0406: }
0407:
0408: protected void addCell(int x, int y, JRPrintText text,
0409: String textStr, StyleInfo baseStyle) throws WriteException,
0410: RowsExceededException, JRException {
0411: CellValue cellValue;
0412: if (isDetectCellType) {
0413: cellValue = getDetectedCellValue(x, y, text, textStr,
0414: baseStyle);
0415: } else if (isAutoDetectCellType) {
0416: cellValue = getAutoDetectedCellValue(x, y, textStr,
0417: baseStyle);
0418: } else {
0419: cellValue = getLabelCell(x, y, textStr, baseStyle);
0420: }
0421:
0422: sheet.addCell(cellValue);
0423: }
0424:
0425: protected CellValue getDetectedCellValue(int x, int y,
0426: JRPrintText text, String textStr, StyleInfo baseStyle)
0427: throws JRException {
0428: TextValue textValue = getTextValue(text, textStr);
0429: CellTextValueHandler handler = new CellTextValueHandler(x, y,
0430: baseStyle);
0431: textValue.handle(handler);
0432: return handler.getResult();
0433: }
0434:
0435: protected class CellTextValueHandler implements TextValueHandler {
0436: private final int x;
0437: private final int y;
0438: private final StyleInfo baseStyle;
0439:
0440: private CellValue result;
0441:
0442: public CellTextValueHandler(int x, int y, StyleInfo baseStyle) {
0443: this .x = x;
0444: this .y = y;
0445: this .baseStyle = baseStyle;
0446: }
0447:
0448: public void handle(StringTextValue textValue)
0449: throws JRException {
0450: WritableCellFormat cellStyle = getLoadedCellStyle(baseStyle);
0451: result = new Label(x, y, textValue.getText(), cellStyle);
0452: }
0453:
0454: public void handle(NumberTextValue textValue)
0455: throws JRException {
0456: if (textValue.getPattern() != null) {
0457: baseStyle.setDisplayFormat(getNumberFormat(textValue
0458: .getPattern()));
0459: }
0460:
0461: WritableCellFormat cellStyle = getLoadedCellStyle(baseStyle);
0462: if (textValue.getValue() == null) {
0463: result = blank(cellStyle);
0464: } else {
0465: result = new Number(x, y, textValue.getValue()
0466: .doubleValue(), cellStyle);
0467: }
0468: }
0469:
0470: public void handle(DateTextValue textValue) throws JRException {
0471: baseStyle.setDisplayFormat(getDateFormat(textValue
0472: .getPattern()));
0473: WritableCellFormat cellStyle = getLoadedCellStyle(baseStyle);
0474: if (textValue.getValue() == null) {
0475: result = blank(cellStyle);
0476: } else {
0477: result = new DateTime(x, y, textValue.getValue(),
0478: cellStyle);
0479: }
0480: }
0481:
0482: public void handle(BooleanTextValue textValue)
0483: throws JRException {
0484: WritableCellFormat cellStyle = getLoadedCellStyle(baseStyle);
0485: if (textValue.getValue() == null) {
0486: result = blank(cellStyle);
0487: } else {
0488: result = new jxl.write.Boolean(x, y, textValue
0489: .getValue().booleanValue(), cellStyle);
0490: }
0491: }
0492:
0493: protected Blank blank(WritableCellFormat cellStyle) {
0494: return new Blank(x, y, cellStyle);
0495: }
0496:
0497: public CellValue getResult() {
0498: return result;
0499: }
0500: }
0501:
0502: protected jxl.write.NumberFormat getNumberFormat(String pattern) {
0503: String convertedPattern = getConvertedPattern(pattern);
0504: jxl.write.NumberFormat cellFormat = (jxl.write.NumberFormat) numberFormats
0505: .get(convertedPattern);
0506: if (cellFormat == null) {
0507: cellFormat = new jxl.write.NumberFormat(convertedPattern);
0508: numberFormats.put(convertedPattern, cellFormat);
0509: }
0510: return cellFormat;
0511: }
0512:
0513: protected jxl.write.DateFormat getDateFormat(String pattern) {
0514: String convertedPattern = getConvertedPattern(pattern);
0515: jxl.write.DateFormat cellFormat = (jxl.write.DateFormat) dateFormats
0516: .get(convertedPattern);
0517: if (cellFormat == null) {
0518: cellFormat = new jxl.write.DateFormat(convertedPattern);
0519: dateFormats.put(convertedPattern, cellFormat);
0520: }
0521: return cellFormat;
0522: }
0523:
0524: protected CellValue getAutoDetectedCellValue(int x, int y,
0525: String textStr, StyleInfo baseStyle) throws JRException {
0526: CellValue cellValue;
0527: try {
0528: double d = Double.parseDouble(textStr);
0529: WritableCellFormat cellStyle = getLoadedCellStyle(baseStyle);
0530: cellValue = new Number(x, y, d, cellStyle);
0531: } catch (NumberFormatException nfe) {
0532: cellValue = getLabelCell(x, y, textStr, baseStyle);
0533: }
0534: return cellValue;
0535: }
0536:
0537: protected CellValue getLabelCell(int x, int y, String textStr,
0538: StyleInfo baseStyle) throws JRException {
0539: WritableCellFormat cellStyle = getLoadedCellStyle(baseStyle);
0540: CellValue cellValue = new Label(x, y, textStr, cellStyle);
0541: return cellValue;
0542: }
0543:
0544: protected void addMergeRegion(JRExporterGridCell gridCell, int x,
0545: int y) throws JRException {
0546: if (gridCell.getColSpan() > 1 || gridCell.getRowSpan() > 1) {
0547: try {
0548: if (isCollapseRowSpan) {
0549: sheet.mergeCells(x, y,
0550: (x + gridCell.getColSpan() - 1), y);
0551: } else {
0552: sheet.mergeCells(x, y,
0553: (x + gridCell.getColSpan() - 1), (y
0554: + gridCell.getRowSpan() - 1));
0555: }
0556: } catch (JXLException e) {
0557: throw new JRException("Can't merge cells.", e);
0558: }
0559: }
0560: }
0561:
0562: private int getHorizontalAlignment(TextAlignHolder alignment) {
0563: switch (alignment.horizontalAlignment) {
0564: case JRAlignment.HORIZONTAL_ALIGN_RIGHT:
0565: return Alignment.RIGHT.getValue();
0566: case JRAlignment.HORIZONTAL_ALIGN_CENTER:
0567: return Alignment.CENTRE.getValue();
0568: case JRAlignment.HORIZONTAL_ALIGN_JUSTIFIED:
0569: return Alignment.JUSTIFY.getValue();
0570: case JRAlignment.HORIZONTAL_ALIGN_LEFT:
0571: default:
0572: return Alignment.LEFT.getValue();
0573: }
0574: }
0575:
0576: private int getVerticalAlignment(TextAlignHolder alignment) {
0577: switch (alignment.verticalAlignment) {
0578: case JRAlignment.VERTICAL_ALIGN_BOTTOM:
0579: return VerticalAlignment.BOTTOM.getValue();
0580: case JRAlignment.VERTICAL_ALIGN_MIDDLE:
0581: return VerticalAlignment.CENTRE.getValue();
0582: case JRAlignment.VERTICAL_ALIGN_JUSTIFIED:
0583: return VerticalAlignment.JUSTIFY.getValue();
0584: case JRAlignment.VERTICAL_ALIGN_TOP:
0585: default:
0586: return VerticalAlignment.TOP.getValue();
0587: }
0588: }
0589:
0590: private int getRotation(TextAlignHolder alignment) {
0591: switch (alignment.rotation) {
0592: case JRTextElement.ROTATION_LEFT:
0593: return Orientation.PLUS_90.getValue();
0594: case JRTextElement.ROTATION_RIGHT:
0595: return Orientation.MINUS_90.getValue();
0596: case JRTextElement.ROTATION_UPSIDE_DOWN:
0597: case JRTextElement.ROTATION_NONE:
0598: default:
0599: return Orientation.HORIZONTAL.getValue();
0600: }
0601: }
0602:
0603: protected void exportImage(JRPrintImage element,
0604: JRExporterGridCell gridCell, int x, int y)
0605: throws JRException {
0606: addMergeRegion(gridCell, x, y);
0607:
0608: try {
0609: int leftPadding = element.getLeftPadding();
0610: int topPadding = element.getTopPadding();
0611: int rightPadding = element.getRightPadding();
0612: int bottomPadding = element.getBottomPadding();
0613:
0614: int availableImageWidth = element.getWidth() - leftPadding
0615: - rightPadding;
0616: availableImageWidth = availableImageWidth < 0 ? 0
0617: : availableImageWidth;
0618:
0619: int availableImageHeight = element.getHeight() - topPadding
0620: - bottomPadding;
0621: availableImageHeight = availableImageHeight < 0 ? 0
0622: : availableImageHeight;
0623:
0624: JRRenderable renderer = element.getRenderer();
0625:
0626: if (renderer != null && availableImageWidth > 0
0627: && availableImageHeight > 0) {
0628: if (renderer.getType() == JRRenderable.TYPE_IMAGE) {
0629: // Image renderers are all asked for their image data and dimension at some point.
0630: // Better to test and replace the renderer now, in case of lazy load error.
0631: renderer = JRImageRenderer
0632: .getOnErrorRendererForImageData(renderer,
0633: element.getOnErrorType());
0634: if (renderer != null) {
0635: renderer = JRImageRenderer
0636: .getOnErrorRendererForDimension(
0637: renderer, element
0638: .getOnErrorType());
0639: }
0640: }
0641: } else {
0642: renderer = null;
0643: }
0644:
0645: if (renderer != null) {
0646: int normalWidth = availableImageWidth;
0647: int normalHeight = availableImageHeight;
0648:
0649: Dimension2D dimension = renderer.getDimension();
0650: if (dimension != null) {
0651: normalWidth = (int) dimension.getWidth();
0652: normalHeight = (int) dimension.getHeight();
0653: }
0654:
0655: float xalignFactor = 0f;
0656: switch (element.getHorizontalAlignment()) {
0657: case JRAlignment.HORIZONTAL_ALIGN_RIGHT: {
0658: xalignFactor = 1f;
0659: break;
0660: }
0661: case JRAlignment.HORIZONTAL_ALIGN_CENTER: {
0662: xalignFactor = 0.5f;
0663: break;
0664: }
0665: case JRAlignment.HORIZONTAL_ALIGN_LEFT:
0666: default: {
0667: xalignFactor = 0f;
0668: break;
0669: }
0670: }
0671:
0672: float yalignFactor = 0f;
0673: switch (element.getVerticalAlignment()) {
0674: case JRAlignment.VERTICAL_ALIGN_BOTTOM: {
0675: yalignFactor = 1f;
0676: break;
0677: }
0678: case JRAlignment.VERTICAL_ALIGN_MIDDLE: {
0679: yalignFactor = 0.5f;
0680: break;
0681: }
0682: case JRAlignment.VERTICAL_ALIGN_TOP:
0683: default: {
0684: yalignFactor = 0f;
0685: break;
0686: }
0687: }
0688:
0689: BufferedImage bi = new BufferedImage(
0690: availableImageWidth, availableImageHeight,
0691: BufferedImage.TYPE_INT_ARGB);
0692: Graphics2D grx = bi.createGraphics();
0693: if (JRElement.MODE_OPAQUE == element.getMode()) {
0694: grx.setColor(element.getBackcolor());
0695: grx.fillRect(0, 0, availableImageWidth,
0696: availableImageHeight);
0697: }
0698:
0699: switch (element.getScaleImage()) {
0700: case JRImage.SCALE_IMAGE_CLIP: {
0701: int xoffset = (int) (xalignFactor * (availableImageWidth - normalWidth));
0702: int yoffset = (int) (yalignFactor * (availableImageHeight - normalHeight));
0703:
0704: renderer.render(grx, new Rectangle(xoffset,
0705: yoffset, normalWidth, normalHeight));
0706:
0707: break;
0708: }
0709: case JRImage.SCALE_IMAGE_FILL_FRAME: {
0710: renderer.render(grx, new Rectangle(0, 0,
0711: availableImageWidth, availableImageHeight));
0712:
0713: break;
0714: }
0715: case JRImage.SCALE_IMAGE_RETAIN_SHAPE:
0716: default: {
0717: if (element.getHeight() > 0) {
0718: double ratio = (double) normalWidth
0719: / (double) normalHeight;
0720:
0721: if (ratio > (double) availableImageWidth
0722: / (double) availableImageHeight) {
0723: normalWidth = availableImageWidth;
0724: normalHeight = (int) (availableImageWidth / ratio);
0725: } else {
0726: normalWidth = (int) (availableImageHeight * ratio);
0727: normalHeight = availableImageHeight;
0728: }
0729:
0730: int xoffset = (int) (xalignFactor * (availableImageWidth - normalWidth));
0731: int yoffset = (int) (yalignFactor * (availableImageHeight - normalHeight));
0732:
0733: renderer.render(grx, new Rectangle(xoffset,
0734: yoffset, normalWidth, normalHeight));
0735: }
0736:
0737: break;
0738: }
0739: }
0740:
0741: Pattern mode = this .backgroundMode;
0742: Colour background = WHITE;
0743: Colour forecolor = getWorkbookColour(element
0744: .getForecolor());
0745:
0746: if (element.getBorderColor() != null) {
0747: forecolor = getWorkbookColour(element
0748: .getBorderColor());
0749: }
0750:
0751: WritableFont cellFont2 = this .getLoadedFont(
0752: getDefaultFont(), forecolor.getValue());
0753:
0754: if (element.getMode() == JRElement.MODE_OPAQUE) {
0755: mode = Pattern.SOLID;
0756: background = getWorkbookColour(element
0757: .getBackcolor());
0758: }
0759:
0760: WritableCellFormat cellStyle2 = this
0761: .getLoadedCellStyle(mode, background,
0762: Alignment.LEFT.getValue(),
0763: VerticalAlignment.TOP.getValue(),
0764: Orientation.HORIZONTAL.getValue(),
0765: cellFont2, gridCell);
0766:
0767: sheet.addCell(new Blank(x, y, cellStyle2));
0768: WritableImage image = new WritableImage(x, y, gridCell
0769: .getColSpan(), gridCell.getRowSpan(),
0770: JRImageLoader.loadImageDataFromAWTImage(bi,
0771: JRRenderable.IMAGE_TYPE_PNG));
0772:
0773: sheet.addImage(image);
0774: }
0775: } catch (Exception ex) {
0776: ex.printStackTrace();
0777: throw new JRException("The cell cannot be added", ex);
0778: } catch (Error err) {
0779: err.printStackTrace();
0780: throw new JRException("The cell cannot be added", err);
0781: }
0782: }
0783:
0784: protected Colour getWorkbookColour(Color awtColor) {
0785: Colour colour;
0786: if (createCustomPalette) {
0787: colour = (Colour) workbookColours.get(awtColor);
0788: if (colour == null) {
0789: colour = determineWorkbookColour(awtColor);
0790: workbookColours.put(awtColor, colour);
0791: }
0792: } else {
0793: colour = getNearestColour(awtColor);
0794: }
0795: return colour;
0796: }
0797:
0798: protected Colour determineWorkbookColour(Color awtColor) {
0799: //nearest match
0800: int minDist = 999;
0801: Colour minColour = null;
0802:
0803: //nearest match among the available (not used) colours
0804: int minDistAvailable = 999;
0805: Colour minColourAvailable = null;
0806:
0807: Colour[] colors = Colour.getAllColours();
0808: for (int i = 0; i < colors.length; i++) {
0809: Colour colour = colors[i];
0810: RGB customRGB = (RGB) usedColours.get(colour);
0811:
0812: RGB rgb = customRGB == null ? colour.getDefaultRGB()
0813: : customRGB;
0814: int dist = rgbDistance(awtColor, rgb);
0815: if (dist < minDist) {
0816: minDist = dist;
0817: minColour = colour;
0818: }
0819:
0820: if (dist == 0)//exact match
0821: {
0822: break;
0823: }
0824:
0825: if (customRGB == null)//the colour is not used
0826: {
0827: if (dist < minDistAvailable) {
0828: minDistAvailable = dist;
0829: minColourAvailable = colour;
0830: }
0831: }
0832: }
0833:
0834: Colour workbookColour;
0835: if (minDist == 0)//exact match found
0836: {
0837: if (!usedColours.containsKey(minColour)) {
0838: //if the colour is not marked as used, mark it
0839: setColourUsed(minColour);
0840: }
0841: workbookColour = minColour;
0842: } else if (minColourAvailable == null)//all the colours are used
0843: {
0844: if (log.isWarnEnabled()) {
0845: log
0846: .warn("No more available colours in the palette. Using the nearest match for "
0847: + awtColor);
0848: }
0849: workbookColour = minColour;
0850: } else {
0851: //modifying the nearest available colour to the report colour
0852: setColourUsed(minColourAvailable, awtColor);
0853: workbookColour = minColourAvailable;
0854: }
0855: return workbookColour;
0856: }
0857:
0858: protected static Colour getNearestColour(Color awtColor) {
0859: Colour color = (Colour) colorsCache.get(awtColor);
0860:
0861: if (color == null) {
0862: Colour[] colors = Colour.getAllColours();
0863: if ((colors != null) && (colors.length > 0)) {
0864: int minDiff = 999;
0865:
0866: for (int i = 0; i < colors.length; i++) {
0867: Colour crtColor = colors[i];
0868: int diff = rgbDistance(awtColor, crtColor
0869: .getDefaultRGB());
0870:
0871: if (diff < minDiff) {
0872: minDiff = diff;
0873: color = crtColor;
0874: }
0875: }
0876: }
0877:
0878: colorsCache.put(awtColor, color);
0879: }
0880:
0881: return color;
0882: }
0883:
0884: protected static int rgbDistance(Color awtColor, RGB rgb) {
0885: int diff = Math.abs(rgb.getRed() - awtColor.getRed())
0886: + Math.abs(rgb.getGreen() - awtColor.getGreen())
0887: + Math.abs(rgb.getBlue() - awtColor.getBlue());
0888: return diff;
0889: }
0890:
0891: /*private static Colour getNearestColour(Color awtColor) {
0892: Colour retVal = null;
0893: Colour[] colors = Colour.getAllColours();
0894:
0895: int diff = 50;
0896:
0897: if (colors != null && colors.length > 0 ){
0898: Colour crtColor = null;
0899: for (int i = 0; i < colors.length; i++) {
0900: crtColor = colors[i];
0901:
0902: int red = crtColor.getDefaultRGB().getRed();
0903: if (Math.abs(awtColor.getRed() - red) < diff) {
0904: int green = crtColor.getDefaultRGB().getGreen();
0905: if (Math.abs(awtColor.getGreen() - green) < diff) {
0906: int blue = crtColor.getDefaultRGB().getBlue();
0907: if (Math.abs(awtColor.getBlue() - blue) < diff) {
0908: retVal = crtColor;
0909: }
0910: }
0911: }
0912: }
0913: }
0914:
0915: return retVal;
0916: }*/
0917:
0918: private WritableFont getLoadedFont(JRFont font, int forecolor)
0919: throws JRException {
0920: WritableFont cellFont = null;
0921:
0922: if (this .loadedFonts != null && this .loadedFonts.size() > 0) {
0923: for (int i = 0; i < this .loadedFonts.size(); i++) {
0924: WritableFont cf = (WritableFont) this .loadedFonts
0925: .get(i);
0926:
0927: int fontSize = font.getFontSize();
0928: if (isFontSizeFixEnabled)
0929: fontSize -= 1;
0930:
0931: String fontName = font.getFontName();
0932: if (fontMap != null && fontMap.containsKey(fontName)) {
0933: fontName = (String) fontMap.get(fontName);
0934: }
0935:
0936: if ((cf.getName().equals(fontName))
0937: && (cf.getColour().getValue() == forecolor)
0938: && (cf.getPointSize() == fontSize)
0939: && (cf.getUnderlineStyle() == UnderlineStyle.SINGLE ? (font
0940: .isUnderline())
0941: : (!font.isUnderline()))
0942: && (cf.isStruckout() == font.isStrikeThrough())
0943: && (cf.getBoldWeight() == BoldStyle.BOLD
0944: .getValue() ? (font.isBold()) : (!font
0945: .isBold()))
0946: && (cf.isItalic() == font.isItalic())) {
0947: cellFont = cf;
0948: break;
0949: }
0950: }
0951: }
0952:
0953: try {
0954: if (cellFont == null) {
0955: int fontSize = font.getFontSize();
0956: if (isFontSizeFixEnabled)
0957: fontSize -= 1;
0958:
0959: String fontName = font.getFontName();
0960: if (fontMap != null && fontMap.containsKey(fontName)) {
0961: fontName = (String) fontMap.get(fontName);
0962: }
0963:
0964: cellFont = new WritableFont(WritableFont
0965: .createFont(fontName), fontSize,
0966: font.isBold() ? WritableFont.BOLD
0967: : WritableFont.NO_BOLD,
0968: font.isItalic(),
0969: font.isUnderline() ? UnderlineStyle.SINGLE
0970: : UnderlineStyle.NO_UNDERLINE, Colour
0971: .getInternalColour(forecolor));
0972: cellFont.setStruckout(font.isStrikeThrough());
0973:
0974: this .loadedFonts.add(cellFont);
0975: }
0976: } catch (Exception e) {
0977: throw new JRException("Can't get loaded fonts.", e);
0978: }
0979:
0980: return cellFont;
0981: }
0982:
0983: protected class BoxStyle {
0984: protected final BorderLineStyle topBorder;
0985: protected final BorderLineStyle bottomBorder;
0986: protected final BorderLineStyle leftBorder;
0987: protected final BorderLineStyle rightBorder;
0988: protected final Colour topBorderColour;
0989: protected final Colour bottomBorderColour;
0990: protected final Colour leftBorderColour;
0991: protected final Colour rightBorderColour;
0992: private final int hash;
0993:
0994: public BoxStyle(JRBox box) {
0995: if (box != null
0996: && box.getTopBorder() != JRGraphicElement.PEN_NONE) {
0997: topBorder = getBorderLineStyle(box.getTopBorder());
0998: topBorderColour = getWorkbookColour(box
0999: .getTopBorderColor());
1000: } else {
1001: topBorder = BorderLineStyle.NONE;
1002: topBorderColour = BLACK;
1003: }
1004:
1005: if (box != null
1006: && box.getBottomBorder() != JRGraphicElement.PEN_NONE) {
1007: bottomBorder = getBorderLineStyle(box.getBottomBorder());
1008: bottomBorderColour = getWorkbookColour(box
1009: .getBottomBorderColor());
1010: } else {
1011: bottomBorder = BorderLineStyle.NONE;
1012: bottomBorderColour = BLACK;
1013: }
1014:
1015: if (box != null
1016: && box.getLeftBorder() != JRGraphicElement.PEN_NONE) {
1017: leftBorder = getBorderLineStyle(box.getLeftBorder());
1018: leftBorderColour = getWorkbookColour(box
1019: .getLeftBorderColor());
1020: } else {
1021: leftBorder = BorderLineStyle.NONE;
1022: leftBorderColour = BLACK;
1023: }
1024:
1025: if (box != null
1026: && box.getRightBorder() != JRGraphicElement.PEN_NONE) {
1027: rightBorder = getBorderLineStyle(box.getRightBorder());
1028: rightBorderColour = getWorkbookColour(box
1029: .getRightBorderColor());
1030: } else {
1031: rightBorder = BorderLineStyle.NONE;
1032: rightBorderColour = BLACK;
1033: }
1034:
1035: hash = computeHash();
1036: }
1037:
1038: private int computeHash() {
1039: int hashCode = topBorder.hashCode();
1040: hashCode = 31 * hashCode + topBorderColour.hashCode();
1041: hashCode = 31 * hashCode + bottomBorder.hashCode();
1042: hashCode = 31 * hashCode + bottomBorderColour.hashCode();
1043: hashCode = 31 * hashCode + leftBorder.hashCode();
1044: hashCode = 31 * hashCode + leftBorderColour.hashCode();
1045: hashCode = 31 * hashCode + rightBorder.hashCode();
1046: hashCode = 31 * hashCode + rightBorderColour.hashCode();
1047: return hashCode;
1048: }
1049:
1050: public int hashCode() {
1051: return hash;
1052: }
1053:
1054: public boolean equals(Object o) {
1055: BoxStyle b = (BoxStyle) o;
1056:
1057: return b.topBorder.equals(topBorder)
1058: && b.topBorderColour.equals(topBorderColour)
1059: && b.bottomBorder.equals(bottomBorder)
1060: && b.bottomBorderColour.equals(bottomBorderColour)
1061: && b.leftBorder.equals(leftBorder)
1062: && b.leftBorderColour.equals(leftBorderColour)
1063: && b.rightBorder.equals(rightBorder)
1064: && b.rightBorderColour.equals(rightBorderColour);
1065: }
1066:
1067: public String toString() {
1068: return "(" + topBorder.getValue() + "/"
1069: + topBorderColour.getValue() + ","
1070: + bottomBorder.getValue() + "/"
1071: + bottomBorderColour.getValue() + ","
1072: + leftBorder.getValue() + "/"
1073: + leftBorderColour.getValue() + ","
1074: + rightBorder.getValue() + "/"
1075: + rightBorderColour.getValue() + ")";
1076: }
1077: }
1078:
1079: protected class StyleInfo {
1080: protected final Pattern mode;
1081: protected final Colour backcolor;
1082: protected final int horizontalAlignment;
1083: protected final int verticalAlignment;
1084: protected final int rotation;
1085: protected final WritableFont font;
1086: protected final BoxStyle box;
1087: private DisplayFormat displayFormat;
1088: private int hashCode;
1089:
1090: protected StyleInfo(Pattern mode, Colour backcolor,
1091: int horizontalAlignment, int verticalAlignment,
1092: int rotation, WritableFont font, JRBox box) {
1093: this .mode = mode;
1094: this .backcolor = backcolor;
1095: this .horizontalAlignment = horizontalAlignment;
1096: this .verticalAlignment = verticalAlignment;
1097: this .rotation = rotation;
1098: this .font = font;
1099: this .box = new BoxStyle(box);
1100:
1101: computeHash();
1102: }
1103:
1104: protected void computeHash() {
1105: int hash = this .mode.hashCode();
1106: hash = 31 * hash + this .backcolor.hashCode();
1107: hash = 31 * hash + this .horizontalAlignment;
1108: hash = 31 * hash + this .verticalAlignment;
1109: hash = 31 * hash + this .rotation;
1110: hash = 31 * hash + this .font.hashCode();
1111: hash = 31 * hash
1112: + (this .box == null ? 0 : this .box.hashCode());
1113: hash = 31
1114: * hash
1115: + (this .displayFormat == null ? 0
1116: : this .displayFormat.hashCode());
1117:
1118: hashCode = hash;
1119: }
1120:
1121: public int hashCode() {
1122: return hashCode;
1123: }
1124:
1125: public boolean equals(Object o) {
1126: StyleInfo k = (StyleInfo) o;
1127:
1128: return k.mode.equals(mode)
1129: && k.backcolor.equals(backcolor)
1130: && k.horizontalAlignment == horizontalAlignment
1131: && k.verticalAlignment == verticalAlignment
1132: && k.rotation == rotation
1133: && k.font.equals(font)
1134: && (k.box == null ? box == null
1135: : (box != null && k.box.equals(box)))
1136: && (k.displayFormat == null ? displayFormat == null
1137: : (displayFormat != null && k.displayFormat
1138: .equals(displayFormat)));
1139: }
1140:
1141: public DisplayFormat getDisplayFormat() {
1142: return displayFormat;
1143: }
1144:
1145: public void setDisplayFormat(DisplayFormat displayFormat) {
1146: this .displayFormat = displayFormat;
1147: computeHash();
1148: }
1149:
1150: public String toString() {
1151: return "(" + mode + "," + backcolor + ","
1152: + horizontalAlignment + "," + verticalAlignment
1153: + "," + rotation + "," + font + "," + box + ","
1154: + displayFormat + ")";
1155: }
1156: }
1157:
1158: private WritableCellFormat getLoadedCellStyle(Pattern mode,
1159: Colour backcolor, int horizontalAlignment,
1160: int verticalAlignment, int rotation, WritableFont font,
1161: JRExporterGridCell gridCell) throws JRException {
1162: StyleInfo styleKey = new StyleInfo(mode, backcolor,
1163: horizontalAlignment, verticalAlignment, rotation, font,
1164: gridCell.getBox());
1165: return getLoadedCellStyle(styleKey);
1166: }
1167:
1168: protected WritableCellFormat getLoadedCellStyle(StyleInfo styleKey)
1169: throws JRException {
1170: WritableCellFormat cellStyle = (WritableCellFormat) loadedCellStyles
1171: .get(styleKey);
1172:
1173: if (cellStyle == null) {
1174: try {
1175: if (styleKey.getDisplayFormat() == null) {
1176: cellStyle = new WritableCellFormat(styleKey.font);
1177: } else {
1178: cellStyle = new WritableCellFormat(styleKey.font,
1179: styleKey.getDisplayFormat());
1180: }
1181: cellStyle.setBackground(styleKey.backcolor,
1182: styleKey.mode);
1183: cellStyle.setAlignment(Alignment
1184: .getAlignment(styleKey.horizontalAlignment));
1185: cellStyle.setVerticalAlignment(VerticalAlignment
1186: .getAlignment(styleKey.verticalAlignment));
1187: cellStyle.setOrientation(Orientation
1188: .getOrientation(styleKey.rotation));
1189: cellStyle.setWrap(true);
1190:
1191: if (!isIgnoreCellBorder) {
1192: BoxStyle box = styleKey.box;
1193: cellStyle.setBorder(Border.TOP, box.topBorder,
1194: box.topBorderColour);
1195: cellStyle.setBorder(Border.BOTTOM,
1196: box.bottomBorder, box.bottomBorderColour);
1197: cellStyle.setBorder(Border.LEFT, box.leftBorder,
1198: box.leftBorderColour);
1199: cellStyle.setBorder(Border.RIGHT, box.rightBorder,
1200: box.rightBorderColour);
1201: }
1202: } catch (Exception e) {
1203: throw new JRException(
1204: "Error setting cellFormat-template.", e);
1205: }
1206:
1207: loadedCellStyles.put(styleKey, cellStyle);
1208: }
1209:
1210: return cellStyle;
1211: }
1212:
1213: /**
1214: * @param lineStyle
1215: */
1216: protected static BorderLineStyle getBorderLineStyle(byte lineStyle) {
1217: BorderLineStyle retVal = null;
1218: switch (lineStyle) {
1219: case JRGraphicElement.PEN_THIN:
1220: retVal = BorderLineStyle.THIN;
1221: break;
1222:
1223: case JRGraphicElement.PEN_1_POINT:
1224: case JRGraphicElement.PEN_2_POINT:
1225: retVal = BorderLineStyle.MEDIUM;
1226: break;
1227:
1228: case JRGraphicElement.PEN_4_POINT:
1229: retVal = BorderLineStyle.THICK;
1230: break;
1231:
1232: case JRGraphicElement.PEN_DOTTED:
1233: retVal = BorderLineStyle.DOTTED;
1234: break;
1235:
1236: default:
1237: retVal = BorderLineStyle.NONE;
1238: }
1239:
1240: return retVal;
1241: }
1242:
1243: private final void setSheetSettings(WritableSheet sheet) {
1244: PageOrientation po;
1245: PaperSize ps;
1246:
1247: if (jasperPrint.getOrientation() == JRReport.ORIENTATION_PORTRAIT)
1248: po = PageOrientation.PORTRAIT;
1249: else
1250: po = PageOrientation.LANDSCAPE;
1251:
1252: if ((ps = getSuitablePaperSize(jasperPrint)) != null)
1253: sheet.setPageSetup(po, ps, 0, 0);
1254: else
1255: sheet.setPageSetup(po);
1256:
1257: SheetSettings sheets = sheet.getSettings();
1258:
1259: sheets.setTopMargin(0.0);
1260: sheets.setLeftMargin(0.0);
1261: sheets.setRightMargin(0.0);
1262: sheets.setBottomMargin(0.0);
1263:
1264: sheets.setHeaderMargin(0.0);
1265: sheets.setFooterMargin(0.0);
1266: }
1267:
1268: private final PaperSize getSuitablePaperSize(JasperPrint jasP) {
1269:
1270: if (jasP == null)
1271: return null;
1272:
1273: long width = 0;
1274: long height = 0;
1275: PaperSize ps = null;
1276:
1277: if ((jasP.getPageWidth() != 0) && (jasP.getPageHeight() != 0)) {
1278:
1279: double dWidth = (jasP.getPageWidth() / 72.0);
1280: double dHeight = (jasP.getPageHeight() / 72.0);
1281:
1282: height = Math.round(dHeight * 25.4);
1283: width = Math.round(dWidth * 25.4);
1284:
1285: // Compare to ISO 216 A-Series (A3-A5). All other ISO 216 formats
1286: // not supported by JExcelApi yet.
1287: for (int i = 3; i < 6; i++) {
1288: int w = calculateWidthForDinAN(i);
1289: int h = calculateHeightForDinAN(i);
1290:
1291: if (((w == width) && (h == height))
1292: || ((h == width) && (w == height))) {
1293: if (i == 3)
1294: ps = PaperSize.A3;
1295: else if (i == 4)
1296: ps = PaperSize.A4;
1297: else if (i == 5)
1298: ps = PaperSize.A5;
1299: break;
1300: }
1301: }
1302:
1303: // Compare to common North American Paper Sizes (ANSI X3.151-1987).
1304: if (ps == null) {
1305: // ANSI X3.151-1987 - "Letter" (216 � 279 mm)
1306: if (((width == 216) && (height == 279))
1307: || ((width == 279) && (height == 216))) {
1308: ps = PaperSize.LETTER;
1309: }
1310: // ANSI X3.151-1987 - "Legal" (216 � 356 mm)
1311: if (((width == 216) && (height == 356))
1312: || ((width == 356) && (height == 216))) {
1313: ps = PaperSize.LEGAL;
1314: }
1315: // ANSI X3.151-1987 - "Executive" (190 � 254 mm)
1316: // Not supperted by JExcelApi yet.
1317:
1318: // ANSI X3.151-1987 - "Ledger/Tabloid" (279 � 432 mm)
1319: // Not supperted by JExcelApi yet.
1320: }
1321: }
1322: return ps;
1323: }
1324:
1325: public static TextAlignHolder getTextAlignHolder(
1326: JRPrintText textElement) {
1327: short horizontalAlignment;
1328: short verticalAlignment;
1329: short rotation = textElement.getRotation();
1330:
1331: switch (textElement.getRotation()) {
1332: case JRTextElement.ROTATION_LEFT: {
1333: switch (textElement.getHorizontalAlignment()) {
1334: case JRAlignment.HORIZONTAL_ALIGN_LEFT: {
1335: verticalAlignment = JRAlignment.VERTICAL_ALIGN_BOTTOM;
1336: break;
1337: }
1338: case JRAlignment.HORIZONTAL_ALIGN_CENTER: {
1339: verticalAlignment = JRAlignment.VERTICAL_ALIGN_MIDDLE;
1340: break;
1341: }
1342: case JRAlignment.HORIZONTAL_ALIGN_RIGHT: {
1343: verticalAlignment = JRAlignment.VERTICAL_ALIGN_TOP;
1344: break;
1345: }
1346: case JRAlignment.HORIZONTAL_ALIGN_JUSTIFIED: {
1347: verticalAlignment = JRAlignment.VERTICAL_ALIGN_JUSTIFIED;
1348: break;
1349: }
1350: default: {
1351: verticalAlignment = JRAlignment.VERTICAL_ALIGN_BOTTOM;
1352: }
1353: }
1354:
1355: switch (textElement.getVerticalAlignment()) {
1356: case JRAlignment.VERTICAL_ALIGN_TOP: {
1357: horizontalAlignment = JRAlignment.HORIZONTAL_ALIGN_LEFT;
1358: break;
1359: }
1360: case JRAlignment.VERTICAL_ALIGN_MIDDLE: {
1361: horizontalAlignment = JRAlignment.HORIZONTAL_ALIGN_CENTER;
1362: break;
1363: }
1364: case JRAlignment.VERTICAL_ALIGN_BOTTOM: {
1365: horizontalAlignment = JRAlignment.HORIZONTAL_ALIGN_RIGHT;
1366: break;
1367: }
1368: default: {
1369: horizontalAlignment = JRAlignment.HORIZONTAL_ALIGN_LEFT;
1370: }
1371: }
1372:
1373: break;
1374: }
1375: case JRTextElement.ROTATION_RIGHT: {
1376: switch (textElement.getHorizontalAlignment()) {
1377: case JRAlignment.HORIZONTAL_ALIGN_LEFT: {
1378: verticalAlignment = JRAlignment.VERTICAL_ALIGN_TOP;
1379: break;
1380: }
1381: case JRAlignment.HORIZONTAL_ALIGN_CENTER: {
1382: verticalAlignment = JRAlignment.VERTICAL_ALIGN_MIDDLE;
1383: break;
1384: }
1385: case JRAlignment.HORIZONTAL_ALIGN_RIGHT: {
1386: verticalAlignment = JRAlignment.VERTICAL_ALIGN_BOTTOM;
1387: break;
1388: }
1389: case JRAlignment.HORIZONTAL_ALIGN_JUSTIFIED: {
1390: verticalAlignment = JRAlignment.VERTICAL_ALIGN_JUSTIFIED;
1391: break;
1392: }
1393: default: {
1394: verticalAlignment = JRAlignment.VERTICAL_ALIGN_TOP;
1395: }
1396: }
1397:
1398: switch (textElement.getVerticalAlignment()) {
1399: case JRAlignment.VERTICAL_ALIGN_TOP: {
1400: horizontalAlignment = JRAlignment.HORIZONTAL_ALIGN_RIGHT;
1401: break;
1402: }
1403: case JRAlignment.VERTICAL_ALIGN_MIDDLE: {
1404: horizontalAlignment = JRAlignment.HORIZONTAL_ALIGN_CENTER;
1405: break;
1406: }
1407: case JRAlignment.VERTICAL_ALIGN_BOTTOM: {
1408: horizontalAlignment = JRAlignment.HORIZONTAL_ALIGN_LEFT;
1409: break;
1410: }
1411: default: {
1412: horizontalAlignment = JRAlignment.HORIZONTAL_ALIGN_RIGHT;
1413: }
1414: }
1415:
1416: break;
1417: }
1418: case JRTextElement.ROTATION_UPSIDE_DOWN:
1419: case JRTextElement.ROTATION_NONE:
1420: default: {
1421: horizontalAlignment = textElement.getHorizontalAlignment();
1422: verticalAlignment = textElement.getVerticalAlignment();
1423: }
1424: }
1425:
1426: return new TextAlignHolder(horizontalAlignment,
1427: verticalAlignment, rotation);
1428: }
1429:
1430: // Berechnungsvorschriften f�r die DIN Formate A, B, und C.
1431: // Die Angabe der Breite/H�he erfolgt in [mm].
1432:
1433: private final int calculateWidthForDinAN(int n) {
1434: return (int) (Math.pow(2.0, (-0.25 - (n / 2.0))) * 1000.0);
1435: }
1436:
1437: private final int calculateHeightForDinAN(int n) {
1438: return (int) (Math.pow(2.0, (0.25 - (n / 2.0))) * 1000.0);
1439: }
1440:
1441: private final int calculateWidthForDinBN(int n) {
1442: return (int) (Math.pow(2.0, -(n / 2.0)) * 1000.0);
1443: }
1444:
1445: private final int calculateHeightForDinBN(int n) {
1446: return (int) (Math.pow(2.0, (0.5 - (n / 2.0))) * 1000.0);
1447: }
1448:
1449: private final int calculateWidthForDinCN(int n) {
1450: return (int) (Math.pow(2.0, (-0.125 - (n / 2.0))) * 1000.0);
1451: }
1452:
1453: private final int calculateHeightForDinCN(int n) {
1454: return (int) (Math.pow(2.0, (0.375 - (n / 2.0))) * 1000.0);
1455: }
1456:
1457: protected void exportFrame(JRPrintFrame frame,
1458: JRExporterGridCell gridCell, int x, int y)
1459: throws JRException {
1460: addMergeRegion(gridCell, x, y);
1461:
1462: Colour forecolor = getWorkbookColour(frame.getForecolor());
1463: Colour backcolor = WHITE;
1464: Pattern mode = backgroundMode;
1465:
1466: if (frame.getMode() == JRElement.MODE_OPAQUE) {
1467: mode = Pattern.SOLID;
1468: backcolor = getWorkbookColour(frame.getBackcolor());
1469: }
1470:
1471: WritableFont cellFont = getLoadedFont(getDefaultFont(),
1472: forecolor.getValue());
1473: WritableCellFormat cellStyle = getLoadedCellStyle(mode,
1474: backcolor, Alignment.LEFT.getValue(),
1475: VerticalAlignment.TOP.getValue(),
1476: Orientation.HORIZONTAL.getValue(), cellFont, gridCell);
1477:
1478: Blank cell = new Blank(x, y, cellStyle);
1479: try {
1480: sheet.addCell(cell);
1481: } catch (JXLException e) {
1482: throw new JRException("Can't add cell.", e);
1483: }
1484: }
1485:
1486: /**
1487: * This method is intended to modify a given format pattern so to include
1488: * only the accepted proprietary format characters. The resulted pattern
1489: * will possibly truncate the original pattern
1490: * @param pattern
1491: * @return pattern converted to accepted proprietary formats
1492: */
1493: private String getConvertedPattern(String pattern) {
1494: if (formatPatternsMap != null
1495: && formatPatternsMap.containsKey(pattern)) {
1496: return (String) formatPatternsMap.get(pattern);
1497: }
1498: return pattern;
1499: }
1500:
1501: protected ExporterNature getNature() {
1502: if (isIgnoreGraphics) {
1503: return JExcelApiTextOnlyExporterNature.getInstance();
1504: } else {
1505: return JExcelApiExporterNature.getInstance();
1506: }
1507: }
1508:
1509: }
|