This fixes some rendering issues as well as some other stuff in JTable.

2005-10-28  Roman Kennke  <[EMAIL PROTECTED]>

        * javax/swing/plaf/basic/BasicTableUI.java
        (MouseInputHandler.mouseDragged): Only react if table is enabled.
        (MouseInputHandler.mousePressed): Only react if table is enabled.
        (MouseInputHandler.mouseReleased): Only react if table is enabled.
        (PropertyChangeHandler): New inner class. Updates the state of
        the table if the model property changes.
        (installDefaults): Don't load the highlightCellBorder here. This
        is done in the cell renderer now.
        (installListeners): Install the new PropertyChangeListener.
        (uninstallListeners): Uninstall the new PropertyChangeListener.
        (paintCell): Fixed determination of the selected and focused state
        of the cell. Removed handling of the border (this is moved into
        the cell renderer).
        (paint): Fixed the rectangle calculation of the cell. Fixed
painting
        of the grid.

/Roman
Index: javax/swing/plaf/basic/BasicTableUI.java
===================================================================
RCS file: /cvsroot/classpath/classpath/javax/swing/plaf/basic/BasicTableUI.java,v
retrieving revision 1.34
diff -u -r1.34 BasicTableUI.java
--- javax/swing/plaf/basic/BasicTableUI.java	20 Oct 2005 18:58:47 -0000	1.34
+++ javax/swing/plaf/basic/BasicTableUI.java	28 Oct 2005 14:45:23 -0000
@@ -52,10 +52,11 @@
 import java.awt.event.KeyEvent;
 import java.awt.event.KeyListener;
 import java.awt.event.MouseEvent;
+import java.beans.PropertyChangeEvent;
+import java.beans.PropertyChangeListener;
 
 import javax.swing.AbstractAction;
 import javax.swing.ActionMap;
-import javax.swing.BorderFactory;
 import javax.swing.CellRendererPane;
 import javax.swing.DefaultListSelectionModel;
 import javax.swing.InputMap;
@@ -94,13 +95,15 @@
   /** The normal cell border. */
   Border cellBorder;
 
