01: /* ====================================================================
02: Licensed to the Apache Software Foundation (ASF) under one or more
03: contributor license agreements. See the NOTICE file distributed with
04: this work for additional information regarding copyright ownership.
05: The ASF licenses this file to You under the Apache License, Version 2.0
06: (the "License"); you may not use this file except in compliance with
07: the License. You may obtain a copy of the License at
08:
09: http://www.apache.org/licenses/LICENSE-2.0
10:
11: Unless required by applicable law or agreed to in writing, software
12: distributed under the License is distributed on an "AS IS" BASIS,
13: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: See the License for the specific language governing permissions and
15: limitations under the License.
16: ==================================================================== */
17:
18: package org.apache.poi.hssf.contrib.view;
19:
20: import java.awt.*;
21: import java.awt.event.*;
22: import java.io.*;
23: import javax.swing.*;
24: import javax.swing.table.*;
25: import javax.swing.event.*;
26:
27: import org.apache.poi.hssf.usermodel.*;
28:
29: /**
30: * This class presents the row header to the table.
31: *
32: *
33: * @author Jason Height
34: */
35: public class SVRowHeader extends JList {
36: /** This model simply returns an integer number up to the number of rows
37: * that are present in the sheet.
38: *
39: */
40: private class SVRowHeaderModel extends AbstractListModel {
41: private HSSFSheet sheet;
42:
43: public SVRowHeaderModel(HSSFSheet sheet) {
44: this .sheet = sheet;
45: }
46:
47: public int getSize() {
48: return sheet.getPhysicalNumberOfRows();
49: }
50:
51: public Object getElementAt(int index) {
52: return Integer.toString(index + 1);
53: }
54: }
55:
56: /** Renderes the row number*/
57: private class RowHeaderRenderer extends JLabel implements
58: ListCellRenderer {
59: private HSSFSheet sheet;
60: private int extraHeight;
61:
62: RowHeaderRenderer(HSSFSheet sheet, JTable table, int extraHeight) {
63: this .sheet = sheet;
64: this .extraHeight = extraHeight;
65: JTableHeader header = table.getTableHeader();
66: setOpaque(true);
67: setBorder(UIManager.getBorder("TableHeader.cellBorder"));
68: setHorizontalAlignment(CENTER);
69: setForeground(header.getForeground());
70: setBackground(header.getBackground());
71: setFont(header.getFont());
72: }
73:
74: public Component getListCellRendererComponent(JList list,
75: Object value, int index, boolean isSelected,
76: boolean cellHasFocus) {
77: Dimension d = getPreferredSize();
78: int rowHeight = (int) sheet.getRow(index)
79: .getHeightInPoints();
80: d.height = rowHeight + extraHeight;
81: setPreferredSize(d);
82: setText((value == null) ? "" : value.toString());
83: return this ;
84: }
85: }
86:
87: public SVRowHeader(HSSFSheet sheet, JTable table, int extraHeight) {
88: ListModel lm = new SVRowHeaderModel(sheet);
89: this .setModel(lm);
90:
91: setFixedCellWidth(50);
92: setCellRenderer(new RowHeaderRenderer(sheet, table, extraHeight));
93: }
94: }
|