📄 closetabbedpaneui.java
字号:
} } } protected void uninstallListeners() { if (mouseListener != null) { if (scrollableTabLayoutEnabled()) { // SCROLL_TAB_LAYOUT tabScroller.tabPanel.removeMouseListener(mouseListener); } else { // WRAP_TAB_LAYOUT tabPane.removeMouseListener(mouseListener); } mouseListener = null; } if (focusListener != null) { tabPane.removeFocusListener(focusListener); focusListener = null; } // PENDING(api): See comment for ContainerHandler if (containerListener != null) { tabPane.removeContainerListener(containerListener); containerListener = null; if (htmlViews!=null) { htmlViews.removeAllElements(); htmlViews = null; } } if (tabChangeListener != null) { tabPane.removeChangeListener(tabChangeListener); tabChangeListener = null; } if (propertyChangeListener != null) { tabPane.removePropertyChangeListener(propertyChangeListener); propertyChangeListener = null; } } protected MouseListener createMouseListener() { return handler;//new MouseHandler(); } protected FocusListener createFocusListener() { return handler;//new FocusHandler(); } protected ChangeListener createChangeListener() { return handler;//new TabSelectionHandler(); } protected PropertyChangeListener createPropertyChangeListener() { return handler;//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); ActionMap am = getActionMap(); SwingUtilities.replaceUIActionMap(tabPane, am); /* if (scrollableTabLayoutEnabled()) { tabScroller.scrollForwardButton.setAction(am.get("scrollTabsForwardAction")); tabScroller.scrollBackwardButton.setAction(am.get("scrollTabsBackwardAction")); } */ } InputMap getInputMap(int condition) { if (condition == JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT) { return (InputMap)UIManager.get("TabbedPane.ancestorInputMap"); } else if (condition == JComponent.WHEN_FOCUSED) { return (InputMap)UIManager.get("TabbedPane.focusInputMap"); } return null; } ActionMap getActionMap() { ActionMap map = (ActionMap)UIManager.get("TabbedPane.actionMap"); if (map == null) { map = createActionMap(); if (map != null) { UIManager.getLookAndFeelDefaults().put("TabbedPane.actionMap", map); } } return map; } ActionMap createActionMap() { ActionMap map = new ActionMapUIResource(); map.put("navigateNext", new NextAction()); map.put("navigatePrevious", new PreviousAction()); map.put("navigateRight", new RightAction()); map.put("navigateLeft", new LeftAction()); map.put("navigateUp", new UpAction()); map.put("navigateDown", new DownAction()); map.put("navigatePageUp", new PageUpAction()); map.put("navigatePageDown", new PageDownAction()); map.put("requestFocus", new RequestFocusAction()); map.put("requestFocusForVisibleComponent", new RequestFocusForVisibleAction()); map.put("setSelectedIndex", new SetSelectedIndexAction());// map.put("scrollTabsForwardAction", new ScrollTabsForwardAction());// map.put("scrollTabsBackwardAction",new ScrollTabsBackwardAction()); 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; } // UI Rendering public void paint(Graphics g, JComponent c) { 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 paintTabArea(g, tabPlacement, selectedIndex); } // Paint content border paintContentBorder(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(Graphics g, int tabPlacement, int selectedIndex) { 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++) { // stupid hack - remove all causes index exception try { if (rects[j].intersects(clipRect)) { paintTab(g, tabPlacement, rects, j, iconRect, textRect); } } catch (ArrayIndexOutOfBoundsException e) { break; } } } // Paint selected tab if its in the front run // since it may overlap other tabs if (selectedIndex >= 0 && getRunForTab(tabCount, selectedIndex) == 0) { if (rects[selectedIndex].intersects(clipRect)) { paintTab(g, tabPlacement, rects, selectedIndex, iconRect, textRect); } } } protected void paintTab(Graphics g, int tabPlacement, Rectangle[] rects, int tabIndex, Rectangle iconRect, Rectangle textRect) { Rectangle tabRect = rects[tabIndex]; int selectedIndex = tabPane.getSelectedIndex(); boolean isSelected = selectedIndex == tabIndex; Graphics2D g2 = null; Polygon cropShape = null; Shape save = null; int cropx = 0; int cropy = 0; if (scrollableTabLayoutEnabled()) { if (g instanceof Graphics2D) { g2 = (Graphics2D)g; // Render visual for cropped tab edge... Rectangle viewRect = tabScroller.viewport.getViewRect(); int cropline; switch(tabPlacement) { case LEFT: case RIGHT: cropline = viewRect.y + viewRect.height; if ((tabRect.y < cropline) && (tabRect.y + tabRect.height > cropline)) { cropShape = createCroppedTabClip(tabPlacement, tabRect, cropline); cropx = tabRect.x; cropy = cropline-1; } break; case TOP: case BOTTOM: default: cropline = viewRect.x + viewRect.width; if ((tabRect.x < cropline) && (tabRect.x + tabRect.width > cropline)) { cropShape = createCroppedTabClip(tabPlacement, tabRect, cropline); cropx = cropline-1; cropy = tabRect.y; } } if (cropShape != null) { save = g2.getClip(); g2.clip(cropShape); } } } paintTabBackground(g, tabPlacement, tabIndex, tabRect.x, tabRect.y, tabRect.width, tabRect.height+1, isSelected); paintTabBorder(g, tabPlacement, tabIndex, tabRect.x, tabRect.y, tabRect.width, tabRect.height+1, isSelected); String title = tabPane.getTitleAt(tabIndex); Font font = tabPane.getFont(); FontMetrics metrics = g.getFontMetrics(font); Icon icon = getIconForTab(tabIndex); layoutLabel(tabPlacement, metrics, tabIndex, title, icon, tabRect, iconRect, textRect, isSelected); paintText(g, tabPlacement, font, metrics, tabIndex, title, textRect, isSelected); paintIcon(g, tabPlacement, tabIndex, icon, iconRect, isSelected); if (cropShape != null) { paintCroppedTabEdge(g, tabPlacement, tabIndex, isSelected, cropx, cropy); g2.setClip(save); } Rectangle closeIconRect = getCloseIconRectangle(tabRect); closeIcon.paintIcon(tabPane, g, closeIconRect.x, closeIconRect.y); if (tabIndex == currentCloseRolloverIndex) { g.setColor(Color.GRAY); g.drawRect(closeIconRect.x - 2, closeIconRect.y - 2, closeIconRect.width + 3, closeIconRect.height + 3); } } private Icon closeIcon = new TabCloseIcon();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -