synthtabbedpaneui.java

来自「java jdk 1.4的源码」· Java 代码 · 共 1,888 行 · 第 1/5 页

JAVA
1,888
字号
                                    FontMetrics metrics) {        Icon icon = getIconForTab(tabIndex);        Insets tabInsets = getTabInsets(tabPlacement, tabIndex);        int width = tabInsets.left + tabInsets.right + 3;        if (icon != null) {            // PENDING: This should call to SynthIcon for size.            width += icon.getIconWidth() + getTextIconGap();        }	View v = getTextViewForTab(tabIndex);	if (v != null) {	    // html	    width += (int)v.getPreferredSpan(View.X_AXIS);	} else {	    // plain text	    String title = tabPane.getTitleAt(tabIndex);            width += tabContext.getStyle().getSynthGraphics(tabContext).                        computeStringWidth(tabContext, metrics.getFont(),                        metrics, title);	}	        return width;    }    private int getTextIconGap() {        Integer gap = (Integer)tabContext.getStyle().get(tabContext,"textIconGap");        if (gap == null) {            return 0;        }        return gap.intValue();    }    protected int calculateMaxTabWidth(int tabPlacement) {        FontMetrics metrics = getFontMetrics(tabContext.getStyle().getFont(                                     tabContext));        int tabCount = tabPane.getTabCount();        int result = 0;         for(int i = 0; i < tabCount; i++) {            result = Math.max(calculateTabWidth(tabPlacement, i, metrics),                              result);        }        return result;     }    protected int calculateTabAreaHeight(int tabPlacement, int horizRunCount,                                         int maxTabHeight) {        Insets tabAreaInsets = getTabAreaInsets(tabPlacement);        int tabRunOverlay = getTabRunOverlay(tabPlacement);        return (horizRunCount > 0?                 horizRunCount * (maxTabHeight-tabRunOverlay) + tabRunOverlay +                 tabAreaInsets.top + tabAreaInsets.bottom :                 0);    }    protected int calculateTabAreaWidth(int tabPlacement, int vertRunCount,                                        int maxTabWidth) {         Insets tabAreaInsets = getTabAreaInsets(tabPlacement);        int tabRunOverlay = getTabRunOverlay(tabPlacement);        return (vertRunCount > 0?                 vertRunCount * (maxTabWidth-tabRunOverlay) + tabRunOverlay +                 tabAreaInsets.left + tabAreaInsets.right :                 0);    }    protected Insets getTabInsets(int tabPlacement, int tabIndex) {        tabContext.update(tabIndex, false, (mouseIndex == tabIndex),                                           (focusIndex == tabIndex));        return tabContext.getStyle().getInsets(tabContext, null);    }    protected Insets getSelectedTabPadInsets(int tabPlacement) {        Insets insets = (Insets)tabContext.getStyle().get(tabContext,                                                "selectedTabPadInsets");        if (insets == null) {            return new Insets(0, 0, 0, 0);        }        Insets result = new Insets(0, 0, 0, 0);        rotateInsets(insets, new Insets(0, 0, 0, 0), tabPlacement);        return result;    }    protected Insets getTabAreaInsets(int tabPlacement) {        return tabAreaContext.getStyle().getInsets(tabAreaContext, null);    }    protected Insets getContentBorderInsets(int tabPlacement) {        return tabContentContext.getStyle().getInsets(tabContentContext,                                                          null);    }        protected FontMetrics getFontMetrics() {        return getFontMetrics(tabContext.getStyle().getFont(tabContext));    }    protected FontMetrics getFontMetrics(Font font) {        return Toolkit.getDefaultToolkit().getFontMetrics(font);    }// Tab Navigation methods    protected void navigateSelectedTab(int direction) {        int tabPlacement = tabPane.getTabPlacement();        int current = selectionFollowsFocus? tabPane.getSelectedIndex() :	    focusIndex;        int tabCount = tabPane.getTabCount();        // If we have no tabs then don't navigate.        if (tabCount <= 0) {            return;        }        int offset;        switch(tabPlacement) {	  case NEXT:	      selectNextTab(current);	      break;	  case PREVIOUS:	      selectPreviousTab(current);	      break;          case LEFT:          case RIGHT:              switch(direction) {                case NORTH:                    selectPreviousTabInRun(current);                    break;                case SOUTH:                    selectNextTabInRun(current);                    break;                case WEST:                    offset = getTabRunOffset(tabPlacement, tabCount, current, false);                    selectAdjacentRunTab(tabPlacement, current, offset);                    break;                case EAST:                    offset = getTabRunOffset(tabPlacement, tabCount, current, true);                    selectAdjacentRunTab(tabPlacement, current, offset);                    break;                default:              }              break;          case BOTTOM:          case TOP:          default:              switch(direction) {                case NORTH:                    offset = getTabRunOffset(tabPlacement, tabCount, current, false);                    selectAdjacentRunTab(tabPlacement, current, offset);                    break;                case SOUTH:                    offset = getTabRunOffset(tabPlacement, tabCount, current, true);                    selectAdjacentRunTab(tabPlacement, current, offset);                    break;                case EAST:                    selectNextTabInRun(current);                    break;                case WEST:                    selectPreviousTabInRun(current);                    break;                default:              }        }    }    protected void selectNextTabInRun(int current) {        int tabCount = tabPane.getTabCount();        int tabIndex = getNextTabIndexInRun(tabCount, current);	while(tabIndex != current && !tabPane.isEnabledAt(tabIndex)) {	    tabIndex = getNextTabIndexInRun(tabCount, tabIndex);	}	navigateTo(tabIndex);    }    protected void selectPreviousTabInRun(int current) {        int tabCount = tabPane.getTabCount();        int tabIndex = getPreviousTabIndexInRun(tabCount, current);	while(tabIndex != current && !tabPane.isEnabledAt(tabIndex)) {	    tabIndex = getPreviousTabIndexInRun(tabCount, tabIndex);	}	navigateTo(tabIndex);    }    protected void selectNextTab(int current) {        int tabIndex = getNextTabIndex(current);                while (tabIndex != current && !tabPane.isEnabledAt(tabIndex)) {            tabIndex = getNextTabIndex(tabIndex);        }        navigateTo(tabIndex);    }    protected void selectPreviousTab(int current) {        int tabIndex = getPreviousTabIndex(current);                while (tabIndex != current && !tabPane.isEnabledAt(tabIndex)) {            tabIndex = getPreviousTabIndex(tabIndex);        }        navigateTo(tabIndex);    }    protected void selectAdjacentRunTab(int tabPlacement,                                         int tabIndex, int offset) {        if ( runCount < 2 ) {            return;         }        int newIndex;        Rectangle r = rects[tabIndex];         switch(tabPlacement) {          case LEFT:          case RIGHT:              newIndex = getTabAtLocation(r.x + r.width/2 + offset,                                       r.y + r.height/2);              break;          case BOTTOM:            case TOP:          default:              newIndex = getTabAtLocation(r.x + r.width/2,                                        r.y + r.height/2 + offset);        }        if (newIndex != -1) {            while (!tabPane.isEnabledAt(newIndex) && newIndex != tabIndex) {                newIndex = getNextTabIndex(newIndex);            }                    navigateTo(newIndex);        }    }    private void navigateTo(int index) {	if (selectionFollowsFocus) {            setFocusIndex(index);	    tabPane.setSelectedIndex(index);	} else {            // Just move focus (not selection)            int oldFocusIndex = focusIndex;	    setFocusIndex(index);	    tabPane.repaint(getTabBounds(tabPane, focusIndex));            if (oldFocusIndex != -1) {		tabPane.repaint(getTabBounds(tabPane, oldFocusIndex));	    }	}    }    void setFocusIndex(int index) {        focusIndex = index;    }    int getFocusIndex() {        return focusIndex;    }    protected int getTabRunOffset(int tabPlacement, int tabCount,                                   int tabIndex, boolean forward) {        int run = getRunForTab(tabCount, tabIndex);        int offset;        switch(tabPlacement) {          case LEFT: {              if (run == 0) {                  offset = (forward?                             -(calculateTabAreaWidth(tabPlacement, runCount, maxTabWidth)-maxTabWidth) :                            -maxTabWidth);              } else if (run == runCount - 1) {                  offset = (forward?                            maxTabWidth :                            calculateTabAreaWidth(tabPlacement, runCount, maxTabWidth)-maxTabWidth);              } else {                  offset = (forward? maxTabWidth : -maxTabWidth);              }              break;          }          case RIGHT: {              if (run == 0) {                  offset = (forward?                             maxTabWidth :                            calculateTabAreaWidth(tabPlacement, runCount, maxTabWidth)-maxTabWidth);              } else if (run == runCount - 1) {                  offset = (forward?                            -(calculateTabAreaWidth(tabPlacement, runCount, maxTabWidth)-maxTabWidth) :                            -maxTabWidth);              } else {                  offset = (forward? maxTabWidth : -maxTabWidth);              }               break;          }          case BOTTOM: {              if (run == 0) {                  offset = (forward?                             maxTabHeight :                            calculateTabAreaHeight(tabPlacement, runCount, maxTabHeight)-maxTabHeight);              } else if (run == runCount - 1) {                  offset = (forward?                            -(calculateTabAreaHeight(tabPlacement, runCount, maxTabHeight)-maxTabHeight) :                            -maxTabHeight);              } else {                  offset = (forward? maxTabHeight : -maxTabHeight);              }               break;          }          case TOP:          default: {              if (run == 0) {                  offset = (forward?                             -(calculateTabAreaHeight(tabPlacement, runCount, maxTabHeight)-maxTabHeight) :                            -maxTabHeight);              } else if (run == runCount - 1) {                  offset = (forward?                            maxTabHeight :                            calculateTabAreaHeight(tabPlacement, runCount, maxTabHeight)-maxTabHeight);              } else {                  offset = (forward? maxTabHeight : -maxTabHeight);              }          }        }        return offset;    }    protected int getPreviousTabIndex(int base) {        int tabIndex = (base - 1 >= 0? base - 1 : tabPane.getTabCount() - 1);        return (tabIndex >= 0? tabIndex : 0);    }    protected int getNextTabIndex(int base) {        return (base+1)%tabPane.getTabCount();    }    protected int getNextTabIndexInRun(int tabCount, int base) {        if (runCount < 2) {	    return getNextTabIndex(base);	}	int currentRun = getRunForTab(tabCount, base);	int next = getNextTabIndex(base);	if (next == tabRuns[getNextTabRun(currentRun)]) {	    return tabRuns[currentRun];	}	return next;    }    protected int getPreviousTabIndexInRun(int tabCount, int base) {        if (runCount < 2) {	    return getPreviousTabIndex(base);	}	int currentRun = getRunForTab(tabCount, base);	if (base == tabRuns[currentRun]) {	    int previous = tabRuns[getNextTabRun(currentRun)]-1;	    return (previous != -1? previous : tabCount-1);	}	return getPreviousTabIndex(base);    }    protected int getPreviousTabRun(int baseRun) {        int runIndex = (baseRun - 1 >= 0? baseRun - 1 : runCount - 1);        return (runIndex >= 0? runIndex : 0);    }    protected int getNextTabRun(int baseRun) {        return (baseRun+1)%runCount;    }    protected static void rotateInsets(Insets topInsets, Insets targetInsets, int targetPlacement) {                switch(targetPlacement) {          case LEFT:              targetInsets.top = topInsets.left;              targetInsets.left = topInsets.top;              targetInsets.bottom = topInsets.right;              targetInsets.right = topInsets.bottom;              break;          case BOTTOM:              targetInsets.top = topInsets.bottom;              targetInsets.left = topInsets.left;              targetInsets.bottom = topInsets.top;              targetInsets.right = topInsets.right;                            break;          case RIGHT:              targetInsets.top = topInsets.left;

⌨️ 快捷键说明

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