KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > poi > hssf > contrib > view > SVRowHeader


1
2 /* ====================================================================
3    Copyright 2002-2004 Apache Software Foundation
4
5    Licensed under the Apache License, Version 2.0 (the "License");
6    you may not use this file except in compliance with the License.
7    You may obtain a copy of the License at
8
9        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
19
20 package org.apache.poi.hssf.contrib.view;
21
22 import java.awt.*;
23 import java.awt.event.*;
24 import java.io.*;
25 import javax.swing.*;
26 import javax.swing.table.*;
27 import javax.swing.event.*;
28
29 import org.apache.poi.hssf.usermodel.*;
30
31 /**
32  * This class presents the row header to the table.
33  *
34  *
35  * @author Jason Height
36  */

37 public class SVRowHeader extends JList {
38   /** This model simply returns an integer number up to the number of rows
39    * that are present in the sheet.
40    *
41    */

42   private class SVRowHeaderModel extends AbstractListModel {
43     private HSSFSheet sheet;
44
45     public SVRowHeaderModel(HSSFSheet sheet) {
46       this.sheet = sheet;
47     }
48
49     public int getSize() { return sheet.getPhysicalNumberOfRows(); }
50     public Object JavaDoc getElementAt(int index) {
51       return Integer.toString(index+1);
52     }
53   }
54
55   /** Renderes the row number*/
56   private class RowHeaderRenderer extends JLabel implements ListCellRenderer {
57     private HSSFSheet sheet;
58     private int extraHeight;
59
60     RowHeaderRenderer(HSSFSheet sheet, JTable table, int extraHeight) {
61       this.sheet = sheet;
62       this.extraHeight = extraHeight;
63       JTableHeader header = table.getTableHeader();
64       setOpaque(true);
65       setBorder(UIManager.getBorder("TableHeader.cellBorder"));
66       setHorizontalAlignment(CENTER);
67       setForeground(header.getForeground());
68       setBackground(header.getBackground());
69       setFont(header.getFont());
70     }
71
72     public Component getListCellRendererComponent( JList list,
73            Object JavaDoc value, int index, boolean isSelected, boolean cellHasFocus) {
74       Dimension d = getPreferredSize();
75       int rowHeight = (int)sheet.getRow(index).getHeightInPoints();
76       d.height = rowHeight+extraHeight;
77       setPreferredSize(d);
78       setText((value == null) ? "" : value.toString());
79       return this;
80     }
81   }
82
83   public SVRowHeader(HSSFSheet sheet, JTable table, int extraHeight) {
84     ListModel lm = new SVRowHeaderModel(sheet);
85     this.setModel(lm);
86
87     setFixedCellWidth(50);
88     setCellRenderer(new RowHeaderRenderer(sheet, table, extraHeight));
89   }
90 }
91
Popular Tags