📄 scrollingtabpane.java
字号:
// remove from the popup menu tabSelectionPopupMenu.removeTabMenuItem(index); int tabCount = components.size(); if (tabCount == 0) { allTabsRemoved(); fireTabClosed(new DockedTabEvent(removed)); return; } // fire the close event fireTabClosed(new DockedTabEvent(removed)); if (selectedIndex >= index) { if (selectedIndex > 0) { selectedIndex--; } } TabComponent tabComponent = components.get(selectedIndex); cardLayout.show(componentPanel, tabComponent.getLayoutName()); // ensure we can still see the selected index ensureIndexVisible(selectedIndex); /* // move the viewport if it all fits now if (tabPanel.getWidth() < (viewport.getViewPosition().x + viewport.getWidth())) { viewport.setViewPosition(new Point(0, 0)); } */ viewport.validate(); viewport.repaint(); tabPanel.repaint(); fireTabSelected(new DockedTabEvent(tabComponent)); removed = null; } // ------------------------------------------------- // scrolling panel controls // ------------------------------------------------- private class ScrollableTabViewport extends JViewport implements ChangeListener { protected int pgHorz = 1; public ScrollableTabViewport() { super(); setScrollMode(SIMPLE_SCROLL_MODE); addChangeListener(this); } public void stateChanged(ChangeEvent e) { enableButtons(getViewPosition()); } protected void movePanel(int x) { // moving in x direction only Point pt = getViewPosition(); pt.x += pgHorz * x; pt.x = Math.max(0, pt.x); pt.x = Math.min(getMaxXExtent(), pt.x); setViewPosition(pt); tabPanel.repaint(); } public void setViewPosition(Point p) { super.setViewPosition(p); enableButtons(p); } protected void enableButtons(Point pt) { scrollButtonPanel.enableButton(WEST, pt.x > 0); scrollButtonPanel.enableButton(EAST, pt.x < getMaxXExtent()); } protected int getMaxXExtent() { return getView().getWidth() - getWidth(); } protected int getMaxYExtent() { return getView().getHeight() - getHeight(); } } // class ScrollableTabViewport private class ScrollButtonPanel extends JPanel { protected static final int SCROLL_EXTENT = 50; // scroll and menu buttons protected ScrollButton scrollLeft; protected ScrollButton scrollRight; protected ScrollButton tabMenuButton; public ScrollButtonPanel() { super(new GridBagLayout()); scrollLeft = new ScrollButton(WEST); scrollRight = new ScrollButton(EAST); tabMenuButton = new ScrollButton(SOUTH); GridBagConstraints gbc = new GridBagConstraints(); gbc.insets = ApplicationConstants.EMPTY_INSETS; gbc.anchor = GridBagConstraints.CENTER; gbc.gridy = 0; gbc.gridx = 0; gbc.insets.left = 2; gbc.insets.bottom = 4; add(scrollLeft, gbc); gbc.gridx++; gbc.insets.left = 0; add(scrollRight, gbc); gbc.insets.right = 2; gbc.insets.left = 2; gbc.gridx++; add(tabMenuButton, gbc); } protected void enableButton(int direction, boolean enable) { switch (direction) { case EAST: scrollRight.setEnabled(enable); break; case WEST: scrollLeft.setEnabled(enable); break; case SOUTH: tabMenuButton.setEnabled(enable); break; } } public void paintComponent(Graphics g) { super.paintComponent(g); // evil hack to continue the tab selection border if (selectedIndex != -1) { g.setColor(tabPanel.controlShadow); g.drawLine(0, getHeight() - tabPanel.TAB_BOTTOM_BORDER_HEIGHT - 1, getWidth(), getHeight() - tabPanel.TAB_BOTTOM_BORDER_HEIGHT - 1); if (isFocusedTabPane) { g.setColor(tabPanel.activeColor); } else { g.setColor(tabPanel.activeNoFocusColor); } g.fillRect(0, getHeight() - tabPanel.TAB_BOTTOM_BORDER_HEIGHT, getWidth() , tabPanel.TAB_BOTTOM_BORDER_HEIGHT); } } } private class ScrollButton extends JButton implements ActionListener { /** Indicates whether to draw a border */ protected boolean drawBorder; /** the disabled button colour */ protected Color disabledColour; /** the direction this button faces */ protected int direction; /** whether the popup menu has been invoked */ protected boolean menuShowing; public ScrollButton(int direction) { this.direction = direction; setBorder(null); setMargin(ApplicationConstants.EMPTY_INSETS); addActionListener(this); disabledColour = UIManager.getColor("Button.disabledText"); } public void paintComponent(Graphics g) { // paint the background g.setColor(tabPanel.background); g.fillRect(0, 0, getWidth(), getHeight()); // draw the border g.setColor(tabPanel.controlShadow); g.drawRect(0, 0, getWidth() - 1, getHeight() - 1); if (isEnabled()) { g.setColor(Color.BLACK); } else { g.setColor(disabledColour); } int iconWidth = 10; int iconHeight = 6; Graphics2D g2 = (Graphics2D)g; AffineTransform oldTransform = g2.getTransform(); // rotate as required if (direction != NORTH) { double theta = 0; double xOrigin = getWidth() / 2; double yOrigin = getHeight() / 2; switch (direction) { case EAST: theta = Math.PI / 2; break; case WEST: theta = Math.PI * 1.5; break; case SOUTH: theta = Math.PI; break; } g2.rotate(theta, xOrigin, yOrigin); } int x = (getWidth() - iconWidth) / 2; int y = getHeight() - ((getHeight() - iconHeight) / 2) - 1; int width = iconWidth; for (int i = 0; i < iconHeight; i++) { g.drawLine(x, y, x + width, y); x++; y--; width -= 2; } g2.setTransform(oldTransform); } public void actionPerformed(ActionEvent e) { switch (direction) { case EAST: viewport.movePanel(ScrollButtonPanel.SCROLL_EXTENT); break; case WEST: viewport.movePanel(-ScrollButtonPanel.SCROLL_EXTENT); break; case SOUTH: tabSelectionPopupMenu.showPopup(); break; } } public Dimension getPreferredSize() { return new Dimension(getWidth(), getHeight()); } public int getWidth() { return 18; } public int getHeight() { return 18; } } // ------------------------------------------------- // tab popup menus // ------------------------------------------------- /** font for the popup menus */ private Font popupMenuFont; /** Inits the popup menu font */ private void initPopupMenuFont() { popupMenuFont = UIManager.getFont("PopupMenu.font"); popupMenuFont = popupMenuFont.deriveFont(Font.PLAIN, 10); } /** * Popup menu for tab components accessible through * mouse right-click action. */ private class TabPopupMenu extends JPopupMenu implements ActionListener { private int popupTabIndex; private JMenuItem close; private JMenuItem closeAll; private JMenuItem closeOther; public TabPopupMenu() { if (popupMenuFont == null) { initPopupMenuFont(); } setFont(popupMenuFont); close = new JMenuItem("Close"); closeAll = new JMenuItem("Close All"); closeOther = new JMenuItem("Close Others"); close.addActionListener(this); closeAll.addActionListener(this); closeOther.addActionListener(this); add(close); add(closeAll); add(closeOther); 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 == closeAll) { removeAllTabs(); } else if (source == closeOther) { removeOthers(popupTabIndex); } } finally { popupTabIndex = -1; } } } /** * Popup menu for the tab selection button */ private class TabSelectionPopupMenu extends JPopupMenu implements ActionListener {// PopupMenuListener { protected boolean menuVisible; protected List<JMenuItem> menuItems; public TabSelectionPopupMenu() { if (popupMenuFont == null) { initPopupMenuFont(); } setFont(popupMenuFont); //addPopupMenuListener(this); } public void addTabMenuItem(TabComponent tabComponent) { if (menuItems == null) { menuItems = new ArrayList<JMenuItem>(); } JMenuItem menuItem = new JMenuItem(tabComponent.getDisplayName(), tabComponent.getIcon()); menuItem.addActionListener(this); menuItems.add(menuItem); add(menuItem); } public void removeTabMenuItem(int index) { JMenuItem menuItem = menuItems.get(index); menuItems.remove(index); remove(menuItem); } public void renameTabMenuItem(int index, String newTitle) { JMenuItem menuItem = menuItems.get(index); menuItem.setText(newTitle); } public void removeAll() { super.removeAll(); menuItems.clear(); } public void showPopup() { if (menuVisible) { menuVisible = false; return; } menuVisible = true; Rectangle bounds = scrollButtonPanel.tabMenuButton.getBounds(); int x = (int)(bounds.getX() + bounds.getWidth()); int y = (int)(bounds.getY() + bounds.getHeight()); pack(); setVisible(true); show(scrollButtonPanel, x - getWidth(), y + 1); } protected void firePopupMenuCanceled() {} protected void firePopupMenuWillBecomeInvisible() {} public void actionPerformed(ActionEvent e) { menuVisible = false; JMenuItem menuItem = (JMenuItem)e.getSource(); int index = menuItems.indexOf(menuItem); setSelectedIndex(index); } /* public void popupMenuWillBecomeVisible(PopupMenuEvent e) {} public void popupMenuWillBecomeInvisible(PopupMenuEvent e) { Log.debug("invisible"); //menuVisible = false; wasCancelled = false; } private boolean wasCancelled = true; public void popupMenuCanceled(PopupMenuEvent e) { Log.debug("cancelling"); wasCancelled = true; } */ } // ------------------------------------------------- // tab display panel and controls // ------------------------------------------------- /** The tab rectangles */ private Rectangle[] tabRects; private class TabPanel extends JPanel { protected Font font; protected Color foreground; protected Color background; protected Color activeColor; protected Color selectedColor; protected Color controlShadow; protected Color activeNoFocusColor; protected int textIconGap; protected Insets tabInsets; private int height; protected static final int TOP_INSET = 5; protected static final int TAB_BOTTOM_BORDER_HEIGHT = 3; protected TabPanel() { initDefaults(); tabRects = new Rectangle[0]; MouseHandler mouseHandler = new MouseHandler();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -