001/* 002 * Copyright 2010-2015 Institut Pasteur. 003 * 004 * This file is part of Icy. 005 * 006 * Icy is free software: you can redistribute it and/or modify 007 * it under the terms of the GNU General Public License as published by 008 * the Free Software Foundation, either version 3 of the License, or 009 * (at your option) any later version. 010 * 011 * Icy is distributed in the hope that it will be useful, 012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 014 * GNU General Public License for more details. 015 * 016 * You should have received a copy of the GNU General Public License 017 * along with Icy. If not, see <http://www.gnu.org/licenses/>. 018 */ 019package icy.gui.component; 020 021import icy.system.thread.ThreadUtil; 022 023import javax.swing.JScrollPane; 024import javax.swing.JTable; 025import javax.swing.event.TableModelListener; 026import javax.swing.table.TableModel; 027 028import jxl.Sheet; 029import jxl.write.WritableSheet; 030 031/** 032 * Excel table view 033 * 034 * @author Fabrice de Chaumont, Alexandre Dufour 035 */ 036public class ExcelTable extends JScrollPane 037{ 038 private static final long serialVersionUID = 1L; 039 040 JTable table; 041 042 public ExcelTable() 043 { 044 045 } 046 047 public ExcelTable(Sheet page) 048 { 049 updateSheet(page); 050 setViewportView(table); 051 setAutoscrolls(true); 052 } 053 054 public ExcelTable(WritableSheet page) 055 { 056 this((Sheet) page); 057 } 058 059 public synchronized void updateSheet(final Sheet page) 060 { 061 ThreadUtil.invokeLater(new Runnable() 062 { 063 @Override 064 public void run() 065 { 066 table = new JTable(); 067 setViewportView(table); 068 if (page != null) 069 table.setModel(new SheetTableModel(page)); 070 } 071 }); 072 } 073 074 public synchronized void updateSheet(final WritableSheet page) 075 { 076 updateSheet((Sheet) page); 077 } 078 079 private class SheetTableModel implements TableModel 080 { 081 private Sheet sheet = null; 082 083 public SheetTableModel(Sheet sheet) 084 { 085 this.sheet = sheet; 086 } 087 088 @Override 089 public int getRowCount() 090 { 091 return sheet.getRows(); 092 } 093 094 @Override 095 public int getColumnCount() 096 { 097 098 return sheet.getColumns(); 099 } 100 101 /** 102 * Copied from javax.swing.table.AbstractTableModel, to name columns using spreadsheet 103 * conventions: A, B, C, . Z, AA, AB, etc. 104 */ 105 @Override 106 public String getColumnName(int column) 107 { 108 String result = ""; 109 for (; column >= 0; column = column / 26 - 1) 110 { 111 result = (char) ((char) (column % 26) + 'A') + result; 112 } 113 return result; 114 } 115 116 @Override 117 public Class<?> getColumnClass(int columnIndex) 118 { 119 return String.class; 120 } 121 122 @Override 123 public boolean isCellEditable(int rowIndex, int columnIndex) 124 { 125 return false; 126 } 127 128 @Override 129 public Object getValueAt(int rowIndex, int columnIndex) 130 { 131 132 try 133 { 134 return sheet.getCell(columnIndex, rowIndex).getContents(); 135 } 136 catch (Exception e) 137 { 138 return null; 139 } 140 } 141 142 @Override 143 public void setValueAt(Object aValue, int rowIndex, int columnIndex) 144 { 145 146 } 147 148 @Override 149 public void addTableModelListener(TableModelListener l) 150 { 151 152 } 153 154 @Override 155 public void removeTableModelListener(TableModelListener l) 156 { 157 158 } 159 } 160}