📄 dockedtabpane.java
字号:
g.drawString(title, textRect.x + 3, textRect.y + textRect.height - 3); g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, antialiasHint); } } private Rectangle calculateTextRect(Rectangle tabRect, Rectangle textRect, int index) { textRect.x = tabRect.x + tabInsets.left; textRect.y = tabRect.y + tabInsets.top + 2; if (index == 0) { textRect.height = tabRect.height - tabInsets.top - tabInsets.bottom; } textRect.width = tabRect.width - tabInsets.left - tabInsets.right; if (selectedIndex == index) { Rectangle closeIconRect = getCloseIconRectangle(tabRect); Rectangle minIconRect = getMinimizeIconRectangle(tabRect); textRect.width = tabRect.width - tabInsets.left - tabInsets.right - closeIconRect.width - minIconRect.width; } return textRect; } // the close and minimise icon private Icon closeIcon = new DockedTabCloseIcon(); private Icon minimizeIcon = new DockedTabMinimizeIcon(parent.getOrientation()); private void initDefaults() { Font _font = DockedTabPane.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(); /* background = DockedTabPane.this.getBackground(); //activeNoFocusColor = background.brighter(); //foreground = UIManager.getColor("InternalFrame.activeTitleForeground"); //activeColor = UIManager.getColor("InternalFrame.activeTitleBackground"); foreground = UIUtils.getDefaultActiveTextColour(); //UIManager.getColor("InternalFrame.activeTitleForeground"); activeColor = UIUtils.getDefaultActiveBackgroundColour(); */ controlShadow = UIManager.getColor("controlShadow"); textIconGap = 2; tabInsets = UIManager.getInsets("TabbedPane.tabInsets"); } } // TabPanel /** The tab's tool tip */ protected DockedTabToolTip toolTip; /** Indicates the current rollover index for the min button */ protected int currentMinimizeRolloverIndex = -1; /** Indicates the current rollover index for the close button */ protected int currentCloseRolloverIndex = -1; private class MouseHandler implements MouseInputListener { private boolean dragging; public void mouseMoved(MouseEvent e) { boolean doRepaint = false; try { if (currentCloseRolloverIndex != -1 || currentMinimizeRolloverIndex != -1) { doRepaint = true; currentCloseRolloverIndex = -1; currentMinimizeRolloverIndex = -1; } int x = e.getX(); int y = e.getY(); int index = -1; 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; } iconRect = getMinimizeIconRectangle(tabRect); if (iconRect.contains(x, y)) { currentMinimizeRolloverIndex = 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 (toolTip == null) { toolTip = new DockedTabToolTip(); ToolTipManager.sharedInstance().registerComponent(tabPanel); } toolTip.setVisible(true); } // -------------------------------------------- } finally { if (doRepaint) { tabPanel.repaint(); } } } 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; } public void mouseDragged(MouseEvent e) { if (!dragging) { dragging = true; draggingIndex = getTabAtLocation(e.getX(), e.getY()); if (draggingIndex == -1) { return; } } parent.dockedTabDragged(new DockedDragEvent( DockedTabPane.this, e, getTabComponentAt(draggingIndex))); } public void mouseEntered(MouseEvent e) {} public void mouseExited(MouseEvent e) { if (currentCloseRolloverIndex != -1 || currentMinimizeRolloverIndex != -1) { currentCloseRolloverIndex = -1; currentMinimizeRolloverIndex = -1; tabPanel.repaint(); } if (toolTip != null && toolTip.isVisible()) { toolTip.setVisible(false); } } public void mouseClicked(MouseEvent e) { maybeShowPopup(e); } public void mousePressed(MouseEvent e) { maybeShowPopup(e); } 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 (!dragging) { // if nothing bail if (index < 0) { return; } // if the index is not the current index, select the tab if (index != selectedIndex) { setSelectedIndex(index); return; } // check if a close button was pushed on the selected index Rectangle tabRect = tabRects[index]; Rectangle iconRect = getCloseIconRectangle(tabRect); if (iconRect.contains(x, y)) { removeIndex(index); return; } // check if a minimise button was pushed on the selected index iconRect = getMinimizeIconRectangle(tabRect); if (iconRect.contains(x, y)) { minimiseIndex(index); return; } } else { parent.dockedTabReleased(new DockedDragEvent( DockedTabPane.this, e, getTabComponentAt(draggingIndex))); } dragging = false; draggingIndex = -1; } finally { currentCloseRolloverIndex = -1; currentMinimizeRolloverIndex = -1; } } } /** * Popup menu for tab components accessible through * mouse right-click action. */ private class TabPopupMenu extends JPopupMenu implements ActionListener { private int popupTabIndex; private JMenuItem minimise; private JMenuItem close; private JMenuItem minimiseAll; public TabPopupMenu() { Font font = UIManager.getFont("PopupMenu.font"). deriveFont(Font.PLAIN, 10); setFont(font); close = new JMenuItem("Close"); minimise = new JMenuItem("Minimize"); minimiseAll = new JMenuItem("Minimize All"); close.addActionListener(this); minimiseAll.addActionListener(this); minimise.addActionListener(this); add(minimise); add(minimiseAll); addSeparator(); add(close); popupTabIndex = -1; } public void showPopup(int index, int x, int y) { popupTabIndex = index; show(tabPanel, x, y); } public void actionPerformed(ActionEvent e) { if (popupTabIndex == -1) { return; } try { Object source = e.getSource(); if (source == close) { removeIndex(popupTabIndex); } else if (source == minimiseAll) { minimiseAll(); } else if (source == minimise) { minimiseIndex(popupTabIndex); } } finally { popupTabIndex = -1; } } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -