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,26 +11,25 @@ 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;
@ -42,18 +37,19 @@ public class SVBorder extends AbstractBorder {
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);
paintSelectedBorder(g, x, y, width, height);
paintNormalBorders(g, x, y, width, height); paintNormalBorders(g, x, y, width, height);
paintDottedBorders(g, x, y, width, height); paintDottedBorders(g, x, y, width, height);
paintDashedBorders(g, x, y, width, height); paintDashedBorders(g, x, y, width, height);
@ -64,6 +60,20 @@ public class SVBorder extends AbstractBorder {
g.setColor(oldColor); g.setColor(oldColor);
} }
private void paintSelectedBorder(Graphics g, int x, int y, int width,
int height) {
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, private void paintNormalBorders(Graphics g, int x, int y, int width,
int height) { int height) {

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;
@ -129,44 +202,20 @@ 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),
clr = (HSSFColor)colors.get(new Integer(f.getColor())); getAWTColor(s.getBottomBorderColor(), black),
if (clr == null) clr = new HSSFColor.BLACK(); getAWTColor(s.getLeftBorderColor(), black),
rgb = clr.getTriplet(); s.getBorderTop(), s.getBorderRight(),
awtcolor = new Color(rgb[0],rgb[1],rgb[2]); s.getBorderBottom(), s.getBorderLeft(),
setForeground(awtcolor); hasFocus);
setBorder(cellBorder);
/* 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; isBorderSet=true;
//Set the value that is rendered for the cell //Set the value that is rendered for the cell
@ -183,7 +232,12 @@ public class SVTableCellRenderer extends JLabel
break; break;
case HSSFCell.CELL_TYPE_FORMULA: case HSSFCell.CELL_TYPE_FORMULA:
case HSSFCell.CELL_TYPE_NUMERIC: case HSSFCell.CELL_TYPE_NUMERIC:
setValue(""+c.getNumericCellValue()); 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; break;
case HSSFCell.CELL_TYPE_STRING: case HSSFCell.CELL_TYPE_STRING:
setValue(c.getStringCellValue()); setValue(c.getStringCellValue());
@ -193,7 +247,6 @@ public class SVTableCellRenderer extends JLabel
} }
//Set the text alignment of the cell //Set the text alignment of the cell
switch (s.getAlignment()) { switch (s.getAlignment()) {
case HSSFCellStyle.ALIGN_GENERAL:
case HSSFCellStyle.ALIGN_LEFT: case HSSFCellStyle.ALIGN_LEFT:
case HSSFCellStyle.ALIGN_JUSTIFY: case HSSFCellStyle.ALIGN_JUSTIFY:
case HSSFCellStyle.ALIGN_FILL: case HSSFCellStyle.ALIGN_FILL:
@ -203,6 +256,7 @@ public class SVTableCellRenderer extends JLabel
case HSSFCellStyle.ALIGN_CENTER_SELECTION: case HSSFCellStyle.ALIGN_CENTER_SELECTION:
setHorizontalAlignment(SwingConstants.CENTER); setHorizontalAlignment(SwingConstants.CENTER);
break; break;
case HSSFCellStyle.ALIGN_GENERAL:
case HSSFCellStyle.ALIGN_RIGHT: case HSSFCellStyle.ALIGN_RIGHT:
setHorizontalAlignment(SwingConstants.RIGHT); setHorizontalAlignment(SwingConstants.RIGHT);
break; break;
@ -210,16 +264,24 @@ public class SVTableCellRenderer extends JLabel
setHorizontalAlignment(SwingConstants.LEFT); setHorizontalAlignment(SwingConstants.LEFT);
break; break;
} }
// }
} else { } else {
setValue(""); setValue("");
setBackground(Color.white); setBackground(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]);
}
} }