synthtabbedpaneui.java
来自「java jdk 1.4的源码」· Java 代码 · 共 1,888 行 · 第 1/5 页
JAVA
1,888 行
return new TabSelectionHandler(); } protected PropertyChangeListener createPropertyChangeListener() { return new PropertyChangeHandler(); } protected void installKeyboardActions() { InputMap km = getInputMap(JComponent. WHEN_ANCESTOR_OF_FOCUSED_COMPONENT); SwingUtilities.replaceUIInputMap(tabPane, JComponent. WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, km); km = getInputMap(JComponent.WHEN_FOCUSED); SwingUtilities.replaceUIInputMap(tabPane, JComponent.WHEN_FOCUSED, km); LazyActionMap.installLazyActionMap(tabPane, SynthTabbedPaneUI.class, "TabbedPane.actionMap"); if (scrollableTabLayoutEnabled()) { tabScroller.scrollForwardButton.setAction( scrollTabsForwardAction); tabScroller.scrollBackwardButton.setAction( scrollTabsBackwardAction); } } InputMap getInputMap(int condition) { SynthContext context = getContext(tabPane, ENABLED); InputMap map = null; if (condition == JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT) { map = (InputMap)context.getStyle().get(context, "TabbedPane.ancestorInputMap"); } else if (condition == JComponent.WHEN_FOCUSED) { map = (InputMap)context.getStyle().get(context, "TabbedPane.focusInputMap"); } context.dispose(); return map; } protected void uninstallKeyboardActions() { SwingUtilities.replaceUIActionMap(tabPane, null); SwingUtilities.replaceUIInputMap(tabPane, JComponent. WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, null); SwingUtilities.replaceUIInputMap(tabPane, JComponent.WHEN_FOCUSED, null); } /** * Reloads the mnemonics. This should be invoked when a memonic changes, * when the title of a mnemonic changes, or when tabs are added/removed. */ private void updateMnemonics() { resetMnemonics(); for (int counter = tabPane.getTabCount() - 1; counter >= 0; counter--) { int mnemonic = tabPane.getMnemonicAt(counter); if (mnemonic > 0) { addMnemonic(counter, mnemonic); } } } /** * Resets the mnemonics bindings to an empty state. */ private void resetMnemonics() { if (mnemonicToIndexMap != null) { mnemonicToIndexMap.clear(); mnemonicInputMap.clear(); } } /** * Adds the specified mnemonic at the specified index. */ private void addMnemonic(int index, int mnemonic) { if (mnemonicToIndexMap == null) { initMnemonics(); } mnemonicInputMap.put(KeyStroke.getKeyStroke(mnemonic, Event.ALT_MASK), "setSelectedIndex"); mnemonicToIndexMap.put(new Integer(mnemonic), new Integer(index)); } /** * Installs the state needed for mnemonics. */ private void initMnemonics() { mnemonicToIndexMap = new Hashtable(); mnemonicInputMap = new InputMapUIResource(); mnemonicInputMap.setParent(SwingUtilities.getUIInputMap(tabPane, JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT)); SwingUtilities.replaceUIInputMap(tabPane, JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, mnemonicInputMap); }// Geometry public Dimension getPreferredSize(JComponent c) { // Default to LayoutManager's preferredLayoutSize return null; } public Dimension getMinimumSize(JComponent c) { // Default to LayoutManager's minimumLayoutSize return null; } public Dimension getMaximumSize(JComponent c) { // Default to LayoutManager's maximumLayoutSize return null; } private void repaintTab(int index) { } private void updateMouseOver(int x, int y) { setMouseOverTab(getTabAtLocation(x, y)); } private void setMouseOverTab(int index) { if (index != mouseIndex) { if (mouseBounds != null) { tabPane.repaint(mouseBounds); } mouseIndex = index; if (index != -1 && index < tabPane.getTabCount()) { mouseBounds = tabPane.getBoundsAt(index); if (mouseBounds != null) { tabPane.repaint(mouseBounds); } } else { mouseBounds = null; } } }// UI Rendering public SynthContext getContext(JComponent c) { return getContext(c, getComponentState(c)); } public SynthContext getContext(JComponent c, int state) { return SynthContext.getContext(SynthContext.class, c, SynthLookAndFeel.getRegion(c),style, state); } public SynthContext getContext(JComponent c, Region subregion) { return getContext(c, subregion, getComponentState(c)); } private SynthContext getContext(JComponent c, Region subregion, int state){ SynthStyle style = null; Class klass = SynthContext.class; if (subregion == Region.TABBED_PANE_TAB) { style = tabStyle; klass = TabContext.class; } else if (subregion == Region.TABBED_PANE_TAB_AREA) { style = tabAreaStyle; } else if (subregion == Region.TABBED_PANE_CONTENT) { style = tabContentStyle; } return SynthContext.getContext(klass, c, subregion, style, state); } private Region getRegion(JComponent c) { return SynthLookAndFeel.getRegion(c); } private int getComponentState(JComponent c) { return SynthLookAndFeel.getComponentState(c); } public void update(Graphics g, JComponent c) { SynthContext context = getContext(c); SynthLookAndFeel.update(context, g); paint(context, g); context.dispose(); } public void paint(Graphics g, JComponent c) { SynthContext context = getContext(c); paint(context, g); context.dispose(); } protected void paint(SynthContext context, Graphics g) { int tc = tabPane.getTabCount(); if (tabCount != tc) { tabCount = tc; updateMnemonics(); } int selectedIndex = tabPane.getSelectedIndex(); int tabPlacement = tabPane.getTabPlacement(); ensureCurrentLayout(); // Paint tab area // If scrollable tabs are enabled, the tab area will be // painted by the scrollable tab panel instead. // if (!scrollableTabLayoutEnabled()) { // WRAP_TAB_LAYOUT Insets insets = tabPane.getInsets(); int x = insets.left; int y = insets.top; int width = tabPane.getWidth() - insets.left - insets.right; int height = tabPane.getHeight() - insets.top - insets.bottom; int size; switch(tabPlacement) { case LEFT: width = calculateTabAreaWidth(tabPlacement, runCount, maxTabWidth); break; case RIGHT: size = calculateTabAreaWidth(tabPlacement, runCount, maxTabWidth); x = x + width - size; width = size; break; case BOTTOM: size = calculateTabAreaHeight(tabPlacement, runCount, maxTabHeight); y = y + height - size; height = size; break; case TOP: default: height = calculateTabAreaHeight(tabPlacement, runCount, maxTabHeight); } paintTabArea(tabAreaContext, g, tabPlacement, selectedIndex, new Rectangle(x, y, width, height)); } // Paint content border paintContentBorder(tabContentContext, g, tabPlacement, selectedIndex); } /** * Paints the tabs in the tab area. * Invoked by paint(). * The graphics parameter must be a valid <code>Graphics</code> * object. Tab placement may be either: * <code>JTabbedPane.TOP</code>, <code>JTabbedPane.BOTTOM</code>, * <code>JTabbedPane.LEFT</code>, or <code>JTabbedPane.RIGHT</code>. * The selected index must be a valid tabbed pane tab index (0 to * tab count - 1, inclusive) or -1 if no tab is currently selected. * The handling of invalid parameters is unspecified. * * @param g the graphics object to use for rendering * @param tabPlacement the placement for the tabs within the JTabbedPane * @param selectedIndex the tab index of the selected component * * @since 1.4 */ protected void paintTabArea(SynthContext ss, Graphics g, int tabPlacement, int selectedIndex, Rectangle tabAreaBounds) { // Paint the tab area. SynthLookAndFeel.updateSubregion(ss, g, tabAreaBounds); int tabCount = tabPane.getTabCount(); Rectangle iconRect = new Rectangle(), textRect = new Rectangle(); Rectangle clipRect = g.getClipBounds(); // Paint tabRuns of tabs from back to front for (int i = runCount - 1; i >= 0; i--) { int start = tabRuns[i]; int next = tabRuns[(i == runCount - 1)? 0 : i + 1]; int end = (next != 0? next - 1: tabCount - 1); for (int j = start; j <= end; j++) { if (rects[j].intersects(clipRect) && selectedIndex != j) { paintTab(tabContext, g, tabPlacement, rects, j, iconRect, textRect); } } } if (selectedIndex >= 0) { if (rects[selectedIndex].intersects(clipRect)) { paintTab(tabContext, g, tabPlacement, rects, selectedIndex, iconRect, textRect); } } } protected void paintTab(SynthContext ss, Graphics g, int tabPlacement, Rectangle[] rects, int tabIndex, Rectangle iconRect, Rectangle textRect) { Rectangle tabRect = rects[tabIndex]; int selectedIndex = tabPane.getSelectedIndex(); boolean isSelected = selectedIndex == tabIndex; tabContext.update(tabIndex, isSelected, (mouseIndex == tabIndex), (focusIndex == tabIndex)); SynthLookAndFeel.updateSubregion(ss, g, tabRect); String title = tabPane.getTitleAt(tabIndex); Font font = ss.getStyle().getFont(ss); FontMetrics metrics = g.getFontMetrics(font); Icon icon = getIconForTab(tabIndex); layoutLabel(ss, tabPlacement, metrics, tabIndex, title, icon, tabRect, iconRect, textRect, isSelected); paintText(ss, g, tabPlacement, font, metrics, tabIndex, title, textRect, isSelected); paintIcon(g, tabPlacement, tabIndex, icon, iconRect, isSelected); } protected void layoutLabel(SynthContext ss, int tabPlacement, FontMetrics metrics, int tabIndex, String title, Icon icon, Rectangle tabRect, Rectangle iconRect, Rectangle textRect, boolean isSelected ) { View v = getTextViewForTab(tabIndex); if (v != null) { tabPane.putClientProperty("html", v); } textRect.x = textRect.y = iconRect.x = iconRect.y = 0; ss.getStyle().getSynthGraphics(ss).layoutText(ss, metrics, title, icon, SwingUtilities.CENTER, SwingUtilities.CENTER, SwingUtilities.LEADING, SwingUtilities.TRAILING, tabRect, iconRect, textRect, getTextIconGap()); tabPane.putClientProperty("html", null); // PENDING: Can these be nuked? Don't fit well... // If this is necessary, they should be in a special TextLayout // that special cases this behavior. int xNudge = getTabLabelShiftX(tabPlacement, tabIndex, isSelected); int yNudge = getTabLabelShiftY(tabPlacement, tabIndex, isSelected); iconRect.x += xNudge; iconRect.y += yNudge; textRect.x += xNudge; textRect.y += yNudge; } protected void paintIcon(Graphics g, int tabPlacement, int tabIndex, Icon icon, Rectangle iconRect, boolean isSelected ) { if (icon != null) { icon.paintIcon(tabPane, g, iconRect.x, iconRect.y); } } protected void paintText(SynthContext ss, Graphics g, int tabPlacement, Font font, FontMetrics metrics, int tabIndex, String title, Rectangle textRect, boolean isSelected) { g.setFont(font); View v = getTextViewForTab(tabIndex); if (v != null) {
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?