You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SVRowHeader.java 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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 javax.swing.*;
  18. import javax.swing.table.*;
  19. import org.apache.poi.hssf.usermodel.*;
  20. /**
  21. * This class presents the row header to the table.
  22. *
  23. *
  24. * @author Jason Height
  25. */
  26. public class SVRowHeader extends JList {
  27. /** This model simply returns an integer number up to the number of rows
  28. * that are present in the sheet.
  29. *
  30. */
  31. private class SVRowHeaderModel extends AbstractListModel {
  32. private HSSFSheet sheet;
  33. public SVRowHeaderModel(HSSFSheet sheet) {
  34. this.sheet = sheet;
  35. }
  36. public int getSize() {
  37. return sheet.getLastRowNum() + 1;
  38. }
  39. public Object getElementAt(int index) {
  40. return Integer.toString(index+1);
  41. }
  42. }
  43. /** Renderes the row number*/
  44. private class RowHeaderRenderer extends JLabel implements ListCellRenderer {
  45. private HSSFSheet sheet;
  46. private int extraHeight;
  47. RowHeaderRenderer(HSSFSheet sheet, JTable table, int extraHeight) {
  48. this.sheet = sheet;
  49. this.extraHeight = extraHeight;
  50. JTableHeader header = table.getTableHeader();
  51. setOpaque(true);
  52. setBorder(UIManager.getBorder("TableHeader.cellBorder"));
  53. setHorizontalAlignment(CENTER);
  54. setForeground(header.getForeground());
  55. setBackground(header.getBackground());
  56. setFont(header.getFont());
  57. }
  58. public Component getListCellRendererComponent( JList list,
  59. Object value, int index, boolean isSelected, boolean cellHasFocus) {
  60. Dimension d = getPreferredSize();
  61. HSSFRow row = sheet.getRow(index);
  62. int rowHeight;
  63. if(row == null) {
  64. rowHeight = (int)sheet.getDefaultRowHeightInPoints();
  65. } else {
  66. rowHeight = (int)row.getHeightInPoints();
  67. }
  68. d.height = rowHeight+extraHeight;
  69. setPreferredSize(d);
  70. setText((value == null) ? "" : value.toString());
  71. return this;
  72. }
  73. }
  74. public SVRowHeader(HSSFSheet sheet, JTable table, int extraHeight) {
  75. ListModel lm = new SVRowHeaderModel(sheet);
  76. this.setModel(lm);
  77. setFixedCellWidth(50);
  78. setCellRenderer(new RowHeaderRenderer(sheet, table, extraHeight));
  79. }
  80. }