Coverage Report - net.wotonomy.ui.swing.components.TreeTableCellRenderer
 
Classes in this File Line Coverage Branch Coverage Complexity
TreeTableCellRenderer
0% 
0% 
2
 
 1  
 /*
 2  
 Wotonomy: OpenStep design patterns for pure Java applications.
 3  
 Copyright (C) 2002 Intersect Software Corporation
 4  
 
 5  
 This library is free software; you can redistribute it and/or
 6  
 modify it under the terms of the GNU Lesser General Public
 7  
 License as published by the Free Software Foundation; either
 8  
 version 2.1 of the License, or (at your option) any later version.
 9  
 
 10  
 This library is distributed in the hope that it will be useful,
 11  
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 12  
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 13  
 Lesser General Public License for more details.
 14  
 
 15  
 You should have received a copy of the GNU Lesser General Public
 16  
 License along with this library; if not, see http://www.gnu.org
 17  
 */
 18  
 
 19  
 package net.wotonomy.ui.swing.components;
 20  
 
 21  
 import java.awt.Component;
 22  
 import java.awt.Point;
 23  
 import java.awt.Rectangle;
 24  
 import java.awt.event.MouseEvent;
 25  
 import java.awt.event.MouseListener;
 26  
 
 27  
 import javax.swing.JComponent;
 28  
 import javax.swing.JLabel;
 29  
 import javax.swing.JTable;
 30  
 import javax.swing.JTree;
 31  
 import javax.swing.JViewport;
 32  
 import javax.swing.table.TableCellRenderer;
 33  
 
 34  
 /**
 35  
 * A TableCellRenderer that paints a portion of a JTree. 
 36  
 * Extends JViewport to take advantage of buffering and
 37  
 * fast blitting (avoids repeated clipping and repainting).
 38  
 * Defaults opaque to false: to see selection background 
 39  
 * painted, call setOpaque( true ).
 40  
 *
 41  
 * @author michael@mpowers.net
 42  
 * @author $Author: cgruber $
 43  
 * @version $Revision: 904 $
 44  
 */
 45  
 public class TreeTableCellRenderer extends JViewport implements TableCellRenderer, MouseListener {
 46  
 
 47  
     JTree tree;
 48  
     Component emptyComponent;
 49  
     JTable delegateTable;
 50  
     int lastKnownColumn;
 51  
 
 52  
     /**
 53  
     * Constructor takes a JTree and modifies it by setting 
 54  
     * rootVisible to false, showsRootHandles to true,
 55  
     * opaque to false, and border to null.
 56  
     */
 57  0
     public TreeTableCellRenderer( JTree aTree )
 58  0
     {
 59  0
         setView( aTree );
 60  0
         setBorder( null );
 61  0
         tree = aTree;
 62  0
         tree.setRootVisible( false );
 63  0
         tree.setShowsRootHandles( true );
 64  0
         tree.setBorder( null );
 65  0
         tree.setOpaque( false );
 66  
         
 67  0
         Object renderer = tree.getCellRenderer();
 68  0
         if ( renderer instanceof JComponent )
 69  
         {
 70  0
             ((JComponent)renderer).setOpaque( false );
 71  
         }
 72  0
         Object editor = tree.getCellEditor();
 73  0
         if ( editor instanceof JComponent )
 74  
         {
 75  0
             ((JComponent)editor).setOpaque( false );
 76  
         }
 77  
         
 78  0
         this.setOpaque( false );
 79  0
         emptyComponent = new JLabel();
 80  0
     }
 81  
 
 82  
     public Component getTableCellRendererComponent(
 83  
                             JTable table, Object value,
 84  
                             boolean isSelected, boolean hasFocus,
 85  
                             int row, int column)
 86  
     {
 87  0
         if ( isSelected )
 88  
         {
 89  0
             setForeground( table.getSelectionForeground() );
 90  0
             setBackground( table.getSelectionBackground() );
 91  0
         }
 92  
         else
 93  
         {
 94  0
             setForeground( table.getForeground() );
 95  0
             setBackground( table.getBackground() );
 96  
         }
 97  
 
 98  0
         lastKnownColumn = column;
 99  0
         if ( delegateTable != table )
 100  
         {
 101  0
             if ( delegateTable != null )
 102  
             {
 103  0
                 delegateTable.removeMouseListener( this );
 104  
             }
 105  0
             table.addMouseListener( this );
 106  0
             delegateTable = table;
 107  
         }
 108  
         
 109  0
         Rectangle rect = tree.getRowBounds( row );
 110  0
         if ( rect != null ) 
 111  
         {
 112  0
             setViewPosition( new Point( 0 /*rect.x*/, rect.y ) );
 113  
             
 114  
             //FIXME: this causes problems for some LAFs (like Metal):
 115  
             // in particular, the table height seems to get stuck.
 116  
             //if ( table.getRowHeight( row ) != rect.height )
 117  
             //{
 118  
             //    table.setRowHeight( row, rect.height );
 119  
             //}
 120  0
             return this;
 121  
         }
 122  
         else
 123  
         {
 124  0
             return emptyComponent;
 125  
         }
 126  
     }
 127  
     
 128  
     public void mouseClicked(MouseEvent e)
 129  
     {
 130  0
         delegateToTree( e );
 131  0
     }
 132  
 
 133  
     public void mousePressed(MouseEvent e)
 134  
     {
 135  0
         delegateToTree( e );
 136  0
     }
 137  
 
 138  
     public void mouseReleased(MouseEvent e)
 139  
     {
 140  0
         delegateToTree( e );
 141  0
     }
 142  
 
 143  
     public void mouseEntered(MouseEvent e)
 144  
     {
 145  0
         delegateToTree( e );
 146  0
     }
 147  
 
 148  
     public void mouseExited(MouseEvent e)
 149  
     {
 150  0
         delegateToTree( e );
 151  0
     }
 152  
 
 153  
     protected void delegateToTree(MouseEvent e)
 154  
     {
 155  0
         int col = delegateTable.getColumnModel().getColumnIndexAtX( e.getX() );
 156  0
         if ( col == lastKnownColumn )
 157  
         {
 158  0
             Rectangle nodeRect = tree.getRowBounds( 0 );
 159  0
             Rectangle cellRect = delegateTable.getCellRect( -1, col, false );
 160  0
             if ( nodeRect != null )
 161  
             {
 162  0
                 e.translatePoint( -cellRect.x, nodeRect.y );
 163  0
                 tree.dispatchEvent( // e );
 164  0
                     new MouseEvent( tree, e.getID(), e.getWhen(), e.getModifiers(), 
 165  0
                         e.getX(), e.getY(), e.getClickCount(), e.isPopupTrigger() ) );
 166  
             }
 167  
         }
 168  0
     }
 169  
 
 170  
     public void repaint()
 171  
     {
 172  
         //if ( delegateTable != null ) delegateTable.repaint();
 173  
 
 174  
         // not calling super.repaint() does not seem to cause 
 175  
         //   any problems so we're not doing it.
 176  0
     }
 177  
 
 178  
 }
 179  
 
 180  
 /*
 181  
  * $Log$
 182  
  * Revision 1.2  2006/02/18 23:19:05  cgruber
 183  
  * Update imports and maven dependencies.
 184  
  *
 185  
  * Revision 1.1  2006/02/16 13:22:22  cgruber
 186  
  * Check in all sources in eclipse-friendly maven-enabled packages.
 187  
  *
 188  
  * Revision 1.11  2003/08/06 23:07:53  chochos
 189  
  * general code cleanup (mostly, removing unused imports)
 190  
  *
 191  
  * Revision 1.10  2002/04/12 20:07:35  mpowers
 192  
  * Fixed cool/annoying view position.
 193  
  *
 194  
  * Revision 1.9  2002/04/09 18:12:21  mpowers
 195  
  * Fixes for 1.4.
 196  
  *
 197  
  * Revision 1.8  2002/03/22 22:39:24  mpowers
 198  
  * Can now move column to any position in the table.
 199  
  *
 200  
  * Revision 1.7  2002/03/11 03:13:22  mpowers
 201  
  * Adjusting for viewport position; no longer responding to repaint().
 202  
  *
 203  
  * Revision 1.6  2002/03/07 23:04:36  mpowers
 204  
  * Refining TreeColumnAssociation.
 205  
  *
 206  
  * Revision 1.5  2002/03/05 23:18:28  mpowers
 207  
  * Added documentation.
 208  
  * Added isSelectionPaintedImmediate and isSelectionTracking attributes
 209  
  * to TableAssociation.
 210  
  * Added getTableAssociation to TableColumnAssociation.
 211  
  *
 212  
  * Revision 1.3  2002/02/27 23:19:17  mpowers
 213  
  * Refactoring of TreeAssociation to create TreeModelAssociation parent.
 214  
  *
 215  
  * Revision 1.2  2002/02/18 23:13:55  mpowers
 216  
  * Only setting row height when needed.
 217  
  *
 218  
  * Revision 1.1  2002/02/18 03:46:08  mpowers
 219  
  * Implemented TreeTableCellRenderer.
 220  
  *
 221  
  *
 222  
  */
 223  
 
 224