swing/src/net/sf/xframe/swing/table DefaultJXTabelCellRenderer.java,NONE,1.1 KTable.java,1.2,1.3

Kurt Riede <[email protected]> Sat, 09 Apr 2005 12:48:15 +0000
Newsgroups gmane.text.xml.xframe.xsddoc.devel
Message-ID <[email protected]>
Update of /cvsroot/xframe/swing/src/net/sf/xframe/swing/table
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv5698/src/net/sf/xframe/swing/table

Modified Files:
	KTable.java 
Added Files:
	DefaultJXTabelCellRenderer.java 
Log Message:
fix for bug [1178013] Wrong isCellEditable concept
support for cell renderers thru new class DefaultJXTableCellRenderer

--- NEW FILE: DefaultJXTabelCellRenderer.java ---
/*
This file is part of the xframe software package
hosted at http://xframe.sourceforge.net

Copyright (c) 2003 Kurt Riede.

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, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package net.sf.xframe.swing.table;

import java.awt.Component;

import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableCellRenderer;

import net.sf.xframe.swing.JXTable;


/**
 * The standard class for rendering (displaying) individual cells
 * in a {@link net.sf.xframe.swing.JXTable JXTable}.
 *
 * @author <a href=mailto:[email protected]>Kurt Riede</a>
 */
public class DefaultJXTabelCellRenderer extends DefaultTableCellRenderer {

    /**
     * @see javax.swing.table.TableCellRenderer#getTableCellRendererComponent(javax.swing.JTable,
     *      java.lang.Object, boolean, boolean, int, int)
     */
    public final Component getTableCellRendererComponent(final JTable table, final Object value, final boolean isSelected,
            final boolean hasFocus, final int row, final int column) {
        super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
        if (table instanceof KTable) {
            final KTable kTable = (KTable) table;
            final JXTable jxTable = kTable.getEnclosingJXTable();
            if (kTable.getType() == KTable.TYPE_LOCKED) {
                return getTableCellRendererComponent(jxTable, value, isSelected, hasFocus, row, column);
            } else if (kTable.getType() == KTable.TYPE_SCROLL) {
                final int frozenColumns = jxTable.getFrozenColumns();
                return getTableCellRendererComponent(jxTable, value, isSelected, hasFocus, row, column + frozenColumns);
            }
        }
        final Component c = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
        return c;
    }

    /**
     * Returns the default table cell renderer.
     *
     * @param jxTable  the <code>JXTable</code>
     * @param value  the value to assign to the cell at
     *          <code>[row, column]</code>
     * @param isSelected true if cell is selected
     * @param hasFocus true if cell has focus
     * @param row  the row of the cell to render
     * @param column the column of the cell to render
     * @return the default table cell renderer
     */
    public Component getTableCellRendererComponent(final JXTable jxTable, final Object value, final boolean isSelected,
            final boolean hasFocus, final int row, final int column) {
        final int frozenColumns = jxTable.getFrozenColumns();
        if (column < frozenColumns) {
            final JTable table = jxTable.getLockedTable();
            final TableCellRenderer renderer = table.getColumnModel().getColumn(column).getCellRenderer();
            return renderer.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
        } else {
            final JTable table = jxTable.getScrollTable();
            final TableCellRenderer renderer = table.getColumnModel().getColumn(column - frozenColumns).getCellRenderer();
            return renderer.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column - frozenColumns);
        }
    }
}

Index: KTable.java
===================================================================
RCS file: /cvsroot/xframe/swing/src/net/sf/xframe/swing/table/KTable.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -C2 -d -r1.2 -r1.3
*** KTable.java	12 Nov 2004 11:37:17 -0000	1.2
--- KTable.java	9 Apr 2005 12:48:13 -0000	1.3
***************
*** 21,24 ****
--- 21,25 ----
  package net.sf.xframe.swing.table;
  
+ import java.awt.Component;
  import java.awt.Rectangle;
  import java.awt.event.ItemEvent;
***************
*** 39,46 ****
  import javax.swing.table.DefaultTableModel;
  import javax.swing.table.JTableHeader;
