📄 basictabbedpaneui.java
字号:
protected void uninstallDefaults() { highlight = null; lightHighlight = null; shadow = null; darkShadow = null; focus = null; tabInsets = null; selectedTabPadInsets = null; tabAreaInsets = null; contentBorderInsets = null; } protected void installListeners() { if ((propertyChangeListener = createPropertyChangeListener()) != null) { tabPane.addPropertyChangeListener(propertyChangeListener); } if ((tabChangeListener = createChangeListener()) != null) { tabPane.addChangeListener(tabChangeListener); } if ((mouseListener = createMouseListener()) != null) { tabPane.addMouseListener(mouseListener); } tabPane.addMouseMotionListener(getHandler()); if ((focusListener = createFocusListener()) != null) { tabPane.addFocusListener(focusListener); } tabPane.addContainerListener(getHandler()); if (tabPane.getTabCount()>0) { htmlViews = createHTMLVector(); } } protected void uninstallListeners() { if (mouseListener != null) { tabPane.removeMouseListener(mouseListener); mouseListener = null; } tabPane.removeMouseMotionListener(getHandler()); if (focusListener != null) { tabPane.removeFocusListener(focusListener); focusListener = null; } tabPane.removeContainerListener(getHandler()); if (htmlViews!=null) { htmlViews.removeAllElements(); htmlViews = null; } if (tabChangeListener != null) { tabPane.removeChangeListener(tabChangeListener); tabChangeListener = null; } if (propertyChangeListener != null) { tabPane.removePropertyChangeListener(propertyChangeListener); propertyChangeListener = null; } handler = null; } protected MouseListener createMouseListener() { return getHandler(); } protected FocusListener createFocusListener() { return getHandler(); } protected ChangeListener createChangeListener() { return getHandler(); } protected PropertyChangeListener createPropertyChangeListener() { return getHandler(); } private Handler getHandler() { if (handler == null) { handler = new Handler(); } return handler; } 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, BasicTabbedPaneUI.class, "TabbedPane.actionMap"); updateMnemonics(); } InputMap getInputMap(int condition) { if (condition == JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT) { return (InputMap)DefaultLookup.get(tabPane, this, "TabbedPane.ancestorInputMap"); } else if (condition == JComponent.WHEN_FOCUSED) { return (InputMap)DefaultLookup.get(tabPane, this, "TabbedPane.focusInputMap"); } return null; } protected void uninstallKeyboardActions() { SwingUtilities.replaceUIActionMap(tabPane, null); SwingUtilities.replaceUIInputMap(tabPane, JComponent. WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, null); SwingUtilities.replaceUIInputMap(tabPane, JComponent.WHEN_FOCUSED, null); SwingUtilities.replaceUIInputMap(tabPane, JComponent.WHEN_IN_FOCUSED_WINDOW, null); mnemonicToIndexMap = null; mnemonicInputMap = 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 ComponentInputMapUIResource(tabPane); mnemonicInputMap.setParent(SwingUtilities.getUIInputMap(tabPane, JComponent.WHEN_IN_FOCUSED_WINDOW)); SwingUtilities.replaceUIInputMap(tabPane, JComponent.WHEN_IN_FOCUSED_WINDOW, mnemonicInputMap); } /** * Sets the tab the mouse is over by location. This is a cover method * for <code>setRolloverTab(tabForCoordinate(x, y, false))</code>. */ private void setRolloverTab(int x, int y) { // NOTE: // This calls in with false otherwise it could trigger a validate, // which should NOT happen if the user is only dragging the // mouse around. setRolloverTab(tabForCoordinate(tabPane, x, y, false)); } /** * Sets the tab the mouse is currently over to <code>index</code>. * <code>index</code> will be -1 if the mouse is no longer over any * tab. No checking is done to ensure the passed in index identifies a * valid tab. * * @param index Index of the tab the mouse is over. * @since 1.5 */ protected void setRolloverTab(int index) { rolloverTabIndex = index; } /** * Returns the tab the mouse is currently over, or {@code -1} if the mouse is no * longer over any tab. * * @return the tab the mouse is currently over, or {@code -1} if the mouse is no * longer over any tab * @since 1.5 */ protected int getRolloverTab() { return rolloverTabIndex; } public Dimension getMinimumSize(JComponent c) { // Default to LayoutManager's minimumLayoutSize return null; } public Dimension getMaximumSize(JComponent c) { // Default to LayoutManager's maximumLayoutSize return null; } /** * Returns the baseline. * * @throws NullPointerException {@inheritDoc} * @throws IllegalArgumentException {@inheritDoc} * @see javax.swing.JComponent#getBaseline(int, int) * @since 1.6 */ public int getBaseline(JComponent c, int width, int height) { super.getBaseline(c, width, height); int baseline = calculateBaselineIfNecessary(); if (baseline != -1) { int placement = tabPane.getTabPlacement(); Insets insets = tabPane.getInsets(); Insets tabAreaInsets = getTabAreaInsets(placement); switch(placement) { case JTabbedPane.TOP: baseline += insets.top + tabAreaInsets.top; return baseline; case JTabbedPane.BOTTOM: baseline = height - insets.bottom - tabAreaInsets.bottom - maxTabHeight + baseline; return baseline; case JTabbedPane.LEFT: case JTabbedPane.RIGHT: baseline += insets.top + tabAreaInsets.top; return baseline; } } return -1; } /** * Returns an enum indicating how the baseline of the component * changes as the size changes. * * @throws NullPointerException {@inheritDoc} * @see javax.swing.JComponent#getBaseline(int, int) * @since 1.6 */ public Component.BaselineResizeBehavior getBaselineResizeBehavior( JComponent c) { super.getBaselineResizeBehavior(c); switch(tabPane.getTabPlacement()) { case JTabbedPane.LEFT: case JTabbedPane.RIGHT: case JTabbedPane.TOP: return Component.BaselineResizeBehavior.CONSTANT_ASCENT; case JTabbedPane.BOTTOM: return Component.BaselineResizeBehavior.CONSTANT_DESCENT; } return Component.BaselineResizeBehavior.OTHER; } /** * Returns the baseline for the specified tab. * * @param tab index of tab to get baseline for * @exception IndexOutOfBoundsException if index is out of range * (index < 0 || index >= tab count) * @return baseline or a value < 0 indicating there is no reasonable * baseline * @since 1.6 */ protected int getBaseline(int tab) { if (tabPane.getTabComponentAt(tab) != null) { int offset = getBaselineOffset(); if (offset != 0) { // The offset is not applied to the tab component, and so // in general we can't get good alignment like with components // in the tab. return -1; } Component c = tabPane.getTabComponentAt(tab); Dimension pref = c.getPreferredSize(); Insets tabInsets = getTabInsets(tabPane.getTabPlacement(), tab); int cellHeight = maxTabHeight - tabInsets.top - tabInsets.bottom; return c.getBaseline(pref.width, pref.height) + (cellHeight - pref.height) / 2 + tabInsets.top; } else { View view = getTextViewForTab(tab); if (view != null) { int viewHeight = (int)view.getPreferredSpan(View.Y_AXIS); int baseline = BasicHTML.getHTMLBaseline( view, (int)view.getPreferredSpan(View.X_AXIS), viewHeight); if (baseline >= 0) { return maxTabHeight / 2 - viewHeight / 2 + baseline + getBaselineOffset(); } return -1; } } FontMetrics metrics = getFontMetrics(); int fontHeight = metrics.getHeight(); int fontBaseline = metrics.getAscent(); return maxTabHeight / 2 - fontHeight / 2 + fontBaseline + getBaselineOffset(); } /** * Returns the amount the baseline is offset by. This is typically * the same as <code>getTabLabelShiftY</code>. * * @return amount to offset the baseline by * @since 1.6 */ protected int getBaselineOffset() { switch(tabPane.getTabPlacement()) { case JTabbedPane.TOP: if (tabPane.getTabCount() > 1) { return 1; } else { return -1; } case JTabbedPane.BOTTOM: if (tabPane.getTabCount() > 1) { return -1; } else { return 1; } default: // RIGHT|LEFT return (maxTabHeight % 2); } } private int calculateBaselineIfNecessary() { if (!calculatedBaseline) { calculatedBaseline = true; baseline = -1; if (tabPane.getTabCount() > 0) { calculateBaseline(); } } return baseline; } private void calculateBaseline() { int tabCount = tabPane.getTabCount(); int tabPlacement = tabPane.getTabPlacement(); maxTabHeight = calculateMaxTabHeight(tabPlacement); baseline = getBaseline(0); if (isHorizontalTabPlacement()) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -