diff --git a/src/contrib/src/org/apache/poi/hssf/contrib/view/SVTableCellRenderer.java b/src/contrib/src/org/apache/poi/hssf/contrib/view/SVTableCellRenderer.java index 478548441..6b3a11280 100644 --- a/src/contrib/src/org/apache/poi/hssf/contrib/view/SVTableCellRenderer.java +++ b/src/contrib/src/org/apache/poi/hssf/contrib/view/SVTableCellRenderer.java @@ -87,50 +87,29 @@ import org.apache.poi.hssf.util.HSSFColor.WHITE; public class SVTableCellRenderer extends JLabel implements TableCellRenderer, Serializable { - protected static Border noFocusBorder = new EmptyBorder(1, 1, 1, 1); - - private Color unselFG; - private Color unselBG; private HSSFWorkbook wb = null; - private HSSFSheet st = null; private Hashtable colors = HSSFColor.getIndexHash(); - public SVTableCellRenderer(HSSFWorkbook wb, HSSFSheet st) { + public SVTableCellRenderer(HSSFWorkbook wb) { super(); setOpaque(true); setBorder(noFocusBorder); this.wb = wb; - this.st = st; - } - - public void setForeground(Color c) { - super.setForeground(c); - unselFG = c; - } - - public void setBackground(Color c) { - super.setBackground(c); - unselBG = c; - } - - public void updateUI() { - super.updateUI(); - setForeground(null); - setBackground(null); } public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { - boolean isBorderSet = false; if (isSelected) { - super.setForeground(table.getSelectionForeground()); - super.setBackground(table.getSelectionBackground()); + setForeground(table.getSelectionForeground()); + setBackground(table.getSelectionBackground()); } - HSSFCell c = getCell(row,column); + //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) { @@ -139,7 +118,7 @@ public class SVTableCellRenderer extends JLabel boolean isbold = f.getBoldweight() > HSSFFont.BOLDWEIGHT_NORMAL; boolean isitalics = f.getItalic(); - int fontstyle = 0; + int fontstyle = Font.PLAIN; if (isbold) fontstyle = Font.BOLD; if (isitalics) fontstyle = fontstyle | Font.ITALIC; @@ -150,7 +129,6 @@ public class SVTableCellRenderer extends JLabel 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())); @@ -178,6 +156,8 @@ public class SVTableCellRenderer extends JLabel 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, @@ -189,25 +169,7 @@ public class SVTableCellRenderer extends JLabel setBorder(border); isBorderSet=true; -// } - } else { - setBackground(Color.white); - } - - - if (hasFocus) { - if (!isBorderSet) { - setBorder( UIManager.getBorder("Table.focusCellHighlightBorder") ); - } - if (table.isCellEditable(row, column)) { - super.setForeground( UIManager.getColor("Table.focusCellForeground") ); - super.setBackground( UIManager.getColor("Table.focusCellBackground") ); - } - } else if (!isBorderSet) { - setBorder(noFocusBorder); - } - - if (c != null) { + //Set the value that is rendered for the cell switch (c.getCellType()) { case HSSFCell.CELL_TYPE_BLANK: setValue(""); @@ -229,11 +191,44 @@ public class SVTableCellRenderer extends JLabel 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); } + 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); + } + // ---- begin optimization to avoid painting background ---- Color back = getBackground(); boolean colorMatch = (back != null) && ( back.equals(table.getBackground()) ) && table.isOpaque(); @@ -261,7 +256,6 @@ public class SVTableCellRenderer extends JLabel public void firePropertyChange(String propertyName, boolean oldValue, boolean newValue) { } - /** * Sets the string to either the value or "" if the value is null. * @@ -269,16 +263,4 @@ public class SVTableCellRenderer extends JLabel protected void setValue(Object value) { setText((value == null) ? "" : value.toString()); } - - /** - * Get a cell at a given row (warning: slow) - * - */ - private HSSFCell getCell(int row, int col) { - HSSFRow r = st.getRow(row); - HSSFCell c = null; - if ( r != null) - c = r.getCell((short)col); - return c; - } } diff --git a/src/contrib/src/org/apache/poi/hssf/contrib/view/SViewer.java b/src/contrib/src/org/apache/poi/hssf/contrib/view/SViewer.java index 157d7f532..b5c327608 100644 --- a/src/contrib/src/org/apache/poi/hssf/contrib/view/SViewer.java +++ b/src/contrib/src/org/apache/poi/hssf/contrib/view/SViewer.java @@ -78,13 +78,11 @@ import org.apache.poi.hssf.usermodel.HSSFCell; * * @author Andrew C. Oliver */ -public class SViewer extends Applet { +public class SViewer extends JApplet { + private SViewerPanel panel; boolean isStandalone = false; String filename = null; - BorderLayout borderLayout = new BorderLayout(); - JScrollPane mainScrollPane = new JScrollPane(); - JTable mainTable = new JTable(); - URLConnection uc = null; + /**Get a parameter value*/ public String getParameter(String key, String def) { return isStandalone ? System.getProperty(key, def) : @@ -94,6 +92,7 @@ public class SViewer extends Applet { /**Construct the applet*/ public SViewer() { } + /**Initialize the applet*/ public void init() { try { @@ -103,6 +102,7 @@ public class SViewer extends Applet { e.printStackTrace(); } } + /**Component initialization*/ private void jbInit() throws Exception { InputStream i = null; @@ -121,16 +121,9 @@ public class SViewer extends Applet { } else { wb = constructWorkbook(filename); } - - HSSFSheet st = wb.getSheetAt(0); - SVTableModel tm = constructTableModel(wb,st); - mainTable.setModel(tm); - SVTableCellRenderer rnd = new SVTableCellRenderer(wb, st); - mainTable.setDefaultRenderer(HSSFCell.class,rnd); - - this.setLayout(borderLayout); - this.add(mainScrollPane, BorderLayout.CENTER); - mainScrollPane.getViewport().add(mainTable, null); + panel = new SViewerPanel(wb); + getContentPane().setLayout(new BorderLayout()); + getContentPane().add(panel, BorderLayout.CENTER); } private HSSFWorkbook constructWorkbook(String filename) { @@ -159,18 +152,6 @@ public class SViewer extends Applet { return wb; } - - private SVTableModel constructTableModel(HSSFWorkbook wb, HSSFSheet st) { - SVTableModel retval = null; - - try { - retval = new SVTableModel(st); - } catch (Exception e) { - e.printStackTrace(); - } - return retval; - } - /**Start the applet*/ public void start() { } @@ -197,9 +178,7 @@ public class SViewer extends Applet { InputStream is = null; try { URL url = new URL(urlstring); - uc = url.openConnection(); - uc.connect(); - is = uc.getInputStream(); + is = url.openStream(); } catch (Exception e) { e.printStackTrace(); }