Line 63
Link Here
|
|
|
63 |
import javax.swing.table.*; |
64 |
import javax.swing.event.*; |
Lines 64-66
Link Here
|
64 |
import org.apache.poi.hssf.usermodel.HSSFWorkbook; |
66 |
import org.apache.poi.hssf.usermodel.*; |
65 |
import org.apache.poi.hssf.usermodel.HSSFSheet; |
|
|
66 |
import org.apache.poi.hssf.usermodel.HSSFCell; |
67 |
-- |
Line 76
Link Here
|
|
|
76 |
/** This field is the magic number to convert from a Character width to a |
77 |
* java pixel width. |
78 |
* |
79 |
* When the "normal" font size in a workbook changes, this effects all |
80 |
* of the heights and widths. Unfortunately there is no way to retrieve this |
81 |
* information, hence the MAGIC number. |
82 |
* |
83 |
* This number may only work for the normal style font size of Arial size 10. |
84 |
* |
85 |
*/ |
86 |
private static final int magicCharFactor = 7; |
87 |
/** Reference to the woorkbook that is being displayed*/ |
Line 77
Link Here
|
77 |
private JTable sheets[]; |
89 |
/** Reference to the tabs component*/ |
78 |
-- |
90 |
private JTabbedPane sheetPane; |
|
|
91 |
/** Reference to the cell renderer that is used to render all cells*/ |
92 |
private SVTableCellRenderer cellRenderer; |
93 |
/** Reference to the cell editor that is used to edit all cells. |
94 |
* Only constructed if editing is allowed |
95 |
*/ |
96 |
private SVTableCellEditor cellEditor; |
97 |
/** Flag indicating if editing is allowed. Otherwise the viewer is in |
98 |
* view only mode. |
99 |
*/ |
100 |
private boolean allowEdits; |
Line 80
Link Here
|
80 |
public SViewerPanel(HSSFWorkbook wb) { |
103 |
public SViewerPanel(HSSFWorkbook wb, boolean allowEdits) { |
81 |
-- |
|
|
Line 82
Link Here
|
|
|
105 |
this.allowEdits = allowEdits; |
106 |
|
107 |
initialiseGui(); |
108 |
} |
109 |
|
110 |
private void initialiseGui() { |
111 |
cellRenderer = new SVTableCellRenderer(this.wb); |
112 |
if (allowEdits) |
113 |
cellEditor = new SVTableCellEditor(this.wb); |
Line 84
Link Here
|
84 |
JTabbedPane sheetPane = new JTabbedPane(JTabbedPane.BOTTOM); |
116 |
sheetPane = new JTabbedPane(JTabbedPane.BOTTOM); |
85 |
-- |
117 |
|
|
|
118 |
if (allowEdits) |
119 |
sheetPane.addMouseListener(createTabListener()); |
Lines 86-87
Link Here
|
86 |
sheets = new JTable[sheetCount]; |
|
|
87 |
SVTableCellRenderer rnd = new SVTableCellRenderer(wb); |
Lines 90-93
Link Here
|
90 |
//Construct the view of the sheet |
|
|
91 |
SVTableModel tm = new SVTableModel(wb.getSheetAt(i)); |
92 |
sheets[i] = new JTable(tm); |
93 |
sheets[i].setDefaultRenderer(HSSFCell.class, rnd); |
Line 95
Link Here
|
95 |
sheetPane.addTab(sheetName, new JScrollPane(sheets[i])); |
124 |
sheetPane.addTab(sheetName, makeSheetView(wb.getSheetAt(i))); |
96 |
-- |
|
|
Line 101
Link Here
|
|
|
130 |
protected JComponent makeSheetView(HSSFSheet sheet) { |
131 |
JTable sheetView = new JTable(new SVTableModel(sheet)); |
132 |
sheetView.setAutoResizeMode(JTable.AUTO_RESIZE_OFF); |
133 |
sheetView.setDefaultRenderer(HSSFCell.class, cellRenderer); |
134 |
if (allowEdits) |
135 |
sheetView.setDefaultEditor(HSSFCell.class, cellEditor); |
136 |
JTableHeader header = sheetView.getTableHeader(); |
137 |
//Dont allow column reordering |
138 |
header.setReorderingAllowed(false); |
139 |
//Only allow column resizing if editing is allowed |
140 |
header.setResizingAllowed(allowEdits); |
141 |
|
142 |
//Set the columns the correct size |
143 |
TableColumnModel columns = sheetView.getColumnModel(); |
144 |
for (int i=0; i< columns.getColumnCount(); i++) { |
145 |
TableColumn column = columns.getColumn(i); |
146 |
short width = sheet.getColumnWidth((short)i); |
147 |
//256 is because the width is in 256ths of a character |
148 |
column.setPreferredWidth(width/256*magicCharFactor); |
149 |
} |
150 |
|
151 |
//Set the rows to the correct size |
152 |
int rows = sheet.getPhysicalNumberOfRows(); |
153 |
Insets insets = cellRenderer.getInsets(); |
154 |
//Need to include the insets in the calculation of the row height to use. |
155 |
int extraHeight = insets.bottom+insets.top; |
156 |
for (int i=0; i< rows; i++) { |
157 |
HSSFRow row = sheet.getRow(i); |
158 |
if (row == null) { |
159 |
sheetView.setRowHeight(i, (int)sheet.getDefaultRowHeightInPoints()+extraHeight); |
160 |
} else { |
161 |
sheetView.setRowHeight(i, (int)row.getHeightInPoints()+extraHeight); |
162 |
} |
163 |
} |
164 |
|
165 |
//Add the row header to the sheet |
166 |
SVRowHeader rowHeader = new SVRowHeader(sheet, sheetView, extraHeight); |
167 |
JScrollPane scroll = new JScrollPane( sheetView ); |
168 |
scroll.setRowHeaderView(rowHeader); |
169 |
return scroll; |
170 |
} |
171 |
|
Line 102
Link Here
|
|
|
173 |
//JMH I am only overriding this to get a picture of the time taken to paint |
Line 108
Link Here
|
|
|
180 |
protected MouseListener createTabListener() { |
181 |
return new TabListener(); |
182 |
} |
183 |
|
184 |
/** This class defines the default MouseListener that listens to |
185 |
* mouse events in the tabbed pane |
186 |
* |
187 |
* The default is to popup a menu when the event occurs over a tab |
188 |
*/ |
189 |
private class TabListener implements MouseListener { |
190 |
public JPopupMenu popup; |
191 |
public TabListener() { |
192 |
popup = new JPopupMenu("Sheet"); |
193 |
popup.add(createInsertSheetAction()); |
194 |
popup.add(createDeleteSheetAction()); |
195 |
popup.add(createRenameSheetAction()); |
196 |
} |
197 |
|
198 |
protected Action createInsertSheetAction() { |
199 |
return new InsertSheetAction(); |
200 |
} |
201 |
|
202 |
protected Action createDeleteSheetAction() { |
203 |
return new DeleteSheetAction(); |
204 |
} |
205 |
|
206 |
protected Action createRenameSheetAction() { |
207 |
return new RenameSheetAction(); |
208 |
} |
209 |
|
210 |
|
211 |
/** This method will display the popup if the mouseevent is a popup event |
212 |
* and the event occurred over a tab |
213 |
*/ |
214 |
protected void checkPopup(MouseEvent e) { |
215 |
if (e.isPopupTrigger()) { |
216 |
int tab = sheetPane.getUI().tabForCoordinate(sheetPane, e.getX(), e.getY()); |
217 |
if (tab != -1) { |
218 |
popup.show(sheetPane, e.getX(), e.getY()); |
219 |
} |
220 |
} |
221 |
} |
222 |
|
223 |
public void mouseClicked(MouseEvent e) { |
224 |
checkPopup(e); |
225 |
} |
226 |
|
227 |
public void mousePressed(MouseEvent e) { |
228 |
checkPopup(e); |
229 |
} |
230 |
|
231 |
public void mouseReleased(MouseEvent e) { |
232 |
checkPopup(e); |
233 |
} |
234 |
|
235 |
public void mouseEntered(MouseEvent e) {} |
236 |
public void mouseExited(MouseEvent e) {} |
237 |
} |
238 |
|
239 |
/** This class defines the action that is performed when the sheet is renamed*/ |
240 |
private class RenameSheetAction extends AbstractAction { |
241 |
public RenameSheetAction() { |
242 |
super("Rename"); |
243 |
} |
244 |
|
245 |
public void actionPerformed(ActionEvent e) { |
246 |
int tabIndex = sheetPane.getSelectedIndex(); |
247 |
if (tabIndex != -1) { |
248 |
String newSheetName = (String)JOptionPane.showInputDialog(sheetPane, "Enter a new Sheetname", "Rename Sheet", JOptionPane.QUESTION_MESSAGE); |
249 |
if (newSheetName != null) { |
250 |
wb.setSheetName(tabIndex, newSheetName); |
251 |
sheetPane.setTitleAt(tabIndex, newSheetName); |
252 |
} |
253 |
} |
254 |
} |
255 |
} |
256 |
|
257 |
/** This class defines the action that is performed when a sheet is inserted*/ |
258 |
private class InsertSheetAction extends AbstractAction { |
259 |
public InsertSheetAction() { |
260 |
super("Insert"); |
261 |
} |
262 |
|
263 |
public void actionPerformed(ActionEvent e) { |
264 |
//Create a new sheet then search for the sheet and make sure that the |
265 |
//sheetPane shows it. |
266 |
HSSFSheet newSheet = wb.createSheet(); |
267 |
for (int i=0; i<wb.getNumberOfSheets();i++) { |
268 |
HSSFSheet sheet = wb.getSheetAt(i); |
269 |
if (newSheet == sheet) { |
270 |
sheetPane.insertTab(wb.getSheetName(i), null, makeSheetView(sheet), null, i); |
271 |
} |
272 |
} |
273 |
} |
274 |
} |
275 |
|
276 |
/** This class defines the action that is performed when the sheet is deleted*/ |
277 |
private class DeleteSheetAction extends AbstractAction { |
278 |
public DeleteSheetAction() { |
279 |
super("Delete"); |
280 |
} |
281 |
|
282 |
public void actionPerformed(ActionEvent e) { |
283 |
int tabIndex = sheetPane.getSelectedIndex(); |
284 |
if (tabIndex != -1) { |
285 |
if (JOptionPane.showConfirmDialog(sheetPane, "Are you sure that you want to delete the selected sheet", "Delete Sheet?", JOptionPane.OK_CANCEL_OPTION) == JOptionPane.OK_OPTION) { |
286 |
wb.removeSheetAt(tabIndex); |
287 |
sheetPane.remove(tabIndex); |
288 |
} |
289 |
} |
290 |
} |
291 |
} |
292 |
|
293 |
public boolean isEditable() { |
294 |
return allowEdits; |
295 |
} |
296 |
|
Line 115
Link Here
|
115 |
SViewerPanel p = new SViewerPanel(wb); |
304 |
SViewerPanel p = new SViewerPanel(wb, true); |
116 |
-- |
|
|
Line 131
Link Here
|
131 |
frame.setSize(400,320); |
320 |
frame.setSize(800,640); |
132 |
-- |
|
|