/* Wotonomy: OpenStep design patterns for pure Java applications. Copyright (C) 2002 Intersect Software Corporation This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, see http://www.gnu.org */ package net.wotonomy.ui.swing.components; import java.awt.Component; import java.awt.Point; import java.awt.Rectangle; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import javax.swing.JComponent; import javax.swing.JLabel; import javax.swing.JTable; import javax.swing.JTree; import javax.swing.JViewport; import javax.swing.table.TableCellRenderer; /** * A TableCellRenderer that paints a portion of a JTree. Extends JViewport to * take advantage of buffering and fast blitting (avoids repeated clipping and * repainting). Defaults opaque to false: to see selection background painted, * call setOpaque( true ). * * @author michael@mpowers.net * @author $Author: cgruber $ * @version $Revision: 904 $ */ public class TreeTableCellRenderer extends JViewport implements TableCellRenderer, MouseListener { JTree tree; Component emptyComponent; JTable delegateTable; int lastKnownColumn; /** * Constructor takes a JTree and modifies it by setting rootVisible to false, * showsRootHandles to true, opaque to false, and border to null. */ public TreeTableCellRenderer(JTree aTree) { setView(aTree); setBorder(null); tree = aTree; tree.setRootVisible(false); tree.setShowsRootHandles(true); tree.setBorder(null); tree.setOpaque(false); Object renderer = tree.getCellRenderer(); if (renderer instanceof JComponent) { ((JComponent) renderer).setOpaque(false); } Object editor = tree.getCellEditor(); if (editor instanceof JComponent) { ((JComponent) editor).setOpaque(false); } this.setOpaque(false); emptyComponent = new JLabel(); } public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { if (isSelected) { setForeground(table.getSelectionForeground()); setBackground(table.getSelectionBackground()); } else { setForeground(table.getForeground()); setBackground(table.getBackground()); } lastKnownColumn = column; if (delegateTable != table) { if (delegateTable != null) { delegateTable.removeMouseListener(this); } table.addMouseListener(this); delegateTable = table; } Rectangle rect = tree.getRowBounds(row); if (rect != null) { setViewPosition(new Point(0 /* rect.x */, rect.y)); // FIXME: this causes problems for some LAFs (like Metal): // in particular, the table height seems to get stuck. // if ( table.getRowHeight( row ) != rect.height ) // { // table.setRowHeight( row, rect.height ); // } return this; } else { return emptyComponent; } } public void mouseClicked(MouseEvent e) { delegateToTree(e); } public void mousePressed(MouseEvent e) { delegateToTree(e); } public void mouseReleased(MouseEvent e) { delegateToTree(e); } public void mouseEntered(MouseEvent e) { delegateToTree(e); } public void mouseExited(MouseEvent e) { delegateToTree(e); } protected void delegateToTree(MouseEvent e) { int col = delegateTable.getColumnModel().getColumnIndexAtX(e.getX()); if (col == lastKnownColumn) { Rectangle nodeRect = tree.getRowBounds(0); Rectangle cellRect = delegateTable.getCellRect(-1, col, false); if (nodeRect != null) { e.translatePoint(-cellRect.x, nodeRect.y); tree.dispatchEvent( // e ); new MouseEvent(tree, e.getID(), e.getWhen(), e.getModifiers(), e.getX(), e.getY(), e.getClickCount(), e.isPopupTrigger())); } } } public void repaint() { // if ( delegateTable != null ) delegateTable.repaint(); // not calling super.repaint() does not seem to cause // any problems so we're not doing it. } } /* * $Log$ Revision 1.2 2006/02/18 23:19:05 cgruber Update imports and maven * dependencies. * * Revision 1.1 2006/02/16 13:22:22 cgruber Check in all sources in * eclipse-friendly maven-enabled packages. * * Revision 1.11 2003/08/06 23:07:53 chochos general code cleanup (mostly, * removing unused imports) * * Revision 1.10 2002/04/12 20:07:35 mpowers Fixed cool/annoying view position. * * Revision 1.9 2002/04/09 18:12:21 mpowers Fixes for 1.4. * * Revision 1.8 2002/03/22 22:39:24 mpowers Can now move column to any position * in the table. * * Revision 1.7 2002/03/11 03:13:22 mpowers Adjusting for viewport position; no * longer responding to repaint(). * * Revision 1.6 2002/03/07 23:04:36 mpowers Refining TreeColumnAssociation. * * Revision 1.5 2002/03/05 23:18:28 mpowers Added documentation. Added * isSelectionPaintedImmediate and isSelectionTracking attributes to * TableAssociation. Added getTableAssociation to TableColumnAssociation. * * Revision 1.3 2002/02/27 23:19:17 mpowers Refactoring of TreeAssociation to * create TreeModelAssociation parent. * * Revision 1.2 2002/02/18 23:13:55 mpowers Only setting row height when needed. * * Revision 1.1 2002/02/18 03:46:08 mpowers Implemented TreeTableCellRenderer. * * */