basictabbedpaneui.java
来自「linux下建立JAVA虚拟机的源码KAFFE」· Java 代码 · 共 2,001 行 · 第 1/5 页
JAVA
2,001 行
// The reason why we can't use runCount: // This method is only called to calculate the size request // for the tabbedPane. However, this size request is dependent on // our desired width. We need to find out what the height would // be IF we got our desired width. for (int i = 0; i < tabPane.getTabCount(); i++) { tabWidth = calculateTabWidth(tabPlacement, i, fm); if (runWidth + tabWidth > width) { runWidth = tabWidth; runs++; } else runWidth += tabWidth; } runs++; int maxTabHeight = calculateMaxTabHeight(tabPlacement); int tabAreaHeight = calculateTabAreaHeight(tabPlacement, runs, maxTabHeight); return tabAreaHeight; } /** * This method calculates the preferred tab area width given a tab * placement and height. * * @param tabPlacement The JTabbedPane's tab placement. * @param height The expected height. * * @return The preferred tab area width. */ protected int preferredTabAreaWidth(int tabPlacement, int height) { if (tabPane.getTabCount() == 0) return calculateTabAreaHeight(tabPlacement, 0, 0); int runs = 0; int runHeight = 0; int tabHeight = 0; FontMetrics fm = getFontMetrics(); Insets tabAreaInsets = getTabAreaInsets(tabPlacement); Insets insets = tabPane.getInsets(); height -= tabAreaInsets.top + tabAreaInsets.bottom + insets.top + insets.bottom; int fontHeight = fm.getHeight(); for (int i = 0; i < tabPane.getTabCount(); i++) { tabHeight = calculateTabHeight(tabPlacement, i, fontHeight); if (runHeight + tabHeight > height) { runHeight = tabHeight; runs++; } else runHeight += tabHeight; } runs++; int maxTabWidth = calculateMaxTabWidth(tabPlacement); int tabAreaWidth = calculateTabAreaWidth(tabPlacement, runs, maxTabWidth); return tabAreaWidth; } /** * This method rotates the places each run in the correct place the * tabRuns array. See the comment for tabRuns for how the runs are placed * in the array. * * @param tabPlacement The JTabbedPane's tab placement. * @param selectedRun The run the current selection is in. */ protected void rotateTabRuns(int tabPlacement, int selectedRun) { if (runCount == 1 || selectedRun == 1 || selectedRun == -1) return; int[] newTabRuns = new int[tabRuns.length]; int currentRun = selectedRun; int i = 1; do { newTabRuns[i] = tabRuns[currentRun]; currentRun = getNextTabRun(currentRun); i++; } while (i < runCount); if (runCount > 1) newTabRuns[0] = tabRuns[currentRun]; tabRuns = newTabRuns; BasicTabbedPaneUI.this.selectedRun = 1; } /** * This method is called when a component is removed from the * JTabbedPane. * * @param comp The component removed. */ public void removeLayoutComponent(Component comp) { // Do nothing. } } /** * This class acts as the LayoutManager for the JTabbedPane in * SCROLL_TAB_MODE. */ private class TabbedPaneScrollLayout extends TabbedPaneLayout { /** * This method returns the preferred layout size for the given container. * * @param parent The container to calculate a size for. * * @return The preferred layout size. */ public Dimension preferredLayoutSize(Container parent) { return super.calculateSize(true); } /** * This method returns the minimum layout size for the given container. * * @param parent The container to calculate a size for. * * @return The minimum layout size. */ public Dimension minimumLayoutSize(Container parent) { return super.calculateSize(true); } /** * This method calculates the tab area height given a desired width. * * @param tabPlacement The JTabbedPane's tab placement. * @param width The expected width. * * @return The tab area height given the width. */ protected int preferredTabAreaHeight(int tabPlacement, int width) { if (tabPane.getTabCount() == 0) return calculateTabAreaHeight(tabPlacement, 0, 0); int runs = 1; int maxTabHeight = calculateMaxTabHeight(tabPlacement); int tabAreaHeight = calculateTabAreaHeight(tabPlacement, runs, maxTabHeight); return tabAreaHeight; } /** * This method calculates the tab area width given a desired height. * * @param tabPlacement The JTabbedPane's tab placement. * @param height The expected height. * * @return The tab area width given the height. */ protected int preferredTabAreaWidth(int tabPlacement, int height) { if (tabPane.getTabCount() == 0) return calculateTabAreaHeight(tabPlacement, 0, 0); int runs = 1; int maxTabWidth = calculateMaxTabWidth(tabPlacement); int tabAreaWidth = calculateTabAreaWidth(tabPlacement, runs, maxTabWidth); return tabAreaWidth; } /** * This method is called to calculate the tab rectangles. This method * will calculate the size and position of all rectangles (taking into * account which ones should be in which tab run). It will pad them and * normalize them as necessary. * * @param tabPlacement The JTabbedPane's tab placement. * @param tabCount The number of tabs. */ protected void calculateTabRects(int tabPlacement, int tabCount) { if (tabCount == 0) return; FontMetrics fm = getFontMetrics(); SwingUtilities.calculateInnerArea(tabPane, calcRect); Insets tabAreaInsets = getTabAreaInsets(tabPlacement); Insets insets = tabPane.getInsets(); int runs = 1; int start = 0; int top = 0; if (tabPlacement == SwingConstants.TOP || tabPlacement == SwingConstants.BOTTOM) { int maxHeight = calculateMaxTabHeight(tabPlacement); calcRect.width -= tabAreaInsets.left + tabAreaInsets.right; start = tabAreaInsets.left + insets.left; int width = 0; int runWidth = start; top = insets.top + tabAreaInsets.top; for (int i = 0; i < tabCount; i++) { width = calculateTabWidth(tabPlacement, i, fm); rects[i] = new Rectangle(runWidth, top, width, maxHeight); runWidth += width; } tabAreaRect.width = tabPane.getWidth() - insets.left - insets.right; tabAreaRect.height = runs * maxTabHeight - (runs - 1) * tabRunOverlay + tabAreaInsets.top + tabAreaInsets.bottom; contentRect.width = tabAreaRect.width; contentRect.height = tabPane.getHeight() - insets.top - insets.bottom - tabAreaRect.height; contentRect.x = insets.left; tabAreaRect.x = insets.left; if (tabPlacement == SwingConstants.BOTTOM) { contentRect.y = insets.top; tabAreaRect.y = contentRect.y + contentRect.height; } else { tabAreaRect.y = insets.top; contentRect.y = tabAreaRect.y + tabAreaRect.height; } } else { int maxWidth = calculateMaxTabWidth(tabPlacement); calcRect.height -= tabAreaInsets.top + tabAreaInsets.bottom; int height = 0; start = tabAreaInsets.top + insets.top; int runHeight = start; int fontHeight = fm.getHeight(); top = insets.left + tabAreaInsets.left; for (int i = 0; i < tabCount; i++) { height = calculateTabHeight(tabPlacement, i, fontHeight); rects[i] = new Rectangle(top, runHeight, maxWidth, height); runHeight += height; } tabAreaRect.width = runs * maxTabWidth - (runs - 1) * tabRunOverlay + tabAreaInsets.left + tabAreaInsets.right; tabAreaRect.height = tabPane.getHeight() - insets.top - insets.bottom; tabAreaRect.y = insets.top; contentRect.width = tabPane.getWidth() - insets.left - insets.right - tabAreaRect.width; contentRect.height = tabAreaRect.height; contentRect.y = insets.top; if (tabPlacement == SwingConstants.LEFT) { tabAreaRect.x = insets.left; contentRect.x = tabAreaRect.x + tabAreaRect.width; } else { contentRect.x = insets.left; tabAreaRect.x = contentRect.x + contentRect.width; } } runCount = runs; if (runCount > tabRuns.length) expandTabRunsArray(); padSelectedTab(tabPlacement, tabPane.getSelectedIndex()); } /** * This method is called when the JTabbedPane is laid out in * SCROLL_TAB_LAYOUT. It finds the position for all components in the * JTabbedPane. * * @param pane The JTabbedPane to be laid out. */ public void layoutContainer(Container pane) { super.layoutContainer(pane); int tabCount = tabPane.getTabCount(); Point p = null; if (tabCount == 0) return; int tabPlacement = tabPane.getTabPlacement(); incrButton.setVisible(false); decrButton.setVisible(false); if (tabPlacement == SwingConstants.TOP || tabPlacement == SwingConstants.BOTTOM) { if (tabAreaRect.x + tabAreaRect.width < rects[tabCount - 1].x + rects[tabCount - 1].width) { Dimension incrDims = incrButton.getPreferredSize(); Dimension decrDims = decrButton.getPreferredSize(); decrButton.setBounds(tabAreaRect.x + tabAreaRect.width - incrDims.width - decrDims.width, tabAreaRect.y, decrDims.width, tabAreaRect.height); incrButton.setBounds(tabAreaRect.x + tabAreaRect.width - incrDims.width, tabAreaRect.y, decrDims.width, tabAreaRect.height); tabAreaRect.width -= decrDims.width + incrDims.width; incrButton.setVisible(true); decrButton.setVisible(true); } } if (tabPlacement == SwingConstants.LEFT || tabPlacement == SwingConstants.RIGHT) { if (tabAreaRect.y + tabAreaRect.height < rects[tabCount - 1].y + rects[tabCount - 1].height) { Dimension incrDims = incrButton.getPreferredSize(); Dimension decrDims = decrButton.getPreferredSize(); decrButton.setBounds(tabAreaRect.x, tabAreaRect.y + tabAreaRect.height - incrDims.height - decrDims.height, tabAreaRect.width, decrDims.height); incrButton.setBounds(tabAreaRect.x, tabAreaRect.y + tabAreaRect.height - incrDims.height, tabAreaRect.width, incrDims.height); tabAreaRect.height -= decrDims.height + incrDims.height; incrButton.setVisible(true); decrButton.setVisible(true); } } viewport.setBounds(tabAreaRect.x, tabAreaRect.y, tabAreaRect.width, tabAreaRect.height); int tabC = tabPane.getTabCount() - 1; if (tabCount > 0) { int w = Math.max(rects[tabC].width + rects[tabC].x, tabAreaRect.width); int h = Math.max(rects[tabC].height, tabAreaRect.height); p = findPointForIndex(currentScrollLocation); // we want to cover that entire space so that borders that run under // the tab area don't show up when we move the viewport around. panel.setSize(w + p.x, h + p.y); } viewport.setViewPosition(p); viewport.repaint(); } } /** * This class handles ChangeEvents from the JTabbedPane. * * @specnote Apparently this class was intended to be protected, * but was made public by a compiler bug and is now * public for compatibility. */ public class TabSelectionHandler implements ChangeListener { /** * This method is called whenever a ChangeEvent is fired from the * JTabbedPane. * * @param e The ChangeEvent fired. */ public void stateChanged(ChangeEvent e) { selectedRun = getRunForTab(tabPane.getTabCount(), tabPane.getSelectedIndex()); tabPane.revalidate(); tabPane.repaint(); } } /** * This helper class is a JPanel that fits inside the ScrollViewport. This * panel's sole job is to paint the tab rectangles inside the viewport so * that it's clipped correctly. */ private class ScrollingPanel extends JPanel { /** * This is a private UI class for our panel. */ private class ScrollingPanelUI extends BasicPanelUI { /** * This method overrides the default paint method. It paints the tab
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?