JTable header contains methods to disable column resizing and reordering when unwanted. This patch updates mouse handler, taking these flags into consideration.

2006-02-17  Audrius Meskauskas  <[EMAIL PROTECTED]>

   * javax/swing/plaf/basic/BasicTableHeaderUI.java (MouseInputHandler):
   Rewritten.
   * javax/swing/table/JTableHeader.java: Documenting related methods.

Index: javax/swing/plaf/basic/BasicTableHeaderUI.java
===================================================================
RCS file: /sources/classpath/classpath/javax/swing/plaf/basic/BasicTableHeaderUI.java,v
retrieving revision 1.17
diff -u -r1.17 BasicTableHeaderUI.java
--- javax/swing/plaf/basic/BasicTableHeaderUI.java	16 Feb 2006 23:22:53 -0000	1.17
+++ javax/swing/plaf/basic/BasicTableHeaderUI.java	17 Feb 2006 10:04:52 -0000
@@ -152,7 +152,7 @@
     public void mouseDragged(MouseEvent e)
     {
       TableColumn resizeIt = header.getResizingColumn();
-      if (resizeIt != null)
+      if (resizeIt != null && header.getResizingAllowed())
         {
           // The timer is intialised on demand.
           if (timer == null)
@@ -172,7 +172,7 @@
           resizeIt.setPreferredWidth(prevPrefWidth + e.getX() - draggingFrom);
           timer.restart();
         }
-      else if (draggingHeaderRect != null)
+      else if (draggingHeaderRect != null && header.getReorderingAllowed())
         {
           draggingHeaderRect.x = e.getX() + draggingFrom;
           header.repaint();
@@ -192,9 +192,9 @@
      */
     public void mouseExited(MouseEvent e)
     {
-      if (header.getResizingColumn() != null)
+      if (header.getResizingColumn() != null && header.getResizingAllowed())
         endResizing();
-      if (header.getDraggedColumn() != null)
+      if (header.getDraggedColumn() != null && header.getReorderingAllowed())
         endDragging(null);
     }
 
@@ -204,58 +204,60 @@
     public void mouseMoved(MouseEvent e)
     {
       // When dragging, the functionality is handled by the mouseDragged.
-      if (e.getButton() != 0)
-        return;
-
-      TableColumnModel model = header.getColumnModel();
-      int n = model.getColumnCount();
-      if (n < 2)
-        // It must be at least two columns to have at least one boundary.
-        // Otherwise, nothing to do.
-        return;
-
-      boolean onBoundary = false;
+      if (e.getButton() == 0 && header.getResizingAllowed())
+        {
+          TableColumnModel model = header.getColumnModel();
+          int n = model.getColumnCount();
+          if (n < 2)
+            // It must be at least two columns to have at least one boundary.
+            // Otherwise, nothing to do.
+            return;
 
-      int x = e.getX();
-      int a = x - COLUMN_BOUNDARY_TOLERANCE;
-      int b = x + COLUMN_BOUNDARY_TOLERANCE;
+          boolean onBoundary = false;
 
-      int p = 0;
+          int x = e.getX();
+          int a = x - COLUMN_BOUNDARY_TOLERANCE;
+          int b = x + COLUMN_BOUNDARY_TOLERANCE;
 
-      Scan: for (int i = 0; i < n - 1; i++)
-        {
-          p += model.getColumn(i).getWidth();
+          int p = 0;
 
-          if (p >= a && p <= b)
+          Scan: for (int i = 0; i < n - 1; i++)
             {
-              TableColumn column = model.getColumn(i);
-              onBoundary = true;
+              p += model.getColumn(i).getWidth();
 
-              draggingFrom = x;
-              prevPrefWidth = column.getWidth();
-              header.setResizingColumn(column);
-              break Scan;
+              if (p >= a && p <= b)
+                {
+                  TableColumn column = model.getColumn(i);
+                  onBoundary = true;
+
+                  draggingFrom = x;
+                  prevPrefWidth = column.getWidth();
+                  header.setResizingColumn(column);
+                  break Scan;
+                }
             }
-        }
 
-      if (onBoundary != showingResizeCursor)
-        {
-          // Change the cursor shape, if needed.
-          if (onBoundary)
+          if (onBoundary != showingResizeCursor)
             {
+              // Change the cursor shape, if needed.
+              if (onBoundary)
+                {
 
-              if (p < x)
-                header.setCursor(Cursor.getPredefinedCursor(Cursor.W_RESIZE_CURSOR));
+                  if (p < x)
+                    header.setCursor(Cursor.getPredefinedCursor
+                                     (Cursor.W_RESIZE_CURSOR));
+                  else
+                    header.setCursor(Cursor.getPredefinedCursor
+                                     (Cursor.E_RESIZE_CURSOR));
+                }
               else
-                header.setCursor(Cursor.getPredefinedCursor(Cursor.E_RESIZE_CURSOR));
-            }
-          else
-            {
-              header.setCursor(Cursor.getDefaultCursor());
-              header.setResizingColumn(null);
-            }
+                {
+                  header.setCursor(Cursor.getDefaultCursor());
+                  header.setResizingColumn(null);
+                }
 
-          showingResizeCursor = onBoundary;
+              showingResizeCursor = onBoundary;
+            }
         }
     }
 
@@ -264,14 +266,20 @@
      */
     public void mousePressed(MouseEvent e)
     {
-      TableColumn resizingColumn = header.getResizingColumn();
-      if (resizingColumn != null)
-        resizingColumn.setPreferredWidth(resizingColumn.getWidth());
-      else
+      if (header.getResizingAllowed())
+        {
+          TableColumn resizingColumn = header.getResizingColumn();
+          if (resizingColumn != null)
+            {
+              resizingColumn.setPreferredWidth(resizingColumn.getWidth());
+              return;
+            }
+        }
+
+      if (header.getReorderingAllowed())
         {
-          // If not resizing, then dragging.
           TableColumnModel model = header.getColumnModel();
-          int n = model.getColumnCount(); 
+          int n = model.getColumnCount();
           if (n < 2)
             // It must be at least two columns to change the column location.
             return;
@@ -309,9 +317,9 @@
      */
     public void mouseReleased(MouseEvent e)
     {
-      if (header.getResizingColumn() != null)
+      if (header.getResizingColumn() != null && header.getResizingAllowed())
         endResizing();
-      if (header.getDraggedColumn() != null)
+      if (header.getDraggedColumn() != null &&  header.getReorderingAllowed())
         endDragging(e);
     }
 
Index: javax/swing/table/JTableHeader.java
===================================================================
RCS file: /sources/classpath/classpath/javax/swing/table/JTableHeader.java,v
retrieving revision 1.14
diff -u -r1.14 JTableHeader.java
--- javax/swing/table/JTableHeader.java	28 Oct 2005 14:50:32 -0000	1.14
+++ javax/swing/table/JTableHeader.java	17 Feb 2006 10:04:54 -0000
@@ -67,6 +67,11 @@
 import javax.swing.event.TableColumnModelListener;
 import javax.swing.plaf.TableHeaderUI;
 
+/**
+ * Represents the table header. The header displays the column header values,
+ * is always visible event if the rest of the table scrolls up and down and
+ * supports column reordering and resizing with mouse.
+ */
 public class JTableHeader extends JComponent
   implements TableColumnModelListener, Accessible
 {
@@ -306,7 +311,10 @@
       }
     };
   }
-
+  
+  /**
+   * Use serialVersionUid for interoperability.
+   */
   private static final long serialVersionUID = 5144633983372967710L;
 
   /**
@@ -409,9 +417,10 @@
   }
 
   /**
-   * Get the value of the [EMAIL PROTECTED] #draggedColumn} property.
+   * Get the column that is currently being dragged. This is used when
+   * handling the column reordering with mouse.
    *
-   * @return The current value of the property
+   * @return the column being dragged, null if none.
    */
   public TableColumn getDraggedColumn()
   {
@@ -429,29 +438,34 @@
   }
 
   /**
-   * Get the value of the [EMAIL PROTECTED] #reorderingAllowed} property.
+   * Check if it is possible to reorder the table columns by dragging column
+   * header with mouse. The table reordering is enabled by default, but can be
+   * disabled with [EMAIL PROTECTED] #setReorderingAllowed(boolean)}.
    *
-   * @return The current value of the property
-   */
+   * @return true if reordering is allowed, false otherwise.
+   */ 
   public boolean getReorderingAllowed()
   {
     return reorderingAllowed;
   }
 
   /**
-   * Get the value of the [EMAIL PROTECTED] #resizingAllowed} property.
+   * Check if it is possible to resize the table columns by dragging the column
+   * boundary in the table header with mouse. The resizing is enabled
+   * by default, but can be disabled with [EMAIL PROTECTED] #setResizingAllowed(boolean)}.
    *
-   * @return The current value of the property
-   */
+   * @return true if resizing is allowed, false otherwise.
+   */ 
   public boolean getResizingAllowed()
   {
     return resizingAllowed;
   }
 
   /**
-   * Get the value of the [EMAIL PROTECTED] #resizingColumn} property.
+   * Get the column that is currently being resized. This is used when
+   * handling the column resizing with mouse.
    *
-   * @return The current value of the property
+   * @return the column being currently resized, null if none.
    */
   public TableColumn getResizingColumn()
   {
@@ -459,9 +473,9 @@
   }
 
   /**
-   * Get the value of the [EMAIL PROTECTED] #table} property.
+   * Get the table, having this header.
    *
-   * @return The current value of the property
+   * @return the table, having this header.
    */
   public JTable getTable()
   {
@@ -501,13 +515,15 @@
   }
 
   /**
-   * Set the value of the [EMAIL PROTECTED] #draggedColumn} property.
+   * Set the column that is currently being dragged. This is used when
+   * dragging the column with mouse. Setting to null will stop the 
+   * dragging session immediately.
    *
-   * @param d The new value of the property
+   * @param draggingIt the column being currently dragged, null if none.
    */ 
-  public void setDraggedColumn(TableColumn d)
+  public void setDraggedColumn(TableColumn draggingIt)
   {
-    draggedColumn = d;
+    draggedColumn = draggingIt;
   }
 
   /**
@@ -531,33 +547,39 @@
   }
 
   /**
-   * Set the value of the [EMAIL PROTECTED] #reorderingAllowed} property.
+   * Set the table ability to reorder columns by dragging column header
+   * with mouse. The table reordering is enabled by default, but can be
+   * disabled with this method.
    *
-   * @param r The new value of the property
+   * @param allowed true if reordering is allowed, false otherwise.
    */ 
-  public void setReorderingAllowed(boolean r)
+  public void setReorderingAllowed(boolean allowed)
   {
-    reorderingAllowed = r;
+    reorderingAllowed = allowed;
   }
 
   /**
-   * Set the value of the [EMAIL PROTECTED] #resizingAllowed} property.
+   * Set the table ability to resize columns by dragging the column
+   * boundary in the table header with mouse. The resizing is enabled
+   * by default, but can be disabled using this method.
    *
-   * @param r The new value of the property
+   * @param allowed true if resizing is allowed, false otherwise.
    */ 
-  public void setResizingAllowed(boolean r)
+  public void setResizingAllowed(boolean allowed)
   {
-    resizingAllowed = r;
+    resizingAllowed = allowed;
   }
 
   /**
-   * Set the value of the [EMAIL PROTECTED] #resizingColumn} property.
+   * The the column that is currently being resized. This property is used
+   * when handling table resizing with mouse. Setting to null would stop
+   * the resizing session immediately.
    *
-   * @param r The new value of the property
+   * @param resizingIt the column being currently resized
    */ 
-  public void setResizingColumn(TableColumn r)
+  public void setResizingColumn(TableColumn resizingIt)
   {
-    resizingColumn = r;
+    resizingColumn = resizingIt;
   }
 
   /**
@@ -609,7 +631,14 @@
   {
     this.cellRenderer = cellRenderer;
   }
-
+  
+  /**
+   * Get the rectangle, occupied by the header of the given column.
+   * 
+   * @param column the column, for that the header area is requested.
+   * 
+   * @return the column header area.
+   */
   public Rectangle getHeaderRect(int column)
   {
     Rectangle r = getTable().getCellRect(-1, column, false);

Reply via email to