📄 tabbedpanel.java
字号:
// Set new selected index if ( selectedIndex != index ) { // Set index selectedIndex = index; // Update selected status of tabs for ( int i = 0; i < elements.size( ); ++i ) elements.get( i ).tab.setSelected( i == selectedIndex ); // Update list of recent elements int recentIndex = recentElements.indexOf( elements.get( selectedIndex ) ); if ( recentIndex > 0 ) recentElements.add( 0, recentElements.remove( recentIndex ) ); // Update components of panel updateComponents( ); // Update start index of tabs updateStartIndex( false ); // Notify listeners of change to selected index fireStateChanged( ); } } //------------------------------------------------------------------ public void addChangeListener( ChangeListener listener ) { changeListeners.add( listener ); } //------------------------------------------------------------------ protected void fireStateChanged( ) { for ( int i = changeListeners.size( ) - 1; i >= 0; --i ) { if ( changeEvent == null ) changeEvent = new ChangeEvent( this ); changeListeners.get( i ).stateChanged( changeEvent ); } } //------------------------------------------------------------------ private Window getWindow( ) { return SwingUtilities.getWindowAncestor( this ); } //------------------------------------------------------------------ private int getMaxTotalTabWidth( ) { return Math.max( 0, getWidth( ) - MIN_HEADER_WIDTH ); } //------------------------------------------------------------------ private int getMaximumStartIndex( ) { int maxTotalTabWidth = getMaxTotalTabWidth( ); int tabWidthSum = 0; int index = elements.size( ); while ( --index >= 0 ) { Tab tab = elements.get( index ).tab; if ( tabWidthSum + Math.min( tab.getPreferredWidth( ), maxTotalTabWidth ) > maxTotalTabWidth ) break; tabWidthSum += tab.getPreferredWidth( ); } ++index; return Math.max( 0, Math.min( index, elements.size( ) - 1 ) ); } //------------------------------------------------------------------ /** * Adjusts the index of the first tab in the tab header so that the selected tab is displayed at its * preferred width. * * @param force if {@code true}, force the index of the first tab to be recalculated; otherwise, adjust * the index only if the selected tab is not displayed in the tab header. */ private void updateStartIndex( boolean force ) { // Ensure that selected index is not beyond tabs int oldStartIndex = startIndex; if ( force || (selectedIndex >= startIndex + numHeaderTabs - 1) ) { startIndex = selectedIndex; int tabWidthSum = 0; int maxTotalTabWidth = getMaxTotalTabWidth( ); while ( startIndex >= 0 ) { Tab tab = elements.get( startIndex ).tab; if ( tabWidthSum + Math.min( tab.getPreferredWidth( ), maxTotalTabWidth ) > maxTotalTabWidth ) break; tabWidthSum += tab.getPreferredWidth( ); --startIndex; } ++startIndex; } // Ensure that start index is not beyond selected index if ( startIndex > selectedIndex ) startIndex = selectedIndex; // Update tabs and buttons if start index has changed if ( startIndex != oldStartIndex ) { updateTabs( ); updateButtons( ); } } //------------------------------------------------------------------ private void updateTabs( ) { // Remove all tabs for ( int i = 0; i < getComponentCount( ); ++i ) { if ( getComponent( i ) instanceof Tab ) { remove( i-- ); --numHeaderTabs; } } // Add tabs addTabs( ); // Revalidate panel and repaint header revalidate( ); repaint( 0, BORDER_TOP, getWidth( ), headerHeight ); } //------------------------------------------------------------------ private void updateButtons( ) { if ( getComponentOrientation( ).isLeftToRight( ) ) { leftScrollButton.setEnabled( startIndex > 0 ); rightScrollButton.setEnabled( startIndex < getMaximumStartIndex( ) ); } else { leftScrollButton.setEnabled( startIndex < getMaximumStartIndex( ) ); rightScrollButton.setEnabled( startIndex > 0 ); } } //------------------------------------------------------------------ private void updateComponents( ) { // Remove all child components removeAll( ); numHeaderTabs = 0; // Add child components if ( !elements.isEmpty( ) ) { // Add tabs addTabs( ); // Add buttons add( leftScrollButton ); add( rightScrollButton ); add( listButton ); // Add main component add( elements.get( selectedIndex ).component ); } // Revalidate and repaint panel revalidate( ); repaint( ); } //------------------------------------------------------------------ private void addTabs( ) { int maxTotalTabWidth = getMaxTotalTabWidth( ); int tabWidthSum = 0; for ( int i = startIndex; i < elements.size( ); ++i ) { Tab tab = elements.get( i ).tab; if ( tabWidthSum + Tab.MIN_WIDTH > maxTotalTabWidth ) break; add( tab, i - startIndex ); ++numHeaderTabs; tabWidthSum += tab.getPreferredWidth( ); } } //------------------------------------------------------------------ private void showTab( int index ) { if ( index == selectedIndex ) updateStartIndex( false ); else setSelectedIndex( index ); } //------------------------------------------------------------------ private void selectTab( Element element ) { if ( element != null ) { int index = elements.indexOf( element ); if ( index >= 0 ) showTab( index ); } } //------------------------------------------------------------------ private Tab createTab( String title, Action closeAction ) { Tab tab = new Tab( title, closeAction ); tab.applyComponentOrientation( getComponentOrientation( ) ); return tab; } //------------------------------------------------------------------ private void createKeySelectionList( boolean decrement ) { if ( elements.size( ) > 1 ) keySelectionListWindow = new KeySelectionListWindow( getWindow( ), decrement ); } //------------------------------------------------------------------ private void destroyKeySelectionList( ) { if ( keySelectionListWindow != null ) { keySelectionListWindow.setVisible( false ); keySelectionListWindow.dispose( ); keySelectionListWindow = null; } } //------------------------------------------------------------------ private void createMouseSelectionList( ) { mouseSelectionListWindow = new MouseSelectionListWindow( getWindow( ) ); } //------------------------------------------------------------------ private void destroyMouseSelectionList( ) { if ( mouseSelectionListWindow != null ) { mouseSelectionListWindow.getToolkit( ).removeAWTEventListener( mouseSelectionListWindow ); mouseSelectionListWindow.setVisible( false ); mouseSelectionListWindow.dispose( ); mouseSelectionListWindow = null; } } //------------------------------------------------------------------ private void startScrollingLeft( ) { scrollDirection = getComponentOrientation( ).isLeftToRight( ) ? ScrollDirection.BACKWARD : ScrollDirection.FORWARD; scrollTimer.start( ); doScroll( ); } //------------------------------------------------------------------ private void startScrollingRight( ) { scrollDirection = getComponentOrientation( ).isLeftToRight( ) ? ScrollDirection.FORWARD : ScrollDirection.BACKWARD; scrollTimer.start( ); doScroll( ); } //------------------------------------------------------------------ private void stopScrolling( ) { scrollTimer.stop( ); } //------------------------------------------------------------------ private void doScroll( ) { switch ( scrollDirection ) { case BACKWARD: { if ( startIndex > 0 ) { if ( --startIndex == 0 ) scrollTimer.stop( ); updateTabs( ); updateButtons( ); } break; } case FORWARD: { int maxStartIndex = getMaximumStartIndex( ); if ( startIndex < maxStartIndex ) { if ( ++startIndex == maxStartIndex ) scrollTimer.stop( ); updateTabs( ); updateButtons( ); } break; } } } //------------------------------------------------------------------ private void doSelectPreviousTab( ) { if ( keySelectionListWindow == null ) createKeySelectionList( true ); else keySelectionListWindow.decrementSelection( ); } //------------------------------------------------------------------ private void doSelectNextTab( ) { if ( keySelectionListWindow == null ) createKeySelectionList( false ); else keySelectionListWindow.incrementSelection( ); } //------------------------------------------------------------------////////////////////////////////////////////////////////////////////////// Instance variables//////////////////////////////////////////////////////////////////////// private ScrollButton leftScrollButton; private ScrollButton rightScrollButton; private ListButton listButton; private List<Element> elements; private List<Element> recentElements; private int startIndex; private int selectedIndex; private int
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -