View Javadoc

1   /*
2   This file is part of the xframe software package
3   hosted at http://xframe.sourceforge.net
4   
5   Copyright (c) 2003 Kurt Riede.
6   
7   This library is free software; you can redistribute it and/or
8   modify it under the terms of the GNU Lesser General Public
9   License as published by the Free Software Foundation; either
10  version 2.1 of the License, or (at your option) any later version.
11  
12  This library is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  Lesser General Public License for more details.
16  
17  You should have received a copy of the GNU Lesser General Public
18  License along with this library; if not, write to the Free Software
19  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20  */
21  package net.sf.xframe.swing.table;
22  
23  import java.awt.Component;
24  
25  import javax.swing.JTable;
26  import javax.swing.table.DefaultTableCellRenderer;
27  import javax.swing.table.TableCellRenderer;
28  
29  import net.sf.xframe.swing.JXTable;
30  
31  
32  /***
33   * The standard class for rendering (displaying) individual cells
34   * in a {@link net.sf.xframe.swing.JXTable JXTable}.
35   *
36   * @author <a href=mailto:kriede@users.sourceforge.net>Kurt Riede</a>
37   */
38  public class DefaultJXTabelCellRenderer extends DefaultTableCellRenderer {
39  
40      /***
41       * @see javax.swing.table.TableCellRenderer#getTableCellRendererComponent(javax.swing.JTable,
42       *      java.lang.Object, boolean, boolean, int, int)
43       */
44      public final Component getTableCellRendererComponent(final JTable table, final Object value, final boolean isSelected,
45              final boolean hasFocus, final int row, final int column) {
46          super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
47          if (table instanceof KTable) {
48              final KTable kTable = (KTable) table;
49              final JXTable jxTable = kTable.getEnclosingJXTable();
50              if (kTable.getType() == KTable.TYPE_LOCKED) {
51                  return getTableCellRendererComponent(jxTable, value, isSelected, hasFocus, row, column);
52              } else if (kTable.getType() == KTable.TYPE_SCROLL) {
53                  final int frozenColumns = jxTable.getFrozenColumns();
54                  return getTableCellRendererComponent(jxTable, value, isSelected, hasFocus, row, column + frozenColumns);
55              }
56          }
57          final Component c = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
58          return c;
59      }
60  
61      /***
62       * Returns the default table cell renderer.
63       *
64       * @param jxTable  the <code>JXTable</code>
65       * @param value  the value to assign to the cell at
66       *          <code>[row, column]</code>
67       * @param isSelected true if cell is selected
68       * @param hasFocus true if cell has focus
69       * @param row  the row of the cell to render
70       * @param column the column of the cell to render
71       * @return the default table cell renderer
72       */
73      public Component getTableCellRendererComponent(final JXTable jxTable, final Object value, final boolean isSelected,
74              final boolean hasFocus, final int row, final int column) {
75          final int frozenColumns = jxTable.getFrozenColumns();
76          if (column < frozenColumns) {
77              final JTable table = jxTable.getLockedTable();
78              final TableCellRenderer renderer = table.getColumnModel().getColumn(column).getCellRenderer();
79              return renderer.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
80          } else {
81              final JTable table = jxTable.getScrollTable();
82              final TableCellRenderer renderer = table.getColumnModel().getColumn(column - frozenColumns).getCellRenderer();
83              return renderer.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column - frozenColumns);
84          }
85      }
86  }