PR:
Obtained from:
Submitted by:
Reviewed by:


git-svn-id: https://svn.apache.org/repos/asf/jakarta/poi/trunk@352771 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Andrew C. Oliver 2002-07-15 01:27:17 +00:00
parent cb6ef3869f
commit e8a9cfa0c2
2 changed files with 295 additions and 209 deletions

View File

@ -1,10 +1,6 @@
package org.apache.poi.hssf.contrib.view; package org.apache.poi.hssf.contrib.view;
import java.awt.Graphics; import java.awt.*;
import java.awt.Insets;
import java.awt.Rectangle;
import java.awt.Color;
import java.awt.Component;
import javax.swing.border.AbstractBorder; import javax.swing.border.AbstractBorder;
@ -15,91 +11,105 @@ import org.apache.poi.hssf.usermodel.HSSFCellStyle;
* *
*/ */
public class SVBorder extends AbstractBorder { public class SVBorder extends AbstractBorder {
Color northColor = null; private Color northColor = null;
Color eastColor = null; private Color eastColor = null;
Color southColor = null; private Color southColor = null;
Color westColor = null; private Color westColor = null;
int northBorderType; private int northBorderType = HSSFCellStyle.BORDER_NONE;
int eastBorderType; private int eastBorderType =HSSFCellStyle.BORDER_NONE;
int southBorderType; private int southBorderType = HSSFCellStyle.BORDER_NONE;
int westBorderType; private int westBorderType = HSSFCellStyle.BORDER_NONE;
boolean northBorder=false; private boolean northBorder=false;
boolean eastBorder=false; private boolean eastBorder=false;
boolean southBorder=false; private boolean southBorder=false;
boolean westBorder=false; private boolean westBorder=false;
private boolean selected = false;
public SVBorder(Color northColor, Color eastColor, public void setBorder(Color northColor, Color eastColor,
Color southColor, Color westColor, Color southColor, Color westColor,
int northBorderType, int eastBorderType, int northBorderType, int eastBorderType,
int southBorderType, int westBorderType, int southBorderType, int westBorderType,
boolean northBorder, boolean eastBorder, boolean selected) {
boolean southBorder, boolean westBorder) {
this.northColor = northColor;
this.eastColor = eastColor; this.eastColor = eastColor;
this.southColor = southColor; this.southColor = southColor;
this.westColor = westColor; this.westColor = westColor;
this.northBorderType = northBorderType; this.northBorderType = northBorderType;
this.eastBorderType = eastBorderType; this.eastBorderType = eastBorderType;
this.southBorderType = southBorderType; this.southBorderType = southBorderType;
this.westBorderType = westBorderType; this.westBorderType = westBorderType;
this.northBorder=northBorder; this.northBorder=northBorderType != HSSFCellStyle.BORDER_NONE;
this.eastBorder=eastBorder; this.eastBorder=eastBorderType != HSSFCellStyle.BORDER_NONE;
this.southBorder=southBorder; this.southBorder=southBorderType != HSSFCellStyle.BORDER_NONE;
this.westBorder=westBorder; this.westBorder=westBorderType != HSSFCellStyle.BORDER_NONE;
this.selected = selected;
} }
public void paintBorder(Component c, Graphics g, int x, int y, int width, public void paintBorder(Component c, Graphics g, int x, int y, int width,
int height) { int height) {
Color oldColor = g.getColor(); Color oldColor = g.getColor();
int i;
// System.err.println("northBorder="+northBorderType);
paintNormalBorders(g, x, y, width, height); paintSelectedBorder(g, x, y, width, height);
paintDottedBorders(g, x, y, width, height); paintNormalBorders(g, x, y, width, height);
paintDottedBorders(g, x, y, width, height);
paintDashedBorders(g, x, y, width, height); paintDashedBorders(g, x, y, width, height);
paintDoubleBorders(g, x, y, width, height); paintDoubleBorders(g, x, y, width, height);
paintDashDotDotBorders(g, x, y, width, height); paintDashDotDotBorders(g, x, y, width, height);
g.setColor(oldColor); g.setColor(oldColor);
} }
private void paintNormalBorders(Graphics g, int x, int y, int width, private void paintSelectedBorder(Graphics g, int x, int y, int width,
int height) { int height) {
if (selected) {
if (northBorder && //Need to setup thickness of 2
g.setColor(Color.black);
//paint the border
g.drawRect(x,y,width-1,height-1);
//paint the filled rectangle at the bottom left hand position
g.fillRect(x+width-5, y+height-5, 5, 5);
}
}
private void paintNormalBorders(Graphics g, int x, int y, int width,
int height) {
if (northBorder &&
((northBorderType == HSSFCellStyle.BORDER_THIN) || ((northBorderType == HSSFCellStyle.BORDER_THIN) ||
(northBorderType == HSSFCellStyle.BORDER_MEDIUM) || (northBorderType == HSSFCellStyle.BORDER_MEDIUM) ||
(northBorderType == HSSFCellStyle.BORDER_THICK) (northBorderType == HSSFCellStyle.BORDER_THICK)
) )
) { ) {
int thickness = getThickness(northBorderType); int thickness = getThickness(northBorderType);
g.setColor(northColor); g.setColor(northColor);
for (int k=0; k < thickness; k++) { for (int k=0; k < thickness; k++) {
g.drawLine(x,y+k,width,y+k); g.drawLine(x,y+k,width,y+k);
} }
} }
if (eastBorder && if (eastBorder &&
((eastBorderType == HSSFCellStyle.BORDER_THIN) || ((eastBorderType == HSSFCellStyle.BORDER_THIN) ||
(eastBorderType == HSSFCellStyle.BORDER_MEDIUM) || (eastBorderType == HSSFCellStyle.BORDER_MEDIUM) ||
(eastBorderType == HSSFCellStyle.BORDER_THICK) (eastBorderType == HSSFCellStyle.BORDER_THICK)
) )
) { ) {
int thickness = getThickness(eastBorderType); int thickness = getThickness(eastBorderType);
g.setColor(eastColor); g.setColor(eastColor);
for (int k=0; k < thickness; k++) { for (int k=0; k < thickness; k++) {
g.drawLine(width-k,y,width-k,height); g.drawLine(width-k,y,width-k,height);
} }
} }
if (southBorder && if (southBorder &&
((southBorderType == HSSFCellStyle.BORDER_THIN) || ((southBorderType == HSSFCellStyle.BORDER_THIN) ||
(southBorderType == HSSFCellStyle.BORDER_MEDIUM) || (southBorderType == HSSFCellStyle.BORDER_MEDIUM) ||
(southBorderType == HSSFCellStyle.BORDER_THICK) (southBorderType == HSSFCellStyle.BORDER_THICK)
@ -108,22 +118,22 @@ public class SVBorder extends AbstractBorder {
int thickness = getThickness(southBorderType); int thickness = getThickness(southBorderType);
g.setColor(southColor); g.setColor(southColor);
for (int k=0; k < thickness; k++) { for (int k=0; k < thickness; k++) {
g.drawLine(x,height - k,width,height - k); g.drawLine(x,height - k,width,height - k);
} }
} }
if (westBorder && if (westBorder &&
((westBorderType == HSSFCellStyle.BORDER_THIN) || ((westBorderType == HSSFCellStyle.BORDER_THIN) ||
(westBorderType == HSSFCellStyle.BORDER_MEDIUM) || (westBorderType == HSSFCellStyle.BORDER_MEDIUM) ||
(westBorderType == HSSFCellStyle.BORDER_THICK) (westBorderType == HSSFCellStyle.BORDER_THICK)
) )
) { ) {
int thickness = getThickness(westBorderType); int thickness = getThickness(westBorderType);
g.setColor(westColor); g.setColor(westColor);
for (int k=0; k < thickness; k++) { for (int k=0; k < thickness; k++) {
g.drawLine(x+k,y,x+k,height); g.drawLine(x+k,y,x+k,height);
@ -131,13 +141,13 @@ public class SVBorder extends AbstractBorder {
} }
} }
private void paintDottedBorders(Graphics g, int x, int y, int width, private void paintDottedBorders(Graphics g, int x, int y, int width,
int height) { int height) {
if (northBorder && if (northBorder &&
northBorderType == HSSFCellStyle.BORDER_DOTTED) { northBorderType == HSSFCellStyle.BORDER_DOTTED) {
int thickness = getThickness(northBorderType); int thickness = getThickness(northBorderType);
g.setColor(northColor); g.setColor(northColor);
for (int k=0; k < thickness; k++) { for (int k=0; k < thickness; k++) {
for (int xc = x; xc < width; xc=xc+2) { for (int xc = x; xc < width; xc=xc+2) {
@ -146,14 +156,14 @@ public class SVBorder extends AbstractBorder {
} }
} }
if (eastBorder && if (eastBorder &&
eastBorderType == HSSFCellStyle.BORDER_DOTTED eastBorderType == HSSFCellStyle.BORDER_DOTTED
) { ) {
int thickness = getThickness(eastBorderType); int thickness = getThickness(eastBorderType);
thickness++; //need for dotted borders to show up east thickness++; //need for dotted borders to show up east
g.setColor(eastColor); g.setColor(eastColor);
for (int k=0; k < thickness; k++) { for (int k=0; k < thickness; k++) {
for (int yc=y;yc < height; yc=yc+2) { for (int yc=y;yc < height; yc=yc+2) {
@ -162,13 +172,13 @@ public class SVBorder extends AbstractBorder {
} }
} }
if (southBorder && if (southBorder &&
southBorderType == HSSFCellStyle.BORDER_DOTTED southBorderType == HSSFCellStyle.BORDER_DOTTED
) { ) {
int thickness = getThickness(southBorderType); int thickness = getThickness(southBorderType);
thickness++; thickness++;
g.setColor(southColor); g.setColor(southColor);
for (int k=0; k < thickness; k++) { for (int k=0; k < thickness; k++) {
for (int xc = x; xc < width; xc=xc+2) { for (int xc = x; xc < width; xc=xc+2) {
g.drawLine(xc,height-k,xc,height-k); g.drawLine(xc,height-k,xc,height-k);
@ -176,14 +186,14 @@ public class SVBorder extends AbstractBorder {
} }
} }
if (westBorder && if (westBorder &&
westBorderType == HSSFCellStyle.BORDER_DOTTED westBorderType == HSSFCellStyle.BORDER_DOTTED
) { ) {
int thickness = getThickness(westBorderType); int thickness = getThickness(westBorderType);
// thickness++; // thickness++;
g.setColor(westColor); g.setColor(westColor);
for (int k=0; k < thickness; k++) { for (int k=0; k < thickness; k++) {
for (int yc=y;yc < height; yc=yc+2) { for (int yc=y;yc < height; yc=yc+2) {
@ -194,20 +204,20 @@ public class SVBorder extends AbstractBorder {
} }
private void paintDashedBorders(Graphics g, int x, int y, int width, private void paintDashedBorders(Graphics g, int x, int y, int width,
int height) { int height) {
if (northBorder && if (northBorder &&
((northBorderType == HSSFCellStyle.BORDER_DASHED) || ((northBorderType == HSSFCellStyle.BORDER_DASHED) ||
(northBorderType == HSSFCellStyle.BORDER_HAIR)) (northBorderType == HSSFCellStyle.BORDER_HAIR))
) { ) {
int thickness = getThickness(northBorderType); int thickness = getThickness(northBorderType);
int dashlength = 1; int dashlength = 1;
if (northBorderType == HSSFCellStyle.BORDER_DASHED)
dashlength = 2;
g.setColor(northColor); if (northBorderType == HSSFCellStyle.BORDER_DASHED)
dashlength = 2;
g.setColor(northColor);
for (int k=0; k < thickness; k++) { for (int k=0; k < thickness; k++) {
for (int xc = x; xc < width; xc=xc+5) { for (int xc = x; xc < width; xc=xc+5) {
@ -216,9 +226,9 @@ public class SVBorder extends AbstractBorder {
} }
} }
if (eastBorder && if (eastBorder &&
((eastBorderType == HSSFCellStyle.BORDER_DASHED) || ((eastBorderType == HSSFCellStyle.BORDER_DASHED) ||
(eastBorderType == HSSFCellStyle.BORDER_HAIR)) (eastBorderType == HSSFCellStyle.BORDER_HAIR))
) { ) {
int thickness = getThickness(eastBorderType); int thickness = getThickness(eastBorderType);
@ -226,11 +236,11 @@ public class SVBorder extends AbstractBorder {
int dashlength = 1; int dashlength = 1;
if (eastBorderType == HSSFCellStyle.BORDER_DASHED)
dashlength = 2;
g.setColor(eastColor); if (eastBorderType == HSSFCellStyle.BORDER_DASHED)
dashlength = 2;
g.setColor(eastColor);
for (int k=0; k < thickness; k++) { for (int k=0; k < thickness; k++) {
for (int yc=y;yc < height; yc=yc+5) { for (int yc=y;yc < height; yc=yc+5) {
@ -239,7 +249,7 @@ public class SVBorder extends AbstractBorder {
} }
} }
if (southBorder && if (southBorder &&
((southBorderType == HSSFCellStyle.BORDER_DASHED) || ((southBorderType == HSSFCellStyle.BORDER_DASHED) ||
(southBorderType == HSSFCellStyle.BORDER_HAIR)) (southBorderType == HSSFCellStyle.BORDER_HAIR))
) { ) {
@ -248,11 +258,11 @@ public class SVBorder extends AbstractBorder {
thickness++; thickness++;
int dashlength = 1; int dashlength = 1;
if (southBorderType == HSSFCellStyle.BORDER_DASHED)
dashlength = 2;
g.setColor(southColor); if (southBorderType == HSSFCellStyle.BORDER_DASHED)
dashlength = 2;
g.setColor(southColor);
for (int k=0; k < thickness; k++) { for (int k=0; k < thickness; k++) {
for (int xc = x; xc < width; xc=xc+5) { for (int xc = x; xc < width; xc=xc+5) {
g.drawLine(xc,height-k,xc+dashlength,height-k); g.drawLine(xc,height-k,xc+dashlength,height-k);
@ -260,7 +270,7 @@ public class SVBorder extends AbstractBorder {
} }
} }
if (westBorder && if (westBorder &&
((westBorderType == HSSFCellStyle.BORDER_DASHED) || ((westBorderType == HSSFCellStyle.BORDER_DASHED) ||
(westBorderType == HSSFCellStyle.BORDER_HAIR)) (westBorderType == HSSFCellStyle.BORDER_HAIR))
) { ) {
@ -269,11 +279,11 @@ public class SVBorder extends AbstractBorder {
// thickness++; // thickness++;
int dashlength = 1; int dashlength = 1;
if (westBorderType == HSSFCellStyle.BORDER_DASHED)
dashlength = 2;
g.setColor(westColor); if (westBorderType == HSSFCellStyle.BORDER_DASHED)
dashlength = 2;
g.setColor(westColor);
for (int k=0; k < thickness; k++) { for (int k=0; k < thickness; k++) {
for (int yc=y;yc < height; yc=yc+5) { for (int yc=y;yc < height; yc=yc+5) {
@ -284,12 +294,12 @@ public class SVBorder extends AbstractBorder {
} }
private void paintDoubleBorders(Graphics g, int x, int y, int width, private void paintDoubleBorders(Graphics g, int x, int y, int width,
int height) { int height) {
if (northBorder && if (northBorder &&
northBorderType == HSSFCellStyle.BORDER_DOUBLE) { northBorderType == HSSFCellStyle.BORDER_DOUBLE) {
g.setColor(northColor); g.setColor(northColor);
int leftx=x; int leftx=x;
int rightx=width; int rightx=width;
@ -306,56 +316,56 @@ public class SVBorder extends AbstractBorder {
g.drawLine(leftx,y+2,rightx,y+2); g.drawLine(leftx,y+2,rightx,y+2);
} }
if (eastBorder && if (eastBorder &&
eastBorderType == HSSFCellStyle.BORDER_DOUBLE eastBorderType == HSSFCellStyle.BORDER_DOUBLE
) { ) {
int thickness = getThickness(eastBorderType); int thickness = getThickness(eastBorderType);
thickness++; //need for dotted borders to show up east thickness++; //need for dotted borders to show up east
g.setColor(eastColor); g.setColor(eastColor);
int topy=y; int topy=y;
int bottomy=height; int bottomy=height;
if (northBorder) if (northBorder)
topy=y+3; topy=y+3;
if (southBorder) if (southBorder)
bottomy=height-3; bottomy=height-3;
g.drawLine(width-1,y,width-1,height); g.drawLine(width-1,y,width-1,height);
g.drawLine(width-3,topy,width-3,bottomy); g.drawLine(width-3,topy,width-3,bottomy);
} }
if (southBorder && if (southBorder &&
southBorderType == HSSFCellStyle.BORDER_DOUBLE southBorderType == HSSFCellStyle.BORDER_DOUBLE
) { ) {
g.setColor(southColor); g.setColor(southColor);
int leftx=y; int leftx=y;
int rightx=width; int rightx=width;
if (westBorder) if (westBorder)
leftx=x+3; leftx=x+3;
if (eastBorder) if (eastBorder)
rightx=width-3; rightx=width-3;
g.drawLine(x,height - 1,width,height - 1); g.drawLine(x,height - 1,width,height - 1);
g.drawLine(leftx,height - 3,rightx,height - 3); g.drawLine(leftx,height - 3,rightx,height - 3);
} }
if (westBorder && if (westBorder &&
westBorderType == HSSFCellStyle.BORDER_DOUBLE westBorderType == HSSFCellStyle.BORDER_DOUBLE
) { ) {
int thickness = getThickness(westBorderType); int thickness = getThickness(westBorderType);
// thickness++; // thickness++;
g.setColor(westColor); g.setColor(westColor);
int topy=y; int topy=y;
int bottomy=height-3; int bottomy=height-3;
@ -372,29 +382,29 @@ public class SVBorder extends AbstractBorder {
} }
private void paintDashDotDotBorders(Graphics g, int x, int y, int width, private void paintDashDotDotBorders(Graphics g, int x, int y, int width,
int height) { int height) {
if (northBorder && if (northBorder &&
((northBorderType == HSSFCellStyle.BORDER_DASH_DOT_DOT) || ((northBorderType == HSSFCellStyle.BORDER_DASH_DOT_DOT) ||
(northBorderType == HSSFCellStyle.BORDER_MEDIUM_DASH_DOT_DOT)) (northBorderType == HSSFCellStyle.BORDER_MEDIUM_DASH_DOT_DOT))
) { ) {
int thickness = getThickness(northBorderType); int thickness = getThickness(northBorderType);
g.setColor(northColor); g.setColor(northColor);
for (int l=x; l < width;) { for (int l=x; l < width;) {
l=l+drawDashDotDot(g, l, y, thickness, true, true); l=l+drawDashDotDot(g, l, y, thickness, true, true);
} }
} }
if (eastBorder && if (eastBorder &&
((eastBorderType == HSSFCellStyle.BORDER_DASH_DOT_DOT) || ((eastBorderType == HSSFCellStyle.BORDER_DASH_DOT_DOT) ||
(eastBorderType == HSSFCellStyle.BORDER_MEDIUM_DASH_DOT_DOT)) (eastBorderType == HSSFCellStyle.BORDER_MEDIUM_DASH_DOT_DOT))
) { ) {
int thickness = getThickness(eastBorderType); int thickness = getThickness(eastBorderType);
g.setColor(eastColor); g.setColor(eastColor);
for (int l=y;l < height;) { for (int l=y;l < height;) {
//System.err.println("drawing east"); //System.err.println("drawing east");
@ -402,14 +412,14 @@ public class SVBorder extends AbstractBorder {
} }
} }
if (southBorder && if (southBorder &&
((southBorderType == HSSFCellStyle.BORDER_DASH_DOT_DOT) || ((southBorderType == HSSFCellStyle.BORDER_DASH_DOT_DOT) ||
(southBorderType == HSSFCellStyle.BORDER_MEDIUM_DASH_DOT_DOT)) (southBorderType == HSSFCellStyle.BORDER_MEDIUM_DASH_DOT_DOT))
) { ) {
int thickness = getThickness(southBorderType); int thickness = getThickness(southBorderType);
g.setColor(southColor); g.setColor(southColor);
for (int l=x; l < width;) { for (int l=x; l < width;) {
//System.err.println("drawing south"); //System.err.println("drawing south");
@ -417,14 +427,14 @@ public class SVBorder extends AbstractBorder {
} }
} }
if (westBorder && if (westBorder &&
((westBorderType == HSSFCellStyle.BORDER_DASH_DOT_DOT) || ((westBorderType == HSSFCellStyle.BORDER_DASH_DOT_DOT) ||
(westBorderType == HSSFCellStyle.BORDER_MEDIUM_DASH_DOT_DOT)) (westBorderType == HSSFCellStyle.BORDER_MEDIUM_DASH_DOT_DOT))
) { ) {
int thickness = getThickness(westBorderType); int thickness = getThickness(westBorderType);
g.setColor(westColor); g.setColor(westColor);
for (int l=y;l < height;) { for (int l=y;l < height;) {
//System.err.println("drawing west"); //System.err.println("drawing west");
@ -436,25 +446,25 @@ public class SVBorder extends AbstractBorder {
/** /**
* Draws one dash dot dot horizontally or vertically with thickness drawn * Draws one dash dot dot horizontally or vertically with thickness drawn
* incrementally to either the right or left. * incrementally to either the right or left.
* *
* @param g graphics object for drawing with * @param g graphics object for drawing with
* @param x the x origin of the line * @param x the x origin of the line
* @param y the y origin of the line * @param y the y origin of the line
* @param thickness the thickness of the line * @param thickness the thickness of the line
* @param horizontal or vertical (true for horizontal) * @param horizontal or vertical (true for horizontal)
* @param right/bottom or left/top thickness (true for right or top), * @param right/bottom or left/top thickness (true for right or top),
* if true then the x or y origin will be incremented to provide * if true then the x or y origin will be incremented to provide
* thickness, if false, they'll be decremented. For vertical * thickness, if false, they'll be decremented. For vertical
* borders, x is incremented or decremented, for horizontal its y. * borders, x is incremented or decremented, for horizontal its y.
* Just set to true for north and west, and false for east and * Just set to true for north and west, and false for east and
* south. * south.
* @returns length - returns the length of the line. * @returns length - returns the length of the line.
*/ */
private int drawDashDotDot(Graphics g,int x, int y, int thickness, private int drawDashDotDot(Graphics g,int x, int y, int thickness,
boolean horizontal, boolean horizontal,
boolean rightBottom) { boolean rightBottom) {
for (int t=0; t < thickness; t++) { for (int t=0; t < thickness; t++) {
if (!rightBottom) { if (!rightBottom) {
t = 0 - t; //add negative thickness so we go the other way t = 0 - t; //add negative thickness so we go the other way
@ -500,7 +510,7 @@ public class SVBorder extends AbstractBorder {
default: default:
retval=1; retval=1;
} }
return retval; return retval;
} }

View File

@ -67,15 +67,11 @@ import java.awt.Rectangle;
import java.awt.Font; import java.awt.Font;
import java.io.Serializable; import java.io.Serializable;
import java.text.*;
import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.util.HSSFColor; import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.hssf.util.HSSFColor.WHITE;
/** /**
@ -87,10 +83,93 @@ import org.apache.poi.hssf.util.HSSFColor.WHITE;
public class SVTableCellRenderer extends JLabel public class SVTableCellRenderer extends JLabel
implements TableCellRenderer, Serializable implements TableCellRenderer, Serializable
{ {
private static final Color black = getAWTColor(new HSSFColor.BLACK());
private static final Color white = getAWTColor(new HSSFColor.WHITE());
protected static Border noFocusBorder = new EmptyBorder(1, 1, 1, 1); protected static Border noFocusBorder = new EmptyBorder(1, 1, 1, 1);
protected SVBorder cellBorder = new SVBorder();
private HSSFWorkbook wb = null; private HSSFWorkbook wb = null;
private Hashtable colors = HSSFColor.getIndexHash(); private Hashtable colors = HSSFColor.getIndexHash();
/** This class holds the references to the predefined cell formats.
*/
private class CellFormatter {
private Format[] textFormatter;
private DecimalFormat generalNumberFormat = new DecimalFormat("0");
public CellFormatter() {
textFormatter = new Format[0x31];
textFormatter[0x01] = new DecimalFormat("0");
textFormatter[0x02] = new DecimalFormat("0.00");
textFormatter[0x03] = new DecimalFormat("#,##0");
textFormatter[0x04] = new DecimalFormat("#,##0.00");
textFormatter[0x05] = new DecimalFormat("$#,##0;$#,##0");
textFormatter[0x06] = new DecimalFormat("$#,##0;$#,##0");
textFormatter[0x07] = new DecimalFormat("$#,##0.00;$#,##0.00");
textFormatter[0x08] = new DecimalFormat("$#,##0.00;$#,##0.00");
textFormatter[0x09] = new DecimalFormat("0%");
textFormatter[0x0A] = new DecimalFormat("0.00%");
textFormatter[0x0B] = new DecimalFormat("0.00E0");
//?? textFormatter[0x0C] = new DecimalFormat("# ?/?");
//?? textFormatter[0x0D] = new DecimalFormat("# ??/??");
textFormatter[0x0E] = new SimpleDateFormat("M/d/yy");
textFormatter[0x0F] = new SimpleDateFormat("d-MMM-yy");
textFormatter[0x10] = new SimpleDateFormat("d-MMM");
textFormatter[0x11] = new SimpleDateFormat("MMM-yy");
textFormatter[0x12] = new SimpleDateFormat("h:mm a");
textFormatter[0x13] = new SimpleDateFormat("h:mm:ss a");
textFormatter[0x14] = new SimpleDateFormat("h:mm");
textFormatter[0x15] = new SimpleDateFormat("h:mm:ss");
textFormatter[0x16] = new SimpleDateFormat("M/d/yy h:mm");
// 0x17 - 0x24 reserved for international and undocumented 0x25, "(#,##0_);(#,##0)"
//start at 0x26
//jmh need to do colour
//"(#,##0_);[Red](#,##0)"
textFormatter[0x26] = new DecimalFormat("#,##0;#,##0");
//jmh need to do colour
//(#,##0.00_);(#,##0.00)
textFormatter[0x27] = new DecimalFormat("#,##0.00;#,##0.00");
textFormatter[0x28] = new DecimalFormat("#,##0.00;#,##0.00");
//?? textFormatter[0x29] = new DecimalFormat("_(*#,##0_);_(*(#,##0);_(* \"-\"_);_(@_)");
//?? textFormatter[0x2A] = new DecimalFormat("_($*#,##0_);_($*(#,##0);_($* \"-\"_);_(@_)");
//?? textFormatter[0x2B] = new DecimalFormat("_(*#,##0.00_);_(*(#,##0.00);_(*\"-\"??_);_(@_)");
//?? textFormatter[0x2C] = new DecimalFormat("_($*#,##0.00_);_($*(#,##0.00);_($*\"-\"??_);_(@_)");
textFormatter[0x2D] = new SimpleDateFormat("mm:ss");
//?? textFormatter[0x2E] = new SimpleDateFormat("[h]:mm:ss");
textFormatter[0x2F] = new SimpleDateFormat("mm:ss.0");
textFormatter[0x30] = new DecimalFormat("##0.0E0");
}
public String format(short index, Object value) {
if (index == 0)
return value.toString();
if (textFormatter[index] == null)
throw new RuntimeException("Sorry. I cant handle the format code :"+Integer.toHexString(index));
return textFormatter[index].format(value);
}
public String format(short index, double value) {
if (index == 0)
return generalNumberFormat.format(value);
if (textFormatter[index] == null)
throw new RuntimeException("Sorry. I cant handle the format code :"+Integer.toHexString(index));
if (textFormatter[index] instanceof DecimalFormat) {
return ((DecimalFormat)textFormatter[index]).format(value);
}
else throw new RuntimeException("Sorry. I cant handle a non decimal formatter for a decimal value :"+Integer.toHexString(index));
}
public boolean useRedColor(short index, double value) {
return (((index == 0x06)||(index == 0x08)||(index == 0x26) || (index == 0x27)) && (value < 0));
}
}
private final CellFormatter cellFormatter = new CellFormatter();
public SVTableCellRenderer(HSSFWorkbook wb) { public SVTableCellRenderer(HSSFWorkbook wb) {
super(); super();
setOpaque(true); setOpaque(true);
@ -102,17 +181,11 @@ public class SVTableCellRenderer extends JLabel
boolean isSelected, boolean hasFocus, int row, int column) { boolean isSelected, boolean hasFocus, int row, int column) {
boolean isBorderSet = false; boolean isBorderSet = false;
if (isSelected) {
setForeground(table.getSelectionForeground());
setBackground(table.getSelectionBackground());
}
//If the JTables default cell renderer has been setup correctly the //If the JTables default cell renderer has been setup correctly the
//value will be the HSSFCell that we are trying to render //value will be the HSSFCell that we are trying to render
HSSFCell c = (HSSFCell)value; HSSFCell c = (HSSFCell)value;
if (c != null) { if (c != null) {
HSSFCellStyle s = c.getCellStyle(); HSSFCellStyle s = c.getCellStyle();
HSSFFont f = wb.getFontAt(s.getFontIndex()); HSSFFont f = wb.getFontAt(s.getFontIndex());
boolean isbold = f.getBoldweight() > HSSFFont.BOLDWEIGHT_NORMAL; boolean isbold = f.getBoldweight() > HSSFFont.BOLDWEIGHT_NORMAL;
@ -128,98 +201,87 @@ public class SVTableCellRenderer extends JLabel
Font font = new Font(f.getFontName(),fontstyle,fontheight); Font font = new Font(f.getFontName(),fontstyle,fontheight);
setFont(font); setFont(font);
HSSFColor clr = null;
if (s.getFillPattern() == HSSFCellStyle.SOLID_FOREGROUND) { if (s.getFillPattern() == HSSFCellStyle.SOLID_FOREGROUND) {
clr = (HSSFColor)colors.get(new Integer(s.getFillForegroundColor())); setBackground(getAWTColor(s.getFillForegroundColor(), white));
} } else setBackground(white);
if (clr == null) clr = new HSSFColor.WHITE();
short[] rgb = clr.getTriplet(); setForeground(getAWTColor(f.getColor(), black));
Color awtcolor = new Color(rgb[0],rgb[1],rgb[2]);
setBackground(awtcolor); cellBorder.setBorder(getAWTColor(s.getTopBorderColor(), black),
getAWTColor(s.getRightBorderColor(), black),
getAWTColor(s.getBottomBorderColor(), black),
getAWTColor(s.getLeftBorderColor(), black),
s.getBorderTop(), s.getBorderRight(),
s.getBorderBottom(), s.getBorderLeft(),
hasFocus);
setBorder(cellBorder);
isBorderSet=true;
clr = (HSSFColor)colors.get(new Integer(f.getColor())); //Set the value that is rendered for the cell
if (clr == null) clr = new HSSFColor.BLACK(); switch (c.getCellType()) {
rgb = clr.getTriplet(); case HSSFCell.CELL_TYPE_BLANK:
awtcolor = new Color(rgb[0],rgb[1],rgb[2]); setValue("");
setForeground(awtcolor); break;
case HSSFCell.CELL_TYPE_BOOLEAN:
/* if (s.getBorderBottom() != HSSFCellStyle.BORDER_NONE || if (c.getBooleanCellValue()) {
s.getBorderTop() != HSSFCellStyle.BORDER_NONE || setValue("true");
s.getBorderLeft() != HSSFCellStyle.BORDER_NONE || } else {
s.getBorderRight() != HSSFCellStyle.BORDER_NONE) { setValue("false");
*/ }
int borderTop = s.getBorderTop(); break;
int borderRight = s.getBorderRight(); case HSSFCell.CELL_TYPE_FORMULA:
int borderBottom = s.getBorderBottom(); case HSSFCell.CELL_TYPE_NUMERIC:
int borderLeft = s.getBorderLeft(); short format = s.getDataFormat();
double numericValue = c.getNumericCellValue();
//OUCH! This could causing rendering performance problems. if (cellFormatter.useRedColor(format, numericValue))
//Need to somehow create once and store setForeground(Color.red);
SVBorder border = new SVBorder(Color.black, Color.black, else setForeground(null);
Color.black, Color.black, setValue(cellFormatter.format(format, c.getNumericCellValue()));
borderTop, borderRight, break;
borderBottom, borderLeft, case HSSFCell.CELL_TYPE_STRING:
s.getBorderTop() != HSSFCellStyle.BORDER_NONE, setValue(c.getStringCellValue());
s.getBorderRight() != HSSFCellStyle.BORDER_NONE, break;
s.getBorderBottom() != HSSFCellStyle.BORDER_NONE, default:
s.getBorderLeft() != HSSFCellStyle.BORDER_NONE); setValue("?");
setBorder(border); }
isBorderSet=true; //Set the text alignment of the cell
switch (s.getAlignment()) {
//Set the value that is rendered for the cell case HSSFCellStyle.ALIGN_LEFT:
switch (c.getCellType()) { case HSSFCellStyle.ALIGN_JUSTIFY:
case HSSFCell.CELL_TYPE_BLANK: case HSSFCellStyle.ALIGN_FILL:
setValue(""); setHorizontalAlignment(SwingConstants.LEFT);
break; break;
case HSSFCell.CELL_TYPE_BOOLEAN: case HSSFCellStyle.ALIGN_CENTER:
if (c.getBooleanCellValue()) { case HSSFCellStyle.ALIGN_CENTER_SELECTION:
setValue("true"); setHorizontalAlignment(SwingConstants.CENTER);
} else { break;
setValue("false"); case HSSFCellStyle.ALIGN_GENERAL:
} case HSSFCellStyle.ALIGN_RIGHT:
break; setHorizontalAlignment(SwingConstants.RIGHT);
case HSSFCell.CELL_TYPE_FORMULA: break;
case HSSFCell.CELL_TYPE_NUMERIC: default:
setValue(""+c.getNumericCellValue()); setHorizontalAlignment(SwingConstants.LEFT);
break; break;
case HSSFCell.CELL_TYPE_STRING: }
setValue(c.getStringCellValue()); } else {
break; setValue("");
default: setBackground(white);
setValue("?"); }
}
//Set the text alignment of the cell
switch (s.getAlignment()) {
case HSSFCellStyle.ALIGN_GENERAL:
case HSSFCellStyle.ALIGN_LEFT:
case HSSFCellStyle.ALIGN_JUSTIFY:
case HSSFCellStyle.ALIGN_FILL:
setHorizontalAlignment(SwingConstants.LEFT);
break;
case HSSFCellStyle.ALIGN_CENTER:
case HSSFCellStyle.ALIGN_CENTER_SELECTION:
setHorizontalAlignment(SwingConstants.CENTER);
break;
case HSSFCellStyle.ALIGN_RIGHT:
setHorizontalAlignment(SwingConstants.RIGHT);
break;
default:
setHorizontalAlignment(SwingConstants.LEFT);
break;
}
// }
} else {
setValue("");
setBackground(Color.white);
}
if (hasFocus) { if (hasFocus) {
if (!isBorderSet) { if (!isBorderSet) {
setBorder( UIManager.getBorder("Table.focusCellHighlightBorder") ); cellBorder.setBorder(black,
black,
black,
black,
HSSFCellStyle.BORDER_NONE,
HSSFCellStyle.BORDER_NONE,
HSSFCellStyle.BORDER_NONE,
HSSFCellStyle.BORDER_NONE,
isSelected);
setBorder(cellBorder);
} }
if (table.isCellEditable(row, column)) { if (table.isCellEditable(row, column)) {
setForeground( UIManager.getColor("Table.focusCellForeground") ); setForeground( UIManager.getColor("Table.focusCellForeground") );
@ -234,11 +296,9 @@ public class SVTableCellRenderer extends JLabel
boolean colorMatch = (back != null) && ( back.equals(table.getBackground()) ) && table.isOpaque(); boolean colorMatch = (back != null) && ( back.equals(table.getBackground()) ) && table.isOpaque();
setOpaque(!colorMatch); setOpaque(!colorMatch);
// ---- end optimization to aviod painting background ---- // ---- end optimization to aviod painting background ----
return this; return this;
} }
public void validate() {} public void validate() {}
public void revalidate() {} public void revalidate() {}
@ -263,4 +323,20 @@ public class SVTableCellRenderer extends JLabel
protected void setValue(Object value) { protected void setValue(Object value) {
setText((value == null) ? "" : value.toString()); setText((value == null) ? "" : value.toString());
} }
/** This method retrieves the AWT Color representation from the colour hash table
*
*/
private final Color getAWTColor(int index, Color deflt) {
HSSFColor clr = (HSSFColor)colors.get(new Integer(index));
if (clr == null) return deflt;
return getAWTColor(clr);
}
private static final Color getAWTColor(HSSFColor clr) {
short[] rgb = clr.getTriplet();
return new Color(rgb[0],rgb[1],rgb[2]);
}
} }