From 52694b35391134dd382eacccad22564195517d51 Mon Sep 17 00:00:00 2001 From: "Andrew C. Oliver" Date: Sat, 22 Jun 2002 16:08:04 +0000 Subject: [PATCH] Added Sucky Viewer for viewing XLS files with HSSF PR: Obtained from: Submitted by: Reviewed by: git-svn-id: https://svn.apache.org/repos/asf/jakarta/poi/trunk@352712 13f79535-47bb-0310-9956-ffa450edef68 --- build.xml | 4 +- .../contrib/view/SVTableCellRenderer.java | 241 ++++++++++++++++++ .../poi/hssf/contrib/view/SVTableModel.java | 113 ++++++++ .../apache/poi/hssf/contrib/view/SViewer.java | 237 +++++++++++++++++ 4 files changed, 593 insertions(+), 2 deletions(-) create mode 100644 src/contrib/src/org/apache/poi/hssf/contrib/view/SVTableCellRenderer.java create mode 100644 src/contrib/src/org/apache/poi/hssf/contrib/view/SVTableModel.java create mode 100644 src/contrib/src/org/apache/poi/hssf/contrib/view/SViewer.java diff --git a/build.xml b/build.xml index 6d32eacfe..e7164250b 100644 --- a/build.xml +++ b/build.xml @@ -122,8 +122,8 @@ These are CONTRIB BUILDS. They are not guaranteed to work. You have been warned. - poibrowser - POIBrowser 0.10 GUI POI Viewer - - + poibrowser - POIBrowser 0.10 GUI POI Viewer + suckyviewer - SuckyViewer 0.10 GUI Applet/Application Viewer for XLS files - - - 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 new file mode 100644 index 000000000..e1cddc1e2 --- /dev/null +++ b/src/contrib/src/org/apache/poi/hssf/contrib/view/SVTableCellRenderer.java @@ -0,0 +1,241 @@ + +/* ==================================================================== + * 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 + * . + */ + +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 Color unselFG; + private Color unselBG; + private HSSFWorkbook wb = null; + private HSSFSheet st = null; + private Hashtable colors = HSSFColor.getIndexHash(); + + public SVTableCellRenderer(HSSFWorkbook wb, HSSFSheet st) { + 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) { + + if (isSelected) { + super.setForeground(table.getSelectionForeground()); + super.setBackground(table.getSelectionBackground()); + } + + HSSFCell c = getCell(row,column); + if (c != null) { + HSSFCellStyle s = c.getCellStyle(); + HSSFFont f = wb.getFontAt(s.getFontIndex()); + boolean isbold = f.getBoldweight() > HSSFFont.BOLDWEIGHT_NORMAL; + boolean isitalics = f.getItalic(); + int fontstyle = 0; + + if (isbold) fontstyle = Font.BOLD; + if (isitalics) fontstyle = fontstyle | Font.ITALIC; + + + Font font = new Font(f.getFontName(),fontstyle,f.getFontHeightInPoints()); + 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); + + } + + + if (hasFocus) { + setBorder( UIManager.getBorder("Table.focusCellHighlightBorder") ); + if (table.isCellEditable(row, column)) { + super.setForeground( UIManager.getColor("Table.focusCellForeground") ); + super.setBackground( UIManager.getColor("Table.focusCellBackground") ); + } + } else { + setBorder(noFocusBorder); + } + + if (c != null) { + 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("?"); + } + } + + + // ---- 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()); + } + + /** + * Get a cell at a given row (warning: slow) + * + */ + private HSSFCell getCell(int row, int col) { + HSSFRow r = st.getRow(row); + HSSFCell c = r.getCell((short)col); + return c; + } +} diff --git a/src/contrib/src/org/apache/poi/hssf/contrib/view/SVTableModel.java b/src/contrib/src/org/apache/poi/hssf/contrib/view/SVTableModel.java new file mode 100644 index 000000000..6cbce8000 --- /dev/null +++ b/src/contrib/src/org/apache/poi/hssf/contrib/view/SVTableModel.java @@ -0,0 +1,113 @@ + +/* ==================================================================== + * 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 + * . + */ + + +package org.apache.poi.hssf.contrib.view; + +import java.util.Iterator; +import javax.swing.table.*; + +import org.apache.poi.hssf.usermodel.HSSFRow; +import org.apache.poi.hssf.usermodel.HSSFSheet; +import org.apache.poi.hssf.usermodel.HSSFCell; + +/** + * Sucky Viewer Table Model - The model for the Sucky Viewer just overrides things. + * @author Andrew C. Oliver + */ + +public class SVTableModel extends AbstractTableModel { + private HSSFSheet st = null; + int maxcol = 0; + + public SVTableModel(HSSFSheet st, int maxcol) { + this.st = st; + this.maxcol=maxcol; + } + + public SVTableModel(HSSFSheet st) { + this.st = st; + Iterator i = st.rowIterator(); + + while (i.hasNext()) { + HSSFRow row = (HSSFRow)i.next(); + if (maxcol < (row.getLastCellNum()+1)) { + this.maxcol = row.getLastCellNum(); + } + } + } + + + public int getColumnCount() { + return this.maxcol; + } + public Object getValueAt(int row, int col) { + HSSFRow r = st.getRow(row); + HSSFCell c = null; + if (r != null) { + c = r.getCell((short)col); + } + return c; + } + public int getRowCount() { + return st.getLastRowNum() + 1; + } + + public Class getColumnClass(int c) { + return getValueAt(0, c).getClass(); + } + + +} \ No newline at end of file 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 new file mode 100644 index 000000000..157d7f532 --- /dev/null +++ b/src/contrib/src/org/apache/poi/hssf/contrib/view/SViewer.java @@ -0,0 +1,237 @@ + +/* ==================================================================== + * 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 + * . + */ + + +package org.apache.poi.hssf.contrib.view; + +import java.awt.*; +import java.awt.event.*; +import java.net.URL; +import java.net.URLConnection; +import java.applet.*; +import java.io.InputStream; +import java.io.FileInputStream; +import javax.swing.*; + +import org.apache.poi.hssf.usermodel.HSSFWorkbook; +import org.apache.poi.hssf.usermodel.HSSFSheet; +import org.apache.poi.hssf.usermodel.HSSFCell; + +/** + * Sucky Viewer - Views XLS files via HSSF. Can be used as an applet with + * filename="" or as a applications (pass the filename as the first parameter). + * Or you can pass it a URL in a "url" parameter when run as an applet or just + * that first parameter must start with http:// and it will guess its a url. I + * only tested it as an applet though, so it probably won't work...you fix it. + * + * @author Andrew C. Oliver + */ +public class SViewer extends Applet { + 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) : + (getParameter(key) != null ? getParameter(key) : def); + } + + /**Construct the applet*/ + public SViewer() { + } + /**Initialize the applet*/ + public void init() { + try { + jbInit(); + } + catch(Exception e) { + e.printStackTrace(); + } + } + /**Component initialization*/ + private void jbInit() throws Exception { + InputStream i = null; + boolean isurl = false; + if (filename == null) filename = getParameter("filename"); + + if (filename == null || filename.substring(0,7).equals("http://")) { + isurl = true; + if (filename == null) filename = getParameter("url"); + i = getXLSFromURL(filename); + } + + HSSFWorkbook wb = null; + if (isurl) { + wb = constructWorkbook(i); + } 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); + } + + private HSSFWorkbook constructWorkbook(String filename) { + HSSFWorkbook wb = null; + + try { + FileInputStream in = new FileInputStream(filename); + wb = new HSSFWorkbook(in); + in.close(); + } catch (Exception e) { + e.printStackTrace(); + } + return wb; + } + + private HSSFWorkbook constructWorkbook(InputStream in) { + HSSFWorkbook wb = null; + + try { + wb = new HSSFWorkbook(in); + in.close(); + + } catch (Exception e) { + e.printStackTrace(); + } + 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() { + } + /**Stop the applet*/ + public void stop() { + } + /**Destroy the applet*/ + public void destroy() { + } + /**Get Applet information*/ + public String getAppletInfo() { + return "Applet Information"; + } + /**Get parameter info*/ + public String[][] getParameterInfo() { + return null; + } + + /** + * opens a url and returns an inputstream + * + */ + private InputStream getXLSFromURL(String urlstring) { + InputStream is = null; + try { + URL url = new URL(urlstring); + uc = url.openConnection(); + uc.connect(); + is = uc.getInputStream(); + } catch (Exception e) { + e.printStackTrace(); + } + return is; + } + + + /**Main method*/ + public static void main(String[] args) { + SViewer applet = new SViewer(); + applet.isStandalone = true; + applet.filename = args[0]; + Frame frame; + frame = new Frame() { + protected void processWindowEvent(WindowEvent e) { + super.processWindowEvent(e); + if (e.getID() == WindowEvent.WINDOW_CLOSING) { + System.exit(0); + } + } + public synchronized void setTitle(String title) { + super.setTitle(title); + enableEvents(AWTEvent.WINDOW_EVENT_MASK); + } + }; + frame.setTitle("Applet Frame"); + frame.add(applet, BorderLayout.CENTER); + applet.init(); + applet.start(); + frame.setSize(400,320); + Dimension d = Toolkit.getDefaultToolkit().getScreenSize(); + frame.setLocation((d.width - frame.getSize().width) / 2, (d.height - frame.getSize().height) / 2); + frame.setVisible(true); + } +}