metaltabbedpaneui.java

来自「java jdk 1.4的源码」· Java 代码 · 共 978 行 · 第 1/3 页

JAVA
978
字号
/* * @(#)MetalTabbedPaneUI.java	1.31 03/01/23 * * Copyright 2003 Sun Microsystems, Inc. All rights reserved. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. */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}. * * @version 1.19 08/28/98 * @author Tom Santos */public class MetalTabbedPaneUI extends BasicTabbedPaneUI {    protected int minTabWidth = 40;    protected Color tabAreaBackground;    protected Color selectColor;    protected Color selectHighlight;    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");    }    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 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        //        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 );	    }	    // Paint left	    if ( tabIndex != tabRuns[ runCount - 1 ] ) {	        // not the first tab in the last run	        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 right	    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 left	    if ( tabIndex==lastIndex ) {	        // last tab in run	        g.drawLine( 0, 1, 0, bottom );	    }	}        //        // Paint Highlight        //        g.setColor( isSelected ? selectHighlight : highlight );	if ( leftToRight ) {	    // Paint slant	    g.drawLine( 1, 6, 6, 1 );	    // Paint top	    g.drawLine( 6, 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 ( 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 : tabPane.getBackgroundAt( i );                }            }            else {	        if ( tabLeft + shadowWidth < x && tabRight >= x ) {                    return selectedIndex == i ? selectColor : tabPane.getBackgroundAt( 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 ) {            g.setColor( tabPane.getSelectedIndex() == tabIndex - 1 ?                        selectColor :                        tabPane.getBackgroundAt( tabIndex - 1 ) );            g.fillRect( 2, 0, 4, 3 );            g.drawLine( 2, 3, 2, 3 );        }        //        // Paint Highlight        //        g.setColor( isSelected ? selectHighlight : highlight );        // Paint slant        g.drawLine( 1, 6, 6, 1 );        // Paint top        g.drawLine( 6, 1, right, 1 );        // Paint left        g.drawLine( 1, 6, 1, bottom );        if ( tabIndex != firstIndex ) {            g.setColor( tabPane.getSelectedIndex() == tabIndex - 1 ?                        selectHighlight :                        highlight );            g.drawLine( 1, 0, 1, 4 );        }

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?