synthtabbedpaneui.java
来自「java jdk 1.4的源码」· Java 代码 · 共 1,888 行 · 第 1/5 页
JAVA
1,888 行
// html v.paint(g, textRect); } else { // plain text int mnemIndex = tabPane.getDisplayedMnemonicIndexAt(tabIndex); g.setColor(ss.getStyle().getColor(ss, ColorType.TEXT_FOREGROUND)); ss.getStyle().getSynthGraphics(ss).paintText(ss, g, title, textRect, mnemIndex); } } protected int getTabLabelShiftX(int tabPlacement, int tabIndex, boolean isSelected) { Rectangle tabRect = rects[tabIndex]; int nudge = 0; switch(tabPlacement) { case LEFT: nudge = isSelected? -1 : 1; break; case RIGHT: nudge = isSelected? 1 : -1; break; case BOTTOM: case TOP: default: nudge = tabRect.width % 2; } return nudge; } protected int getTabLabelShiftY(int tabPlacement, int tabIndex, boolean isSelected) { Rectangle tabRect = rects[tabIndex]; int nudge = 0; switch(tabPlacement) { case BOTTOM: nudge = isSelected? 1 : -1; break; case LEFT: case RIGHT: nudge = tabRect.height % 2; break; case TOP: default: nudge = isSelected? -1 : 1;; } return nudge; } protected void paintContentBorder(SynthContext ss, Graphics g, int tabPlacement, int selectedIndex) { int width = tabPane.getWidth(); int height = tabPane.getHeight(); Insets insets = tabPane.getInsets(); int x = insets.left; int y = insets.top; int w = width - insets.right - insets.left; int h = height - insets.top - insets.bottom; switch(tabPlacement) { case LEFT: x += calculateTabAreaWidth(tabPlacement, runCount, maxTabWidth); w -= (x - insets.left); break; case RIGHT: w -= calculateTabAreaWidth(tabPlacement, runCount, maxTabWidth); break; case BOTTOM: h -= calculateTabAreaHeight(tabPlacement, runCount, maxTabHeight); break; case TOP: default: y += calculateTabAreaHeight(tabPlacement, runCount, maxTabHeight); h -= (y - insets.top); } SynthLookAndFeel.updateSubregion(ss, g, new Rectangle(x, y, w, h)); } private void ensureCurrentLayout() { if (!tabPane.isValid()) { tabPane.validate(); } /* If tabPane doesn't have a peer yet, the validate() call will * silently fail. We handle that by forcing a layout if tabPane * is still invalid. See bug 4237677. */ if (!tabPane.isValid()) { TabbedPaneLayout layout = (TabbedPaneLayout)tabPane.getLayout(); layout.calculateLayoutInfo(); } } // TabbedPaneUI methods /** * Returns the bounds of the specified tab index. The bounds are * with respect to the JTabbedPane's coordinate space. */ public Rectangle getTabBounds(JTabbedPane pane, int i) { ensureCurrentLayout(); Rectangle tabRect = new Rectangle(); return getTabBounds(i, tabRect); } public int getTabRunCount(JTabbedPane pane) { ensureCurrentLayout(); return runCount; } /** * Returns the tab index which intersects the specified point * in the JTabbedPane's coordinate space. */ public int tabForCoordinate(JTabbedPane pane, int x, int y) { ensureCurrentLayout(); Point p = new Point(x, y); if (scrollableTabLayoutEnabled()) { translatePointToTabPanel(x, y, p); } int tabCount = tabPane.getTabCount(); for (int i = 0; i < tabCount; i++) { if (rects[i].contains(p.x, p.y)) { return i; } } return -1; } /** * Returns the bounds of the specified tab in the coordinate space * of the JTabbedPane component. This is required because the tab rects * are by default defined in the coordinate space of the component where * they are rendered, which could be the JTabbedPane * (for WRAP_TAB_LAYOUT) or a ScrollableTabPanel (SCROLL_TAB_LAYOUT). * This method should be used whenever the tab rectangle must be relative * to the JTabbedPane itself and the result should be placed in a * designated Rectangle object (rather than instantiating and returning * a new Rectangle each time). The tab index parameter must be a valid * tabbed pane tab index (0 to tab count - 1, inclusive). The destination * rectangle parameter must be a valid <code>Rectangle</code> instance. * The handling of invalid parameters is unspecified. * * @param tabIndex the index of the tab * @param dest the rectangle where the result should be placed * @return the resulting rectangle * * @since 1.4 */ protected Rectangle getTabBounds(int tabIndex, Rectangle dest) { dest.width = rects[tabIndex].width; dest.height = rects[tabIndex].height; if (scrollableTabLayoutEnabled()) { // SCROLL_TAB_LAYOUT // Need to translate coordinates based on viewport location & // view position Point vpp = tabScroller.viewport.getLocation(); Point viewp = tabScroller.viewport.getViewPosition(); dest.x = rects[tabIndex].x-vpp.x-viewp.x; dest.y = rects[tabIndex].y-vpp.y-viewp.y; } else { // WRAP_TAB_LAYOUT dest.x = rects[tabIndex].x; dest.y = rects[tabIndex].y; } return dest; } /** * Returns the tab index which intersects the specified point * in the coordinate space of the component where the * tabs are actually rendered, which could be the JTabbedPane * (for WRAP_TAB_LAYOUT) or a ScrollableTabPanel (SCROLL_TAB_LAYOUT). */ private int getTabAtLocation(int x, int y) { ensureCurrentLayout(); int tabCount = tabPane.getTabCount(); for (int i = 0; i < tabCount; i++) { if (rects[i].contains(x, y)) { return i; } } return -1; } /** * Returns the index of the tab closest to the passed in location, note * that the returned tab may not contain the location x,y. */ private int getClosestTab(int x, int y) { int min = 0; int tabCount = Math.min(rects.length, tabPane.getTabCount()); int max = tabCount; int tabPlacement = tabPane.getTabPlacement(); boolean useX = (tabPlacement == TOP || tabPlacement == BOTTOM); int want = (useX) ? x : y; while (min != max) { int current = (max + min) / 2; int minLoc; int maxLoc; if (useX) { minLoc = rects[current].x; maxLoc = minLoc + rects[current].width; } else { minLoc = rects[current].y; maxLoc = minLoc + rects[current].height; } if (want < minLoc) { max = current; if (min == max) { return Math.max(0, current - 1); } } else if (want >= maxLoc) { min = current; if (max - min <= 1) { return Math.max(current + 1, tabCount - 1); } } else { return current; } } return min; } /** * Returns a point which is translated from the specified point in the * JTabbedPane's coordinate space to the coordinate space of the * ScrollableTabPanel. This is used for SCROLL_TAB_LAYOUT ONLY. */ private Point translatePointToTabPanel(int srcx, int srcy, Point dest) { Point vpp = tabScroller.viewport.getLocation(); Point viewp = tabScroller.viewport.getViewPosition(); dest.x = srcx + vpp.x + viewp.x; dest.y = srcy + vpp.y + viewp.y; return dest; }// BasicTabbedPaneUI methods protected Component getVisibleComponent() { return visibleComponent; } protected void setVisibleComponent(Component component) { if (visibleComponent != null && visibleComponent != component && visibleComponent.getParent() == tabPane) { visibleComponent.setVisible(false); } if (component != null && !component.isVisible()) { component.setVisible(true); } visibleComponent = component; } protected void assureRectsCreated(int tabCount) { int rectArrayLen = rects.length; if (tabCount != rectArrayLen ) { Rectangle[] tempRectArray = new Rectangle[tabCount]; System.arraycopy(rects, 0, tempRectArray, 0, Math.min(rectArrayLen, tabCount)); rects = tempRectArray; for (int rectIndex = rectArrayLen; rectIndex < tabCount; rectIndex++) { rects[rectIndex] = new Rectangle(); } } } protected void expandTabRunsArray() { int rectLen = tabRuns.length; int[] newArray = new int[rectLen+10]; System.arraycopy(tabRuns, 0, newArray, 0, runCount); tabRuns = newArray; } protected int getRunForTab(int tabCount, int tabIndex) { for (int i = 0; i < runCount; i++) { int first = tabRuns[i]; int last = lastTabInRun(tabCount, i); if (tabIndex >= first && tabIndex <= last) { return i; } } return 0; } protected int lastTabInRun(int tabCount, int run) { if (runCount == 1) { return tabCount - 1; } int nextRun = (run == runCount - 1? 0 : run + 1); if (tabRuns[nextRun] == 0) { return tabCount - 1; } return tabRuns[nextRun]-1; } protected int getTabRunOverlay(int tabPlacement) { return tabRunOverlay; } protected int getTabRunIndent(int tabPlacement, int run) { return 0; } protected boolean shouldPadTabRun(int tabPlacement, int run) { return runCount > 1; } protected boolean shouldRotateTabRuns(int tabPlacement) { return true; } protected Icon getIconForTab(int tabIndex) { return (!tabPane.isEnabled() || !tabPane.isEnabledAt(tabIndex))? tabPane.getDisabledIconAt(tabIndex) : tabPane.getIconAt(tabIndex); } /** * Returns the text View object required to render stylized text (HTML) for * the specified tab or null if no specialized text rendering is needed * 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(tabContext.getStyle().getFont( tabContext)); 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,
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?