📄 dockedtabpane.java
字号:
// retrieve the component from the cache TabComponent tabComponent = components.get(index); // remove from the tab display and minimise parent.minimiseComponent(tabComponent); removeIndex(index); // fire the event fireTabMinimised(new DockedTabEvent(tabComponent)); } /** * Removes all tab components from this panel */ protected void removeAllTabs() { cardLayout.invalidateLayout(componentPanel); // fire the close event for (int i = 0, k = components.size(); i < k; i++) { TabComponent tabComponent = components.get(i); if (okToClose(tabComponent)) { fireTabClosed(new DockedTabEvent(tabComponent)); } else { return; } } allTabsRemoved(); } /** * Cleanup method following removal of all tabs. */ private void allTabsRemoved() { selectedIndex = -1; components.clear(); components = null; cardLayout = null; componentPanel = null; tabPanel = null; parent.removeTabPane(this); } /** * Removes the tab from the panel at the specified index. * * @param the index to be removed */ protected void removeIndex(int index) { if (index < 0) { return; } TabComponent removed = components.get(index); if (!okToClose(removed)) { return; } // remove from the component cache components.remove(index); // reset the layout cardLayout.invalidateLayout(componentPanel); componentPanel.removeAll(); int tabCount = components.size(); if (tabCount == 0) { allTabsRemoved(); fireTabClosed(new DockedTabEvent(removed)); return; } String layoutName = null; for (int i = 0; i < tabCount; i++) { TabComponent tabComponent = components.get(i); layoutName = tabComponent.getLayoutName(); componentPanel.add(tabComponent.getComponent(), layoutName); cardLayout.addLayoutComponent(tabComponent.getComponent(), layoutName); } // check if the last panel was removed if (index == tabCount) { // reset the index index--; } selectedIndex = index; TabComponent tabComponent = components.get(index); cardLayout.show(componentPanel, tabComponent.getLayoutName()); tabPanel.repaint(); // fire the event fireTabClosed(new DockedTabEvent(removed)); } protected int getTabPanePosition() { return parent.getTabPanePosition(this); } private Rectangle[] tabRects; private class TabPanel extends JPanel { protected Font font; protected Color foreground; protected Color background; protected Color activeColor; protected Color activeNoFocusColor; protected Color controlShadow; protected int textIconGap; protected Insets tabInsets; private int height; protected static final int TEXT_CROP_OFFSET = 10; protected static final int TAB_BOTTOM_BORDER_HEIGHT = 3; protected TabPanel() { initDefaults(); tabRects = new Rectangle[0]; MouseHandler mouseHandler = new MouseHandler(); addMouseListener(mouseHandler); addMouseMotionListener(mouseHandler); } public String getToolTipText(MouseEvent e) { // check if we are over a button if (currentCloseRolloverIndex != -1 || currentMinimizeRolloverIndex != -1) { return null; } int x = e.getX(); int y = e.getY(); int index = getTabAtLocation(x, y); if (index == -1) { return null; } TabComponent tabComponent = components.get(index); return tabComponent.getToolTip(); } public int getHeight() { if (height == 0) { calculateTabHeight(); } return height; } public Dimension getPreferredSize() { return new Dimension(getWidth(), getHeight()); } public Dimension getMaximumSize() { return getPreferredSize(); } protected void calculateTabHeight() { FontMetrics metrics = getFontMetrics(font); height = metrics.getHeight() + tabInsets.top + tabInsets.bottom + TAB_BOTTOM_BORDER_HEIGHT + 6; } protected Rectangle getTabRectangleAt(int x, int y) { return null; } private int getTabHeight() { return height - TAB_BOTTOM_BORDER_HEIGHT; } public void paintComponent(Graphics g) { if (components == null) { return; } int tabCount = components.size(); if (tabCount == 0) { return; } calculateTabHeight(); calculateTabRects(tabCount); int tabWidth = getTabWidth(tabCount); boolean isSelected = false; int x = getX(); int y = getY(); int w = getWidth(); int h = getHeight(); // fill the background g.setColor(background); g.fillRect(x, y, w, h); // fill the selected background if (selectedIndex != -1) { if (isFocusedTabPane) { g.setColor(activeColor); Rectangle selected = tabRects[selectedIndex]; g.fillRect(selected.x, selected.y, selected.width + 1, selected.height); // fill the bottom border g.fillRect(x, h - TAB_BOTTOM_BORDER_HEIGHT, w , TAB_BOTTOM_BORDER_HEIGHT); } } // draw the borders g.setColor(controlShadow); // left and right absolute borders g.drawLine(x, y, x, y + h); // left-most g.drawLine(x + w - 1, y, x + w - 1, y + h); // right-most for (int i = 0; i < tabCount; i++) { Rectangle rect = tabRects[i]; x = rect.x; y = rect.y; w = rect.width; h = rect.height; g.drawLine(x, y, x, y+h-2); // left side g.drawLine(x+1, y, x+w+2, y); // top side /* if (i < tabCount - 1) { g.drawLine(x+w-1, y, x+w-1, y+h-2); // right side } */ // bottom side if (i != selectedIndex) { g.drawLine(x, y + h - 1, x + w, y + h - 1); } else { g.drawLine(0, y + h + TAB_BOTTOM_BORDER_HEIGHT - 1, getWidth(), y + h + TAB_BOTTOM_BORDER_HEIGHT - 1); } } // draw the text Rectangle iconRect = new Rectangle(); Rectangle textRect = new Rectangle(); g.setFont(font); FontMetrics metrics = getFontMetrics(font); for (int i = 0; i < tabCount; i++) { Rectangle tabRect = new Rectangle(tabRects[i]); calculateTextRect(tabRect, textRect, i); TabComponent tabComponent = components.get(i); // if tab selected make crop smaller to // account for min and close buttons if (i == selectedIndex) { // paint the close button Rectangle buttonRect = getCloseIconRectangle(tabRect); closeIcon.paintIcon(this, g, buttonRect.x, buttonRect.y); if (selectedIndex == currentCloseRolloverIndex) { g.setColor(TabControlIcon.ICON_COLOR); g.drawRect(buttonRect.x - 2, buttonRect.y - 2, buttonRect.width + 3, buttonRect.height + 3); } // paint the minimise button buttonRect = getMinimizeIconRectangle(tabRect); minimizeIcon.paintIcon(this, g, buttonRect.x, buttonRect.y); if (selectedIndex == currentMinimizeRolloverIndex) { g.setColor(TabControlIcon.ICON_COLOR); g.drawRect(buttonRect.x - 2, buttonRect.y - 2, buttonRect.width + 3, buttonRect.height + 3); } // smaller text crop area tabRect.width -= (TabControlIcon.ICON_WIDTH * 2) + 4; } // text crop offset tabRect.width -= TEXT_CROP_OFFSET; textRect.x = textRect.y = iconRect.x = iconRect.y = 0; String title = SwingUtilities.layoutCompoundLabel( this, metrics, tabComponent.getDisplayName(), tabComponent.getIcon(), SwingUtilities.CENTER, SwingUtilities.LEFT, SwingUtilities.CENTER, SwingUtilities.TRAILING, tabRect, iconRect, textRect, textIconGap); g.setColor(foreground); Graphics2D g2d = (Graphics2D)g; Object antialiasHint = g2d.getRenderingHint( RenderingHints.KEY_ANTIALIASING); g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -