jdk/src/share/classes/sun/swing/SwingUtilities2.java
changeset 5590 3ec077d7e893
parent 4278 b7a976422d27
child 5597 ab490f66d2cf
--- a/jdk/src/share/classes/sun/swing/SwingUtilities2.java	Tue May 25 20:39:52 2010 +0400
+++ b/jdk/src/share/classes/sun/swing/SwingUtilities2.java	Tue May 25 20:54:59 2010 +0400
@@ -44,6 +44,8 @@
 import javax.swing.text.DefaultHighlighter;
 import javax.swing.text.DefaultCaret;
 import javax.swing.table.TableCellRenderer;
+import javax.swing.table.TableColumnModel;
+
 import sun.swing.PrintColorUIResource;
 import sun.swing.ImageIconUIResource;
 import sun.print.ProxyPrintGraphics;
@@ -1807,4 +1809,54 @@
                                          boolean three) {
         return liesIn(rect, p, false, false, three);
     }
+
+    /**
+     * Maps the index of the column in the view at
+     * {@code viewColumnIndex} to the index of the column
+     * in the table model.  Returns the index of the corresponding
+     * column in the model.  If {@code viewColumnIndex}
+     * is less than zero, returns {@code viewColumnIndex}.
+     *
+     * @param cm the table model
+     * @param   viewColumnIndex     the index of the column in the view
+     * @return  the index of the corresponding column in the model
+     *
+     * @see JTable#convertColumnIndexToModel(int)
+     * @see javax.swing.plaf.basic.BasicTableHeaderUI
+     */
+    public static int convertColumnIndexToModel(TableColumnModel cm,
+                                                int viewColumnIndex) {
+        if (viewColumnIndex < 0) {
+            return viewColumnIndex;
+        }
+        return cm.getColumn(viewColumnIndex).getModelIndex();
+    }
+
+    /**
+     * Maps the index of the column in the {@code cm} at
+     * {@code modelColumnIndex} to the index of the column
+     * in the view.  Returns the index of the
+     * corresponding column in the view; returns {@code -1} if this column
+     * is not being displayed. If {@code modelColumnIndex} is less than zero,
+     * returns {@code modelColumnIndex}.
+     *
+     * @param cm the table model
+     * @param modelColumnIndex the index of the column in the model
+     * @return the index of the corresponding column in the view
+     *
+     * @see JTable#convertColumnIndexToView(int)
+     * @see javax.swing.plaf.basic.BasicTableHeaderUI
+     */
+    public static int convertColumnIndexToView(TableColumnModel cm,
+                                        int modelColumnIndex) {
+        if (modelColumnIndex < 0) {
+            return modelColumnIndex;
+        }
+        for (int column = 0; column < cm.getColumnCount(); column++) {
+            if (cm.getColumn(column).getModelIndex() == modelColumnIndex) {
+                return column;
+            }
+        }
+        return -1;
+    }
 }