-  /** The cell border for selected/highlighted cells. */
-  Border highlightCellBorder;
-
   /** The action bound to KeyStrokes. */
   TableAction action;
 
   /**
+   * Listens for changes to the tables properties.
+   */
+  private PropertyChangeListener propertyChangeListener;
+
+  /**
    * Handles key events for the JTable. Key events should be handled through
    * the InputMap/ActionMap mechanism since JDK1.3. This class is only there
    * for backwards compatibility.
@@ -198,8 +201,11 @@
 
     public void mouseDragged(MouseEvent e) 
     {
-      curr = new Point(e.getX(), e.getY());
-      updateSelection(e.isControlDown());      
+      if (table.isEnabled())
+        {
+          curr = new Point(e.getX(), e.getY());
+          updateSelection(e.isControlDown());
+        }
     }
 
     public void mouseEntered(MouseEvent e) 
@@ -219,37 +225,94 @@
 
     public void mousePressed(MouseEvent e) 
     {
-      ListSelectionModel rowModel = table.getSelectionModel();
-      ListSelectionModel colModel = table.getColumnModel().getSelectionModel();
-      int rowLead = rowModel.getLeadSelectionIndex();
-      int colLead = colModel.getLeadSelectionIndex();
+      if (table.isEnabled())
+        {
+          ListSelectionModel rowModel = table.getSelectionModel();
+          ListSelectionModel colModel = table.getColumnModel().getSelectionModel();
+          int rowLead = rowModel.getLeadSelectionIndex();
+          int colLead = colModel.getLeadSelectionIndex();
+
+          begin = new Point(e.getX(), e.getY());
+          curr = new Point(e.getX(), e.getY());
+          //if control is pressed and the cell is already selected, deselect it
+          if (e.isControlDown() && table.
+              isCellSelected(table.rowAtPoint(begin),table.columnAtPoint(begin)))
+            {                                       
+              table.getSelectionModel().
+              removeSelectionInterval(table.rowAtPoint(begin), 
+                                      table.rowAtPoint(begin));
+              table.getColumnModel().getSelectionModel().
+              removeSelectionInterval(table.columnAtPoint(begin), 
+                                      table.columnAtPoint(begin));
+            }
+          else
+            updateSelection(e.isControlDown());
 
-      begin = new Point(e.getX(), e.getY());
-      curr = new Point(e.getX(), e.getY());
-      //if control is pressed and the cell is already selected, deselect it
-      if (e.isControlDown() && table.
-          isCellSelected(table.rowAtPoint(begin),table.columnAtPoint(begin)))
-        {                                       
-          table.getSelectionModel().
-            removeSelectionInterval(table.rowAtPoint(begin), 
-                                    table.rowAtPoint(begin));
-          table.getColumnModel().getSelectionModel().
-            removeSelectionInterval(table.columnAtPoint(begin), 
-                                    table.columnAtPoint(begin));
+          // If we were editing, but the moved to another cell, stop editing
+          if (rowLead != rowModel.getLeadSelectionIndex() ||
+              colLead != colModel.getLeadSelectionIndex())
+            if (table.isEditing())
+              table.editingStopped(new ChangeEvent(e));
         }
-      else
-        updateSelection(e.isControlDown());
-
-      // If we were editing, but the moved to another cell, stop editing
-      if (rowLead != rowModel.getLeadSelectionIndex() ||
-          colLead != colModel.getLeadSelectionIndex())
-        if (table.isEditing())
-          table.editingStopped(new ChangeEvent(e));
     }
+
     public void mouseReleased(MouseEvent e) 
     {
-      begin = null;
-      curr = null;
+      if (table.isEnabled())
+        {
+          begin = null;
+          curr = null;
+        }
+    }
+  }
+
+  /**
+   * Listens for changes to the model property of the JTable and adjusts some
+   * settings.
+   *
+   * @author Roman Kennke ([EMAIL PROTECTED])
+   */
+  private class PropertyChangeHandler implements PropertyChangeListener
+  {
+    /**
+     * Receives notification if one of the JTable's properties changes.
+     *
+     * @param ev the property change event
+     */
+    public void propertyChange(PropertyChangeEvent ev)
+    {
+      String propName = ev.getPropertyName();
+      if (propName.equals("model"))
+        {
+          ListSelectionModel rowSel = table.getSelectionModel();
+          rowSel.clearSelection();
+          ListSelectionModel colSel = table.getColumnModel().getSelectionModel();
+          colSel.clearSelection();
+          TableModel model = table.getModel();
+
+          // Adjust lead and anchor selection indices of the row and column
+          // selection models.
+          if (model.getRowCount() > 0)
+            {
+              rowSel.setAnchorSelectionIndex(0);
+              rowSel.setLeadSelectionIndex(0);
+            }
+          else
+            {
+              rowSel.setAnchorSelectionIndex(-1);
+              rowSel.setLeadSelectionIndex(-1);
+            }
+          if (model.getColumnCount() > 0)
+            {
+              colSel.setAnchorSelectionIndex(0);
+              colSel.setLeadSelectionIndex(0);
+            }
+          else
+            {
+              colSel.setAnchorSelectionIndex(-1);
+              colSel.setLeadSelectionIndex(-1);
+            }
+        }
     }
   }
 
@@ -329,9 +392,6 @@
     table.setSelectionForeground(UIManager.getColor("Table.selectionForeground"));
     table.setSelectionBackground(UIManager.getColor("Table.selectionBackground"));
     table.setOpaque(true);
-
-    highlightCellBorder = UIManager.getBorder("Table.focusCellHighlightBorder");
-    cellBorder = BorderFactory.createEmptyBorder(1, 1, 1, 1);
     rendererPane = new CellRendererPane();
   }
 
@@ -1061,6 +1121,9 @@
       mouseInputListener = createMouseInputListener();
     table.addMouseListener(mouseInputListener);    
     table.addMouseMotionListener(mouseInputListener);
+    if (propertyChangeListener == null)
+      propertyChangeListener = new PropertyChangeHandler();
+    table.addPropertyChangeListener(propertyChangeListener);
   }
 
   protected void uninstallDefaults() 
@@ -1094,6 +1157,8 @@
     table.removeKeyListener(keyListener);
     table.removeMouseListener(mouseInputListener);    
     table.removeMouseMotionListener(mouseInputListener);
+    table.removePropertyChangeListener(propertyChangeListener);
+    propertyChangeListener = null;
   }
 
   public void installUI(JComponent comp) 
@@ -1129,20 +1194,27 @@
                  TableCellRenderer rend, TableModel data,
                  int rowLead, int colLead)
   {
-    boolean isSel = table.isCellSelected(row, col);
-    boolean hasFocus = (table.getSelectionModel().getLeadSelectionIndex() == row) && table.hasFocus();
+    boolean rowSelAllowed = table.getRowSelectionAllowed();
+    boolean colSelAllowed = table.getColumnSelectionAllowed();
+    boolean isSel = false;
+    if (rowSelAllowed && colSelAllowed || !rowSelAllowed && !colSelAllowed)
+      isSel = table.isCellSelected(row, col);
+    else
+      isSel = table.isRowSelected(row) && table.getRowSelectionAllowed()
+           || table.isColumnSelected(col) && table.getColumnSelectionAllowed();
+
+    // Determine the focused cell. The focused cell is the cell at the
+    // leadSelectionIndices of the row and column selection model.
+    ListSelectionModel rowSel = table.getSelectionModel();
+    ListSelectionModel colSel = table.getColumnModel().getSelectionModel();
+    boolean hasFocus = table.hasFocus() && table.isEnabled()
+                       && rowSel.getLeadSelectionIndex() == row
+                       && colSel.getLeadSelectionIndex() == col;
+
     Component comp = rend.getTableCellRendererComponent(table,
                                                        data.getValueAt(row, col),
                                                        isSel, hasFocus, row, col);
     
-    // If the cell is the lead selection then highlight its border
-    if (table.getSelectionModel().getLeadSelectionIndex() == row
-        && table.getColumnModel().getSelectionModel().
-        getLeadSelectionIndex() == col)
-      ((JComponent) comp).setBorder(highlightCellBorder);
-    else
-      ((JComponent) comp).setBorder(cellBorder);   
-      
     rendererPane.paintComponent(g, comp, table, bounds);
     
     // FIXME: this is manual painting of the Caret, why doesn't the 
@@ -1183,23 +1255,22 @@
         y = y0;
         TableColumn col = cols.getColumn(c);
         int width = col.getWidth();
-
+        int halfGapWidth = gap.width / 2;
+        int halfGapHeight = gap.height / 2;
         for (int r = 0; r < nrows && y < ymax; ++r)
           {
-            Rectangle bounds = new Rectangle(x, y, width, height);
-              if (bounds.intersects(clip))
+            Rectangle bounds = new Rectangle(x + halfGapWidth,
+                                             y + halfGapHeight + 1,
+                                             width - gap.width + 1,
+                                             height - gap.height);
+            if (bounds.intersects(clip))
               {                                                     
-                paintCell(
-                          gfx,
-                          r,
-                          c,
-                          bounds,
-                          table.getCellRenderer(r, c),
+                paintCell(gfx, r, c, bounds, table.getCellRenderer(r, c),
                           table.getModel(),
                           table.getSelectionModel().getLeadSelectionIndex(),
                           table.getColumnModel().getSelectionModel().getLeadSelectionIndex());
               }
-              y += height;
+            y += height;
           }
         x += width;
       }
@@ -1210,7 +1281,7 @@
 
     Color grid = table.getGridColor();    
 
-    // paint vertical grid lines    
+    // paint vertical grid lines
     if (grid != null && table.getShowVerticalLines())
       {    
         x = x0;
@@ -1220,7 +1291,7 @@
         for (int c = 0; c < ncols && x < xmax; ++c)
           {
             x += cols.getColumn(c).getWidth();
-            gfx.drawLine(x - gap.width, y0, x - gap.width, ymax);
+            gfx.drawLine(x, y0, x, ymax);
             paintedLine = true;
           }
         gfx.setColor(save);
_______________________________________________
Classpath-patches mailing list
[email protected]
http://lists.gnu.org/mailman/listinfo/classpath-patches

Reply via email to