001: /*
002: * RowNumberHeader.java
003: *
004: * Copyright (C) 2002, 2003, 2004, 2005, 2006 Takis Diakoumis
005: *
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU General Public License
008: * as published by the Free Software Foundation; either version 2
009: * of the License, or any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU General Public License for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
019: *
020: */
021:
022: package org.underworldlabs.swing.table;
023:
024: import java.awt.Component;
025: import java.awt.Font;
026: import java.awt.FontMetrics;
027: import javax.swing.AbstractListModel;
028: import javax.swing.JLabel;
029: import javax.swing.JList;
030: import javax.swing.JTable;
031: import javax.swing.ListCellRenderer;
032: import javax.swing.UIManager;
033: import javax.swing.event.ListSelectionEvent;
034: import javax.swing.event.ListSelectionListener;
035: import javax.swing.table.JTableHeader;
036:
037: /* ----------------------------------------------------------
038: * CVS NOTE: Changes to the CVS repository prior to the
039: * release of version 3.0.0beta1 has meant a
040: * resetting of CVS revision numbers.
041: * ----------------------------------------------------------
042: */
043:
044: /**
045: * Provides row numbers for a <code>JTable</code>.
046: *
047: * @author Takis Diakoumis
048: * @version $Revision: 1.4 $
049: * @date $Date: 2006/05/14 06:56:07 $
050: */
051: public class RowNumberHeader extends JList implements
052: ListSelectionListener {
053:
054: /** The table to apply the row header */
055: protected JTable table;
056:
057: /** the row count to be displayed */
058: protected int rowCount;
059:
060: /** the list model */
061: protected RowHeaderListModel model;
062:
063: /** the default min width */
064: private static final int MINIMUM_WIDTH = 20;
065:
066: /**
067: * Constructs a new instance with the specified table.
068: *
069: * @param the table to apply the row header to
070: */
071: public RowNumberHeader(JTable table) {
072: this .table = table;
073: initRowHeaderView();
074:
075: model = new RowHeaderListModel();
076: setModel(model);
077:
078: addListSelectionListener(this );
079: setCellRenderer(new RowHeaderRenderer(table));
080: }
081:
082: protected void initRowHeaderView() {
083: if (table == null) {
084: return;
085: }
086: rowCount = table.getRowCount();
087:
088: // determine the width based on the largest number displayed
089: JTableHeader header = table.getTableHeader();
090: Font headerFont = header.getFont();
091: FontMetrics metrics = header.getFontMetrics(headerFont);
092:
093: String rowValueString = String.valueOf(rowCount) + " ";
094: int width = Math.max(MINIMUM_WIDTH, metrics
095: .stringWidth(rowValueString));
096: // add a couple of pixels left/right
097: width += 4;
098: setFixedCellWidth(width);
099: setFixedCellHeight(table.getRowHeight());
100:
101: // force an update of the model
102: if (model != null) {
103: model.contentsChanged();
104: }
105: }
106:
107: public void valueChanged(ListSelectionEvent e) {
108: int[] selections = getSelectedIndices();
109: if (selections != null && selections.length > 0) {
110: table.clearSelection();
111: table.setColumnSelectionAllowed(false);
112: table.setRowSelectionAllowed(true);
113:
114: for (int i = 0; i < selections.length; i++) {
115: table.addRowSelectionInterval(selections[i],
116: selections[i]);
117: }
118:
119: }
120: }
121:
122: public void setTable(JTable table) {
123: this .table = table;
124: initRowHeaderView();
125: }
126:
127: class RowHeaderRenderer extends JLabel implements ListCellRenderer {
128:
129: RowHeaderRenderer(JTable table) {
130: setOpaque(true);
131: setHorizontalAlignment(RIGHT);
132: setBorder(UIManager.getBorder("TableHeader.cellBorder"));
133: setFont(UIManager.getFont("TableHeader.font"));
134: setForeground(UIManager.getColor("TableHeader.foreground"));
135: setBackground(UIManager.getColor("TableHeader.background"));
136: //setToolTipText("Add this to the row selection");
137: }
138:
139: public Component getListCellRendererComponent(JList list,
140: Object value, int index, boolean isSelected,
141: boolean cellHasFocus) {
142: setText((value == null) ? "" : value.toString() + " ");
143: return this ;
144: }
145:
146: } // class RowHeaderRenderer
147:
148: class RowHeaderListModel extends AbstractListModel {
149:
150: RowHeaderListModel() {
151: }
152:
153: public int getSize() {
154: return rowCount;
155: }
156:
157: public Object getElementAt(int index) {
158: return String.valueOf(index + 1);
159: }
160:
161: protected void contentsChanged() {
162: fireContentsChanged(this , -1, -1);
163: }
164:
165: } // class RowHeaderListModel
166:
167: }
|