! import javax.swing.table.TableColumn;
  import javax.swing.table.TableColumnModel;
  import javax.swing.table.TableModel;
  
  /**
   * Internal extension of JTable fixes for common swing problems and additional
--- 40,49 ----
  import javax.swing.table.DefaultTableModel;
  import javax.swing.table.JTableHeader;
! import javax.swing.table.TableCellRenderer;
  import javax.swing.table.TableColumnModel;
  import javax.swing.table.TableModel;
  
+ import net.sf.xframe.swing.JXTable;
+ 
  /**
   * Internal extension of JTable fixes for common swing problems and additional
***************
*** 51,57 ****
--- 54,72 ----
  public final class KTable extends JTable {
  
+     /** Identifies a KTable that is a locked table within a {@link net.sf.xframe.swing.JXTable JXTable}. */
+     public static final int TYPE_NONE = 0;
+ 
+     /** Identifies a KTable that is a locked table within a {@link net.sf.xframe.swing.JXTable JXTable}. */
+     public static final int TYPE_LOCKED = 1;
+ 
+     /** Identifies a KTable that is a scrollable table within a {@link net.sf.xframe.swing.JXTable JXTable}. */
+     public static final int TYPE_SCROLL = 2;
+ 
      /** List of event listeners. */
      private EventListenerList listenerList = new EventListenerList();
  
+     /** Type of tabel, can be {@link #TYPE_LOCKED}, {@link #TYPE_SCROLL} or {@link #TYPE_NONE}. */
+     private int type = TYPE_NONE;
+ 
      /**
       * Constructor.
***************
*** 117,123 ****
       *
       * @param dm        the data model for the table
       */
!     public KTable(final TableModel dm) {
!         super(dm);
      }
  
--- 132,139 ----
       *
       * @param dm        the data model for the table
+      * @param tableType the type of the ({@link #TYPE_LOCKED}, {@link #TYPE_SCROLL} or {@link #TYPE_NONE})
       */
!     public KTable(final TableModel dm, final int tableType) {
!         this(dm, null, tableType);
      }
  
***************
*** 131,135 ****
       */
      public KTable(final TableModel dm, final TableColumnModel cm) {
!         super(dm, cm);
      }
  
--- 147,164 ----
       */
      public KTable(final TableModel dm, final TableColumnModel cm) {
!         this(dm, cm, TYPE_NONE);
!     }
! 
!     /**
!      * Constructs a table that is initialized with
!      * <code>dm</code> as the data model, <code>cm</code>
!      * as the column model, and a default selection model.
!      *
!      * @param dm        the data model for the table
!      * @param cm        the column model for the table
!      * @param tableType the type of the ({@link #TYPE_LOCKED}, {@link #TYPE_SCROLL} or {@link #TYPE_NONE})
!      */
!     public KTable(final TableModel dm, final TableColumnModel cm, final int tableType) {
!         this(dm, cm, null, tableType);
      }
  
***************
*** 150,154 ****
--- 179,273 ----
       */
      public KTable(final TableModel dm, final TableColumnModel cm, final ListSelectionModel sm) {
+         this(dm, cm, sm, TYPE_NONE);
+     }
+ 
+     /**
+      * Constructs a table that is initialized with
+      * <code>dm</code> as the data model, <code>cm</code> as the
+      * column model, and <code>sm</code> as the selection model.
+      * If any of the parameters are <code>null</code> this method
+      * will initialize the table with the corresponding default model.
+      * The <code>autoCreateColumnsFromModel</code> flag is set to false
+      * if <code>cm</code> is non-null, otherwise it is set to true
+      * and the column model is populated with suitable
+      * <code>TableColumns</code> for the columns in <code>dm</code>.
+      *
+      * @param dm        the data model for the table
+      * @param cm        the column model for the table
+      * @param sm        the row selection model for the table
+      * @param tableType the type of the ({@link #TYPE_LOCKED}, {@link #TYPE_SCROLL} or {@link #TYPE_NONE})
+      */
+     public KTable(final TableModel dm, final TableColumnModel cm, final ListSelectionModel sm, final int tableType) {
          super(dm, cm, sm);
+         type = tableType;
+     }
+ 
+     /**
+      * Getter method for type property.
+      *
+      * @return type, can be {@link #LOCKED_TABLE} or {@link #SCROLL_TABLE}
+      */
+     protected int getType() {
+         return type;
+     }
+ 
+     /**
+      * Sets the parent and type property.
+      *
+      * @param pType type of tabel, can be {@link #LOCKED_TABLE} or {@link #SCROLL_TABLE}
+      */
+     protected void setParent(final int pType) {
+         type = pType;
+     }
+ 
+     /**
+      * @see javax.swing.JTable#isCellEditable(int, int)
+      */
+     public boolean isCellEditable(final int row, final int column) {
+         final JXTable table = getEnclosingJXTable();
+         if (table != null) {
+             return table.isCellEditable(row, type == TYPE_SCROLL ? column + table.getFrozenColumns() : column);
+         }
+         return super.isCellEditable(row, column);
+     }
+ 
+     /**
+      * @see javax.swing.JTable#prepareRenderer(javax.swing.table.TableCellRenderer, int, int)
+      */
+     public Component prepareRenderer(final TableCellRenderer renderer, final int row, final int column) {
+         final JXTable table = getEnclosingJXTable();
+         if (type == TYPE_SCROLL) {
+             return table.prepareRenderer(renderer, row, column + table.getFrozenColumns());
+         } else if (type == TYPE_LOCKED) {
+             return table.prepareRenderer(renderer, row, column);
+         } else {
+             return table.prepareRenderer(renderer, row, column);
+         }
+     }
+ 
+     /**
+      * Prepares a renderer from the usper class JTable.
+      *
+      * @param renderer  the <code>TableCellRenderer</code> to prepare
+      * @param row       the row of the cell to render, where 0 is the first row
+      * @param column    the column of the cell to render,
+      *          where 0 is the first column
+      * @return          the <code>Component</code> under the event location
+      */
+     public Component prepareRendererSuper(final TableCellRenderer renderer, final int row, final int column) {
+         return super.prepareRenderer(renderer, row, column);
+     }
+ 
+     /**
+      * Returns the enclosing JXTable if exists, else <tt>null</tt>.
+      *
+      * @return enclosing JXTable or <tt>null</tt>
+      */
+     JXTable getEnclosingJXTable() {
+         try {
+             return (JXTable) getParent().getParent().getParent();
+         } catch (RuntimeException e) {
+             return null;
+         }
      }
  
***************
*** 390,442 ****
  
      /**
-      * Returns the index of a column in a column model.
-      *
-      * @param cm the column model
-      * @param column the column
-      * @return the index of the colum nin the column model
-      */
-     private static int indexOf(final TableColumnModel cm, final TableColumn column) {
-         for (int i = cm.getColumnCount() - 1; i >= 0; --i) {
-             if (column.equals(cm.getColumn(i))) {
-                 return i;
-             }
-         }
-         return -1;
-     }
- 
-     /**
-      * Selection change handler.
-      *
-      * <p>
-      * This implementation uses the following conventions:
-      * <ul>
-      * <li> <code>toggle</code>: <em>false</em>, <code>extend</code>: <em>false</em>.
-      *      Clear the previous selection and ensure the new cell is selected.
-      * <li> <code>toggle</code>: <em>false</em>, <code>extend</code>: <em>true</em>.
-      *      Extend the previous selection to include the specified cell.
-      * <li> <code>toggle</code>: <em>true</em>, <code>extend</code>: <em>false</em>.
-      *      If the specified cell is selected, deselect it. If it is not selected, select it.
-      * <li> <code>toggle</code>: <em>true</em>, <code>extend</code>: <em>true</em>.
-      *      Leave the selection state as it is, but move the anchor index to the specified location.
-      * </ul>
-      * </p>
-      *
-      * @param sm the selection model
-      * @param index the index
-      * @param toggle  see description above
-      * @param extend  if true, extend the current selection
-      */
-     private static void changeTableColumnModels(
-         final ListSelectionModel sm,
-         final int index,
-         final boolean toggle,
-         final boolean extend) {
-         final int anchor = sm.getAnchorSelectionIndex();
-         final boolean selected = sm.isSelectedIndex(index);
-         final boolean anchorSelected = anchor != -1 && sm.isSelectedIndex(anchor);
-         changeTableColumnModels(sm, index, toggle, extend, selected, anchorSelected);
-     }
- 
-     /**
       * Selection change handler.
       *
--- 509,512 ----



-------------------------------------------------------
SF email is sponsored by - The IT Product Guide
Read honest & candid reviews on hundreds of IT Products from real users.
Discover which products truly live up to the hype. Start reading now.
http://ads.osdn.com/?ad_id=6595&alloc_id=14396&op=click