001: /*
002: * Copyright (C) 2005 Jeff Tassin
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2.1 of the License, or (at your option) any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: */
018:
019: package com.jeta.swingbuilder.gui.components;
020:
021: import java.awt.Rectangle;
022:
023: import javax.swing.JLabel;
024: import javax.swing.JList;
025: import javax.swing.JTable;
026: import javax.swing.UIManager;
027: import javax.swing.table.JTableHeader;
028:
029: /**
030: * This class implements a Row header for a given table
031: *
032: * @author Jeff Tassin
033: */
034: public class TableRowHeader extends JList {
035: private JTable table;
036: private int m_width;
037:
038: /**
039: * flag that indicates if we are currently resizing a row by dragging the
040: * mouse
041: */
042: private boolean m_resizing = false;
043:
044: public TableRowHeader(JTable table) {
045: this (table, -1);
046: }
047:
048: /**
049: * ctor
050: *
051: * @param table
052: * the table to set the header for
053: * @parma width the width (in characters) to size the header column
054: */
055: public TableRowHeader(JTable table, int width) {
056: super (new TableRowHeaderModel(table));
057: m_width = width;
058:
059: this .table = table;
060: setFixedCellHeight(table.getRowHeight());
061: setFixedCellWidth(preferredHeaderWidth());
062:
063: setCellRenderer(new RowHeaderRenderer(table));
064: setFocusable(false);
065: }
066:
067: /**
068: * @return the table
069: */
070: public JTable getTable() {
071: return table;
072: }
073:
074: public void setSelectedIndex(int index) {
075: super .setSelectedIndex(index);
076: }
077:
078: public void setSelectedIndices(int[] indices) {
079: super .setSelectedIndices(indices);
080: }
081:
082: /**
083: * Returns the bounds of the specified range of items in JList coordinates.
084: * Returns null if index isn't valid.
085: *
086: * @param index0
087: * the index of the first JList cell in the range
088: * @param index1
089: * the index of the last JList cell in the range
090: * @return the bounds of the indexed cells in pixels
091: */
092: public Rectangle getCellBounds(int index0, int index1) {
093: Rectangle rect0 = table.getCellRect(index0, 0, true);
094: Rectangle rect1 = table.getCellRect(index1, 0, true);
095: int y, height;
096: if (rect0.y < rect1.y) {
097: y = rect0.y;
098: height = rect1.y + rect1.height - y;
099: } else {
100: y = rect1.y;
101: height = rect0.y + rect0.height - y;
102: }
103: return new Rectangle(0, y, getFixedCellWidth(), height);
104: }
105:
106: // assume that row header width should be big enough to display row number
107: // Integer.MAX_VALUE completely
108:
109: private int preferredHeaderWidth() {
110: JLabel longestRowLabel = new JLabel("65356#");
111: if (m_width == 1)
112: longestRowLabel = new JLabel("#");
113: else if (m_width == 2)
114: longestRowLabel = new JLabel("##");
115: else if (m_width == 3)
116: longestRowLabel = new JLabel("###");
117: else if (m_width == 4)
118: longestRowLabel = new JLabel("####");
119: else if (m_width == 5)
120: longestRowLabel = new JLabel("#####");
121:
122: JTableHeader header = table.getTableHeader();
123: longestRowLabel.setBorder(header.getBorder());
124: // UIManager.getBorder("TableHeader.cellBorder"));
125: longestRowLabel.setHorizontalAlignment(JLabel.CENTER);
126: longestRowLabel.setFont(header.getFont());
127: return longestRowLabel.getPreferredSize().width;
128: }
129:
130: public void updateUI() {
131: super .updateUI();
132:
133: if (table != null) {
134: setCellRenderer(new RowHeaderRenderer(table));
135: }
136: setOpaque(true);
137: setBackground(UIManager.getColor("panel.background"));
138: revalidate();
139: repaint();
140: }
141:
142: }
|