2002-06-22 12:08:04 -04:00
|
|
|
|
|
|
|
/* ====================================================================
|
|
|
|
* The Apache Software License, Version 1.1
|
|
|
|
*
|
|
|
|
* Copyright (c) 2002 The Apache Software Foundation. All rights
|
|
|
|
* reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
*
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in
|
|
|
|
* the documentation and/or other materials provided with the
|
|
|
|
* distribution.
|
|
|
|
*
|
|
|
|
* 3. The end-user documentation included with the redistribution,
|
|
|
|
* if any, must include the following acknowledgment:
|
|
|
|
* "This product includes software developed by the
|
|
|
|
* Apache Software Foundation (http://www.apache.org/)."
|
|
|
|
* Alternately, this acknowledgment may appear in the software itself,
|
|
|
|
* if and wherever such third-party acknowledgments normally appear.
|
|
|
|
*
|
|
|
|
* 4. The names "Apache" and "Apache Software Foundation" and
|
|
|
|
* "Apache POI" must not be used to endorse or promote products
|
|
|
|
* derived from this software without prior written permission. For
|
|
|
|
* written permission, please contact apache@apache.org.
|
|
|
|
*
|
|
|
|
* 5. Products derived from this software may not be called "Apache",
|
|
|
|
* "Apache POI", nor may "Apache" appear in their name, without
|
|
|
|
* prior written permission of the Apache Software Foundation.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
|
|
|
|
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
|
|
|
|
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
|
|
|
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
|
|
|
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
|
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
|
|
|
|
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
* ====================================================================
|
|
|
|
*
|
|
|
|
* This software consists of voluntary contributions made by many
|
|
|
|
* individuals on behalf of the Apache Software Foundation. For more
|
|
|
|
* information on the Apache Software Foundation, please see
|
|
|
|
* <http://www.apache.org/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package org.apache.poi.hssf.contrib.view;
|
|
|
|
|
|
|
|
import java.util.Hashtable;
|
|
|
|
|
|
|
|
import javax.swing.*;
|
|
|
|
import javax.swing.table.TableCellRenderer;
|
|
|
|
import javax.swing.border.*;
|
|
|
|
|
|
|
|
import java.awt.Component;
|
|
|
|
import java.awt.Color;
|
|
|
|
import java.awt.Rectangle;
|
|
|
|
import java.awt.Font;
|
|
|
|
|
|
|
|
import java.io.Serializable;
|
|
|
|
|
|
|
|
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.util.HSSFColor;
|
|
|
|
import org.apache.poi.hssf.util.HSSFColor.WHITE;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sucky Viewer Table Cell Render -- not commented via javadoc as it
|
|
|
|
* nearly completely consists of overridden methods.
|
|
|
|
*
|
|
|
|
* @author Andrew C. Oliver
|
|
|
|
*/
|
|
|
|
public class SVTableCellRenderer extends JLabel
|
|
|
|
implements TableCellRenderer, Serializable
|
|
|
|
{
|
|
|
|
protected static Border noFocusBorder = new EmptyBorder(1, 1, 1, 1);
|
|
|
|
private HSSFWorkbook wb = null;
|
|
|
|
private Hashtable colors = HSSFColor.getIndexHash();
|
|
|
|
|
2002-07-05 14:22:05 -04:00
|
|
|
public SVTableCellRenderer(HSSFWorkbook wb) {
|
2002-06-22 12:08:04 -04:00
|
|
|
super();
|
|
|
|
setOpaque(true);
|
|
|
|
setBorder(noFocusBorder);
|
|
|
|
this.wb = wb;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Component getTableCellRendererComponent(JTable table, Object value,
|
|
|
|
boolean isSelected, boolean hasFocus, int row, int column) {
|
2002-06-22 22:54:08 -04:00
|
|
|
boolean isBorderSet = false;
|
|
|
|
|
2002-06-22 12:08:04 -04:00
|
|
|
if (isSelected) {
|
2002-07-05 14:22:05 -04:00
|
|
|
setForeground(table.getSelectionForeground());
|
|
|
|
setBackground(table.getSelectionBackground());
|
2002-06-22 12:08:04 -04:00
|
|
|
}
|
|
|
|
|
2002-07-05 14:22:05 -04:00
|
|
|
//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;
|
2002-07-04 00:47:21 -04:00
|
|
|
|
2002-06-22 12:08:04 -04:00
|
|
|
if (c != null) {
|
2002-07-04 00:47:21 -04:00
|
|
|
|
2002-06-22 12:08:04 -04:00
|
|
|
HSSFCellStyle s = c.getCellStyle();
|
|
|
|
HSSFFont f = wb.getFontAt(s.getFontIndex());
|
|
|
|
boolean isbold = f.getBoldweight() > HSSFFont.BOLDWEIGHT_NORMAL;
|
|
|
|
boolean isitalics = f.getItalic();
|
2002-07-04 00:47:21 -04:00
|
|
|
|
2002-07-05 14:22:05 -04:00
|
|
|
int fontstyle = Font.PLAIN;
|
2002-06-22 12:08:04 -04:00
|
|
|
|
|
|
|
if (isbold) fontstyle = Font.BOLD;
|
|
|
|
if (isitalics) fontstyle = fontstyle | Font.ITALIC;
|
|
|
|
|
2002-06-22 22:54:08 -04:00
|
|
|
int fontheight = f.getFontHeightInPoints();
|
|
|
|
if (fontheight == 9) fontheight = 10; //fix for stupid ol Windows
|
|
|
|
|
|
|
|
Font font = new Font(f.getFontName(),fontstyle,fontheight);
|
2002-06-22 12:08:04 -04:00
|
|
|
setFont(font);
|
2002-06-22 16:54:14 -04:00
|
|
|
|
2002-06-22 12:08:04 -04:00
|
|
|
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);
|
|
|
|
|
2002-06-22 16:54:14 -04:00
|
|
|
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);
|
2002-06-22 22:54:08 -04:00
|
|
|
|
2002-07-04 00:47:21 -04:00
|
|
|
/* if (s.getBorderBottom() != HSSFCellStyle.BORDER_NONE ||
|
2002-06-22 22:54:08 -04:00
|
|
|
s.getBorderTop() != HSSFCellStyle.BORDER_NONE ||
|
|
|
|
s.getBorderLeft() != HSSFCellStyle.BORDER_NONE ||
|
|
|
|
s.getBorderRight() != HSSFCellStyle.BORDER_NONE) {
|
2002-07-04 00:47:21 -04:00
|
|
|
*/
|
|
|
|
int borderTop = s.getBorderTop();
|
|
|
|
int borderRight = s.getBorderRight();
|
|
|
|
int borderBottom = s.getBorderBottom();
|
|
|
|
int borderLeft = s.getBorderLeft();
|
|
|
|
|
2002-07-05 14:22:05 -04:00
|
|
|
//OUCH! This could causing rendering performance problems.
|
|
|
|
//Need to somehow create once and store
|
2002-06-22 22:54:08 -04:00
|
|
|
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;
|
2002-07-04 00:47:21 -04:00
|
|
|
|
2002-07-05 14:22:05 -04:00
|
|
|
//Set the value that is rendered for the cell
|
2002-06-22 12:08:04 -04:00
|
|
|
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("?");
|
|
|
|
}
|
2002-07-05 14:22:05 -04:00
|
|
|
//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;
|
|
|
|
}
|
|
|
|
// }
|
2002-06-23 10:12:36 -04:00
|
|
|
} else {
|
|
|
|
setValue("");
|
2002-07-05 14:22:05 -04:00
|
|
|
setBackground(Color.white);
|
2002-06-22 12:08:04 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-07-05 14:22:05 -04:00
|
|
|
if (hasFocus) {
|
|
|
|
if (!isBorderSet) {
|
|
|
|
setBorder( UIManager.getBorder("Table.focusCellHighlightBorder") );
|
|
|
|
}
|
|
|
|
if (table.isCellEditable(row, column)) {
|
|
|
|
setForeground( UIManager.getColor("Table.focusCellForeground") );
|
|
|
|
setBackground( UIManager.getColor("Table.focusCellBackground") );
|
|
|
|
}
|
|
|
|
} else if (!isBorderSet) {
|
|
|
|
setBorder(noFocusBorder);
|
|
|
|
}
|
|
|
|
|
2002-06-22 12:08:04 -04:00
|
|
|
// ---- begin optimization to avoid painting background ----
|
|
|
|
Color back = getBackground();
|
|
|
|
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() {}
|
|
|
|
|
|
|
|
public void repaint(long tm, int x, int y, int width, int height) {}
|
|
|
|
|
|
|
|
public void repaint(Rectangle r) { }
|
|
|
|
|
|
|
|
protected void firePropertyChange(String propertyName, Object oldValue, Object newValue) {
|
|
|
|
// Strings get interned...
|
|
|
|
if (propertyName=="text") {
|
|
|
|
super.firePropertyChange(propertyName, oldValue, newValue);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void firePropertyChange(String propertyName, boolean oldValue, boolean newValue) { }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the string to either the value or "" if the value is null.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
protected void setValue(Object value) {
|
|
|
|
setText((value == null) ? "" : value.toString());
|
|
|
|
}
|
|
|
|
}
|