📄 scrollingtabpane.java
字号:
addMouseListener(mouseHandler); addMouseMotionListener(mouseHandler); } /** * Returns the tool tip text of the current * mouse rollover tab. * * @param the mouse event * @return the tool tip of the rolled over tab - or null */ public String getToolTipText(MouseEvent e) { // check if we are over a button if (currentCloseRolloverIndex != -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(); } /** * Returns the calculated tab height for this panel. * * @return the tab height */ public int getHeight() { if (height == 0) { calculateTabHeight(); } return height + TOP_INSET; } /** * Returns the width based on the position of the last * tab rectangle. * * @return the tab panel width */ public int getWidth() { int tabCount = components.size(); if (tabCount == 0) { return 0; } if (tabRects.length != tabCount) { calculateTabRects(tabCount); } Rectangle lastRect = tabRects[tabRects.length - 1]; int width = lastRect.x + lastRect.width + 2; return Math.max(width, viewport.getWidth()); } 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 + 7; } protected Rectangle getTabRectangleAt(int x, int y) { return null; } private int getTabHeight() { return height - TAB_BOTTOM_BORDER_HEIGHT; } protected void calculateTabRects(int tabCount) { // check that we still have the right count if (tabRects.length != tabCount) { tabRects = new Rectangle[tabCount]; } FontMetrics metrics = getFontMetrics(font); for (int i = 0; i < tabCount; i++) { Rectangle rect = null; if (tabRects[i] == null) { rect = new Rectangle(); tabRects[i] = rect; } else { rect = tabRects[i]; } if (i > 0) { rect.x = tabRects[i-1].x + tabRects[i-1].width; } else { rect.x = 0; } rect.y = TOP_INSET; // add the left and right insets rect.width = 0; rect.width += tabInsets.left + tabInsets.right; TabComponent tabComponent = components.get(i); // add the icon width if (tabComponent.hasIcon()) { Icon icon = tabComponent.getIcon(); rect.width += icon.getIconWidth() + textIconGap; } // compute and add the text width rect.width += SwingUtilities.computeStringWidth( metrics, tabComponent.getDisplayName()); // add the close icon width rect.width += TabControlIcon.ICON_WIDTH + 6; rect.height = tabPanel.getTabHeight(); } } public void paintComponent(Graphics g) { if (components == null) { return; } int tabCount = components.size(); if (tabCount == 0) { return; } calculateTabHeight(); calculateTabRects(tabCount); int x = getX(); int y = getY(); int viewX = viewport.getViewPosition().x; g.setClip(viewX, 0, viewport.getWidth(), getHeight()); int w = getWidth(); int h = getHeight(); // fill the background g.setColor(background); g.fillRect(0, 0, w, h); // fill the selected tab background if (selectedIndex != -1) { if (isFocusedTabPane) { g.setColor(activeColor); } else { g.setColor(activeNoFocusColor); } Rectangle selected = tabRects[selectedIndex]; g.fillRect(selected.x + 1, selected.y + 1, selected.width - 2, selected.height - 1); } // ----------------------------------- // draw the borders g.setColor(controlShadow); // left far side g.drawLine(x, y + h - 3, x - 1, y + h + 3); // right far side g.drawLine(viewX + w, y + h - 5, x + w, y + h); 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+2, x, y+h-1); // left side g.drawLine(x+1, y+1, x+1, y+1); // top-left side g.drawLine(x+2, y, x+w-3, y); // top side g.drawLine(x+w-2, y+1, x+w-2, y+1); // top-right side g.drawLine(x+w-1, y+2, x+w-1, y+h-1); // right side // bottom side if (i != selectedIndex) { g.drawLine(x - 1, y + h - 1, x + w, y + h - 1); } } // fill the bottom border with the active colour // if a selected index is valid if (selectedIndex != -1) { Rectangle selected = tabRects[selectedIndex]; if (isFocusedTabPane) { g.setColor(activeColor); } else { g.setColor(activeNoFocusColor); } g.fillRect(viewX, getHeight() - TAB_BOTTOM_BORDER_HEIGHT, getWidth(), TAB_BOTTOM_BORDER_HEIGHT); g.setColor(controlShadow); } // complete the bottom border from the last rectangle g.drawLine(x + w - 1, y + h - 1, viewport.getViewPosition().x + viewport.getWidth() - 1, y + h - 1); // ------------------------------- // draw the text and any icons g.setFont(font); FontMetrics metrics = getFontMetrics(font); for (int i = 0; i < tabCount; i++) { Rectangle tabRect = new Rectangle(tabRects[i]); TabComponent tabComponent = components.get(i); // paint the close button Rectangle buttonRect = getCloseIconRectangle(tabRect); closeIcon.paintIcon(this, g, buttonRect.x, buttonRect.y); if (i == currentCloseRolloverIndex) { g.setColor(TabControlIcon.ICON_COLOR); g.drawRect(buttonRect.x - 2, buttonRect.y - 2, buttonRect.width + 3, buttonRect.height + 3); } // add the text g.setColor(foreground); x = tabRect.x + tabInsets.left; if (tabComponent.hasIcon()) { // draw the icon y = tabRect.y + tabInsets.top + 4; tabComponent.getIcon().paintIcon(this, g, x, y); // increment x position x += tabComponent.getIcon().getIconWidth() + textIconGap; } y = metrics.getHeight() + tabRect.y + tabInsets.top + 1; Graphics2D g2d = (Graphics2D)g; Object antialiasHint = g2d.getRenderingHint( RenderingHints.KEY_ANTIALIASING); g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g.drawString(tabComponent.getDisplayName(), x, y); g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, antialiasHint); } } /** the close icon for each tab */ private Icon closeIcon = new DockedTabCloseIcon(); private void initDefaults() { Font _font = ScrollingTabPane.this.getFont(); if (_font != null) { font = _font.deriveFont(Font.PLAIN, 11); } else { font = UIManager.getFont("TabbedPane.font").deriveFont(Font.PLAIN, 11); } FontMetrics fontMetrics = getFontMetrics(font); background = getTabBackground(); activeNoFocusColor = getNofocusTabBackground(); foreground = getTabForeground(); activeColor = getSelectedTabBackground(); controlShadow = UIManager.getColor("controlShadow"); textIconGap = UIManager.getInt("TabbedPane.textIconGap"); tabInsets = UIManager.getInsets("TabbedPane.tabInsets"); tabInsets.left = 5; } } // class TabPanel /** The tab's tool tip */ protected DockedTabToolTip toolTip; /** Indicates the current rollover index for the close button */ protected int currentCloseRolloverIndex = -1; private class MouseHandler implements MouseInputListener { public void mouseMoved(MouseEvent e) { boolean doRepaint = false; try { if (currentCloseRolloverIndex != -1) { doRepaint = true; currentCloseRolloverIndex = -1; } int x = e.getX(); int y = e.getY(); int index = -1; //Log.debug("mouse x: " + x); for (int i = 0, k = components.size(); i < k; i++) { Rectangle tabRect = tabRects[i]; if (tabRect.contains(x, y)) { index = i; Rectangle iconRect = getCloseIconRectangle(tabRect); if (iconRect.contains(x, y)) { currentCloseRolloverIndex = i; doRepaint = true; return; } break; } } // -------------------------------------------- // tool tip display // -------------------------------------------- if (index == -1) { if (toolTip != null && toolTip.isVisible()) { toolTip.setVisible(false); } return; } TabComponent tabComponent = components.get(index); if (tabComponent.hasToolTipText()) { if (!toolTipRegistered) { toolTipRegistered = true; ToolTipManager.sharedInstance().registerComponent(tabPanel); } /* if (toolTip == null) { toolTip = new DockedTabToolTip(); ToolTipManager.sharedInstance().registerComponent(tabPanel); } toolTip.setVisible(true); */ } // -------------------------------------------- } finally { if (doRepaint) { tabPanel.repaint(); } } } private boolean toolTipRegistered; public void mouseClicked(MouseEvent e) { maybeShowPopup(e); } public void mousePressed(MouseEvent e) { maybeShowPopup(e); } public void mouseDragged(MouseEvent e) {} public void mouseEntered(MouseEvent e) {} public void mouseExited(MouseEvent e) { if (currentCloseRolloverIndex != -1) { currentCloseRolloverIndex = -1; tabPanel.repaint(); } /* if (toolTip != null && toolTip.isVisible()) { toolTip.setVisible(false); } */ } private boolean maybeShowPopup(MouseEvent e) { int x = e.getX(); int y = e.getY(); int index = getTabAtLocation(x, y); if (index == -1) { return false; } if (e.isPopupTrigger()) { if (tabPopupMenu == null) { tabPopupMenu = new TabPopupMenu(); } tabPopupMenu.showPopup(index, x, y); return true; } return false; } private void resetRepaint() { currentCloseRolloverIndex = -1; tabPanel.repaint(); } public void mouseReleased(MouseEvent e) { if (maybeShowPopup(e) || (tabPopupMenu != null && tabPopupMenu.isVisible())) { return; } try { int x = e.getX(); int y = e.getY(); int index = getTabAtLocation(x, y); if (index == -1) { return; } resetRepaint(); // check if a close button was pushed Rectangle iconRect = getCloseIconRectangle(tabRects[index]); if (iconRect.contains(x, y)) { removeIndex(index); return; } // if the index is not the current index, select the tab if (index != selectedIndex) { setSelectedIndex(index); return; } } finally { currentCloseRolloverIndex = -1; } } } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -