basictabbedpaneui.java
来自「linux下建立JAVA虚拟机的源码KAFFE」· Java 代码 · 共 2,001 行 · 第 1/5 页
JAVA
2,001 行
/* 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) { if (tabPane.isEnabled()) { int index = tabForCoordinate(tabPane, e.getX(), e.getY()); if (index >= 0 && tabPane.isEnabledAt(index)) { tabPane.setSelectedIndex(index); } } } /** * Receives notification when the mouse pointer has entered the tabbed * pane. * * @param ev the mouse event */ public void mouseEntered(MouseEvent ev) { int tabIndex = tabForCoordinate(tabPane, ev.getX(), ev.getY()); setRolloverTab(tabIndex); } /** * Receives notification when the mouse pointer has exited the tabbed * pane. * * @param ev the mouse event */ public void mouseExited(MouseEvent ev) { setRolloverTab(-1); } /** * Receives notification when the mouse pointer has moved over the tabbed * pane. * * @param ev the mouse event */ public void mouseMoved(MouseEvent ev) { int tabIndex = tabForCoordinate(tabPane, ev.getX(), ev.getY()); setRolloverTab(tabIndex); } } /** * 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.revalidate(); 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() { int count = tabPane.getTabCount(); assureRectsCreated(count); calculateTabRects(tabPane.getTabPlacement(), count); tabRunsDirty = false; } /** * 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; Component c; Dimension dims; // Find out the minimum/preferred size to display the largest child // of the tabbed pane. for (int i = 0; i < tabPane.getTabCount(); i++) { c = tabPane.getComponentAt(i); if (c == null) continue; dims = minimum ? c.getMinimumSize() : c.getPreferredSize(); if (dims != null) { height = Math.max(height, dims.height); width = Math.max(width, dims.width); } } Insets tabAreaInsets = getTabAreaInsets(tabPlacement); if (tabPlacement == SwingConstants.TOP || tabPlacement == SwingConstants.BOTTOM) { int min = calculateMaxTabWidth(tabPlacement); width = Math.max(min, width); int tabAreaHeight = preferredTabAreaHeight(tabPlacement, width - tabAreaInsets.left -tabAreaInsets.right); height += tabAreaHeight; } else { int min = calculateMaxTabHeight(tabPlacement); height = Math.max(min, height); int tabAreaWidth = preferredTabAreaWidth(tabPlacement, height - tabAreaInsets.top - tabAreaInsets.bottom); width += tabAreaWidth; } Insets tabPaneInsets = tabPane.getInsets(); return new Dimension(width + tabPaneInsets.left + tabPaneInsets.right, height + tabPaneInsets.top + tabPaneInsets.bottom); } // 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) { Insets insets = tabPane.getInsets(); Insets tabAreaInsets = getTabAreaInsets(tabPlacement); Dimension size = tabPane.getSize(); // The coordinates of the upper left corner of the tab area. int x; int y; // The location at which the runs must be broken. int breakAt; // Calculate the bounds for the tab area. switch (tabPlacement) { case LEFT: maxTabWidth = calculateMaxTabWidth(tabPlacement); x = insets.left + tabAreaInsets.left; y = insets.top + tabAreaInsets.top; breakAt = size.height - (insets.bottom + tabAreaInsets.bottom); break; case RIGHT: maxTabWidth = calculateMaxTabWidth(tabPlacement); x = size.width - (insets.right + tabAreaInsets.right) - maxTabWidth; y = insets.top + tabAreaInsets.top; breakAt = size.height - (insets.bottom + tabAreaInsets.bottom); break; case BOTTOM: maxTabHeight = calculateMaxTabHeight(tabPlacement); x = insets.left + tabAreaInsets.left; y = size.height - (insets.bottom + tabAreaInsets.bottom) - maxTabHeight; breakAt = size.width - (insets.right + tabAreaInsets.right); break; case TOP: default: maxTabHeight = calculateMaxTabHeight(tabPlacement); x = insets.left + tabAreaInsets.left; y = insets.top + tabAreaInsets.top; breakAt = size.width - (insets.right + tabAreaInsets.right); break; } if (tabCount == 0) return; FontMetrics fm = getFontMetrics(); runCount = 0; selectedRun = -1; int selectedIndex = tabPane.getSelectedIndex(); Rectangle rect; // Go through all the tabs and build the tab runs. if (tabPlacement == SwingConstants.TOP || tabPlacement == SwingConstants.BOTTOM) { for (int i = 0; i < tabCount; i++) { rect = rects[i]; if (i > 0) { rect.x = rects[i - 1].x + rects[i - 1].width; } else { tabRuns[0] = 0; runCount = 1; maxTabWidth = 0; rect.x = x; } rect.width = calculateTabWidth(tabPlacement, i, fm); maxTabWidth = Math.max(maxTabWidth, rect.width); if (rect.x != 2 + insets.left && rect.x + rect.width > breakAt) { if (runCount > tabRuns.length - 1) expandTabRunsArray(); tabRuns[runCount] = i;
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?