001/* ----------------------------------------------------------------------------
002   The Kiwi Toolkit - A Java Class Library
003   Copyright (C) 1998-2004 Mark A. Lindner
004
005   This library is free software; you can redistribute it and/or
006   modify it under the terms of the GNU General Public License as
007   published by the Free Software Foundation; either version 2 of the
008   License, or (at your option) any later version.
009
010   This library is distributed in the hope that it will be useful,
011   but WITHOUT ANY WARRANTY; without even the implied warranty of
012   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013   General Public License for more details.
014
015   You should have received a copy of the GNU General Public License
016   along with this library; if not, write to the Free Software
017   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
018   02111-1307, USA.
019 
020   The author may be contacted at: mark_a_lindner@yahoo.com
021   ----------------------------------------------------------------------------
022   $Log: AbstractCellRenderer.java,v $
023   Revision 1.3  2004/05/31 07:25:59  markl
024   propagate font from containing component to renderer.
025
026   Revision 1.2  2004/05/12 19:03:56  markl
027   finalized methods that should not be overridden
028
029   Revision 1.1  2004/01/22 23:45:15  markl
030   new class
031   ----------------------------------------------------------------------------
032*/
033
034package kiwi.ui;
035
036import java.awt.*;
037import javax.swing.*;
038import javax.swing.border.*;
039import javax.swing.table.*;
040
041/** An abstract class providing base functionality for table and list
042 * cell renderers. Concrete subclasses provide the actual rendering component,
043 * which is then decorated with a border and background color based on the
044 * selection and focus states of the cell.
045 *
046 * @author Mark Lindner
047 * @since Kiwi 2.0
048 */
049
050public abstract class AbstractCellRenderer
051  implements ListCellRenderer, TableCellRenderer
052  {
053  private Border emptyBorder;
054
055  /** Construct a new <code>AbstractCellRenderer</code>.
056   */
057  
058  protected AbstractCellRenderer()
059    {
060    emptyBorder = BorderFactory.createEmptyBorder(1, 2, 1, 2);
061    }
062
063  /** Get a table cell renderer component.
064   */
065
066  public final Component getTableCellRendererComponent(JTable table,
067                                                       Object value,
068                                                       boolean isSelected,
069                                                       boolean hasFocus,
070                                                       int row,
071                                                       int column)
072    {
073    JComponent c = getCellRenderer(table, value, row, column);
074
075    c.setFont(table.getFont());
076    
077    if(isSelected)
078      {
079      c.setForeground(table.getSelectionForeground());
080      c.setBackground(table.getSelectionBackground());
081      }
082    else
083      {
084      c.setForeground(table.getForeground());
085      c.setBackground(table.getBackground());
086      }
087
088    c.setBorder(hasFocus
089                ? UIManager.getBorder("Table.focusCellHighlightBorder")
090                : emptyBorder);
091
092    c.setOpaque(isSelected);
093    
094    return(c);
095    }
096  
097  /** Get a list cell renderer component.
098   */
099  
100  public final Component getListCellRendererComponent(JList list, Object value,
101                                                      int index,
102                                                      boolean isSelected,
103                                                      boolean hasFocus)
104    {
105    JComponent c = getCellRenderer(list, value, index, 0);
106
107    c.setFont(list.getFont());
108    
109    if(isSelected)
110      {
111      c.setBackground(list.getSelectionBackground());
112      c.setForeground(list.getSelectionForeground());
113      }
114    else
115      {
116      c.setBackground(list.getBackground());
117      c.setForeground(list.getForeground());
118      }
119    
120    c.setBorder(hasFocus
121                ? UIManager.getBorder("List.focusCellHighlightBorder")
122                : emptyBorder);
123
124    c.setOpaque(isSelected);
125
126    return(c);
127    }
128
129  /**
130   * Get the actual renderer component.
131   *
132   * @param component The <code>JList</code> or <code>JTable</code> for which
133   * the renderer is being requested.
134   * @param value The value to render.
135   * @param row The row in the table or index in the list of the cell being
136   * rendered.
137   * @param column The column in the table of the cell being rendered, (or 0
138   * if a list).
139   * @return The renderer component.
140   */
141
142  protected abstract JComponent getCellRenderer(JComponent component,
143                                                Object value, int row,
144                                                int column);
145  }
146
147/* end of source file */