1229 lines · 42.7 KB · Java
RawDownload
/* * Copyright (c) 1998, 2006, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation.  Oracle designates this * particular file as subject to the "Classpath" exception as provided * by Oracle in the LICENSE file that accompanied this code. * * This code 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 General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ package javax.swing.plaf.metal; import javax.swing.*;import javax.swing.event.*;import java.awt.*;import java.awt.event.*;import javax.swing.plaf.*;import java.io.Serializable;import javax.swing.plaf.basic.BasicTabbedPaneUI; /** * The Metal subclass of BasicTabbedPaneUI. * <p> * <strong>Warning:</strong> * Serialized objects of this class will not be compatible with * future Swing releases. The current serialization support is * appropriate for short term storage or RMI between applications running * the same version of Swing.  As of 1.4, support for long term storage * of all JavaBeans<sup><font size="-2">TM</font></sup> * has been added to the <code>java.beans</code> package. * Please see {@link java.beans.XMLEncoder}. * * @author Tom Santos */ public class MetalTabbedPaneUI extends BasicTabbedPaneUI {     protected int minTabWidth = 40;    // Background color for unselected tabs that don't have an explicitly    // set color.    private Color unselectedBackground;    protected Color tabAreaBackground;    protected Color selectColor;    protected Color selectHighlight;    private boolean tabsOpaque = true;     // Whether or not we're using ocean. This is cached as it is used    // extensively during painting.    private boolean ocean;    // Selected border color for ocean.    private Color oceanSelectedBorderColor;     public static ComponentUI createUI( JComponent x ) {        return new MetalTabbedPaneUI();    }     protected LayoutManager createLayoutManager() {        if (tabPane.getTabLayoutPolicy() == JTabbedPane.SCROLL_TAB_LAYOUT) {            return super.createLayoutManager();        }        return new TabbedPaneLayout();    }     protected void installDefaults() {        super.installDefaults();         tabAreaBackground = UIManager.getColor("TabbedPane.tabAreaBackground");        selectColor = UIManager.getColor("TabbedPane.selected");        selectHighlight = UIManager.getColor("TabbedPane.selectHighlight");        tabsOpaque = UIManager.getBoolean("TabbedPane.tabsOpaque");        unselectedBackground = UIManager.getColor(                                         "TabbedPane.unselectedBackground");        ocean = MetalLookAndFeel.usingOcean();        if (ocean) {            oceanSelectedBorderColor = UIManager.getColor(                         "TabbedPane.borderHightlightColor");        }    }      protected void paintTabBorder( Graphics g, int tabPlacement,                                   int tabIndex, int x, int y, int w, int h,                                   boolean isSelected) {        int bottom = y + (h-1);        int right = x + (w-1);         switch ( tabPlacement ) {        case LEFT:            paintLeftTabBorder(tabIndex, g, x, y, w, h, bottom, right, isSelected);            break;        case BOTTOM:            paintBottomTabBorder(tabIndex, g, x, y, w, h, bottom, right, isSelected);            break;        case RIGHT:            paintRightTabBorder(tabIndex, g, x, y, w, h, bottom, right, isSelected);            break;        case TOP:        default:            paintTopTabBorder(tabIndex, g, x, y, w, h, bottom, right, isSelected);        }    }      protected void paintTopTabBorder( int tabIndex, Graphics g,                                      int x, int y, int w, int h,                                      int btm, int rght,                                      boolean isSelected ) {        int currentRun = getRunForTab( tabPane.getTabCount(), tabIndex );        int lastIndex = lastTabInRun( tabPane.getTabCount(), currentRun );        int firstIndex = tabRuns[ currentRun ];        boolean leftToRight = MetalUtils.isLeftToRight(tabPane);        int selectedIndex = tabPane.getSelectedIndex();        int bottom = h - 1;        int right = w - 1;         //        // Paint Gap        //         if (shouldFillGap( currentRun, tabIndex, x, y ) ) {            g.translate( x, y );             if ( leftToRight ) {                g.setColor( getColorForGap( currentRun, x, y + 1 ) );                g.fillRect( 1, 0, 5, 3 );                g.fillRect( 1, 3, 2, 2 );            } else {                g.setColor( getColorForGap( currentRun, x + w - 1, y + 1 ) );                g.fillRect( right - 5, 0, 5, 3 );                g.fillRect( right - 2, 3, 2, 2 );            }             g.translate( -x, -y );        }         g.translate( x, y );         //        // Paint Border        //         if (ocean && isSelected) {            g.setColor(oceanSelectedBorderColor);        }        else {            g.setColor( darkShadow );        }         if ( leftToRight ) {             // Paint slant            g.drawLine( 1, 5, 6, 0 );             // Paint top            g.drawLine( 6, 0, right, 0 );             // Paint right            if ( tabIndex==lastIndex ) {                // last tab in run                g.drawLine( right, 1, right, bottom );            }             if (ocean && tabIndex - 1 == selectedIndex &&                                currentRun == getRunForTab(                                tabPane.getTabCount(), selectedIndex)) {                g.setColor(oceanSelectedBorderColor);            }             // Paint left            if ( tabIndex != tabRuns[ runCount - 1 ] ) {                // not the first tab in the last run                if (ocean && isSelected) {                    g.drawLine(0, 6, 0, bottom);                    g.setColor(darkShadow);                    g.drawLine(0, 0, 0, 5);                }                else {                    g.drawLine( 0, 0, 0, bottom );                }            } else {                // the first tab in the last run                g.drawLine( 0, 6, 0, bottom );            }        } else {             // Paint slant            g.drawLine( right - 1, 5, right - 6, 0 );             // Paint top            g.drawLine( right - 6, 0, 0, 0 );             // Paint left            if ( tabIndex==lastIndex ) {                // last tab in run                g.drawLine( 0, 1, 0, bottom );            }             // Paint right            if (ocean && tabIndex - 1 == selectedIndex &&                                currentRun == getRunForTab(                                tabPane.getTabCount(), selectedIndex)) {                g.setColor(oceanSelectedBorderColor);                g.drawLine(right, 0, right, bottom);            }            else if (ocean && isSelected) {                g.drawLine(right, 6, right, bottom);                if (tabIndex != 0) {                    g.setColor(darkShadow);                    g.drawLine(right, 0, right, 5);                }            }            else {                if ( tabIndex != tabRuns[ runCount - 1 ] ) {                    // not the first tab in the last run                    g.drawLine( right, 0, right, bottom );                } else {                    // the first tab in the last run                    g.drawLine( right, 6, right, bottom );                }            }        }         //        // Paint Highlight        //         g.setColor( isSelected ? selectHighlight : highlight );         if ( leftToRight ) {             // Paint slant            g.drawLine( 1, 6, 6, 1 );             // Paint top            g.drawLine( 6, 1, (tabIndex == lastIndex) ? right - 1 : right, 1 );             // Paint left            g.drawLine( 1, 6, 1, bottom );             // paint highlight in the gap on tab behind this one            // on the left end (where they all line up)            if ( tabIndex==firstIndex && tabIndex!=tabRuns[runCount - 1] ) {                //  first tab in run but not first tab in last run                if (tabPane.getSelectedIndex()==tabRuns[currentRun+1]) {                    // tab in front of selected tab                    g.setColor( selectHighlight );                }                else {                    // tab in front of normal tab                    g.setColor( highlight );                }                g.drawLine( 1, 0, 1, 4 );            }        } else {             // Paint slant            g.drawLine( right - 1, 6, right - 6, 1 );             // Paint top            g.drawLine( right - 6, 1, 1, 1 );             // Paint left            if ( tabIndex==lastIndex ) {                // last tab in run                g.drawLine( 1, 1, 1, bottom );            } else {                g.drawLine( 0, 1, 0, bottom );            }        }         g.translate( -x, -y );    }     protected boolean shouldFillGap( int currentRun, int tabIndex, int x, int y ) {        boolean result = false;         if (!tabsOpaque) {            return false;        }         if ( currentRun == runCount - 2 ) {  // If it's the second to last row.            Rectangle lastTabBounds = getTabBounds( tabPane, tabPane.getTabCount() - 1 );            Rectangle tabBounds = getTabBounds( tabPane, tabIndex );            if (MetalUtils.isLeftToRight(tabPane)) {                int lastTabRight = lastTabBounds.x + lastTabBounds.width - 1;                 // is the right edge of the last tab to the right                // of the left edge of the current tab?                if ( lastTabRight > tabBounds.x + 2 ) {                    return true;                }            } else {                int lastTabLeft = lastTabBounds.x;                int currentTabRight = tabBounds.x + tabBounds.width - 1;                 // is the left edge of the last tab to the left                // of the right edge of the current tab?                if ( lastTabLeft < currentTabRight - 2 ) {                    return true;                }            }        } else {            // fill in gap for all other rows except last row            result = currentRun != runCount - 1;        }         return result;    }     protected Color getColorForGap( int currentRun, int x, int y ) {        final int shadowWidth = 4;        int selectedIndex = tabPane.getSelectedIndex();        int startIndex = tabRuns[ currentRun + 1 ];        int endIndex = lastTabInRun( tabPane.getTabCount(), currentRun + 1 );        int tabOverGap = -1;        // Check each tab in the row that is 'on top' of this row        for ( int i = startIndex; i <= endIndex; ++i ) {            Rectangle tabBounds = getTabBounds( tabPane, i );            int tabLeft = tabBounds.x;            int tabRight = (tabBounds.x + tabBounds.width) - 1;            // Check to see if this tab is over the gap            if ( MetalUtils.isLeftToRight(tabPane) ) {                if ( tabLeft <= x && tabRight - shadowWidth > x ) {                    return selectedIndex == i ? selectColor : getUnselectedBackgroundAt( i );                }            }            else {                if ( tabLeft + shadowWidth < x && tabRight >= x ) {                    return selectedIndex == i ? selectColor : getUnselectedBackgroundAt( i );                }            }        }         return tabPane.getBackground();    }     protected void paintLeftTabBorder( int tabIndex, Graphics g,                                       int x, int y, int w, int h,                                       int btm, int rght,                                       boolean isSelected ) {        int tabCount = tabPane.getTabCount();        int currentRun = getRunForTab( tabCount, tabIndex );        int lastIndex = lastTabInRun( tabCount, currentRun );        int firstIndex = tabRuns[ currentRun ];         g.translate( x, y );         int bottom = h - 1;        int right = w - 1;         //        // Paint part of the tab above        //         if ( tabIndex != firstIndex && tabsOpaque ) {            g.setColor( tabPane.getSelectedIndex() == tabIndex - 1 ?                        selectColor :                        getUnselectedBackgroundAt( tabIndex - 1 ) );            g.fillRect( 2, 0, 4, 3 );            g.drawLine( 2, 3, 2, 3 );        }          //        // Paint Highlight        //         if (ocean) {            g.setColor(isSelected ? selectHighlight :                       MetalLookAndFeel.getWhite());        }        else {            g.setColor( isSelected ? selectHighlight : highlight );        }         // Paint slant        g.drawLine( 1, 6, 6, 1 );         // Paint left        g.drawLine( 1, 6, 1, bottom );         // Paint top        g.drawLine( 6, 1, right, 1 );         if ( tabIndex != firstIndex ) {            if (tabPane.getSelectedIndex() == tabIndex - 1) {                g.setColor(selectHighlight);            } else {                g.setColor(ocean ? MetalLookAndFeel.getWhite() : highlight);            }             g.drawLine( 1, 0, 1, 4 );        }         //        // Paint Border        //         if (ocean) {            if (isSelected) {                g.setColor(oceanSelectedBorderColor);            }            else {                g.setColor( darkShadow );            }        }        else {            g.setColor( darkShadow );        }         // Paint slant        g.drawLine( 1, 5, 6, 0 );         // Paint top        g.drawLine( 6, 0, right, 0 );         // Paint bottom        if ( tabIndex == lastIndex ) {            g.drawLine( 0, bottom, right, bottom );        }         // Paint left        if (ocean) {            if (tabPane.getSelectedIndex() == tabIndex - 1) {                g.drawLine(0, 5, 0, bottom);                g.setColor(oceanSelectedBorderColor);                g.drawLine(0, 0, 0, 5);            }            else if (isSelected) {                g.drawLine( 0, 6, 0, bottom );                if (tabIndex != 0) {                    g.setColor(darkShadow);                    g.drawLine(0, 0, 0, 5);                }            }            else if ( tabIndex != firstIndex ) {                g.drawLine( 0, 0, 0, bottom );            } else {                g.drawLine( 0, 6, 0, bottom );            }        }        else { // metal            if ( tabIndex != firstIndex ) {                g.drawLine( 0, 0, 0, bottom );            } else {                g.drawLine( 0, 6, 0, bottom );            }        }         g.translate( -x, -y );    }      protected void paintBottomTabBorder( int tabIndex, Graphics g,                                         int x, int y, int w, int h,                                         int btm, int rght,                                         boolean isSelected ) {        int tabCount = tabPane.getTabCount();        int currentRun = getRunForTab( tabCount, tabIndex );        int lastIndex = lastTabInRun( tabCount, currentRun );        int firstIndex = tabRuns[ currentRun ];        boolean leftToRight = MetalUtils.isLeftToRight(tabPane);         int bottom = h - 1;        int right = w - 1;         //        // Paint Gap        //         if ( shouldFillGap( currentRun, tabIndex, x, y ) ) {            g.translate( x, y );             if ( leftToRight ) {                g.setColor( getColorForGap( currentRun, x, y ) );                g.fillRect( 1, bottom - 4, 3, 5 );                g.fillRect( 4, bottom - 1, 2, 2 );            } else {                g.setColor( getColorForGap( currentRun, x + w - 1, y ) );                g.fillRect( right - 3, bottom - 3, 3, 4 );                g.fillRect( right - 5, bottom - 1, 2, 2 );                g.drawLine( right - 1, bottom - 4, right - 1, bottom - 4 );            }             g.translate( -x, -y );

Lines 1–500 of 1229.