Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

SViewerPanel.java 9.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. /* ====================================================================
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.hssf.view;
  16. import java.awt.*;
  17. import java.awt.event.*;
  18. import java.io.*;
  19. import javax.swing.*;
  20. import javax.swing.table.*;
  21. import org.apache.poi.hssf.usermodel.*;
  22. /**
  23. * This class presents the sheets to the user.
  24. *
  25. *
  26. * @author Andrew C. Oliver
  27. * @author Jason Height
  28. */
  29. public class SViewerPanel extends JPanel {
  30. /** This field is the magic number to convert from a Character width to a
  31. * java pixel width.
  32. *
  33. * When the "normal" font size in a workbook changes, this effects all
  34. * of the heights and widths. Unfortunately there is no way to retrieve this
  35. * information, hence the MAGIC number.
  36. *
  37. * This number may only work for the normal style font size of Arial size 10.
  38. *
  39. */
  40. private static final int magicCharFactor = 7;
  41. /** Reference to the wookbook that is being displayed*/
  42. /* package */ HSSFWorkbook wb;
  43. /** Reference to the tabs component*/
  44. /* package */ JTabbedPane sheetPane;
  45. /** Reference to the cell renderer that is used to render all cells*/
  46. private SVTableCellRenderer cellRenderer;
  47. /** Reference to the cell editor that is used to edit all cells.
  48. * Only constructed if editing is allowed
  49. */
  50. private SVTableCellEditor cellEditor;
  51. /** Flag indicating if editing is allowed. Otherwise the viewer is in
  52. * view only mode.
  53. */
  54. private boolean allowEdits;
  55. /**Construct the representation of the workbook*/
  56. public SViewerPanel(HSSFWorkbook wb, boolean allowEdits) {
  57. this.wb = wb;
  58. this.allowEdits = allowEdits;
  59. initialiseGui();
  60. }
  61. private void initialiseGui() {
  62. cellRenderer = new SVTableCellRenderer(this.wb);
  63. if (allowEdits)
  64. cellEditor = new SVTableCellEditor(this.wb);
  65. //Initialise the Panel
  66. sheetPane = new JTabbedPane(JTabbedPane.BOTTOM);
  67. if (allowEdits)
  68. sheetPane.addMouseListener(createTabListener());
  69. int sheetCount = wb.getNumberOfSheets();
  70. for (int i=0; i<sheetCount;i++) {
  71. String sheetName = wb.getSheetName(i);
  72. //Add the new sheet to the tabbed pane
  73. sheetPane.addTab(sheetName, makeSheetView(wb.getSheetAt(i)));
  74. }
  75. setLayout(new BorderLayout());
  76. add(sheetPane, BorderLayout.CENTER);
  77. }
  78. protected JComponent makeSheetView(HSSFSheet sheet) {
  79. JTable sheetView = new JTable(new SVTableModel(sheet));
  80. sheetView.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
  81. sheetView.setDefaultRenderer(HSSFCell.class, cellRenderer);
  82. if (allowEdits)
  83. sheetView.setDefaultEditor(HSSFCell.class, cellEditor);
  84. JTableHeader header = sheetView.getTableHeader();
  85. //Dont allow column reordering
  86. header.setReorderingAllowed(false);
  87. //Only allow column resizing if editing is allowed
  88. header.setResizingAllowed(allowEdits);
  89. //Set the columns the correct size
  90. TableColumnModel columns = sheetView.getColumnModel();
  91. for (int i=0; i< columns.getColumnCount(); i++) {
  92. TableColumn column = columns.getColumn(i);
  93. int width = sheet.getColumnWidth(i);
  94. //256 is because the width is in 256ths of a character
  95. column.setPreferredWidth(width/256*magicCharFactor);
  96. }
  97. //Set the rows to the correct size
  98. int rows = sheet.getPhysicalNumberOfRows();
  99. Insets insets = cellRenderer.getInsets();
  100. //Need to include the insets in the calculation of the row height to use.
  101. int extraHeight = insets.bottom+insets.top;
  102. for (int i=0; i< rows; i++) {
  103. HSSFRow row = sheet.getRow(i);
  104. if (row == null) {
  105. sheetView.setRowHeight(i, (int)sheet.getDefaultRowHeightInPoints()+extraHeight);
  106. } else {
  107. sheetView.setRowHeight(i, (int)row.getHeightInPoints()+extraHeight);
  108. }
  109. }
  110. //Add the row header to the sheet
  111. SVRowHeader rowHeader = new SVRowHeader(sheet, sheetView, extraHeight);
  112. JScrollPane scroll = new JScrollPane( sheetView );
  113. scroll.setRowHeaderView(rowHeader);
  114. return scroll;
  115. }
  116. public void paint(Graphics g) {
  117. //JMH I am only overriding this to get a picture of the time taken to paint
  118. long start = System.currentTimeMillis();
  119. super.paint(g);
  120. long elapsed = System.currentTimeMillis()-start;
  121. System.out.println("Paint time = "+elapsed);
  122. }
  123. protected MouseListener createTabListener() {
  124. return new TabListener();
  125. }
  126. /** This class defines the default MouseListener that listens to
  127. * mouse events in the tabbed pane
  128. *
  129. * The default is to popup a menu when the event occurs over a tab
  130. */
  131. private class TabListener implements MouseListener {
  132. public JPopupMenu popup;
  133. public TabListener() {
  134. popup = new JPopupMenu("Sheet");
  135. popup.add(createInsertSheetAction());
  136. popup.add(createDeleteSheetAction());
  137. popup.add(createRenameSheetAction());
  138. }
  139. protected Action createInsertSheetAction() {
  140. return new InsertSheetAction();
  141. }
  142. protected Action createDeleteSheetAction() {
  143. return new DeleteSheetAction();
  144. }
  145. protected Action createRenameSheetAction() {
  146. return new RenameSheetAction();
  147. }
  148. /** This method will display the popup if the mouseevent is a popup event
  149. * and the event occurred over a tab
  150. */
  151. protected void checkPopup(MouseEvent e) {
  152. if (e.isPopupTrigger()) {
  153. int tab = sheetPane.getUI().tabForCoordinate(sheetPane, e.getX(), e.getY());
  154. if (tab != -1) {
  155. popup.show(sheetPane, e.getX(), e.getY());
  156. }
  157. }
  158. }
  159. public void mouseClicked(MouseEvent e) {
  160. checkPopup(e);
  161. }
  162. public void mousePressed(MouseEvent e) {
  163. checkPopup(e);
  164. }
  165. public void mouseReleased(MouseEvent e) {
  166. checkPopup(e);
  167. }
  168. public void mouseEntered(MouseEvent e) {}
  169. public void mouseExited(MouseEvent e) {}
  170. }
  171. /** This class defines the action that is performed when the sheet is renamed*/
  172. private class RenameSheetAction extends AbstractAction {
  173. public RenameSheetAction() {
  174. super("Rename");
  175. }
  176. public void actionPerformed(ActionEvent e) {
  177. int tabIndex = sheetPane.getSelectedIndex();
  178. if (tabIndex != -1) {
  179. String newSheetName = JOptionPane.showInputDialog(sheetPane, "Enter a new Sheetname", "Rename Sheet", JOptionPane.QUESTION_MESSAGE);
  180. if (newSheetName != null) {
  181. wb.setSheetName(tabIndex, newSheetName);
  182. sheetPane.setTitleAt(tabIndex, newSheetName);
  183. }
  184. }
  185. }
  186. }
  187. /** This class defines the action that is performed when a sheet is inserted*/
  188. private class InsertSheetAction extends AbstractAction {
  189. public InsertSheetAction() {
  190. super("Insert");
  191. }
  192. public void actionPerformed(ActionEvent e) {
  193. //Create a new sheet then search for the sheet and make sure that the
  194. //sheetPane shows it.
  195. HSSFSheet newSheet = wb.createSheet();
  196. for (int i=0; i<wb.getNumberOfSheets();i++) {
  197. HSSFSheet sheet = wb.getSheetAt(i);
  198. if (newSheet == sheet) {
  199. sheetPane.insertTab(wb.getSheetName(i), null, makeSheetView(sheet), null, i);
  200. }
  201. }
  202. }
  203. }
  204. /** This class defines the action that is performed when the sheet is deleted*/
  205. private class DeleteSheetAction extends AbstractAction {
  206. public DeleteSheetAction() {
  207. super("Delete");
  208. }
  209. public void actionPerformed(ActionEvent e) {
  210. int tabIndex = sheetPane.getSelectedIndex();
  211. if (tabIndex != -1) {
  212. if (JOptionPane.showConfirmDialog(sheetPane, "Are you sure that you want to delete the selected sheet", "Delete Sheet?", JOptionPane.OK_CANCEL_OPTION) == JOptionPane.OK_OPTION) {
  213. wb.removeSheetAt(tabIndex);
  214. sheetPane.remove(tabIndex);
  215. }
  216. }
  217. }
  218. }
  219. public boolean isEditable() {
  220. return allowEdits;
  221. }
  222. /**Main method*/
  223. public static void main(String[] args) {
  224. if(args.length < 1) {
  225. throw new IllegalArgumentException("A filename to view must be supplied as the first argument, but none was given");
  226. }
  227. try {
  228. FileInputStream in = new FileInputStream(args[0]);
  229. HSSFWorkbook wb = new HSSFWorkbook(in);
  230. in.close();
  231. SViewerPanel p = new SViewerPanel(wb, true);
  232. JFrame frame;
  233. frame = new JFrame() {
  234. protected void processWindowEvent(WindowEvent e) {
  235. super.processWindowEvent(e);
  236. if (e.getID() == WindowEvent.WINDOW_CLOSING) {
  237. System.exit(0);
  238. }
  239. }
  240. public synchronized void setTitle(String title) {
  241. super.setTitle(title);
  242. enableEvents(AWTEvent.WINDOW_EVENT_MASK);
  243. }
  244. };
  245. frame.setTitle("Viewer Frame");
  246. frame.getContentPane().add(p, BorderLayout.CENTER);
  247. frame.setSize(800,640);
  248. Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
  249. frame.setLocation((d.width - frame.getSize().width) / 2, (d.height - frame.getSize().height) / 2);
  250. frame.setVisible(true);
  251. } catch (IOException ex) {
  252. ex.printStackTrace();
  253. System.exit(1);
  254. }
  255. }
  256. }