KickJava   Java API By Example, From Geeks To Geeks.

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


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

28
29 /*
30  * Contributors:
31  * Wolfgang - javabreak@users.sourceforge.net
32  * Mario Daepp - mdaepp@users.sourceforge.net
33  */

34 package net.sf.jasperreports.engine.export;
35
36 import java.awt.Color JavaDoc;
37 import java.io.IOException JavaDoc;
38 import java.io.OutputStream JavaDoc;
39 import java.util.Collection JavaDoc;
40 import java.util.HashMap JavaDoc;
41 import java.util.Iterator JavaDoc;
42 import java.util.Map JavaDoc;
43
44 import net.sf.jasperreports.engine.JRAlignment;
45 import net.sf.jasperreports.engine.JRBox;
46 import net.sf.jasperreports.engine.JRElement;
47 import net.sf.jasperreports.engine.JRException;
48 import net.sf.jasperreports.engine.JRFont;
49 import net.sf.jasperreports.engine.JRGraphicElement;
50 import net.sf.jasperreports.engine.JRPrintElement;
51 import net.sf.jasperreports.engine.JRPrintFrame;
52 import net.sf.jasperreports.engine.JRPrintImage;
53 import net.sf.jasperreports.engine.JRPrintLine;
54 import net.sf.jasperreports.engine.JRPrintText;
55 import net.sf.jasperreports.engine.JRTextElement;
56 import net.sf.jasperreports.engine.export.JRGridLayout.ExporterElements;
57 import net.sf.jasperreports.engine.export.data.BooleanTextValue;
58 import net.sf.jasperreports.engine.export.data.DateTextValue;
59 import net.sf.jasperreports.engine.export.data.NumberTextValue;
60 import net.sf.jasperreports.engine.export.data.StringTextValue;
61 import net.sf.jasperreports.engine.export.data.TextValue;
62 import net.sf.jasperreports.engine.export.data.TextValueHandler;
63 import net.sf.jasperreports.engine.util.JRStringUtil;
64 import net.sf.jasperreports.engine.util.JRStyledText;
65
66 import org.apache.commons.collections.ReferenceMap;
67 import org.apache.poi.hssf.usermodel.HSSFCell;
68 import org.apache.poi.hssf.usermodel.HSSFCellStyle;
69 import org.apache.poi.hssf.usermodel.HSSFDataFormat;
70 import org.apache.poi.hssf.usermodel.HSSFFont;
71 import org.apache.poi.hssf.usermodel.HSSFRow;
72 import org.apache.poi.hssf.usermodel.HSSFSheet;
73 import org.apache.poi.hssf.usermodel.HSSFWorkbook;
74 import org.apache.poi.hssf.util.HSSFColor;
75 import org.apache.poi.hssf.util.Region;
76
77
78 /**
79  * Exports a JasperReports document to XLS format. It has binary output type and exports the document to
80  * a grid-based layout.
81  * <p>
82  * Since classic AWT fonts can be sometimes very different from system fonts (which are used by XLS viewers),
83  * a font mapping feature was added. By using the {@link net.sf.jasperreports.engine.JRExporterParameter#FONT_MAP} parameter, a logical
84  * font like "sansserif" can be mapped to a system specific font, like "Comic Sans MS". Both map keys and values are strings.
85  * @author Teodor Danciu (teodord@users.sourceforge.net)
86  * @version $Id: JRXlsExporter.java 1483 2006-11-13 11:56:53 +0200 (Mon, 13 Nov 2006) teodord $
87  */

88 public class JRXlsExporter extends JRXlsAbstractExporter
89 {
90
91     private static Map JavaDoc hssfColorsCache = new ReferenceMap();
92
93     protected Map JavaDoc loadedCellStyles = new HashMap JavaDoc();
94
95     /**
96      *
97      */

98     protected HSSFWorkbook workbook = null;
99     protected HSSFSheet sheet = null;
100     protected HSSFRow row = null;
101     protected HSSFCell cell = null;
102     protected HSSFCellStyle emptyCellStyle = null;
103
104     /**
105      *
106      */

107     protected short whiteIndex = (new HSSFColor.WHITE()).getIndex();
108     protected short blackIndex = (new HSSFColor.BLACK()).getIndex();
109     
110     protected short backgroundMode = HSSFCellStyle.SOLID_FOREGROUND;
111     
112     protected HSSFDataFormat dataFormat = null;
113     protected Map JavaDoc formatPatternsMap = null;
114
115     
116     protected void setParameters()
117     {
118         super.setParameters();
119         
120         formatPatternsMap = (Map JavaDoc)getParameter(JRXlsExporterParameter.FORMAT_PATTERNS_MAP);
121     }
122
123
124     protected void setBackground()
125     {
126         if (!isWhitePageBackground)
127         {
128             backgroundMode = HSSFCellStyle.NO_FILL;
129         }
130     }
131
132
133     protected void openWorkbook(OutputStream JavaDoc os)
134     {
135         workbook = new HSSFWorkbook();
136         emptyCellStyle = workbook.createCellStyle();
137         emptyCellStyle.setFillForegroundColor((new HSSFColor.WHITE()).getIndex());
138         emptyCellStyle.setFillPattern(backgroundMode);
139         dataFormat = workbook.createDataFormat();
140     }
141     
142     protected void createSheet(String JavaDoc name)
143     {
144         sheet = workbook.createSheet(name);
145     }
146     
147     protected void closeWorkbook(OutputStream JavaDoc os) throws JRException
148     {
149         try
150         {
151             workbook.write(os);
152         }
153         catch (IOException JavaDoc e)
154         {
155             throw new JRException("Error generating XLS report : " + jasperPrint.getName(), e);
156         }
157     }
158
159     protected void setColumnWidth(short index, short width)
160     {
161         sheet.setColumnWidth(index, width);
162     }
163
164     protected void setRowHeight(int rowIndex, int lastRowHeight)
165     {
166         row = sheet.getRow((short)rowIndex);
167         if (row == null)
168         {
169             row = sheet.createRow((short)rowIndex);
170         }
171         
172         row.setHeightInPoints((short)lastRowHeight);
173     }
174
175     protected void setCell(int colIndex, int rowIndex)
176     {
177         HSSFCell emptyCell = row.getCell((short)colIndex);
178         if (emptyCell == null)
179         {
180             emptyCell = row.createCell((short)colIndex);
181             emptyCell.setCellStyle(emptyCellStyle);
182         }
183     }
184
185     protected void addBlankCell(JRExporterGridCell gridCell, int colIndex, int rowIndex)
186     {
187         cell = row.createCell((short) colIndex);
188         
189         short mode = backgroundMode;
190         short backcolor = whiteIndex;
191         if (gridCell.getBackcolor() != null)
192         {
193             mode = HSSFCellStyle.SOLID_FOREGROUND;
194             backcolor = getNearestColor(gridCell.getBackcolor()).getIndex();
195         }
196         
197         short forecolor = blackIndex;
198         if (gridCell.getForecolor() != null)
199         {
200             forecolor = getNearestColor(gridCell.getForecolor()).getIndex();
201         }
202
203         HSSFCellStyle cellStyle =
204             getLoadedCellStyle(
205                 mode,
206                 backcolor,
207                 HSSFCellStyle.ALIGN_LEFT,
208                 HSSFCellStyle.VERTICAL_TOP,
209                 (short)0,
210                 getLoadedFont(getDefaultFont(), forecolor),
211                 gridCell
212                 );
213         
214         cell.setCellStyle(cellStyle);
215     }
216
217     /**
218      *
219      */

220     protected void exportLine(JRPrintLine line, JRExporterGridCell gridCell, int colIndex, int rowIndex)
221     {
222         short forecolor = getNearestColor(line.getForecolor()).getIndex();
223
224         HSSFCellStyle cellStyle =
225             getLoadedCellStyle(
226                 HSSFCellStyle.SOLID_FOREGROUND,
227                 forecolor,
228                 HSSFCellStyle.ALIGN_LEFT,
229                 HSSFCellStyle.VERTICAL_TOP,
230                 (short)0,
231                 getLoadedFont(getDefaultFont(), forecolor),
232                 gridCell
233                 );
234         
235         createMergeRegion(gridCell, colIndex, rowIndex, cellStyle);
236
237         cell = row.createCell((short)colIndex);
238         cell.setEncoding(HSSFCell.ENCODING_UTF_16);
239         cell.setCellValue("");
240         cell.setCellStyle(cellStyle);
241     }
242
243
244     /**
245      *
246      */

247     protected void exportRectangle(JRPrintElement element, JRExporterGridCell gridCell, int colIndex, int rowIndex)
248     {
249         short forecolor = getNearestColor(element.getForecolor()).getIndex();
250
251         short mode = backgroundMode;
252         short backcolor = whiteIndex;
253         if (element.getMode() == JRElement.MODE_OPAQUE)
254         {
255             mode = HSSFCellStyle.SOLID_FOREGROUND;
256             backcolor = getNearestColor(element.getBackcolor()).getIndex();
257         }
258         else if (gridCell.getBackcolor() != null)
259         {
260             mode = HSSFCellStyle.SOLID_FOREGROUND;
261             backcolor = getNearestColor(gridCell.getBackcolor()).getIndex();
262         }
263
264         HSSFCellStyle cellStyle =
265             getLoadedCellStyle(
266                 mode,
267                 backcolor,
268                 HSSFCellStyle.ALIGN_LEFT,
269                 HSSFCellStyle.VERTICAL_TOP,
270                 (short)0,
271                 getLoadedFont(getDefaultFont(), forecolor),
272                 gridCell
273                 );
274         
275         createMergeRegion(gridCell, colIndex, rowIndex, cellStyle);
276
277         cell = row.createCell((short)colIndex);
278         cell.setEncoding(HSSFCell.ENCODING_UTF_16);
279         cell.setCellValue("");
280         cell.setCellStyle(cellStyle);
281     }
282     
283
284
285     protected void exportText(JRPrintText textElement, JRExporterGridCell gridCell, int colIndex, int rowIndex) throws JRException
286     {
287         JRStyledText styledText = getStyledText(textElement);
288
289         if (styledText == null)
290         {
291             return;
292         }
293
294         short forecolor = getNearestColor(textElement.getForecolor()).getIndex();
295
296         TextAlignHolder textAlignHolder = getTextAlignHolder(textElement);
297         short horizontalAlignment = getHorizontalAlignment(textAlignHolder);
298         short verticalAlignment = getVerticalAlignment(textAlignHolder);
299         short rotation = getRotation(textAlignHolder);
300
301         short mode = backgroundMode;
302         short backcolor = whiteIndex;
303         if (textElement.getMode() == JRElement.MODE_OPAQUE)
304         {
305             mode = HSSFCellStyle.SOLID_FOREGROUND;
306             backcolor = getNearestColor(textElement.getBackcolor()).getIndex();
307         }
308         else if (gridCell.getBackcolor() != null)
309         {
310             mode = HSSFCellStyle.SOLID_FOREGROUND;
311             backcolor = getNearestColor(gridCell.getBackcolor()).getIndex();
312         }
313
314         StyleInfo baseStyle = getStyleInfo(
315                 mode,
316                 backcolor,
317                 horizontalAlignment,
318                 verticalAlignment,
319                 rotation,
320                 getLoadedFont(textElement, forecolor),
321                 gridCell
322                 );
323         
324         createTextCell(textElement, gridCell, colIndex, rowIndex, styledText, baseStyle);
325     }
326
327
328     protected void createTextCell(JRPrintText textElement, final JRExporterGridCell gridCell, final int colIndex, final int rowIndex, JRStyledText styledText, final StyleInfo baseStyle) throws JRException
329     {
330         String JavaDoc textStr = styledText.getText();
331         if (isDetectCellType)
332         {
333             TextValue value = getTextValue(textElement, textStr);
334             value.handle(new TextValueHandler()
335             {
336                 public void handle(StringTextValue textValue)
337                 {
338                     HSSFCellStyle cellStyle = initCreateCell(gridCell, colIndex, rowIndex, baseStyle);
339                     setStringCellValue(textValue.getText());
340                     endCreateCell(cellStyle);
341                 }
342
343                 public void handle(NumberTextValue textValue)
344                 {
345                     if (textValue.getPattern() != null)
346                     {
347                         baseStyle.setDataFormat(
348                             dataFormat.getFormat(
349                                 getConvertedPattern(textValue.getPattern())
350                                 )
351                             );
352                     }
353
354                     HSSFCellStyle cellStyle = initCreateCell(gridCell, colIndex, rowIndex, baseStyle);
355                     if (textValue.getValue() == null)
356                     {
357                         cell.setCellType(HSSFCell.CELL_TYPE_BLANK);
358                     }
359                     else
360                     {
361                         cell.setCellValue(textValue.getValue().doubleValue());
362                     }
363                     endCreateCell(cellStyle);
364                 }
365
366                 public void handle(DateTextValue textValue)
367                 {
368                     baseStyle.setDataFormat(
369                         dataFormat.getFormat(
370                             getConvertedPattern(textValue.getPattern())
371                             )
372                         );
373                     HSSFCellStyle cellStyle = initCreateCell(gridCell, colIndex, rowIndex, baseStyle);
374                     if (textValue.getValue() == null)
375                     {
376                         cell.setCellType(HSSFCell.CELL_TYPE_BLANK);
377                     }
378                     else
379                     {
380                         cell.setCellValue(textValue.getValue());
381                     }
382                     endCreateCell(cellStyle);
383                 }
384
385                 public void handle(BooleanTextValue textValue)
386                 {
387                     HSSFCellStyle cellStyle = initCreateCell(gridCell, colIndex, rowIndex, baseStyle);
388                     if (textValue.getValue() == null)
389                     {
390                         cell.setCellType(HSSFCell.CELL_TYPE_BLANK);
391                     }
392                     else
393                     {
394                         cell.setCellValue(textValue.getValue().booleanValue());
395                     }
396                     endCreateCell(cellStyle);
397                 }
398                 
399                 /**
400                  * This method is intended to modify a given format pattern so to include
401                  * only the accepted proprietary format characters. The resulted pattern
402                  * will possibly truncate the original pattern
403                  * @param pattern
404                  * @return pattern converted to accepted proprietary formats
405                  */

406                 private String JavaDoc getConvertedPattern(String JavaDoc pattern)
407                 {
408                     if (formatPatternsMap != null && formatPatternsMap.containsKey(pattern))
409                     {
410                         return (String JavaDoc) formatPatternsMap.get(pattern);
411                     }
412                     return pattern;
413                 }
414
415             });
416         }
417         else if (isAutoDetectCellType)
418         {
419             HSSFCellStyle cellStyle = initCreateCell(gridCell, colIndex, rowIndex, baseStyle);
420             try
421             {
422                 cell.setCellValue(Double.parseDouble(textStr));
423             }
424             catch(NumberFormatException JavaDoc e)
425             {
426                 setStringCellValue(textStr);
427             }
428             endCreateCell(cellStyle);
429         }
430         else
431         {
432             HSSFCellStyle cellStyle = initCreateCell(gridCell, colIndex, rowIndex, baseStyle);
433             setStringCellValue(textStr);
434             endCreateCell(cellStyle);
435         }
436     }
437
438
439     protected HSSFCellStyle initCreateCell(JRExporterGridCell gridCell, int colIndex, int rowIndex, StyleInfo baseStyle)
440     {
441         HSSFCellStyle cellStyle = getLoadedCellStyle(baseStyle);
442         createMergeRegion(gridCell, colIndex, rowIndex, cellStyle);
443         cell = row.createCell((short)colIndex);
444         cell.setEncoding(HSSFCell.ENCODING_UTF_16);
445         return cellStyle;
446     }
447
448     protected void endCreateCell(HSSFCellStyle cellStyle)
449     {
450         cell.setCellStyle(cellStyle);
451     }
452
453     protected final void setStringCellValue(String JavaDoc textStr)
454     {
455         cell.setCellValue(JRStringUtil.replaceDosEOL(textStr));
456     }
457
458     
459     protected void createMergeRegion(JRExporterGridCell gridCell, int colIndex, int rowIndex, HSSFCellStyle cellStyle)
460     {
461         if (gridCell.colSpan > 1 || gridCell.rowSpan > 1)
462         {
463             sheet.addMergedRegion(new Region(rowIndex, (short)colIndex, (rowIndex + gridCell.rowSpan - 1), (short)(colIndex + gridCell.colSpan - 1)));
464
465             for(int i = 0; i < gridCell.rowSpan; i++)
466             {
467                 HSSFRow spanRow = sheet.getRow(rowIndex + i);
468                 if (spanRow == null)
469                 {
470                     spanRow = sheet.createRow(rowIndex + i);
471                 }
472                 for(int j = 0; j < gridCell.colSpan; j++)
473                 {
474                     HSSFCell spanCell = spanRow.getCell((short)(colIndex + j));
475                     if (spanCell == null)
476                     {
477                         spanCell = spanRow.createCell((short)(colIndex + j));
478                     }
479                     spanCell.setCellStyle(cellStyle);
480                 }
481             }
482         }
483     }
484
485     private short getHorizontalAlignment(TextAlignHolder alignment)
486     {
487         switch (alignment.horizontalAlignment)
488         {
489             case JRAlignment.HORIZONTAL_ALIGN_RIGHT:
490                 return HSSFCellStyle.ALIGN_RIGHT;
491             case JRAlignment.HORIZONTAL_ALIGN_CENTER:
492                 return HSSFCellStyle.ALIGN_CENTER;
493             case JRAlignment.HORIZONTAL_ALIGN_JUSTIFIED:
494                 return HSSFCellStyle.ALIGN_JUSTIFY;
495             case JRAlignment.HORIZONTAL_ALIGN_LEFT:
496             default:
497                 return HSSFCellStyle.ALIGN_LEFT;
498         }
499     }
500
501     private short getVerticalAlignment(TextAlignHolder alignment)
502     {
503         switch (alignment.verticalAlignment)
504         {
505             case JRAlignment.VERTICAL_ALIGN_BOTTOM:
506                 return HSSFCellStyle.VERTICAL_BOTTOM;
507             case JRAlignment.VERTICAL_ALIGN_MIDDLE:
508                 return HSSFCellStyle.VERTICAL_CENTER;
509             case JRAlignment.VERTICAL_ALIGN_JUSTIFIED:
510                 return HSSFCellStyle.VERTICAL_JUSTIFY;
511             case JRAlignment.VERTICAL_ALIGN_TOP:
512             default:
513                 return HSSFCellStyle.VERTICAL_TOP;
514         }
515     }
516
517     private short getRotation(TextAlignHolder alignment)
518     {
519         switch (alignment.rotation)
520         {
521             case JRTextElement.ROTATION_LEFT:
522                 return 90;
523             case JRTextElement.ROTATION_RIGHT:
524                 return -90;
525             case JRTextElement.ROTATION_UPSIDE_DOWN:
526             case JRTextElement.ROTATION_NONE:
527             default:
528                 return 0;
529         }
530     }
531     
532     /**
533      *
534      */

535     protected static HSSFColor getNearestColor(Color JavaDoc awtColor)
536     {
537         HSSFColor color = (HSSFColor) hssfColorsCache.get(awtColor);
538
539         if (color == null)
540         {
541             Map JavaDoc triplets = HSSFColor.getTripletHash();
542             if (triplets != null)
543             {
544                 Collection JavaDoc keys = triplets.keySet();
545                 if (keys != null && keys.size() > 0)
546                 {
547                     Object JavaDoc key = null;
548                     HSSFColor crtColor = null;
549                     short[] rgb = null;
550                     int diff = 0;
551                     int minDiff = 999;
552                     for (Iterator JavaDoc it = keys.iterator(); it.hasNext();)
553                     {
554                         key = it.next();
555
556                         crtColor = (HSSFColor) triplets.get(key);
557                         rgb = crtColor.getTriplet();
558
559                         diff = Math.abs(rgb[0] - awtColor.getRed()) + Math.abs(rgb[1] - awtColor.getGreen()) + Math.abs(rgb[2] - awtColor.getBlue());
560
561                         if (diff < minDiff)
562                         {
563                             minDiff = diff;
564                             color = crtColor;
565                         }
566                     }
567                 }
568             }
569             
570             hssfColorsCache.put(awtColor, color);
571         }
572
573         return color;
574     }
575
576
577     /**
578      *
579      */

580     protected HSSFFont getLoadedFont(JRFont font, short forecolor)
581     {
582         HSSFFont cellFont = null;
583         
584         String JavaDoc fontName = font.getFontName();
585         if (fontMap != null && fontMap.containsKey(fontName))
586         {
587             fontName = (String JavaDoc) fontMap.get(fontName);
588         }
589
590         for (int i = 0; i < loadedFonts.size(); i++)
591         {
592             HSSFFont cf = (HSSFFont)loadedFonts.get(i);
593             
594             short fontSize = (short)font.getFontSize();
595             if (isFontSizeFixEnabled)
596                 fontSize -= 1;
597             
598             if (
599                 cf.getFontName().equals(fontName) &&
600                 (cf.getColor() == forecolor) &&
601                 (cf.getFontHeightInPoints() == fontSize) &&
602                 ((cf.getUnderline() == HSSFFont.U_SINGLE)?(font.isUnderline()):(!font.isUnderline())) &&
603                 (cf.getStrikeout() == font.isStrikeThrough()) &&
604                 ((cf.getBoldweight() == HSSFFont.BOLDWEIGHT_BOLD)?(font.isBold()):(!font.isBold())) &&
605                 (cf.getItalic() == font.isItalic())
606                 )
607             {
608                 cellFont = cf;
609                 break;
610             }
611         }
612         
613         if (cellFont == null)
614         {
615             cellFont = workbook.createFont();
616
617             cellFont.setFontName(fontName);
618             cellFont.setColor(forecolor);
619
620             short fontSize = (short)font.getFontSize();
621             if (isFontSizeFixEnabled)
622                 fontSize -= 1;
623             
624             cellFont.setFontHeightInPoints(fontSize);
625
626             if (font.isUnderline())
627             {
628                 cellFont.setUnderline(HSSFFont.U_SINGLE);
629             }
630             if (font.isStrikeThrough())
631             {
632                 cellFont.setStrikeout(true);
633             }
634             if (font.isBold())
635             {
636                 cellFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
637             }
638             if (font.isItalic())
639             {
640                 cellFont.setItalic(true);
641             }
642             
643             loadedFonts.add(cellFont);
644         }
645             
646         return cellFont;
647     }
648
649
650     protected HSSFCellStyle getLoadedCellStyle(StyleInfo style)
651     {
652         HSSFCellStyle cellStyle = (HSSFCellStyle) loadedCellStyles.get(style);
653         if (cellStyle == null)
654         {
655             cellStyle = workbook.createCellStyle();
656             cellStyle.setFillForegroundColor(style.backcolor);
657             cellStyle.setFillPattern(style.mode);
658             cellStyle.setAlignment(style.horizontalAlignment);
659             cellStyle.setVerticalAlignment(style.verticalAlignment);
660             cellStyle.setRotation(style.rotation);
661             cellStyle.setFont(style.font);
662             cellStyle.setWrapText(true);
663             
664             if (style.hasDataFormat())
665             {
666                 cellStyle.setDataFormat(style.getDataFormat());
667             }
668             
669             BoxStyle box = style.box;
670             cellStyle.setBorderTop(box.topBorder);
671             cellStyle.setTopBorderColor(box.topBorderColour);
672             cellStyle.setBorderLeft(box.leftBorder);
673             cellStyle.setLeftBorderColor(box.leftBorderColour);
674             cellStyle.setBorderBottom(box.bottomBorder);
675             cellStyle.setBottomBorderColor(box.bottomBorderColour);
676             cellStyle.setBorderRight(box.rightBorder);
677             cellStyle.setRightBorderColor(box.rightBorderColour);
678             
679             loadedCellStyles.put(style, cellStyle);
680         }
681         return cellStyle;
682     }
683
684     protected HSSFCellStyle getLoadedCellStyle(
685             short mode,
686             short backcolor,
687             short horizontalAlignment,
688             short verticalAlignment,
689             short rotation,
690             HSSFFont font,
691             JRExporterGridCell gridCell
692             )
693     {
694         StyleInfo style = getStyleInfo(mode, backcolor, horizontalAlignment, verticalAlignment, rotation, font, gridCell);
695         return getLoadedCellStyle(style);
696     }
697
698
699     protected StyleInfo getStyleInfo(short mode, short backcolor, short horizontalAlignment, short verticalAlignment, short rotation, HSSFFont font, JRExporterGridCell gridCell)
700     {
701         short gridForecolor = gridCell.getForecolor() == null ? blackIndex : getNearestColor(gridCell.getForecolor()).getIndex();
702         BoxStyle boxStyle = new BoxStyle(gridCell.getBox(), backcolor, gridForecolor);
703         StyleInfo style = new StyleInfo(mode, backcolor, horizontalAlignment, verticalAlignment, rotation, font, boxStyle);
704         return style;
705     }
706
707     /**
708      *
709      */

710     protected static short getBorder(byte pen)
711     {
712         short border = HSSFCellStyle.BORDER_NONE;
713         
714         switch (pen)
715         {
716             case JRGraphicElement.PEN_DOTTED :
717             {
718                 border = HSSFCellStyle.BORDER_DASHED;
719                 break;
720             }
721             case JRGraphicElement.PEN_4_POINT :
722             {
723                 border = HSSFCellStyle.BORDER_THICK;
724                 break;
725             }
726             case JRGraphicElement.PEN_2_POINT :
727             {
728                 border = HSSFCellStyle.BORDER_THICK;
729                 break;
730             }
731             case JRGraphicElement.PEN_THIN :
732             {
733                 border = HSSFCellStyle.BORDER_THIN;
734                 break;
735             }
736             case JRGraphicElement.PEN_NONE :
737             {
738                 border = HSSFCellStyle.BORDER_NONE;
739                 break;
740             }
741             case JRGraphicElement.PEN_1_POINT :
742             default :
743             {
744                 border = HSSFCellStyle.BORDER_MEDIUM;
745                 break;
746             }
747         }
748         
749         return border;
750     }
751
752     protected void exportImage(JRPrintImage image, JRExporterGridCell gridCell, int colIndex, int rowIndex)
753     {
754         //nothing
755
}
756
757     protected ExporterElements getExporterElements()
758     {
759         return JRGridLayout.NO_IMAGES_EXPORTER;
760     }
761
762
763     protected void exportFrame(JRPrintFrame frame, JRExporterGridCell gridCell, int x, int y)
764     {
765         short mode = backgroundMode;
766         short backcolor = whiteIndex;
767         if (frame.getMode() == JRElement.MODE_OPAQUE)
768         {
769             mode = HSSFCellStyle.SOLID_FOREGROUND;
770             backcolor = getNearestColor(frame.getBackcolor()).getIndex();
771         }
772         
773         short forecolor = getNearestColor(frame.getForecolor()).getIndex();
774
775         HSSFCellStyle cellStyle =
776             getLoadedCellStyle(
777                 mode,
778                 backcolor,
779                 HSSFCellStyle.ALIGN_LEFT,
780                 HSSFCellStyle.VERTICAL_TOP,
781                 (short)0,
782                 getLoadedFont(getDefaultFont(), forecolor),
783                 gridCell
784                 );
785
786         createMergeRegion(gridCell, x, y, cellStyle);
787
788         cell = row.createCell((short)x);
789         cell.setCellStyle(cellStyle);
790     }
791     
792
793     protected static class BoxStyle
794     {
795         protected final short topBorder;
796         protected final short bottomBorder;
797         protected final short leftBorder;
798         protected final short rightBorder;
799         protected final short topBorderColour;
800         protected final short bottomBorderColour;
801         protected final short leftBorderColour;
802         protected final short rightBorderColour;
803         private final int hash;
804
805         public BoxStyle(JRBox box, short backcolor, short forecolor)
806         {
807             if(box != null && box.getTopBorder() != JRGraphicElement.PEN_NONE)
808             {
809                 topBorder = getBorder(box.getTopBorder());
810                 topBorderColour = box.getTopBorderColor() == null ? forecolor : getNearestColor(box.getTopBorderColor()).getIndex();
811             }
812             else
813             {
814                 topBorder = HSSFCellStyle.BORDER_NONE;
815                 topBorderColour = backcolor;
816             }
817             
818             if(box != null && box.getBottomBorder() != JRGraphicElement.PEN_NONE)
819             {
820                 bottomBorder = getBorder(box.getBottomBorder());
821                 bottomBorderColour = box.getBottomBorderColor() == null ? forecolor : getNearestColor(box.getBottomBorderColor()).getIndex();
822             }
823             else
824             {
825                 bottomBorder = HSSFCellStyle.BORDER_NONE;
826                 bottomBorderColour = backcolor;
827             }
828             
829             if(box != null && box.getLeftBorder() != JRGraphicElement.PEN_NONE)
830             {
831                 leftBorder = getBorder(box.getLeftBorder());
832                 leftBorderColour = box.getLeftBorderColor() == null ? forecolor : getNearestColor(box.getLeftBorderColor()).getIndex();
833             }
834             else
835             {
836                 leftBorder = HSSFCellStyle.BORDER_NONE;
837                 leftBorderColour = backcolor;
838             }
839             
840             if(box != null && box.getRightBorder() != JRGraphicElement.PEN_NONE)
841             {
842                 rightBorder = getBorder(box.getRightBorder());
843                 rightBorderColour = box.getRightBorderColor() == null ? forecolor : getNearestColor(box.getRightBorderColor()).getIndex();
844             }
845             else
846             {
847                 rightBorder = HSSFCellStyle.BORDER_NONE;
848                 rightBorderColour = backcolor;
849             }
850             
851             
852             hash = computeHash();
853         }
854
855         private int computeHash()
856         {
857             int hashCode = topBorder;
858             hashCode = 31*hashCode + topBorderColour;
859             hashCode = 31*hashCode + bottomBorder;
860             hashCode = 31*hashCode + bottomBorderColour;
861             hashCode = 31*hashCode + leftBorder;
862             hashCode = 31*hashCode + leftBorderColour;
863             hashCode = 31*hashCode + rightBorder;
864             hashCode = 31*hashCode + rightBorderColour;
865             return hashCode;
866         }
867         
868         public int hashCode()
869         {
870             return hash;
871         }
872         
873         public boolean equals(Object JavaDoc o)
874         {
875             BoxStyle b = (BoxStyle) o;
876             
877             return
878                 b.topBorder == topBorder &&
879                 b.topBorderColour == topBorderColour &&
880                 b.bottomBorder == bottomBorder &&
881                 b.bottomBorderColour == bottomBorderColour &&
882                 b.leftBorder == leftBorder &&
883                 b.leftBorderColour == leftBorderColour &&
884                 b.rightBorder == rightBorder &&
885                 b.rightBorderColour == rightBorderColour;
886         }
887         
888         public String JavaDoc toString()
889         {
890             return "(" +
891                 topBorder + "/" + topBorderColour + "," +
892                 bottomBorder + "/" + bottomBorderColour + "," +
893                 leftBorder + "/" + leftBorderColour + "," +
894                 rightBorder + "/" + rightBorderColour + ")";
895         }
896     }
897
898     
899     protected static class StyleInfo
900     {
901         protected final short mode;
902         protected final short backcolor;
903         protected final short horizontalAlignment;
904         protected final short verticalAlignment;
905         protected final short rotation;
906         protected final HSSFFont font;
907         protected final BoxStyle box;
908         private short dataFormat = -1;
909         private int hashCode;
910         
911         public StyleInfo(short mode, short backcolor, short horizontalAlignment, short verticalAlignment, short rotation, HSSFFont font, BoxStyle box)
912         {
913             this.mode = mode;
914             this.backcolor = backcolor;
915             this.horizontalAlignment = horizontalAlignment;
916             this.verticalAlignment = verticalAlignment;
917             this.rotation = rotation;
918             this.font = font;
919             this.box = box;
920             
921             hashCode = computeHash();
922         }
923
924         protected int computeHash()
925         {
926             int hash = mode;
927             hash = 31*hash + backcolor;
928             hash = 31*hash + horizontalAlignment;
929             hash = 31*hash + verticalAlignment;
930             hash = 31*hash + rotation;
931             hash = 31*hash + (font == null ? 0 : font.getIndex());
932             hash = 31*hash + (box == null ? 0 : box.hashCode());
933             hash = 31*hash + dataFormat;
934             return hash;
935         }
936         
937         public void setDataFormat(short dataFormat)
938         {
939             this.dataFormat = dataFormat;
940             hashCode = computeHash();
941         }
942         
943         public boolean hasDataFormat()
944         {
945             return dataFormat != -1;
946         }
947         
948         public short getDataFormat()
949         {
950             return dataFormat;
951         }
952         
953         public int hashCode()
954         {
955             return hashCode;
956         }
957         
958         public boolean equals(Object JavaDoc o)
959         {
960             StyleInfo s = (StyleInfo) o;
961             
962             return s.mode == mode
963                     && s.backcolor == backcolor
964                     && s.horizontalAlignment == horizontalAlignment
965                     && s.verticalAlignment == verticalAlignment
966                     && s.rotation == rotation
967                     && (s.font == null ? font == null : (font != null && s.font.getIndex() == font.getIndex()))
968                     && (s.box == null ? box == null : (box != null && s.box.equals(box)))
969                     && s.rotation == rotation;
970         }
971         
972         public String JavaDoc toString()
973         {
974             return "(" +
975                 mode + "," + backcolor + "," +
976                 horizontalAlignment + "," + verticalAlignment + "," +
977                 rotation + "," + font + "," +
978                 box + "," + dataFormat + ")";
979         }
980     }
981 }
982
983
Popular Tags