package org.apache.poi.hssf.contrib.view;
-import java.awt.Graphics;
-import java.awt.Insets;
-import java.awt.Rectangle;
-import java.awt.Color;
-import java.awt.Component;
+import java.awt.*;
import javax.swing.border.AbstractBorder;
*
*/
public class SVBorder extends AbstractBorder {
- Color northColor = null;
- Color eastColor = null;
- Color southColor = null;
- Color westColor = null;
- int northBorderType;
- int eastBorderType;
- int southBorderType;
- int westBorderType;
- boolean northBorder=false;
- boolean eastBorder=false;
- boolean southBorder=false;
- boolean westBorder=false;
-
- public SVBorder(Color northColor, Color eastColor,
- Color southColor, Color westColor,
- int northBorderType, int eastBorderType,
- int southBorderType, int westBorderType,
- boolean northBorder, boolean eastBorder,
- boolean southBorder, boolean westBorder) {
- this.northColor = northColor;
+ private Color northColor = null;
+ private Color eastColor = null;
+ private Color southColor = null;
+ private Color westColor = null;
+ private int northBorderType = HSSFCellStyle.BORDER_NONE;
+ private int eastBorderType =HSSFCellStyle.BORDER_NONE;
+ private int southBorderType = HSSFCellStyle.BORDER_NONE;
+ private int westBorderType = HSSFCellStyle.BORDER_NONE;
+ private boolean northBorder=false;
+ private boolean eastBorder=false;
+ private boolean southBorder=false;
+ private boolean westBorder=false;
+ private boolean selected = false;
+
+ public void setBorder(Color northColor, Color eastColor,
+ Color southColor, Color westColor,
+ int northBorderType, int eastBorderType,
+ int southBorderType, int westBorderType,
+ boolean selected) {
this.eastColor = eastColor;
this.southColor = southColor;
this.westColor = westColor;
this.northBorderType = northBorderType;
this.eastBorderType = eastBorderType;
this.southBorderType = southBorderType;
- this.westBorderType = westBorderType;
- this.northBorder=northBorder;
- this.eastBorder=eastBorder;
- this.southBorder=southBorder;
- this.westBorder=westBorder;
+ this.westBorderType = westBorderType;
+ this.northBorder=northBorderType != HSSFCellStyle.BORDER_NONE;
+ this.eastBorder=eastBorderType != HSSFCellStyle.BORDER_NONE;
+ this.southBorder=southBorderType != HSSFCellStyle.BORDER_NONE;
+ this.westBorder=westBorderType != HSSFCellStyle.BORDER_NONE;
+ this.selected = selected;
}
public void paintBorder(Component c, Graphics g, int x, int y, int width,
int height) {
Color oldColor = g.getColor();
- int i;
- // System.err.println("northBorder="+northBorderType);
- paintNormalBorders(g, x, y, width, height);
- paintDottedBorders(g, x, y, width, height);
+
+ paintSelectedBorder(g, x, y, width, height);
+ paintNormalBorders(g, x, y, width, height);
+ paintDottedBorders(g, x, y, width, height);
paintDashedBorders(g, x, y, width, height);
paintDoubleBorders(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) {
-
- if (northBorder &&
+ if (selected) {
+ //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_MEDIUM) ||
- (northBorderType == HSSFCellStyle.BORDER_THICK)
+ (northBorderType == HSSFCellStyle.BORDER_THICK)
)
) {
int thickness = getThickness(northBorderType);
- g.setColor(northColor);
+ g.setColor(northColor);
for (int k=0; k < thickness; k++) {
g.drawLine(x,y+k,width,y+k);
}
}
- if (eastBorder &&
+ if (eastBorder &&
((eastBorderType == HSSFCellStyle.BORDER_THIN) ||
(eastBorderType == HSSFCellStyle.BORDER_MEDIUM) ||
- (eastBorderType == HSSFCellStyle.BORDER_THICK)
+ (eastBorderType == HSSFCellStyle.BORDER_THICK)
)
) {
int thickness = getThickness(eastBorderType);
- g.setColor(eastColor);
+ g.setColor(eastColor);
for (int k=0; k < thickness; k++) {
g.drawLine(width-k,y,width-k,height);
}
}
- if (southBorder &&
+ if (southBorder &&
((southBorderType == HSSFCellStyle.BORDER_THIN) ||
(southBorderType == HSSFCellStyle.BORDER_MEDIUM) ||
(southBorderType == HSSFCellStyle.BORDER_THICK)
int thickness = getThickness(southBorderType);
- g.setColor(southColor);
+ g.setColor(southColor);
for (int k=0; k < thickness; k++) {
g.drawLine(x,height - k,width,height - k);
}
}
- if (westBorder &&
+ if (westBorder &&
((westBorderType == HSSFCellStyle.BORDER_THIN) ||
(westBorderType == HSSFCellStyle.BORDER_MEDIUM) ||
- (westBorderType == HSSFCellStyle.BORDER_THICK)
+ (westBorderType == HSSFCellStyle.BORDER_THICK)
)
) {
int thickness = getThickness(westBorderType);
- g.setColor(westColor);
+ g.setColor(westColor);
for (int k=0; k < thickness; k++) {
g.drawLine(x+k,y,x+k,height);
}
}
- private void paintDottedBorders(Graphics g, int x, int y, int width,
+ private void paintDottedBorders(Graphics g, int x, int y, int width,
int height) {
- if (northBorder &&
+ if (northBorder &&
northBorderType == HSSFCellStyle.BORDER_DOTTED) {
int thickness = getThickness(northBorderType);
- g.setColor(northColor);
+ g.setColor(northColor);
for (int k=0; k < thickness; k++) {
for (int xc = x; xc < width; xc=xc+2) {
}
}
- if (eastBorder &&
+ if (eastBorder &&
eastBorderType == HSSFCellStyle.BORDER_DOTTED
) {
int thickness = getThickness(eastBorderType);
thickness++; //need for dotted borders to show up east
- g.setColor(eastColor);
+ g.setColor(eastColor);
for (int k=0; k < thickness; k++) {
for (int yc=y;yc < height; yc=yc+2) {
}
}
- if (southBorder &&
+ if (southBorder &&
southBorderType == HSSFCellStyle.BORDER_DOTTED
) {
int thickness = getThickness(southBorderType);
thickness++;
- g.setColor(southColor);
+ g.setColor(southColor);
for (int k=0; k < thickness; k++) {
for (int xc = x; xc < width; xc=xc+2) {
g.drawLine(xc,height-k,xc,height-k);
}
}
- if (westBorder &&
+ if (westBorder &&
westBorderType == HSSFCellStyle.BORDER_DOTTED
) {
int thickness = getThickness(westBorderType);
// thickness++;
- g.setColor(westColor);
+ g.setColor(westColor);
for (int k=0; k < thickness; k++) {
for (int yc=y;yc < height; yc=yc+2) {
}
- private void paintDashedBorders(Graphics g, int x, int y, int width,
+ private void paintDashedBorders(Graphics g, int x, int y, int width,
int height) {
- if (northBorder &&
+ if (northBorder &&
((northBorderType == HSSFCellStyle.BORDER_DASHED) ||
(northBorderType == HSSFCellStyle.BORDER_HAIR))
) {
int thickness = getThickness(northBorderType);
int dashlength = 1;
-
+
if (northBorderType == HSSFCellStyle.BORDER_DASHED)
- dashlength = 2;
+ dashlength = 2;
- g.setColor(northColor);
+ g.setColor(northColor);
for (int k=0; k < thickness; k++) {
for (int xc = x; xc < width; xc=xc+5) {
}
}
- if (eastBorder &&
+ if (eastBorder &&
((eastBorderType == HSSFCellStyle.BORDER_DASHED) ||
- (eastBorderType == HSSFCellStyle.BORDER_HAIR))
+ (eastBorderType == HSSFCellStyle.BORDER_HAIR))
) {
int thickness = getThickness(eastBorderType);
int dashlength = 1;
-
+
if (eastBorderType == HSSFCellStyle.BORDER_DASHED)
- dashlength = 2;
+ dashlength = 2;
- g.setColor(eastColor);
+ g.setColor(eastColor);
for (int k=0; k < thickness; k++) {
for (int yc=y;yc < height; yc=yc+5) {
}
}
- if (southBorder &&
+ if (southBorder &&
((southBorderType == HSSFCellStyle.BORDER_DASHED) ||
(southBorderType == HSSFCellStyle.BORDER_HAIR))
) {
thickness++;
int dashlength = 1;
-
+
if (southBorderType == HSSFCellStyle.BORDER_DASHED)
- dashlength = 2;
+ dashlength = 2;
- g.setColor(southColor);
+ g.setColor(southColor);
for (int k=0; k < thickness; k++) {
for (int xc = x; xc < width; xc=xc+5) {
g.drawLine(xc,height-k,xc+dashlength,height-k);
}
}
- if (westBorder &&
+ if (westBorder &&
((westBorderType == HSSFCellStyle.BORDER_DASHED) ||
(westBorderType == HSSFCellStyle.BORDER_HAIR))
) {
// thickness++;
int dashlength = 1;
-
+
if (westBorderType == HSSFCellStyle.BORDER_DASHED)
- dashlength = 2;
+ dashlength = 2;
- g.setColor(westColor);
+ g.setColor(westColor);
for (int k=0; k < thickness; k++) {
for (int yc=y;yc < height; yc=yc+5) {
}
- private void paintDoubleBorders(Graphics g, int x, int y, int width,
+ private void paintDoubleBorders(Graphics g, int x, int y, int width,
int height) {
- if (northBorder &&
+ if (northBorder &&
northBorderType == HSSFCellStyle.BORDER_DOUBLE) {
- g.setColor(northColor);
+ g.setColor(northColor);
int leftx=x;
int rightx=width;
g.drawLine(leftx,y+2,rightx,y+2);
}
- if (eastBorder &&
+ if (eastBorder &&
eastBorderType == HSSFCellStyle.BORDER_DOUBLE
) {
int thickness = getThickness(eastBorderType);
thickness++; //need for dotted borders to show up east
- g.setColor(eastColor);
+ g.setColor(eastColor);
int topy=y;
int bottomy=height;
if (northBorder)
topy=y+3;
-
+
if (southBorder)
- bottomy=height-3;
+ bottomy=height-3;
g.drawLine(width-1,y,width-1,height);
g.drawLine(width-3,topy,width-3,bottomy);
}
- if (southBorder &&
+ if (southBorder &&
southBorderType == HSSFCellStyle.BORDER_DOUBLE
) {
- g.setColor(southColor);
+ g.setColor(southColor);
int leftx=y;
int rightx=width;
-
+
if (westBorder)
leftx=x+3;
if (eastBorder)
rightx=width-3;
-
+
g.drawLine(x,height - 1,width,height - 1);
g.drawLine(leftx,height - 3,rightx,height - 3);
}
- if (westBorder &&
+ if (westBorder &&
westBorderType == HSSFCellStyle.BORDER_DOUBLE
) {
int thickness = getThickness(westBorderType);
// thickness++;
- g.setColor(westColor);
+ g.setColor(westColor);
int topy=y;
int bottomy=height-3;
}
- private void paintDashDotDotBorders(Graphics g, int x, int y, int width,
+ private void paintDashDotDotBorders(Graphics g, int x, int y, int width,
int height) {
- if (northBorder &&
+ if (northBorder &&
((northBorderType == HSSFCellStyle.BORDER_DASH_DOT_DOT) ||
(northBorderType == HSSFCellStyle.BORDER_MEDIUM_DASH_DOT_DOT))
) {
int thickness = getThickness(northBorderType);
- g.setColor(northColor);
+ g.setColor(northColor);
for (int l=x; l < width;) {
l=l+drawDashDotDot(g, l, y, thickness, true, true);
}
}
- if (eastBorder &&
+ if (eastBorder &&
((eastBorderType == HSSFCellStyle.BORDER_DASH_DOT_DOT) ||
(eastBorderType == HSSFCellStyle.BORDER_MEDIUM_DASH_DOT_DOT))
) {
int thickness = getThickness(eastBorderType);
- g.setColor(eastColor);
+ g.setColor(eastColor);
for (int l=y;l < height;) {
//System.err.println("drawing east");
}
}
- if (southBorder &&
+ if (southBorder &&
((southBorderType == HSSFCellStyle.BORDER_DASH_DOT_DOT) ||
(southBorderType == HSSFCellStyle.BORDER_MEDIUM_DASH_DOT_DOT))
) {
int thickness = getThickness(southBorderType);
- g.setColor(southColor);
+ g.setColor(southColor);
for (int l=x; l < width;) {
//System.err.println("drawing south");
}
}
- if (westBorder &&
+ if (westBorder &&
((westBorderType == HSSFCellStyle.BORDER_DASH_DOT_DOT) ||
(westBorderType == HSSFCellStyle.BORDER_MEDIUM_DASH_DOT_DOT))
) {
int thickness = getThickness(westBorderType);
- g.setColor(westColor);
+ g.setColor(westColor);
for (int l=y;l < height;) {
//System.err.println("drawing west");
/**
* 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 x the x origin of the line
- * @param y the y origin of the line
- * @param thickness the thickness of the line
+ * @param y the y origin of the line
+ * @param thickness the thickness of the line
* @param horizontal or vertical (true for horizontal)
* @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
- * thickness, if false, they'll be decremented. For vertical
+ * if true then the x or y origin will be incremented to provide
+ * thickness, if false, they'll be decremented. For vertical
* 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.
- * @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 rightBottom) {
-
+
for (int t=0; t < thickness; t++) {
if (!rightBottom) {
t = 0 - t; //add negative thickness so we go the other way
default:
retval=1;
}
- return retval;
+ return retval;
}
import java.awt.Font;
import java.io.Serializable;
+import java.text.*;
-import org.apache.poi.hssf.usermodel.HSSFWorkbook;
-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.usermodel.*;
import org.apache.poi.hssf.util.HSSFColor;
-import org.apache.poi.hssf.util.HSSFColor.WHITE;
+
/**
public class SVTableCellRenderer extends JLabel
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 SVBorder cellBorder = new SVBorder();
+
+
private HSSFWorkbook wb = null;
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) {
super();
setOpaque(true);
boolean isSelected, boolean hasFocus, int row, int column) {
boolean isBorderSet = false;
- if (isSelected) {
- setForeground(table.getSelectionForeground());
- setBackground(table.getSelectionBackground());
- }
-
//If the JTables default cell renderer has been setup correctly the
//value will be the HSSFCell that we are trying to render
HSSFCell c = (HSSFCell)value;
if (c != null) {
-
HSSFCellStyle s = c.getCellStyle();
HSSFFont f = wb.getFontAt(s.getFontIndex());
boolean isbold = f.getBoldweight() > HSSFFont.BOLDWEIGHT_NORMAL;
Font font = new Font(f.getFontName(),fontstyle,fontheight);
setFont(font);
-
- HSSFColor clr = null;
+
if (s.getFillPattern() == HSSFCellStyle.SOLID_FOREGROUND) {
- clr = (HSSFColor)colors.get(new Integer(s.getFillForegroundColor()));
- }
- if (clr == null) clr = new HSSFColor.WHITE();
-
- short[] rgb = clr.getTriplet();
- Color awtcolor = new Color(rgb[0],rgb[1],rgb[2]);
-
- setBackground(awtcolor);
-
- clr = (HSSFColor)colors.get(new Integer(f.getColor()));
- if (clr == null) clr = new HSSFColor.BLACK();
- rgb = clr.getTriplet();
- awtcolor = new Color(rgb[0],rgb[1],rgb[2]);
- setForeground(awtcolor);
-
-/* if (s.getBorderBottom() != HSSFCellStyle.BORDER_NONE ||
- s.getBorderTop() != HSSFCellStyle.BORDER_NONE ||
- s.getBorderLeft() != HSSFCellStyle.BORDER_NONE ||
- s.getBorderRight() != HSSFCellStyle.BORDER_NONE) {
-*/
- int borderTop = s.getBorderTop();
- int borderRight = s.getBorderRight();
- int borderBottom = s.getBorderBottom();
- int borderLeft = s.getBorderLeft();
-
- //OUCH! This could causing rendering performance problems.
- //Need to somehow create once and store
- SVBorder border = new SVBorder(Color.black, Color.black,
- Color.black, Color.black,
- borderTop, borderRight,
- borderBottom, borderLeft,
- s.getBorderTop() != HSSFCellStyle.BORDER_NONE,
- s.getBorderRight() != HSSFCellStyle.BORDER_NONE,
- s.getBorderBottom() != HSSFCellStyle.BORDER_NONE,
- s.getBorderLeft() != HSSFCellStyle.BORDER_NONE);
- setBorder(border);
- isBorderSet=true;
-
- //Set the value that is rendered for the cell
- switch (c.getCellType()) {
- case HSSFCell.CELL_TYPE_BLANK:
- setValue("");
- break;
- case HSSFCell.CELL_TYPE_BOOLEAN:
- if (c.getBooleanCellValue()) {
- setValue("true");
- } else {
- setValue("false");
- }
- break;
- case HSSFCell.CELL_TYPE_FORMULA:
- case HSSFCell.CELL_TYPE_NUMERIC:
- setValue(""+c.getNumericCellValue());
- break;
- case HSSFCell.CELL_TYPE_STRING:
- setValue(c.getStringCellValue());
- break;
- default:
- 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);
- }
+ setBackground(getAWTColor(s.getFillForegroundColor(), white));
+ } else setBackground(white);
+
+ setForeground(getAWTColor(f.getColor(), black));
+
+ 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;
+
+ //Set the value that is rendered for the cell
+ switch (c.getCellType()) {
+ case HSSFCell.CELL_TYPE_BLANK:
+ setValue("");
+ break;
+ case HSSFCell.CELL_TYPE_BOOLEAN:
+ if (c.getBooleanCellValue()) {
+ setValue("true");
+ } else {
+ setValue("false");
+ }
+ break;
+ case HSSFCell.CELL_TYPE_FORMULA:
+ case HSSFCell.CELL_TYPE_NUMERIC:
+ short format = s.getDataFormat();
+ double numericValue = c.getNumericCellValue();
+ if (cellFormatter.useRedColor(format, numericValue))
+ setForeground(Color.red);
+ else setForeground(null);
+ setValue(cellFormatter.format(format, c.getNumericCellValue()));
+ break;
+ case HSSFCell.CELL_TYPE_STRING:
+ setValue(c.getStringCellValue());
+ break;
+ default:
+ setValue("?");
+ }
+ //Set the text alignment of the cell
+ switch (s.getAlignment()) {
+ 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_GENERAL:
+ case HSSFCellStyle.ALIGN_RIGHT:
+ setHorizontalAlignment(SwingConstants.RIGHT);
+ break;
+ default:
+ setHorizontalAlignment(SwingConstants.LEFT);
+ break;
+ }
+ } else {
+ setValue("");
+ setBackground(white);
+ }
if (hasFocus) {
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)) {
setForeground( UIManager.getColor("Table.focusCellForeground") );
boolean colorMatch = (back != null) && ( back.equals(table.getBackground()) ) && table.isOpaque();
setOpaque(!colorMatch);
// ---- end optimization to aviod painting background ----
-
return this;
}
-
public void validate() {}
public void revalidate() {}
protected void setValue(Object value) {
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]);
+ }
+
+
}