📄 scrollingtabpane.java
字号:
/* * ScrollingTabPane.java * * Copyright (C) 2002, 2003, 2004, 2005, 2006 Takis Diakoumis * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * */package org.executequery.base;import java.awt.BorderLayout;import java.awt.Color;import java.awt.Component;import java.awt.Dimension;import java.awt.Font;import java.awt.FontMetrics;import java.awt.Graphics;import java.awt.Graphics2D;import java.awt.GridBagConstraints;import java.awt.GridBagLayout;import java.awt.Insets;import java.awt.Point;import java.awt.Rectangle;import java.awt.RenderingHints;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.MouseEvent;import java.awt.geom.AffineTransform;import java.util.ArrayList;import java.util.List;import javax.swing.BorderFactory;import javax.swing.Icon;import javax.swing.JButton;import javax.swing.JMenuItem;import javax.swing.JPanel;import javax.swing.JPopupMenu;import javax.swing.JViewport;import javax.swing.SwingConstants;import javax.swing.SwingUtilities;import javax.swing.ToolTipManager;import javax.swing.UIManager;import javax.swing.event.ChangeEvent;import javax.swing.event.ChangeListener;import javax.swing.event.MouseInputListener;/* ---------------------------------------------------------- * CVS NOTE: Changes to the CVS repository prior to the * release of version 3.0.0beta1 has meant a * resetting of CVS revision numbers. * ---------------------------------------------------------- *//** * Central tab pane with scroll and menu buttons. * * @author Takis Diakoumis * @version $Revision: 1.7 $ * @date $Date: 2006/08/11 12:33:14 $ */public class ScrollingTabPane extends AbstractTabPane implements SwingConstants { /** the tab panel */ private TabPanel tabPanel; /** the viewport for tab scrolling */ private ScrollableTabViewport viewport; /** the tab scrolling panel */ private JPanel scrollingPanel; /** the scroll button panel */ private ScrollButtonPanel scrollButtonPanel; /** the tab popup menu */ private TabPopupMenu tabPopupMenu; /** the tab selection popup menu */ private TabSelectionPopupMenu tabSelectionPopupMenu; /** * Creates a new instance of DockedTabPane with * the specified parent container * * @param the enclosing tab container */ public ScrollingTabPane(DockedTabContainer parent) { setLayout(new BorderLayout()); this.parent = parent; isFocusedTabPane = true; init(); // change the focus on all other panes so this one has focus parent.tabPaneFocusChange(this); } /** Initialises the state of this object */ private void init() { super.initComponents(); // panel where actual tabs are drawn tabPanel = new TabPanel(); viewport = new ScrollableTabViewport(); viewport.setView(tabPanel); scrollButtonPanel = new ScrollButtonPanel(); scrollingPanel = new JPanel(new BorderLayout()); scrollingPanel.add(viewport, BorderLayout.CENTER); scrollingPanel.add(scrollButtonPanel, BorderLayout.EAST); setTabPanel(scrollingPanel); add(componentPanel, BorderLayout.CENTER); setBorder(BorderFactory.createLineBorder(tabPanel.controlShadow)); viewport.movePanel(0); } /** * Indicates a top-level focus change. */ protected void focusChanged() { tabPanel.repaint(); scrollButtonPanel.repaint(); } /** * Sets the title at index to title which can be null. * An internal exception is raised if there is no tab at that index. * * @param the tab index where the title should be set * @param the title to be displayed in the tab */ public void setTabTitleAt(int index, String title) { super.setTabTitleAt(index, title); TabComponent tabComponent = components.get(index); if (tabComponent.getTitleSuffix() != null) { title += tabComponent.getTitleSuffix(); } tabSelectionPopupMenu.renameTabMenuItem(index, title); tabPanel.repaint(); } /** * Adds the specified tab component to the pane. * * @param the component to be added */ public void addTab(TabComponent tabComponent) { if (components.size() == 0) { tabPanel.setVisible(true); componentPanel.setVisible(true); } components.add(tabComponent); // make sure the title is unique String suffix = getTitleSuffix(tabComponent); if (suffix != null) { tabComponent.setTitleSuffix(suffix); } Component component = tabComponent.getComponent(); String layoutName = tabComponent.getLayoutName(); componentPanel.add(component, layoutName); cardLayout.addLayoutComponent(component, layoutName); if (components.size() == 1) { setSelectedTab(tabComponent); } if (tabSelectionPopupMenu == null) { tabSelectionPopupMenu = new TabSelectionPopupMenu(); } tabSelectionPopupMenu.addTabMenuItem(tabComponent); scrollButtonPanel.enableButton(SOUTH, true); } /** * Returns whether the specified x-y coordinates * intersect the tab area (tab panel). * * @param the x coordinate * @param the x coordinate * @return true if the point intersects, false otherwise */ protected boolean intersectsTabArea(int x, int y) { return tabPanel.getBounds().contains(x, y); } /** * Calculates the close icon rectangle for the specified * tab bounds. * * @param the tab bounds * @return the close icon bounds */ private Rectangle getCloseIconRectangle(Rectangle tabRect) { int y = tabRect.y + ((tabRect.height - TabControlIcon.ICON_HEIGHT) / 2); int x = tabRect.x + tabRect.width - TabControlIcon.ICON_WIDTH - 6; return new Rectangle(x, y, TabControlIcon.ICON_WIDTH, TabControlIcon.ICON_HEIGHT); } /** * Returns the tab bounds at the specified x-y coordinate * * @param the x coordinate * @param the x coordinate * @return the tab bounds */ public Rectangle getBoundsAt(int x, int y) { int index = getTabAtLocation(x, y); if (index == -1) { return null; } return tabRects[index]; } /** * Returns the index in the tab pane of the specified * tab rectangle. if the specified tab rectangle is * not in the tab pane, -1 is returned. * * @param the tab rectangle * @return the index of the tab rectangle or -1 if its * not present */ protected int getTabRectangleIndex(Rectangle tabRect) { for (int i = 0; i < tabRects.length; i++) { if (tabRects[i] == tabRect) { return i; } } return -1; } /** * Returns the tab component object at the * specified x and y coordinate. * * @param x coordinate * @param y coordinate * @return the component index at the x-y coords */ protected int getTabAtLocation(int x, int y) { for (int i = 0; i < tabRects.length; i++) { if (tabRects[i].contains(x, y)) { return i; } } return -1; } /** * Returns the tab count for this component. * * @return the tab count */ public int getTabCount() { if (components != null) { return components.size(); } return 0; } /** * Sets the selected index to that specified. * * @param the index to set selected */ public void setSelectedIndex(int index) { if (selectedIndex != -1) { // fire the deselected event // ------------ // the selected index may however be invalid // after a close others command - so check if (selectedIndex < components.size()) { TabComponent tabComponent = components.get(selectedIndex); if (tabComponent.getComponent() instanceof TabView) { TabView dockedView = (TabView)tabComponent.getComponent(); if (dockedView.tabViewDeselected()) { fireTabDeselected(new DockedTabEvent(tabComponent)); } else { return; } } } } selectedIndex = index; TabComponent tabComponent = components.get(index); cardLayout.show(componentPanel, tabComponent.getLayoutName()); // make sure we can see the selected tab in full ensureIndexVisible(index); /* // make sure we can see the selected tab in full tabPanel.calculateTabRects(components.size()); Rectangle tabRect = tabRects[index]; Rectangle viewRect = new Rectangle( viewport.getViewPosition().x, 0, viewport.getWidth(), viewport.getHeight()); if (!viewRect.contains(tabRect)) { int x = ((tabRect.x - 5) < 0) ? 0 : (tabRect.x - 5); viewport.setViewPosition(new Point(x, 0)); } */ viewport.validate(); viewport.repaint(); tabPanel.repaint(); focusGained(); fireTabSelected(new DockedTabEvent(components.get(index))); } /** * Ensures the visibility of the tab at the specified * index within the scrolling viewport. * * @param the tab index */ protected void ensureIndexVisible(int index) { // make sure we can see the selected tab in full tabPanel.calculateTabRects(components.size()); Rectangle tabRect = tabRects[index]; Rectangle viewRect = new Rectangle( viewport.getViewPosition().x, 0, viewport.getWidth(), viewport.getHeight()); // check if all the tabs fit in the view if (tabPanel.getWidth() <= viewRect.width) { viewport.setViewPosition(new Point(0, 0)); } // otherwise just make sure the index is visible else if (!viewRect.contains(tabRect)) { int x = ((tabRect.x - 5) < 0) ? 0 : (tabRect.x - 5); viewport.setViewPosition(new Point(x, 0)); } } /** * Removes all tab components except that at the * specified index. * * @param the tab index NOT to remove */ protected void removeOthers(int exceptIndex) { // populate a temp list int count = components.size(); String[] names = new String[count]; for (int i = 0; i < count; i++) { if (i != exceptIndex) { names[i] = components.get(i).getDisplayName(); } } // work on the temp list and remove as required for (int i = 0; i < count; i++) { if (names[i] != null) { closeTabComponent(names[i]); } } names = null; viewport.setViewPosition(new Point(0, 0)); } /** * Overide to do nothing. */ protected void fireTabMinimised(DockedTabEvent e) {} /** * Overide to do nothing. */ protected void fireTabRestored(DockedTabEvent e) {} /** * Removes all tab components from this panel */ protected void removeAllTabs() { // populate a temp list int count = components.size(); String[] names = new String[count]; for (int i = 0; i < count; i++) { names[i] = components.get(i).getDisplayName(); } // work on the temp list and remove as required for (int i = 0; i < count; i++) { closeTabComponent(names[i]); } names = null; if (components.size() == 0) { viewport.setViewPosition(new Point(0, 0)); viewport.validate(); viewport.repaint(); } } /** * Cleanup method following removal of all tabs. */ private void allTabsRemoved() { tabPanel.setVisible(false); componentPanel.setVisible(false); selectedIndex = -1; tabSelectionPopupMenu.removeAll(); scrollButtonPanel.enableButton(SOUTH, false); scrollButtonPanel.repaint(); } /** * 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; } // remove from the component cache TabComponent removed = components.get(index); if (!okToClose(removed)) { return; } // remove from the component cache components.remove(index); // remove from the layout cardLayout.removeLayoutComponent(removed.getComponent()); // remove from the base panel componentPanel.remove(removed.getComponent());
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -