📄 basictabbedpaneui.java
字号:
/* BasicTabbedPaneUI.java -- Copyright (C) 2002, 2004, 2005 Free Software Foundation, Inc.This file is part of GNU Classpath.GNU Classpath is free software; you can redistribute it and/or modifyit under the terms of the GNU General Public License as published bythe Free Software Foundation; either version 2, or (at your option)any later version.GNU Classpath is distributed in the hope that it will be useful, butWITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNUGeneral Public License for more details.You should have received a copy of the GNU General Public Licensealong with GNU Classpath; see the file COPYING. If not, write to theFree Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA02110-1301 USA.Linking this library statically or dynamically with other modules ismaking a combined work based on this library. Thus, the terms andconditions of the GNU General Public License cover the wholecombination.As a special exception, the copyright holders of this library give youpermission to link this library with independent modules to produce anexecutable, regardless of the license terms of these independentmodules, and to copy and distribute the resulting executable underterms of your choice, provided that you also meet, for each linkedindependent module, the terms and conditions of the license of thatmodule. An independent module is a module which is not derived fromor based on this library. If you modify this library, you may extendthis exception to your version of the library, but you are notobligated to do so. If you do not wish to do so, delete thisexception statement from your version. */package javax.swing.plaf.basic;import java.awt.Color;import java.awt.Component;import java.awt.Container;import java.awt.Dimension;import java.awt.Font;import java.awt.FontMetrics;import java.awt.Graphics;import java.awt.Insets;import java.awt.LayoutManager;import java.awt.Point;import java.awt.Rectangle;import java.awt.event.FocusAdapter;import java.awt.event.FocusEvent;import java.awt.event.FocusListener;import java.awt.event.MouseAdapter;import java.awt.event.MouseEvent;import java.awt.event.MouseListener;import java.beans.PropertyChangeEvent;import java.beans.PropertyChangeListener;import javax.swing.Icon;import javax.swing.JComponent;import javax.swing.JPanel;import javax.swing.JTabbedPane;import javax.swing.JViewport;import javax.swing.KeyStroke;import javax.swing.LookAndFeel;import javax.swing.SwingConstants;import javax.swing.SwingUtilities;import javax.swing.UIManager;import javax.swing.event.ChangeEvent;import javax.swing.event.ChangeListener;import javax.swing.plaf.ComponentUI;import javax.swing.plaf.PanelUI;import javax.swing.plaf.TabbedPaneUI;import javax.swing.plaf.UIResource;import javax.swing.text.View;/** * This is the Basic Look and Feel's UI delegate for JTabbedPane. */public class BasicTabbedPaneUI extends TabbedPaneUI implements SwingConstants{ /** * A helper class that handles focus. * * @specnote Apparently this class was intended to be protected, * but was made public by a compiler bug and is now * public for compatibility. */ public class FocusHandler extends FocusAdapter { /** * This method is called when the component gains focus. * * @param e The FocusEvent. */ public void focusGained(FocusEvent e) { // FIXME: Implement. } /** * This method is called when the component loses focus. * * @param e The FocusEvent. */ public void focusLost(FocusEvent e) { // FIXME: Implement. } } /** * A helper class for determining if mouse presses occur inside tabs and * sets the index appropriately. In SCROLL_TAB_MODE, this class also * handles the mouse clicks on the scrolling buttons. * * @specnote Apparently this class was intended to be protected, * but was made public by a compiler bug and is now * public for compatibility. */ public class MouseHandler extends MouseAdapter { /** * This method is called when the mouse is pressed. The index cannot * change to a tab that is not enabled. * * @param e The MouseEvent. */ public void mousePressed(MouseEvent e) { int x = e.getX(); int y = e.getY(); int tabCount = tabPane.getTabCount(); if (tabPane.getTabLayoutPolicy() == JTabbedPane.SCROLL_TAB_LAYOUT) { if (e.getSource() == incrButton) { if (++currentScrollLocation >= tabCount) currentScrollLocation = tabCount - 1; int width = 0; for (int i = currentScrollLocation - 1; i < tabCount; i++) width += rects[i].width; if (width < viewport.getWidth()) // FIXME: Still getting mouse events after the button is disabled. // incrButton.setEnabled(false); currentScrollLocation--; else if (! decrButton.isEnabled()) decrButton.setEnabled(true); tabPane.revalidate(); tabPane.repaint(); return; } else if (e.getSource() == decrButton) { if (--currentScrollLocation < 0) currentScrollLocation = 0; if (currentScrollLocation == 0) decrButton.setEnabled(false); else if (! incrButton.isEnabled()) incrButton.setEnabled(true); tabPane.revalidate(); tabPane.repaint(); return; } } int index = tabForCoordinate(tabPane, x, y); // We need to check since there are areas where tabs cannot be // e.g. in the inset area. if (index != -1 && tabPane.isEnabledAt(index)) tabPane.setSelectedIndex(index); tabPane.revalidate(); tabPane.repaint(); } } /** * This class handles PropertyChangeEvents fired from the JTabbedPane. * * @specnote Apparently this class was intended to be protected, * but was made public by a compiler bug and is now * public for compatibility. */ public class PropertyChangeHandler implements PropertyChangeListener { /** * This method is called whenever one of the properties of the JTabbedPane * changes. * * @param e The PropertyChangeEvent. */ public void propertyChange(PropertyChangeEvent e) { if (e.getPropertyName().equals("tabLayoutPolicy")) { layoutManager = createLayoutManager(); tabPane.setLayout(layoutManager); } else if (e.getPropertyName().equals("tabPlacement") && tabPane.getTabLayoutPolicy() == JTabbedPane.SCROLL_TAB_LAYOUT) { incrButton = createIncreaseButton(); decrButton = createDecreaseButton(); } tabPane.layout(); tabPane.repaint(); } } /** * A LayoutManager responsible for placing all the tabs and the visible * component inside the JTabbedPane. This class is only used for * WRAP_TAB_LAYOUT. * * @specnote Apparently this class was intended to be protected, * but was made public by a compiler bug and is now * public for compatibility. */ public class TabbedPaneLayout implements LayoutManager { /** * This method is called when a component is added to the JTabbedPane. * * @param name The name of the component. * @param comp The component being added. */ public void addLayoutComponent(String name, Component comp) { // Do nothing. } /** * This method is called when the rectangles need to be calculated. It * also fixes the size of the visible component. */ public void calculateLayoutInfo() { calculateTabRects(tabPane.getTabPlacement(), tabPane.getTabCount()); if (tabPane.getSelectedIndex() != -1) { Component visible = getVisibleComponent(); Insets insets = getContentBorderInsets(tabPane.getTabPlacement()); if (visible != null) visible.setBounds(contentRect.x + insets.left, contentRect.y + insets.top, contentRect.width - insets.left - insets.right, contentRect.height - insets.top - insets.bottom); } } /** * This method calculates the size of the the JTabbedPane. * * @param minimum Whether the JTabbedPane will try to be as small as it * can. * * @return The desired size of the JTabbedPane. */ protected Dimension calculateSize(boolean minimum) { int tabPlacement = tabPane.getTabPlacement(); int width = 0; int height = 0; int componentHeight = 0; int componentWidth = 0; Component c; Dimension dims; for (int i = 0; i < tabPane.getTabCount(); i++) { c = tabPane.getComponentAt(i); if (c == null) continue; calcRect = c.getBounds(); dims = c.getPreferredSize(); if (dims != null) { componentHeight = Math.max(componentHeight, dims.height); componentWidth = Math.max(componentWidth, dims.width); } } Insets insets = tabPane.getInsets(); if (tabPlacement == SwingConstants.TOP || tabPlacement == SwingConstants.BOTTOM) { int min = calculateMaxTabWidth(tabPlacement); width = Math.max(min, componentWidth); int tabAreaHeight = preferredTabAreaHeight(tabPlacement, width); height = tabAreaHeight + componentHeight; } else { int min = calculateMaxTabHeight(tabPlacement); height = Math.max(min, componentHeight); int tabAreaWidth = preferredTabAreaWidth(tabPlacement, height); width = tabAreaWidth + componentWidth; } return new Dimension(width, height); } // if tab placement is LEFT OR RIGHT, they share width. // if tab placement is TOP OR BOTTOM, they share height // PRE STEP: finds the default sizes for the labels as well as their locations. // AND where they will be placed within the run system. // 1. calls normalizeTab Runs. // 2. calls rotate tab runs. // 3. pads the tab runs. // 4. pads the selected tab. /** * This method is called to calculate the tab rectangles. This method * will calculate the size and position of all rectangles (taking into * account which ones should be in which tab run). It will pad them and * normalize them as necessary. * * @param tabPlacement The JTabbedPane's tab placement. * @param tabCount The run the current selection is in. */ protected void calculateTabRects(int tabPlacement, int tabCount) { if (tabCount == 0) return; assureRectsCreated(tabCount); FontMetrics fm = getFontMetrics(); SwingUtilities.calculateInnerArea(tabPane, calcRect); Insets tabAreaInsets = getTabAreaInsets(tabPlacement); Insets insets = tabPane.getInsets(); int max = 0; int runs = 0; int start = getTabRunIndent(tabPlacement, 1); if (tabPlacement == SwingConstants.TOP || tabPlacement == SwingConstants.BOTTOM) { int maxHeight = calculateMaxTabHeight(tabPlacement); calcRect.width -= tabAreaInsets.left + tabAreaInsets.right; max = calcRect.width + tabAreaInsets.left + insets.left; start += tabAreaInsets.left + insets.left; int width = 0; int runWidth = start; for (int i = 0; i < tabCount; i++) { width = calculateTabWidth(tabPlacement, i, fm); if (runWidth + width > max) { runWidth = tabAreaInsets.left + insets.left + getTabRunIndent(tabPlacement, ++runs); rects[i] = new Rectangle(runWidth, insets.top + tabAreaInsets.top, width, maxHeight); runWidth += width; if (runs > tabRuns.length - 1) expandTabRunsArray(); tabRuns[runs] = i; } else { rects[i] = new Rectangle(runWidth, insets.top + tabAreaInsets.top, width, maxHeight); runWidth += width; } } runs++; tabAreaRect.width = tabPane.getWidth() - insets.left - insets.right; tabAreaRect.height = runs * maxTabHeight - (runs - 1) * tabRunOverlay + tabAreaInsets.top + tabAreaInsets.bottom; contentRect.width = tabAreaRect.width; contentRect.height = tabPane.getHeight() - insets.top - insets.bottom - tabAreaRect.height; contentRect.x = insets.left; tabAreaRect.x = insets.left; if (tabPlacement == SwingConstants.BOTTOM) { contentRect.y = insets.top; tabAreaRect.y = contentRect.y + contentRect.height; } else { tabAreaRect.y = insets.top; contentRect.y = tabAreaRect.y + tabAreaRect.height; } } else
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -