📄 basictabbedpaneui.java
字号:
* for this tab. This is provided to support html rendering inside tabs. * * @param tabIndex the index of the tab * @return the text view to render the tab's text or null if no * specialized rendering is required * * @since 1.4 */ protected View getTextViewForTab(int tabIndex) { if (htmlViews != null) { return (View)htmlViews.elementAt(tabIndex); } return null; } protected int calculateTabHeight(int tabPlacement, int tabIndex, int fontHeight) { int height = 0; View v = getTextViewForTab(tabIndex); if (v != null) { // html height += (int)v.getPreferredSpan(View.Y_AXIS); } else { // plain text height += fontHeight; } Icon icon = getIconForTab(tabIndex); Insets tabInsets = getTabInsets(tabPlacement, tabIndex); if (icon != null) { height = Math.max(height, icon.getIconHeight()); } height += tabInsets.top + tabInsets.bottom + 2; return height; } protected int calculateMaxTabHeight(int tabPlacement) { FontMetrics metrics = getFontMetrics(); int tabCount = tabPane.getTabCount(); int result = 0; int fontHeight = metrics.getHeight(); for(int i = 0; i < tabCount; i++) { result = Math.max(calculateTabHeight(tabPlacement, i, fontHeight), result); } return result; } protected int calculateTabWidth(int tabPlacement, int tabIndex, FontMetrics metrics) { Icon icon = getIconForTab(tabIndex); Insets tabInsets = getTabInsets(tabPlacement, tabIndex); int width = tabInsets.left + tabInsets.right + 3; if (icon != null) { width += icon.getIconWidth() + textIconGap; } View v = getTextViewForTab(tabIndex); if (v != null) { // html width += (int)v.getPreferredSpan(View.X_AXIS); } else { // plain text String title = tabPane.getTitleAt(tabIndex); width += SwingUtilities2.stringWidth(tabPane, metrics, title); } return width; } protected int calculateMaxTabWidth(int tabPlacement) { FontMetrics metrics = getFontMetrics(); 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) { return tabInsets; } protected Insets getSelectedTabPadInsets(int tabPlacement) { rotateInsets(selectedTabPadInsets, currentPadInsets, tabPlacement); return currentPadInsets; } protected Insets getTabAreaInsets(int tabPlacement) { rotateInsets(tabAreaInsets, currentTabAreaInsets, tabPlacement); return currentTabAreaInsets; } protected Insets getContentBorderInsets(int tabPlacement) { return contentBorderInsets; } protected FontMetrics getFontMetrics() { Font font = tabPane.getFont(); return tabPane.getFontMetrics(font); }// Tab Navigation methods protected void navigateSelectedTab(int direction) { int tabPlacement = tabPane.getTabPlacement(); int current = DefaultLookup.getBoolean(tabPane, this, "TabbedPane.selectionFollowsFocus", true) ? tabPane.getSelectedIndex() : getFocusIndex(); int tabCount = tabPane.getTabCount(); boolean leftToRight = BasicGraphicsUtils.isLeftToRight(tabPane); // If we have no tabs then don't navigate. if (tabCount <= 0) { return; } int offset; switch(tabPlacement) { case LEFT: case RIGHT: switch(direction) { case NEXT: selectNextTab(current); break; case PREVIOUS: selectPreviousTab(current); break; 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 NEXT: selectNextTab(current); break; case PREVIOUS: selectPreviousTab(current); break; 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: if (leftToRight) { selectNextTabInRun(current); } else { selectPreviousTabInRun(current); } break; case WEST: if (leftToRight) { selectPreviousTabInRun(current); } else { selectNextTabInRun(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 = tabForCoordinate(tabPane, r.x + r.width/2 + offset, r.y + r.height/2); break; case BOTTOM: case TOP: default: newIndex = tabForCoordinate(tabPane, 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 (DefaultLookup.getBoolean(tabPane, this, "TabbedPane.selectionFollowsFocus", true)) { tabPane.setSelectedIndex(index); } else { // Just move focus (not selection) setFocusIndex(index, true); } } void setFocusIndex(int index, boolean repaint) { if (repaint && !isRunsDirty) { repaintTab(focusIndex); focusIndex = index; repaintTab(focusIndex); } else { focusIndex = index; } } /** * Repaints the specified tab. */ private void repaintTab(int index) { // If we're not valid that means we will shortly be validated and // painted, which means we don't have to do anything here. if (!isRunsDirty && index >= 0 && index < tabPane.getTabCount()) { tabPane.repaint(getTabBounds(tabPane, index)); } } /** * Makes sure the focusIndex is valid. */ private void validateFocusIndex() { if (focusIndex >= tabPane.getTabCount()) { setFocusIndex(tabPane.getSelectedIndex(), false); } } /** * Returns the index of the tab that has focus. * * @return index of tab that has focus * @since 1.5 */ protected 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
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -