FileDocCategorySizeDatePackage
SViewerPanel.javaAPI DocApache Poi 3.0.19781Mon Jan 01 12:39:34 GMT 2007org.apache.poi.hssf.contrib.view

SViewerPanel

public class SViewerPanel extends JPanel
This class presents the sheets to the user.
author
Andrew C. Oliver
author
Jason Height

Fields Summary
private static final int
magicCharFactor
This field is the magic number to convert from a Character width to a java pixel width. When the "normal" font size in a workbook changes, this effects all of the heights and widths. Unfortunately there is no way to retrieve this information, hence the MAGIC number. This number may only work for the normal style font size of Arial size 10.
private HSSFWorkbook
wb
Reference to the woorkbook that is being displayed
private JTabbedPane
sheetPane
Reference to the tabs component
private SVTableCellRenderer
cellRenderer
Reference to the cell renderer that is used to render all cells
private SVTableCellEditor
cellEditor
Reference to the cell editor that is used to edit all cells. Only constructed if editing is allowed
private boolean
allowEdits
Flag indicating if editing is allowed. Otherwise the viewer is in view only mode.
Constructors Summary
public SViewerPanel(HSSFWorkbook wb, boolean allowEdits)
Construct the representation of the workbook


       
       
    this.wb = wb;
    this.allowEdits = allowEdits;

    initialiseGui();
  
Methods Summary
protected java.awt.event.MouseListenercreateTabListener()

    return new TabListener();
  
private voidinitialiseGui()

    cellRenderer = new SVTableCellRenderer(this.wb);
    if (allowEdits)
      cellEditor = new SVTableCellEditor(this.wb);

    //Initialise the Panel
    sheetPane = new JTabbedPane(JTabbedPane.BOTTOM);

    if (allowEdits)
      sheetPane.addMouseListener(createTabListener());
    int sheetCount = wb.getNumberOfSheets();
    for (int i=0; i<sheetCount;i++) {
      String sheetName = wb.getSheetName(i);
      //Add the new sheet to the tabbed pane
      sheetPane.addTab(sheetName, makeSheetView(wb.getSheetAt(i)));
    }
    setLayout(new BorderLayout());
    add(sheetPane, BorderLayout.CENTER);
  
public booleanisEditable()

    return allowEdits;
  
public static voidmain(java.lang.String[] args)
Main method

    try {
      FileInputStream in = new FileInputStream(args[0]);
      HSSFWorkbook wb = new HSSFWorkbook(in);
      in.close();

      SViewerPanel p = new SViewerPanel(wb, true);
      JFrame frame;
      frame = new JFrame() {
        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("Viewer Frame");
      frame.getContentPane().add(p, BorderLayout.CENTER);
      frame.setSize(800,640);
      Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
      frame.setLocation((d.width - frame.getSize().width) / 2, (d.height - frame.getSize().height) / 2);
      frame.setVisible(true);
    } catch (IOException ex) {
      ex.printStackTrace();
      System.exit(1);
    }
  
protected javax.swing.JComponentmakeSheetView(org.apache.poi.hssf.usermodel.HSSFSheet sheet)

    JTable sheetView = new JTable(new SVTableModel(sheet));
    sheetView.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
    sheetView.setDefaultRenderer(HSSFCell.class, cellRenderer);
    if (allowEdits)
      sheetView.setDefaultEditor(HSSFCell.class, cellEditor);
    JTableHeader header = sheetView.getTableHeader();
    //Dont allow column reordering
    header.setReorderingAllowed(false);
    //Only allow column resizing if editing is allowed
    header.setResizingAllowed(allowEdits);

    //Set the columns the correct size
    TableColumnModel columns = sheetView.getColumnModel();
    for (int i=0; i< columns.getColumnCount(); i++) {
      TableColumn column = columns.getColumn(i);
      short width = sheet.getColumnWidth((short)i);
      //256 is because the width is in 256ths of a character
      column.setPreferredWidth(width/256*magicCharFactor);
    }

    //Set the rows to the correct size
    int rows = sheet.getPhysicalNumberOfRows();
    Insets insets = cellRenderer.getInsets();
    //Need to include the insets in the calculation of the row height to use.
    int extraHeight = insets.bottom+insets.top;
    for (int i=0; i< rows; i++) {
      HSSFRow row = sheet.getRow(i);
      if (row == null) {
        sheetView.setRowHeight(i, (int)sheet.getDefaultRowHeightInPoints()+extraHeight);
      } else {
        sheetView.setRowHeight(i, (int)row.getHeightInPoints()+extraHeight);
      }
    }

    //Add the row header to the sheet
    SVRowHeader rowHeader = new SVRowHeader(sheet, sheetView, extraHeight);
    JScrollPane scroll = new JScrollPane( sheetView );
    scroll.setRowHeaderView(rowHeader);
    return scroll;
  
public voidpaint(java.awt.Graphics g)

    //JMH I am only overriding this to get a picture of the time taken to paint
    long start = System.currentTimeMillis();
    super.paint(g);
    long elapsed = System.currentTimeMillis()-start;
    System.out.println("Paint time = "+elapsed);