This fixes a small layout issue with wide tab labels, and removes some cruft that was pointed out by Eclipse.

2006-08-24  Roman Kennke  <[EMAIL PROTECTED]>

        * javax/swing/plaf/basic/BasicTabbedPaneUI.java
        (TabbedPaneLayout.normalizeTabRuns): Removed unused statement.
        (TabbedPaneScrollLayout.layoutContainer): Likewise.
        (ScrollingPane.updateUI): Likewise.
        (calculateTabWidth): Rewritten to correctly and efficiently
        layout the tab width.
        (layoutLabel): Call SwingUtilities method with the tabPane
        as argument.
        (paintContentBorderLeftEdge): Removed unused statement.
        (paintContentBorderRightEdge): Removed unused statement.
        (paintContentBorder): Removed unused statement.

/Roman
Index: javax/swing/plaf/basic/BasicTabbedPaneUI.java
===================================================================
RCS file: /cvsroot/classpath/classpath/javax/swing/plaf/basic/BasicTabbedPaneUI.java,v
retrieving revision 1.54
diff -u -1 -2 -r1.54 BasicTabbedPaneUI.java
--- javax/swing/plaf/basic/BasicTabbedPaneUI.java	18 Aug 2006 11:17:58 -0000	1.54
+++ javax/swing/plaf/basic/BasicTabbedPaneUI.java	24 Aug 2006 17:28:26 -0000
@@ -948,25 +948,24 @@
     /**
      * This method tries to "even out" the number of tabs in each run based on
      * their widths.
      *
      * @param tabPlacement The JTabbedPane's tab placement.
      * @param tabCount The number of tabs.
      * @param start The x position where the tabs will begin.
      * @param max The maximum x position where the tab can run to.
      */
     protected void normalizeTabRuns(int tabPlacement, int tabCount, int start,
                                     int max)
     {
-      Insets tabAreaInsets = getTabAreaInsets(tabPlacement);
       if (tabPlacement == SwingUtilities.TOP
           || tabPlacement == SwingUtilities.BOTTOM)
         {
           // We should only do this for runCount - 1, cause we can
           // only shift that many times between runs.
           for (int i = 1; i < runCount; i++)
             {
               Rectangle currRun = rects[lastTabInRun(tabCount, i)];
               Rectangle nextRun = rects[lastTabInRun(tabCount,
                                                      getNextTabRun(i))];
               int spaceInCurr = currRun.x + currRun.width;
               int spaceInNext = nextRun.x + nextRun.width;
@@ -1435,25 +1434,24 @@
 
     /**
      * This method is called when the JTabbedPane is laid out in
      * SCROLL_TAB_LAYOUT. It finds the position for all components in the
      * JTabbedPane.
      *
      * @param pane The JTabbedPane to be laid out.
      */
     public void layoutContainer(Container pane)
     {
       super.layoutContainer(pane);
       int tabCount = tabPane.getTabCount();
-      Point p = null;
       if (tabCount == 0)
         return;
       int tabPlacement = tabPane.getTabPlacement();
       
       if (tabPlacement == SwingConstants.TOP
           || tabPlacement == SwingConstants.BOTTOM)
         {
           if (tabAreaRect.x + tabAreaRect.width < rects[tabCount - 1].x
               + rects[tabCount - 1].width)
             {
               Dimension incrDims = incrButton.getPreferredSize();
               Dimension decrDims = decrButton.getPreferredSize();
@@ -1622,25 +1620,25 @@
                      tabAreaRect.width, tabAreaRect.height);
     
         paintTabArea(g, placement, tabPane.getSelectedIndex());
       }
     }
 
     /**
      * This method overrides the updateUI method. It makes the default UI for
      * this ScrollingPanel to be  a ScrollingPanelUI.
      */
     public void updateUI()
     {
-      setUI((PanelUI) new ScrollingPanelUI());
+      setUI(new ScrollingPanelUI());
     }
   }
 
   /**
    * This is a helper class that paints the panel that paints tabs. This
    * custom JViewport is used so that the tabs painted in the panel will be
    * clipped. This class implements UIResource so tabs are not added when
    * this objects of this class are added to the  JTabbedPane.
    */
   private class ScrollingViewport extends JViewport implements UIResource
   {
     // TODO: Maybe remove this inner class.
@@ -2492,25 +2490,25 @@
                              Rectangle textRect, boolean isSelected)
   {
     // Reset the icon and text rectangles, as the result is not specified
     // when the locations are not (0,0).
     textRect.x = 0;
     textRect.y = 0;
     textRect.width = 0;
     textRect.height = 0;
     iconRect.x = 0;
     iconRect.y = 0;
     iconRect.width = 0;
     iconRect.height = 0;
-    SwingUtilities.layoutCompoundLabel(metrics, title, icon,
+    SwingUtilities.layoutCompoundLabel(tabPane, metrics, title, icon,
                                        SwingConstants.CENTER,
                                        SwingConstants.CENTER,
                                        SwingConstants.CENTER,
                                        SwingConstants.RIGHT, tabRect,
                                        iconRect, textRect, textIconGap);
 
     int shiftX = getTabLabelShiftX(tabPlacement, tabIndex, isSelected);
     int shiftY = getTabLabelShiftY(tabPlacement, tabIndex, isSelected);
 
     iconRect.x += shiftX;
     iconRect.y += shiftY;
 
@@ -2887,25 +2885,24 @@
    * This method paints the border around the content area.
    *
    * @param g The Graphics object to paint with.
    * @param tabPlacement The JTabbedPane's tab placement.
    * @param selectedIndex The index of the selected tab.
    */
   protected void paintContentBorder(Graphics g, int tabPlacement,
                                     int selectedIndex)
   {
     int width = tabPane.getWidth();
     int height = tabPane.getHeight();
     Insets insets = tabPane.getInsets();
-    Insets tabAreaInsets = getTabAreaInsets(tabPlacement);
 
     // Calculate coordinates of content area.
     int x = insets.left;
     int y = insets.top;
     int w = width - insets.left - insets.right;
     int h = height - insets.top - insets.bottom;
 
     switch (tabPlacement)
     {
     case LEFT:
       x += calculateTabAreaWidth(tabPlacement, runCount, maxTabWidth);
       w -= x - insets.left;
@@ -2992,26 +2989,24 @@
    */
   protected void paintContentBorderLeftEdge(Graphics g, int tabPlacement,
                                             int selectedIndex, int x, int y,
                                             int w, int h)
   {
     Color saved = g.getColor();
     g.setColor(lightHighlight);
 
     int startgap = rects[selectedIndex].y - currentScrollOffset;
     int endgap = rects[selectedIndex].y + rects[selectedIndex].height
                  - currentScrollOffset;
 
-    int diff = 0;
-
     if (tabPlacement == SwingConstants.LEFT && startgap >= 0)
       {
         g.drawLine(x, y, x, startgap);
         g.drawLine(x, endgap, x, y + h - 1);
         
         g.setColor(selectedColor);
         g.drawLine(x, startgap, x, endgap - 1);
       }
     else
       g.drawLine(x, y, x, y + h - 1);
     
     g.setColor(selectedColor);
@@ -3080,26 +3075,24 @@
    * @param w The width of the content area.
    * @param h The height of the content area.
    */
   protected void paintContentBorderRightEdge(Graphics g, int tabPlacement,
                                              int selectedIndex, int x, int y,
                                              int w, int h)
   {
     Color saved = g.getColor();
     int startgap = rects[selectedIndex].y - currentScrollOffset;
     int endgap = rects[selectedIndex].y + rects[selectedIndex].height
                  - currentScrollOffset;
 
-    int diff = 0;
-
     if (tabPlacement == SwingConstants.RIGHT && startgap >= 0)
       {
         g.setColor(shadow);
         g.drawLine(x + w - 2, y + 1, x + w - 2, startgap);
         g.drawLine(x + w - 2, endgap, x + w - 2, y + h - 2);
 
         g.setColor(darkShadow);
         g.drawLine(x + w - 1, y, x + w - 1, startgap);
         g.drawLine(x + w - 1, endgap, x + w - 1, y + h - 2);
         
         g.setColor(selectedColor);
         g.drawLine(x + w - 2, startgap, x + w - 2, endgap - 1);
@@ -3475,39 +3468,38 @@
    * @param tabPlacement The JTabbedPane's tab placement.
    * @param tabIndex The tab index to calculate for.
    * @param metrics The font's metrics.
    *
    * @return The tab width for the given index.
    */
   protected int calculateTabWidth(int tabPlacement, int tabIndex,
                                   FontMetrics metrics)
   {
     Icon icon = getIconForTab(tabIndex);
     Insets insets = getTabInsets(tabPlacement, tabIndex);
 
-    int width = 0;
+    int width = insets.bottom + insets.right + 3;
     if (icon != null)
       {
-        Rectangle vr = new Rectangle();
-        Rectangle ir = new Rectangle();
-        Rectangle tr = new Rectangle();
-        layoutLabel(tabPlacement, getFontMetrics(), tabIndex,
-                    tabPane.getTitleAt(tabIndex), icon, vr, ir, tr,
-                    tabIndex == tabPane.getSelectedIndex());
-        width = tr.union(ir).width;
+        width += icon.getIconWidth() + textIconGap;
       }
-    else
-      width = metrics.stringWidth(tabPane.getTitleAt(tabIndex));
 
-    width += insets.left + insets.right;
+    View v = getTextViewForTab(tabIndex);
+    if (v != null)
+      width += v.getPreferredSpan(View.X_AXIS);
+    else
+      {
+        String label = tabPane.getTitleAt(tabIndex);
+        width += metrics.stringWidth(label);
+      }
     return width;
   }
 
   /**
    * This method calculates the max tab width.
    *
    * @param tabPlacement The JTabbedPane's tab placement.
    *
    * @return The maximum tab width.
    */
   protected int calculateMaxTabWidth(int tabPlacement)
   {

Reply via email to