1. /*
  2. * @(#)BasicTabbedPaneUI.java 1.151 04/06/16
  3. *
  4. * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package javax.swing.plaf.basic;
  8. import com.sun.java.swing.SwingUtilities2;
  9. import javax.swing.*;
  10. import javax.swing.event.*;
  11. import javax.swing.plaf.*;
  12. import javax.swing.text.View;
  13. import java.awt.*;
  14. import java.awt.event.*;
  15. import java.beans.PropertyChangeListener;
  16. import java.beans.PropertyChangeEvent;
  17. import java.util.Vector;
  18. import java.util.Hashtable;
  19. import sun.swing.DefaultLookup;
  20. import sun.swing.UIAction;
  21. /**
  22. * A Basic L&F implementation of TabbedPaneUI.
  23. *
  24. * @version 1.87 06/08/99
  25. * @author Amy Fowler
  26. * @author Philip Milne
  27. * @author Steve Wilson
  28. * @author Tom Santos
  29. * @author Dave Moore
  30. */
  31. public class BasicTabbedPaneUI extends TabbedPaneUI implements SwingConstants {
  32. // Instance variables initialized at installation
  33. protected JTabbedPane tabPane;
  34. protected Color highlight;
  35. protected Color lightHighlight;
  36. protected Color shadow;
  37. protected Color darkShadow;
  38. protected Color focus;
  39. private Color selectedColor;
  40. protected int textIconGap;
  41. protected int tabRunOverlay;
  42. protected Insets tabInsets;
  43. protected Insets selectedTabPadInsets;
  44. protected Insets tabAreaInsets;
  45. protected Insets contentBorderInsets;
  46. private boolean tabsOverlapBorder;
  47. private boolean tabsOpaque = true;
  48. private boolean contentOpaque = true;
  49. /**
  50. * As of Java 2 platform v1.3 this previously undocumented field is no
  51. * longer used.
  52. * Key bindings are now defined by the LookAndFeel, please refer to
  53. * the key bindings specification for further details.
  54. *
  55. * @deprecated As of Java 2 platform v1.3.
  56. */
  57. @Deprecated
  58. protected KeyStroke upKey;
  59. /**
  60. * As of Java 2 platform v1.3 this previously undocumented field is no
  61. * longer used.
  62. * Key bindings are now defined by the LookAndFeel, please refer to
  63. * the key bindings specification for further details.
  64. *
  65. * @deprecated As of Java 2 platform v1.3.
  66. */
  67. @Deprecated
  68. protected KeyStroke downKey;
  69. /**
  70. * As of Java 2 platform v1.3 this previously undocumented field is no
  71. * longer used.
  72. * Key bindings are now defined by the LookAndFeel, please refer to
  73. * the key bindings specification for further details.
  74. *
  75. * @deprecated As of Java 2 platform v1.3.
  76. */
  77. @Deprecated
  78. protected KeyStroke leftKey;
  79. /**
  80. * As of Java 2 platform v1.3 this previously undocumented field is no
  81. * longer used.
  82. * Key bindings are now defined by the LookAndFeel, please refer to
  83. * the key bindings specification for further details.
  84. *
  85. * @deprecated As of Java 2 platform v1.3.
  86. */
  87. @Deprecated
  88. protected KeyStroke rightKey;
  89. // Transient variables (recalculated each time TabbedPane is layed out)
  90. protected int tabRuns[] = new int[10];
  91. protected int runCount = 0;
  92. protected int selectedRun = -1;
  93. protected Rectangle rects[] = new Rectangle[0];
  94. protected int maxTabHeight;
  95. protected int maxTabWidth;
  96. // Listeners
  97. protected ChangeListener tabChangeListener;
  98. protected PropertyChangeListener propertyChangeListener;
  99. protected MouseListener mouseListener;
  100. protected FocusListener focusListener;
  101. // Private instance data
  102. private Insets currentPadInsets = new Insets(0,0,0,0);
  103. private Insets currentTabAreaInsets = new Insets(0,0,0,0);
  104. private Component visibleComponent;
  105. // PENDING(api): See comment for ContainerHandler
  106. private Vector htmlViews;
  107. private Hashtable mnemonicToIndexMap;
  108. /**
  109. * InputMap used for mnemonics. Only non-null if the JTabbedPane has
  110. * mnemonics associated with it. Lazily created in initMnemonics.
  111. */
  112. private InputMap mnemonicInputMap;
  113. // For use when tabLayoutPolicy = SCROLL_TAB_LAYOUT
  114. private ScrollableTabSupport tabScroller;
  115. /**
  116. * A rectangle used for general layout calculations in order
  117. * to avoid constructing many new Rectangles on the fly.
  118. */
  119. protected transient Rectangle calcRect = new Rectangle(0,0,0,0);
  120. /**
  121. * Tab that has focus.
  122. */
  123. private int focusIndex;
  124. /**
  125. * Combined listeners.
  126. */
  127. private Handler handler;
  128. /**
  129. * Index of the tab the mouse is over.
  130. */
  131. private int rolloverTabIndex;
  132. /**
  133. * This is set to true when a component is added/removed from the tab
  134. * pane and set to false when layout happens. If true it indicates that
  135. * tabRuns is not valid and shouldn't be used.
  136. */
  137. private boolean isRunsDirty;
  138. // UI creation
  139. public static ComponentUI createUI(JComponent c) {
  140. return new BasicTabbedPaneUI();
  141. }
  142. static void loadActionMap(LazyActionMap map) {
  143. map.put(new Actions(Actions.NEXT));
  144. map.put(new Actions(Actions.PREVIOUS));
  145. map.put(new Actions(Actions.RIGHT));
  146. map.put(new Actions(Actions.LEFT));
  147. map.put(new Actions(Actions.UP));
  148. map.put(new Actions(Actions.DOWN));
  149. map.put(new Actions(Actions.PAGE_UP));
  150. map.put(new Actions(Actions.PAGE_DOWN));
  151. map.put(new Actions(Actions.REQUEST_FOCUS));
  152. map.put(new Actions(Actions.REQUEST_FOCUS_FOR_VISIBLE));
  153. map.put(new Actions(Actions.SET_SELECTED));
  154. map.put(new Actions(Actions.SELECT_FOCUSED));
  155. map.put(new Actions(Actions.SCROLL_FORWARD));
  156. map.put(new Actions(Actions.SCROLL_BACKWARD));
  157. }
  158. // UI Installation/De-installation
  159. public void installUI(JComponent c) {
  160. this.tabPane = (JTabbedPane)c;
  161. rolloverTabIndex = -1;
  162. c.setLayout(createLayoutManager());
  163. installComponents();
  164. installDefaults();
  165. installListeners();
  166. installKeyboardActions();
  167. }
  168. public void uninstallUI(JComponent c) {
  169. uninstallKeyboardActions();
  170. uninstallListeners();
  171. uninstallDefaults();
  172. uninstallComponents();
  173. c.setLayout(null);
  174. this.tabPane = null;
  175. }
  176. /**
  177. * Invoked by <code>installUI</code> to create
  178. * a layout manager object to manage
  179. * the <code>JTabbedPane</code>.
  180. *
  181. * @return a layout manager object
  182. *
  183. * @see TabbedPaneLayout
  184. * @see javax.swing.JTabbedPane#getTabLayoutPolicy
  185. */
  186. protected LayoutManager createLayoutManager() {
  187. if (tabPane.getTabLayoutPolicy() == JTabbedPane.SCROLL_TAB_LAYOUT) {
  188. return new TabbedPaneScrollLayout();
  189. } else { /* WRAP_TAB_LAYOUT */
  190. return new TabbedPaneLayout();
  191. }
  192. }
  193. /* In an attempt to preserve backward compatibility for programs
  194. * which have extended BasicTabbedPaneUI to do their own layout, the
  195. * UI uses the installed layoutManager (and not tabLayoutPolicy) to
  196. * determine if scrollTabLayout is enabled.
  197. */
  198. private boolean scrollableTabLayoutEnabled() {
  199. return (tabPane.getLayout() instanceof TabbedPaneScrollLayout);
  200. }
  201. /**
  202. * Creates and installs any required subcomponents for the JTabbedPane.
  203. * Invoked by installUI.
  204. *
  205. * @since 1.4
  206. */
  207. protected void installComponents() {
  208. if (scrollableTabLayoutEnabled()) {
  209. if (tabScroller == null) {
  210. tabScroller = new ScrollableTabSupport(tabPane.getTabPlacement());
  211. tabPane.add(tabScroller.viewport);
  212. }
  213. }
  214. }
  215. /**
  216. * Creates and returns a JButton that will provide the user
  217. * with a way to scroll the tabs in a particular direction. The
  218. * returned JButton must be instance of UIResource.
  219. *
  220. * @param direction One of the SwingConstants constants:
  221. * SOUTH, NORTH, EAST or WEST
  222. * @return Widget for user to
  223. * @see javax.swing.JTabbedPane#setTabPlacement
  224. * @see javax.swing.SwingConstants
  225. * @throws IllegalArgumentException if direction is not one of
  226. * NORTH, SOUTH, EAST or WEST
  227. * @since 1.5
  228. */
  229. protected JButton createScrollButton(int direction) {
  230. if (direction != SOUTH && direction != NORTH && direction != EAST &&
  231. direction != WEST) {
  232. throw new IllegalArgumentException("Direction must be one of: " +
  233. "SOUTH, NORTH, EAST or WEST");
  234. }
  235. return new ScrollableTabButton(direction);
  236. }
  237. /**
  238. * Removes any installed subcomponents from the JTabbedPane.
  239. * Invoked by uninstallUI.
  240. *
  241. * @since 1.4
  242. */
  243. protected void uninstallComponents() {
  244. if (scrollableTabLayoutEnabled()) {
  245. tabPane.remove(tabScroller.viewport);
  246. tabPane.remove(tabScroller.scrollForwardButton);
  247. tabPane.remove(tabScroller.scrollBackwardButton);
  248. tabScroller = null;
  249. }
  250. }
  251. protected void installDefaults() {
  252. LookAndFeel.installColorsAndFont(tabPane, "TabbedPane.background",
  253. "TabbedPane.foreground", "TabbedPane.font");
  254. highlight = UIManager.getColor("TabbedPane.light");
  255. lightHighlight = UIManager.getColor("TabbedPane.highlight");
  256. shadow = UIManager.getColor("TabbedPane.shadow");
  257. darkShadow = UIManager.getColor("TabbedPane.darkShadow");
  258. focus = UIManager.getColor("TabbedPane.focus");
  259. selectedColor = UIManager.getColor("TabbedPane.selected");
  260. textIconGap = UIManager.getInt("TabbedPane.textIconGap");
  261. tabInsets = UIManager.getInsets("TabbedPane.tabInsets");
  262. selectedTabPadInsets = UIManager.getInsets("TabbedPane.selectedTabPadInsets");
  263. tabAreaInsets = UIManager.getInsets("TabbedPane.tabAreaInsets");
  264. tabsOverlapBorder = UIManager.getBoolean("TabbedPane.tabsOverlapBorder");
  265. contentBorderInsets = UIManager.getInsets("TabbedPane.contentBorderInsets");
  266. tabRunOverlay = UIManager.getInt("TabbedPane.tabRunOverlay");
  267. tabsOpaque = UIManager.getBoolean("TabbedPane.tabsOpaque");
  268. contentOpaque = UIManager.getBoolean("TabbedPane.contentOpaque");
  269. Object opaque = UIManager.get("TabbedPane.opaque");
  270. if (opaque == null) {
  271. opaque = Boolean.FALSE;
  272. }
  273. LookAndFeel.installProperty(tabPane, "opaque", opaque);
  274. }
  275. protected void uninstallDefaults() {
  276. highlight = null;
  277. lightHighlight = null;
  278. shadow = null;
  279. darkShadow = null;
  280. focus = null;
  281. tabInsets = null;
  282. selectedTabPadInsets = null;
  283. tabAreaInsets = null;
  284. contentBorderInsets = null;
  285. }
  286. protected void installListeners() {
  287. if ((propertyChangeListener = createPropertyChangeListener()) != null) {
  288. tabPane.addPropertyChangeListener(propertyChangeListener);
  289. }
  290. if ((tabChangeListener = createChangeListener()) != null) {
  291. tabPane.addChangeListener(tabChangeListener);
  292. }
  293. if ((mouseListener = createMouseListener()) != null) {
  294. tabPane.addMouseListener(mouseListener);
  295. }
  296. tabPane.addMouseMotionListener(getHandler());
  297. if ((focusListener = createFocusListener()) != null) {
  298. tabPane.addFocusListener(focusListener);
  299. }
  300. tabPane.addContainerListener(getHandler());
  301. if (tabPane.getTabCount()>0) {
  302. htmlViews = createHTMLVector();
  303. }
  304. }
  305. protected void uninstallListeners() {
  306. if (mouseListener != null) {
  307. tabPane.removeMouseListener(mouseListener);
  308. mouseListener = null;
  309. }
  310. tabPane.removeMouseMotionListener(getHandler());
  311. if (focusListener != null) {
  312. tabPane.removeFocusListener(focusListener);
  313. focusListener = null;
  314. }
  315. tabPane.removeContainerListener(getHandler());
  316. if (htmlViews!=null) {
  317. htmlViews.removeAllElements();
  318. htmlViews = null;
  319. }
  320. if (tabChangeListener != null) {
  321. tabPane.removeChangeListener(tabChangeListener);
  322. tabChangeListener = null;
  323. }
  324. if (propertyChangeListener != null) {
  325. tabPane.removePropertyChangeListener(propertyChangeListener);
  326. propertyChangeListener = null;
  327. }
  328. handler = null;
  329. }
  330. protected MouseListener createMouseListener() {
  331. return getHandler();
  332. }
  333. protected FocusListener createFocusListener() {
  334. return getHandler();
  335. }
  336. protected ChangeListener createChangeListener() {
  337. return getHandler();
  338. }
  339. protected PropertyChangeListener createPropertyChangeListener() {
  340. return getHandler();
  341. }
  342. private Handler getHandler() {
  343. if (handler == null) {
  344. handler = new Handler();
  345. }
  346. return handler;
  347. }
  348. protected void installKeyboardActions() {
  349. InputMap km = getInputMap(JComponent.
  350. WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
  351. SwingUtilities.replaceUIInputMap(tabPane, JComponent.
  352. WHEN_ANCESTOR_OF_FOCUSED_COMPONENT,
  353. km);
  354. km = getInputMap(JComponent.WHEN_FOCUSED);
  355. SwingUtilities.replaceUIInputMap(tabPane, JComponent.WHEN_FOCUSED, km);
  356. LazyActionMap.installLazyActionMap(tabPane, BasicTabbedPaneUI.class,
  357. "TabbedPane.actionMap");
  358. }
  359. InputMap getInputMap(int condition) {
  360. if (condition == JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT) {
  361. return (InputMap)DefaultLookup.get(tabPane, this,
  362. "TabbedPane.ancestorInputMap");
  363. }
  364. else if (condition == JComponent.WHEN_FOCUSED) {
  365. return (InputMap)DefaultLookup.get(tabPane, this,
  366. "TabbedPane.focusInputMap");
  367. }
  368. return null;
  369. }
  370. protected void uninstallKeyboardActions() {
  371. SwingUtilities.replaceUIActionMap(tabPane, null);
  372. SwingUtilities.replaceUIInputMap(tabPane, JComponent.
  373. WHEN_ANCESTOR_OF_FOCUSED_COMPONENT,
  374. null);
  375. SwingUtilities.replaceUIInputMap(tabPane, JComponent.WHEN_FOCUSED,
  376. null);
  377. }
  378. /**
  379. * Reloads the mnemonics. This should be invoked when a memonic changes,
  380. * when the title of a mnemonic changes, or when tabs are added/removed.
  381. */
  382. private void updateMnemonics() {
  383. resetMnemonics();
  384. for (int counter = tabPane.getTabCount() - 1; counter >= 0;
  385. counter--) {
  386. int mnemonic = tabPane.getMnemonicAt(counter);
  387. if (mnemonic > 0) {
  388. addMnemonic(counter, mnemonic);
  389. }
  390. }
  391. }
  392. /**
  393. * Resets the mnemonics bindings to an empty state.
  394. */
  395. private void resetMnemonics() {
  396. if (mnemonicToIndexMap != null) {
  397. mnemonicToIndexMap.clear();
  398. mnemonicInputMap.clear();
  399. }
  400. }
  401. /**
  402. * Adds the specified mnemonic at the specified index.
  403. */
  404. private void addMnemonic(int index, int mnemonic) {
  405. if (mnemonicToIndexMap == null) {
  406. initMnemonics();
  407. }
  408. mnemonicInputMap.put(KeyStroke.getKeyStroke(mnemonic, Event.ALT_MASK),
  409. "setSelectedIndex");
  410. mnemonicToIndexMap.put(new Integer(mnemonic), new Integer(index));
  411. }
  412. /**
  413. * Installs the state needed for mnemonics.
  414. */
  415. private void initMnemonics() {
  416. mnemonicToIndexMap = new Hashtable();
  417. mnemonicInputMap = new ComponentInputMapUIResource(tabPane);
  418. mnemonicInputMap.setParent(SwingUtilities.getUIInputMap(tabPane,
  419. JComponent.WHEN_IN_FOCUSED_WINDOW));
  420. SwingUtilities.replaceUIInputMap(tabPane,
  421. JComponent.WHEN_IN_FOCUSED_WINDOW,
  422. mnemonicInputMap);
  423. }
  424. /**
  425. * Sets the tab the mouse is over by location. This is a cover method
  426. * for <code>setRolloverTab(tabForCoordinate(x, y, false))</code>.
  427. */
  428. private void setRolloverTab(int x, int y) {
  429. // NOTE:
  430. // This calls in with false otherwise it could trigger a validate,
  431. // which should NOT happen if the user is only dragging the
  432. // mouse around.
  433. setRolloverTab(tabForCoordinate(tabPane, x, y, false));
  434. }
  435. /**
  436. * Sets the tab the mouse is currently over to <code>index</code>.
  437. * <code>index</code> will be -1 if the mouse is no longer over any
  438. * tab. No checking is done to ensure the passed in index identifies a
  439. * valid tab.
  440. *
  441. * @param index Index of the tab the mouse is over.
  442. * @since 1.5
  443. */
  444. protected void setRolloverTab(int index) {
  445. rolloverTabIndex = index;
  446. }
  447. /**
  448. * Returns the tab the mouse is currently over, or -1 if the mouse is no
  449. * longer over any tab.
  450. *
  451. * @param index Index of the tab the mouse is over.
  452. * @since 1.5
  453. */
  454. protected int getRolloverTab() {
  455. return rolloverTabIndex;
  456. }
  457. public Dimension getMinimumSize(JComponent c) {
  458. // Default to LayoutManager's minimumLayoutSize
  459. return null;
  460. }
  461. public Dimension getMaximumSize(JComponent c) {
  462. // Default to LayoutManager's maximumLayoutSize
  463. return null;
  464. }
  465. // UI Rendering
  466. public void paint(Graphics g, JComponent c) {
  467. int selectedIndex = tabPane.getSelectedIndex();
  468. int tabPlacement = tabPane.getTabPlacement();
  469. ensureCurrentLayout();
  470. // Paint content border and tab area
  471. if (tabsOverlapBorder) {
  472. paintContentBorder(g, tabPlacement, selectedIndex);
  473. }
  474. // If scrollable tabs are enabled, the tab area will be
  475. // painted by the scrollable tab panel instead.
  476. //
  477. if (!scrollableTabLayoutEnabled()) { // WRAP_TAB_LAYOUT
  478. paintTabArea(g, tabPlacement, selectedIndex);
  479. }
  480. if (!tabsOverlapBorder) {
  481. paintContentBorder(g, tabPlacement, selectedIndex);
  482. }
  483. }
  484. /**
  485. * Paints the tabs in the tab area.
  486. * Invoked by paint().
  487. * The graphics parameter must be a valid <code>Graphics</code>
  488. * object. Tab placement may be either:
  489. * <code>JTabbedPane.TOP</code>, <code>JTabbedPane.BOTTOM</code>,
  490. * <code>JTabbedPane.LEFT</code>, or <code>JTabbedPane.RIGHT</code>.
  491. * The selected index must be a valid tabbed pane tab index (0 to
  492. * tab count - 1, inclusive) or -1 if no tab is currently selected.
  493. * The handling of invalid parameters is unspecified.
  494. *
  495. * @param g the graphics object to use for rendering
  496. * @param tabPlacement the placement for the tabs within the JTabbedPane
  497. * @param selectedIndex the tab index of the selected component
  498. *
  499. * @since 1.4
  500. */
  501. protected void paintTabArea(Graphics g, int tabPlacement, int selectedIndex) {
  502. int tabCount = tabPane.getTabCount();
  503. Rectangle iconRect = new Rectangle(),
  504. textRect = new Rectangle();
  505. Rectangle clipRect = g.getClipBounds();
  506. // Paint tabRuns of tabs from back to front
  507. for (int i = runCount - 1; i >= 0; i--) {
  508. int start = tabRuns[i];
  509. int next = tabRuns[(i == runCount - 1)? 0 : i + 1];
  510. int end = (next != 0? next - 1: tabCount - 1);
  511. for (int j = start; j <= end; j++) {
  512. if (j != selectedIndex && rects[j].intersects(clipRect)) {
  513. paintTab(g, tabPlacement, rects, j, iconRect, textRect);
  514. }
  515. }
  516. }
  517. // Paint selected tab if its in the front run
  518. // since it may overlap other tabs
  519. if (selectedIndex >= 0 && rects[selectedIndex].intersects(clipRect)) {
  520. paintTab(g, tabPlacement, rects, selectedIndex, iconRect, textRect);
  521. }
  522. }
  523. protected void paintTab(Graphics g, int tabPlacement,
  524. Rectangle[] rects, int tabIndex,
  525. Rectangle iconRect, Rectangle textRect) {
  526. Rectangle tabRect = rects[tabIndex];
  527. int selectedIndex = tabPane.getSelectedIndex();
  528. boolean isSelected = selectedIndex == tabIndex;
  529. Graphics2D g2 = null;
  530. Polygon cropShape = null;
  531. Shape save = null;
  532. int cropx = 0;
  533. int cropy = 0;
  534. if (scrollableTabLayoutEnabled()) {
  535. if (g instanceof Graphics2D) {
  536. g2 = (Graphics2D)g;
  537. // Render visual for cropped tab edge...
  538. Rectangle viewRect = tabScroller.viewport.getViewRect();
  539. int cropline;
  540. switch(tabPlacement) {
  541. case LEFT:
  542. case RIGHT:
  543. cropline = viewRect.y + viewRect.height;
  544. if ((tabRect.y < cropline) && (tabRect.y + tabRect.height > cropline)) {
  545. cropShape = createCroppedTabClip(tabPlacement, tabRect, cropline);
  546. cropx = tabRect.x;
  547. cropy = cropline-1;
  548. }
  549. break;
  550. case TOP:
  551. case BOTTOM:
  552. default:
  553. cropline = viewRect.x + viewRect.width;
  554. if ((tabRect.x < cropline) && (tabRect.x + tabRect.width > cropline)) {
  555. cropShape = createCroppedTabClip(tabPlacement, tabRect, cropline);
  556. cropx = cropline-1;
  557. cropy = tabRect.y;
  558. }
  559. }
  560. if (cropShape != null) {
  561. save = g2.getClip();
  562. g2.clip(cropShape);
  563. }
  564. }
  565. }
  566. if (tabsOpaque || tabPane.isOpaque()) {
  567. paintTabBackground(g, tabPlacement, tabIndex, tabRect.x, tabRect.y,
  568. tabRect.width, tabRect.height, isSelected);
  569. }
  570. paintTabBorder(g, tabPlacement, tabIndex, tabRect.x, tabRect.y,
  571. tabRect.width, tabRect.height, isSelected);
  572. String title = tabPane.getTitleAt(tabIndex);
  573. Font font = tabPane.getFont();
  574. FontMetrics metrics = SwingUtilities2.getFontMetrics(tabPane, g, font);
  575. Icon icon = getIconForTab(tabIndex);
  576. layoutLabel(tabPlacement, metrics, tabIndex, title, icon,
  577. tabRect, iconRect, textRect, isSelected);
  578. paintText(g, tabPlacement, font, metrics,
  579. tabIndex, title, textRect, isSelected);
  580. paintIcon(g, tabPlacement, tabIndex, icon, iconRect, isSelected);
  581. paintFocusIndicator(g, tabPlacement, rects, tabIndex,
  582. iconRect, textRect, isSelected);
  583. if (cropShape != null) {
  584. paintCroppedTabEdge(g, tabPlacement, tabIndex, isSelected, cropx, cropy);
  585. g2.setClip(save);
  586. }
  587. }
  588. /* This method will create and return a polygon shape for the given tab rectangle
  589. * which has been cropped at the specified cropline with a torn edge visual.
  590. * e.g. A "File" tab which has cropped been cropped just after the "i":
  591. * -------------
  592. * | ..... |
  593. * | . |
  594. * | ... . |
  595. * | . . |
  596. * | . . |
  597. * | . . |
  598. * --------------
  599. *
  600. * The x, y arrays below define the pattern used to create a "torn" edge
  601. * segment which is repeated to fill the edge of the tab.
  602. * For tabs placed on TOP and BOTTOM, this righthand torn edge is created by
  603. * line segments which are defined by coordinates obtained by
  604. * subtracting xCropLen[i] from (tab.x + tab.width) and adding yCroplen[i]
  605. * to (tab.y).
  606. * For tabs placed on LEFT or RIGHT, the bottom torn edge is created by
  607. * subtracting xCropLen[i] from (tab.y + tab.height) and adding yCropLen[i]
  608. * to (tab.x).
  609. */
  610. private int xCropLen[] = {1,1,0,0,1,1,2,2};
  611. private int yCropLen[] = {0,3,3,6,6,9,9,12};
  612. private static final int CROP_SEGMENT = 12;
  613. private Polygon createCroppedTabClip(int tabPlacement, Rectangle tabRect, int cropline) {
  614. int rlen = 0;
  615. int start = 0;
  616. int end = 0;
  617. int ostart = 0;
  618. switch(tabPlacement) {
  619. case LEFT:
  620. case RIGHT:
  621. rlen = tabRect.width;
  622. start = tabRect.x;
  623. end = tabRect.x + tabRect.width;
  624. ostart = tabRect.y;
  625. break;
  626. case TOP:
  627. case BOTTOM:
  628. default:
  629. rlen = tabRect.height;
  630. start = tabRect.y;
  631. end = tabRect.y + tabRect.height;
  632. ostart = tabRect.x;
  633. }
  634. int rcnt = rlenCROP_SEGMENT;
  635. if (rlen%CROP_SEGMENT > 0) {
  636. rcnt++;
  637. }
  638. int npts = 2 + (rcnt*8);
  639. int xp[] = new int[npts];
  640. int yp[] = new int[npts];
  641. int pcnt = 0;
  642. xp[pcnt] = ostart;
  643. yp[pcnt++] = end;
  644. xp[pcnt] = ostart;
  645. yp[pcnt++] = start;
  646. for(int i = 0; i < rcnt; i++) {
  647. for(int j = 0; j < xCropLen.length; j++) {
  648. xp[pcnt] = cropline - xCropLen[j];
  649. yp[pcnt] = start + (i*CROP_SEGMENT) + yCropLen[j];
  650. if (yp[pcnt] >= end) {
  651. yp[pcnt] = end;
  652. pcnt++;
  653. break;
  654. }
  655. pcnt++;
  656. }
  657. }
  658. if (tabPlacement == JTabbedPane.TOP || tabPlacement == JTabbedPane.BOTTOM) {
  659. return new Polygon(xp, yp, pcnt);
  660. } else { // LEFT or RIGHT
  661. return new Polygon(yp, xp, pcnt);
  662. }
  663. }
  664. /* If tabLayoutPolicy == SCROLL_TAB_LAYOUT, this method will paint an edge
  665. * indicating the tab is cropped in the viewport display
  666. */
  667. private void paintCroppedTabEdge(Graphics g, int tabPlacement, int tabIndex,
  668. boolean isSelected,
  669. int x, int y) {
  670. switch(tabPlacement) {
  671. case LEFT:
  672. case RIGHT:
  673. int xx = x;
  674. g.setColor(shadow);
  675. while(xx <= x+rects[tabIndex].width) {
  676. for (int i=0; i < xCropLen.length; i+=2) {
  677. g.drawLine(xx+yCropLen[i],y-xCropLen[i],
  678. xx+yCropLen[i+1]-1,y-xCropLen[i+1]);
  679. }
  680. xx+=CROP_SEGMENT;
  681. }
  682. break;
  683. case TOP:
  684. case BOTTOM:
  685. default:
  686. int yy = y;
  687. g.setColor(shadow);
  688. while(yy <= y+rects[tabIndex].height) {
  689. for (int i=0; i < xCropLen.length; i+=2) {
  690. g.drawLine(x-xCropLen[i],yy+yCropLen[i],
  691. x-xCropLen[i+1],yy+yCropLen[i+1]-1);
  692. }
  693. yy+=CROP_SEGMENT;
  694. }
  695. }
  696. }
  697. protected void layoutLabel(int tabPlacement,
  698. FontMetrics metrics, int tabIndex,
  699. String title, Icon icon,
  700. Rectangle tabRect, Rectangle iconRect,
  701. Rectangle textRect, boolean isSelected ) {
  702. textRect.x = textRect.y = iconRect.x = iconRect.y = 0;
  703. View v = getTextViewForTab(tabIndex);
  704. if (v != null) {
  705. tabPane.putClientProperty("html", v);
  706. }
  707. SwingUtilities.layoutCompoundLabel((JComponent) tabPane,
  708. metrics, title, icon,
  709. SwingUtilities.CENTER,
  710. SwingUtilities.CENTER,
  711. SwingUtilities.CENTER,
  712. SwingUtilities.TRAILING,
  713. tabRect,
  714. iconRect,
  715. textRect,
  716. textIconGap);
  717. tabPane.putClientProperty("html", null);
  718. int xNudge = getTabLabelShiftX(tabPlacement, tabIndex, isSelected);
  719. int yNudge = getTabLabelShiftY(tabPlacement, tabIndex, isSelected);
  720. iconRect.x += xNudge;
  721. iconRect.y += yNudge;
  722. textRect.x += xNudge;
  723. textRect.y += yNudge;
  724. }
  725. protected void paintIcon(Graphics g, int tabPlacement,
  726. int tabIndex, Icon icon, Rectangle iconRect,
  727. boolean isSelected ) {
  728. if (icon != null) {
  729. icon.paintIcon(tabPane, g, iconRect.x, iconRect.y);
  730. }
  731. }
  732. protected void paintText(Graphics g, int tabPlacement,
  733. Font font, FontMetrics metrics, int tabIndex,
  734. String title, Rectangle textRect,
  735. boolean isSelected) {
  736. g.setFont(font);
  737. View v = getTextViewForTab(tabIndex);
  738. if (v != null) {
  739. // html
  740. v.paint(g, textRect);
  741. } else {
  742. // plain text
  743. int mnemIndex = tabPane.getDisplayedMnemonicIndexAt(tabIndex);
  744. if (tabPane.isEnabled() && tabPane.isEnabledAt(tabIndex)) {
  745. Color fg = tabPane.getForegroundAt(tabIndex);
  746. if (isSelected && (fg instanceof UIResource)) {
  747. Color selectedFG = UIManager.getColor(
  748. "TabbedPane.selectedForeground");
  749. if (selectedFG != null) {
  750. fg = selectedFG;
  751. }
  752. }
  753. g.setColor(fg);
  754. SwingUtilities2.drawStringUnderlineCharAt(tabPane, g,
  755. title, mnemIndex,
  756. textRect.x, textRect.y + metrics.getAscent());
  757. } else { // tab disabled
  758. g.setColor(tabPane.getBackgroundAt(tabIndex).brighter());
  759. SwingUtilities2.drawStringUnderlineCharAt(tabPane, g,
  760. title, mnemIndex,
  761. textRect.x, textRect.y + metrics.getAscent());
  762. g.setColor(tabPane.getBackgroundAt(tabIndex).darker());
  763. SwingUtilities2.drawStringUnderlineCharAt(tabPane, g,
  764. title, mnemIndex,
  765. textRect.x - 1, textRect.y + metrics.getAscent() - 1);
  766. }
  767. }
  768. }
  769. protected int getTabLabelShiftX(int tabPlacement, int tabIndex, boolean isSelected) {
  770. Rectangle tabRect = rects[tabIndex];
  771. int nudge = 0;
  772. switch(tabPlacement) {
  773. case LEFT:
  774. nudge = isSelected? -1 : 1;
  775. break;
  776. case RIGHT:
  777. nudge = isSelected? 1 : -1;
  778. break;
  779. case BOTTOM:
  780. case TOP:
  781. default:
  782. nudge = tabRect.width % 2;
  783. }
  784. return nudge;
  785. }
  786. protected int getTabLabelShiftY(int tabPlacement, int tabIndex, boolean isSelected) {
  787. Rectangle tabRect = rects[tabIndex];
  788. int nudge = 0;
  789. switch(tabPlacement) {
  790. case BOTTOM:
  791. nudge = isSelected? 1 : -1;
  792. break;
  793. case LEFT:
  794. case RIGHT:
  795. nudge = tabRect.height % 2;
  796. break;
  797. case TOP:
  798. default:
  799. nudge = isSelected? -1 : 1;;
  800. }
  801. return nudge;
  802. }
  803. protected void paintFocusIndicator(Graphics g, int tabPlacement,
  804. Rectangle[] rects, int tabIndex,
  805. Rectangle iconRect, Rectangle textRect,
  806. boolean isSelected) {
  807. Rectangle tabRect = rects[tabIndex];
  808. if (tabPane.hasFocus() && isSelected) {
  809. int x, y, w, h;
  810. g.setColor(focus);
  811. switch(tabPlacement) {
  812. case LEFT:
  813. x = tabRect.x + 3;
  814. y = tabRect.y + 3;
  815. w = tabRect.width - 5;
  816. h = tabRect.height - 6;
  817. break;
  818. case RIGHT:
  819. x = tabRect.x + 2;
  820. y = tabRect.y + 3;
  821. w = tabRect.width - 5;
  822. h = tabRect.height - 6;
  823. break;
  824. case BOTTOM:
  825. x = tabRect.x + 3;
  826. y = tabRect.y + 2;
  827. w = tabRect.width - 6;
  828. h = tabRect.height - 5;
  829. break;
  830. case TOP:
  831. default:
  832. x = tabRect.x + 3;
  833. y = tabRect.y + 3;
  834. w = tabRect.width - 6;
  835. h = tabRect.height - 5;
  836. }
  837. BasicGraphicsUtils.drawDashedRect(g, x, y, w, h);
  838. }
  839. }
  840. /**
  841. * this function draws the border around each tab
  842. * note that this function does now draw the background of the tab.
  843. * that is done elsewhere
  844. */
  845. protected void paintTabBorder(Graphics g, int tabPlacement,
  846. int tabIndex,
  847. int x, int y, int w, int h,
  848. boolean isSelected ) {
  849. g.setColor(lightHighlight);
  850. switch (tabPlacement) {
  851. case LEFT:
  852. g.drawLine(x+1, y+h-2, x+1, y+h-2); // bottom-left highlight
  853. g.drawLine(x, y+2, x, y+h-3); // left highlight
  854. g.drawLine(x+1, y+1, x+1, y+1); // top-left highlight
  855. g.drawLine(x+2, y, x+w-1, y); // top highlight
  856. g.setColor(shadow);
  857. g.drawLine(x+2, y+h-2, x+w-1, y+h-2); // bottom shadow
  858. g.setColor(darkShadow);
  859. g.drawLine(x+2, y+h-1, x+w-1, y+h-1); // bottom dark shadow
  860. break;
  861. case RIGHT:
  862. g.drawLine(x, y, x+w-3, y); // top highlight
  863. g.setColor(shadow);
  864. g.drawLine(x, y+h-2, x+w-3, y+h-2); // bottom shadow
  865. g.drawLine(x+w-2, y+2, x+w-2, y+h-3); // right shadow
  866. g.setColor(darkShadow);
  867. g.drawLine(x+w-2, y+1, x+w-2, y+1); // top-right dark shadow
  868. g.drawLine(x+w-2, y+h-2, x+w-2, y+h-2); // bottom-right dark shadow
  869. g.drawLine(x+w-1, y+2, x+w-1, y+h-3); // right dark shadow
  870. g.drawLine(x, y+h-1, x+w-3, y+h-1); // bottom dark shadow
  871. break;
  872. case BOTTOM:
  873. g.drawLine(x, y, x, y+h-3); // left highlight
  874. g.drawLine(x+1, y+h-2, x+1, y+h-2); // bottom-left highlight
  875. g.setColor(shadow);
  876. g.drawLine(x+2, y+h-2, x+w-3, y+h-2); // bottom shadow
  877. g.drawLine(x+w-2, y, x+w-2, y+h-3); // right shadow
  878. g.setColor(darkShadow);
  879. g.drawLine(x+2, y+h-1, x+w-3, y+h-1); // bottom dark shadow
  880. g.drawLine(x+w-2, y+h-2, x+w-2, y+h-2); // bottom-right dark shadow
  881. g.drawLine(x+w-1, y, x+w-1, y+h-3); // right dark shadow
  882. break;
  883. case TOP:
  884. default:
  885. g.drawLine(x, y+2, x, y+h-1); // left highlight
  886. g.drawLine(x+1, y+1, x+1, y+1); // top-left highlight
  887. g.drawLine(x+2, y, x+w-3, y); // top highlight
  888. g.setColor(shadow);
  889. g.drawLine(x+w-2, y+2, x+w-2, y+h-1); // right shadow
  890. g.setColor(darkShadow);
  891. g.drawLine(x+w-1, y+2, x+w-1, y+h-1); // right dark-shadow
  892. g.drawLine(x+w-2, y+1, x+w-2, y+1); // top-right shadow
  893. }
  894. }
  895. protected void paintTabBackground(Graphics g, int tabPlacement,
  896. int tabIndex,
  897. int x, int y, int w, int h,
  898. boolean isSelected ) {
  899. g.setColor(!isSelected || selectedColor == null?
  900. tabPane.getBackgroundAt(tabIndex) : selectedColor);
  901. switch(tabPlacement) {
  902. case LEFT:
  903. g.fillRect(x+1, y+1, w-1, h-3);
  904. break;
  905. case RIGHT:
  906. g.fillRect(x, y+1, w-2, h-3);
  907. break;
  908. case BOTTOM:
  909. g.fillRect(x+1, y, w-3, h-1);
  910. break;
  911. case TOP:
  912. default:
  913. g.fillRect(x+1, y+1, w-3, h-1);
  914. }
  915. }
  916. protected void paintContentBorder(Graphics g, int tabPlacement, int selectedIndex) {
  917. int width = tabPane.getWidth();
  918. int height = tabPane.getHeight();
  919. Insets insets = tabPane.getInsets();
  920. Insets tabAreaInsets = getTabAreaInsets(tabPlacement);
  921. int x = insets.left;
  922. int y = insets.top;
  923. int w = width - insets.right - insets.left;
  924. int h = height - insets.top - insets.bottom;
  925. switch(tabPlacement) {
  926. case LEFT:
  927. x += calculateTabAreaWidth(tabPlacement, runCount, maxTabWidth);
  928. if (tabsOverlapBorder) {
  929. x -= tabAreaInsets.right;
  930. }
  931. w -= (x - insets.left);
  932. break;
  933. case RIGHT:
  934. w -= calculateTabAreaWidth(tabPlacement, runCount, maxTabWidth);
  935. if (tabsOverlapBorder) {
  936. w += tabAreaInsets.left;
  937. }
  938. break;
  939. case BOTTOM:
  940. h -= calculateTabAreaHeight(tabPlacement, runCount, maxTabHeight);
  941. if (tabsOverlapBorder) {
  942. h += tabAreaInsets.top;
  943. }
  944. break;
  945. case TOP:
  946. default:
  947. y += calculateTabAreaHeight(tabPlacement, runCount, maxTabHeight);
  948. if (tabsOverlapBorder) {
  949. y -= tabAreaInsets.bottom;
  950. }
  951. h -= (y - insets.top);
  952. }
  953. if (contentOpaque || tabPane.isOpaque()) {
  954. // Fill region behind content area
  955. Color color = UIManager.getColor("TabbedPane.contentAreaColor");
  956. if (color != null) {
  957. g.setColor(color);
  958. }
  959. else if (selectedColor == null) {
  960. g.setColor(tabPane.getBackground());
  961. }
  962. else {
  963. g.setColor(selectedColor);
  964. }
  965. g.fillRect(x,y,w,h);
  966. }
  967. paintContentBorderTopEdge(g, tabPlacement, selectedIndex, x, y, w, h);
  968. paintContentBorderLeftEdge(g, tabPlacement, selectedIndex, x, y, w, h);
  969. paintContentBorderBottomEdge(g, tabPlacement, selectedIndex, x, y, w, h);
  970. paintContentBorderRightEdge(g, tabPlacement, selectedIndex, x, y, w, h);
  971. }
  972. protected void paintContentBorderTopEdge(Graphics g, int tabPlacement,
  973. int selectedIndex,
  974. int x, int y, int w, int h) {
  975. Rectangle selRect = selectedIndex < 0? null :
  976. getTabBounds(selectedIndex, calcRect);
  977. g.setColor(lightHighlight);
  978. // Draw unbroken line if tabs are not on TOP, OR
  979. // selected tab is not in run adjacent to content, OR
  980. // selected tab is not visible (SCROLL_TAB_LAYOUT)
  981. //
  982. if (tabPlacement != TOP || selectedIndex < 0 ||
  983. (selRect.y + selRect.height + 1 < y) ||
  984. (selRect.x < x || selRect.x > x + w)) {
  985. g.drawLine(x, y, x+w-2, y);
  986. } else {
  987. // Break line to show visual connection to selected tab
  988. g.drawLine(x, y, selRect.x - 1, y);
  989. if (selRect.x + selRect.width < x + w - 2) {
  990. g.drawLine(selRect.x + selRect.width, y,
  991. x+w-2, y);
  992. } else {
  993. g.setColor(shadow);
  994. g.drawLine(x+w-2, y, x+w-2, y);
  995. }
  996. }
  997. }
  998. protected void paintContentBorderLeftEdge(Graphics g, int tabPlacement,
  999. int selectedIndex,
  1000. int x, int y, int w, int h) {
  1001. Rectangle selRect = selectedIndex < 0? null :
  1002. getTabBounds(selectedIndex, calcRect);
  1003. g.setColor(lightHighlight);
  1004. // Draw unbroken line if tabs are not on LEFT, OR
  1005. // selected tab is not in run adjacent to content, OR
  1006. // selected tab is not visible (SCROLL_TAB_LAYOUT)
  1007. //
  1008. if (tabPlacement != LEFT || selectedIndex < 0 ||
  1009. (selRect.x + selRect.width + 1 < x) ||
  1010. (selRect.y < y || selRect.y > y + h)) {
  1011. g.drawLine(x, y, x, y+h-2);
  1012. } else {
  1013. // Break line to show visual connection to selected tab
  1014. g.drawLine(x, y, x, selRect.y - 1);
  1015. if (selRect.y + selRect.height < y + h - 2) {
  1016. g.drawLine(x, selRect.y + selRect.height,
  1017. x, y+h-2);
  1018. }
  1019. }
  1020. }
  1021. protected void paintContentBorderBottomEdge(Graphics g, int tabPlacement,
  1022. int selectedIndex,
  1023. int x, int y, int w, int h) {
  1024. Rectangle selRect = selectedIndex < 0? null :
  1025. getTabBounds(selectedIndex, calcRect);
  1026. g.setColor(shadow);
  1027. // Draw unbroken line if tabs are not on BOTTOM, OR
  1028. // selected tab is not in run adjacent to content, OR
  1029. // selected tab is not visible (SCROLL_TAB_LAYOUT)
  1030. //
  1031. if (tabPlacement != BOTTOM || selectedIndex < 0 ||
  1032. (selRect.y - 1 > h) ||
  1033. (selRect.x < x || selRect.x > x + w)) {
  1034. g.drawLine(x+1, y+h-2, x+w-2, y+h-2);
  1035. g.setColor(darkShadow);
  1036. g.drawLine(x, y+h-1, x+w-1, y+h-1);
  1037. } else {
  1038. // Break line to show visual connection to selected tab
  1039. g.drawLine(x+1, y+h-2, selRect.x - 1, y+h-2);
  1040. g.setColor(darkShadow);
  1041. g.drawLine(x, y+h-1, selRect.x - 1, y+h-1);
  1042. if (selRect.x + selRect.width < x + w - 2) {
  1043. g.setColor(shadow);
  1044. g.drawLine(selRect.x + selRect.width, y+h-2, x+w-2, y+h-2);
  1045. g.setColor(darkShadow);
  1046. g.drawLine(selRect.x + selRect.width, y+h-1, x+w-1, y+h-1);
  1047. }
  1048. }
  1049. }
  1050. protected void paintContentBorderRightEdge(Graphics g, int tabPlacement,
  1051. int selectedIndex,
  1052. int x, int y, int w, int h) {
  1053. Rectangle selRect = selectedIndex < 0? null :
  1054. getTabBounds(selectedIndex, calcRect);
  1055. g.setColor(shadow);
  1056. // Draw unbroken line if tabs are not on RIGHT, OR
  1057. // selected tab is not in run adjacent to content, OR
  1058. // selected tab is not visible (SCROLL_TAB_LAYOUT)
  1059. //
  1060. if (tabPlacement != RIGHT || selectedIndex < 0 ||
  1061. (selRect.x - 1 > w) ||
  1062. (selRect.y < y || selRect.y > y + h)) {
  1063. g.drawLine(x+w-2, y+1, x+w-2, y+h-3);
  1064. g.setColor(darkShadow);
  1065. g.drawLine(x+w-1, y, x+w-1, y+h-1);
  1066. } else {
  1067. // Break line to show visual connection to selected tab
  1068. g.drawLine(x+w-2, y+1, x+w-2, selRect.y - 1);
  1069. g.setColor(darkShadow);
  1070. g.drawLine(x+w-1, y, x+w-1, selRect.y - 1);
  1071. if (selRect.y + selRect.height < y + h - 2) {
  1072. g.setColor(shadow);
  1073. g.drawLine(x+w-2, selRect.y + selRect.height,
  1074. x+w-2, y+h-2);
  1075. g.setColor(darkShadow);
  1076. g.drawLine(x+w-1, selRect.y + selRect.height,
  1077. x+w-1, y+h-2);
  1078. }
  1079. }
  1080. }
  1081. private void ensureCurrentLayout() {
  1082. if (!tabPane.isValid()) {
  1083. tabPane.validate();
  1084. }
  1085. /* If tabPane doesn't have a peer yet, the validate() call will
  1086. * silently fail. We handle that by forcing a layout if tabPane
  1087. * is still invalid. See bug 4237677.
  1088. */
  1089. if (!tabPane.isValid()) {
  1090. TabbedPaneLayout layout = (TabbedPaneLayout)tabPane.getLayout();
  1091. layout.calculateLayoutInfo();
  1092. }
  1093. }
  1094. // TabbedPaneUI methods
  1095. /**
  1096. * Returns the bounds of the specified tab index. The bounds are
  1097. * with respect to the JTabbedPane's coordinate space.
  1098. */
  1099. public Rectangle getTabBounds(JTabbedPane pane, int i) {
  1100. ensureCurrentLayout();
  1101. Rectangle tabRect = new Rectangle();
  1102. return getTabBounds(i, tabRect);
  1103. }
  1104. public int getTabRunCount(JTabbedPane pane) {
  1105. ensureCurrentLayout();
  1106. return runCount;
  1107. }
  1108. /**
  1109. * Returns the tab index which intersects the specified point
  1110. * in the JTabbedPane's coordinate space.
  1111. */
  1112. public int tabForCoordinate(JTabbedPane pane, int x, int y) {
  1113. return tabForCoordinate(pane, x, y, true);
  1114. }
  1115. private int tabForCoordinate(JTabbedPane pane, int x, int y,
  1116. boolean validateIfNecessary) {
  1117. if (validateIfNecessary) {
  1118. ensureCurrentLayout();
  1119. }
  1120. if (isRunsDirty) {
  1121. // We didn't recalculate the layout, runs and tabCount may not
  1122. // line up, bail.
  1123. return -1;
  1124. }
  1125. Point p = new Point(x, y);
  1126. if (scrollableTabLayoutEnabled()) {
  1127. translatePointToTabPanel(x, y, p);
  1128. Rectangle viewRect = tabScroller.viewport.getViewRect();
  1129. if (!viewRect.contains(p)) {
  1130. return -1;
  1131. }
  1132. }
  1133. int tabCount = tabPane.getTabCount();
  1134. for (int i = 0; i < tabCount; i++) {
  1135. if (rects[i].contains(p.x, p.y)) {
  1136. return i;
  1137. }
  1138. }
  1139. return -1;
  1140. }
  1141. /**
  1142. * Returns the bounds of the specified tab in the coordinate space
  1143. * of the JTabbedPane component. This is required because the tab rects
  1144. * are by default defined in the coordinate space of the component where
  1145. * they are rendered, which could be the JTabbedPane
  1146. * (for WRAP_TAB_LAYOUT) or a ScrollableTabPanel (SCROLL_TAB_LAYOUT).
  1147. * This method should be used whenever the tab rectangle must be relative
  1148. * to the JTabbedPane itself and the result should be placed in a
  1149. * designated Rectangle object (rather than instantiating and returning
  1150. * a new Rectangle each time). The tab index parameter must be a valid
  1151. * tabbed pane tab index (0 to tab count - 1, inclusive). The destination
  1152. * rectangle parameter must be a valid <code>Rectangle</code> instance.
  1153. * The handling of invalid parameters is unspecified.
  1154. *
  1155. * @param tabIndex the index of the tab
  1156. * @param dest the rectangle where the result should be placed
  1157. * @return the resulting rectangle
  1158. *
  1159. * @since 1.4
  1160. */
  1161. protected Rectangle getTabBounds(int tabIndex, Rectangle dest) {
  1162. dest.width = rects[tabIndex].width;
  1163. dest.height = rects[tabIndex].height;
  1164. if (scrollableTabLayoutEnabled()) { // SCROLL_TAB_LAYOUT
  1165. // Need to translate coordinates based on viewport location &
  1166. // view position
  1167. Point vpp = tabScroller.viewport.getLocation();
  1168. Point viewp = tabScroller.viewport.getViewPosition();
  1169. dest.x = rects[tabIndex].x + vpp.x - viewp.x;
  1170. dest.y = rects[tabIndex].y + vpp.y - viewp.y;
  1171. } else { // WRAP_TAB_LAYOUT
  1172. dest.x = rects[tabIndex].x;
  1173. dest.y = rects[tabIndex].y;
  1174. }
  1175. return dest;
  1176. }
  1177. /**
  1178. * Returns the index of the tab closest to the passed in location, note
  1179. * that the returned tab may not contain the location x,y.
  1180. */
  1181. private int getClosestTab(int x, int y) {
  1182. int min = 0;
  1183. int tabCount = Math.min(rects.length, tabPane.getTabCount());
  1184. int max = tabCount;
  1185. int tabPlacement = tabPane.getTabPlacement();
  1186. boolean useX = (tabPlacement == TOP || tabPlacement == BOTTOM);
  1187. int want = (useX) ? x : y;
  1188. while (min != max) {
  1189. int current = (max + min) / 2;
  1190. int minLoc;
  1191. int maxLoc;
  1192. if (useX) {
  1193. minLoc = rects[current].x;
  1194. maxLoc = minLoc + rects[current].width;
  1195. }
  1196. else {
  1197. minLoc = rects[current].y;
  1198. maxLoc = minLoc + rects[current].height;
  1199. }
  1200. if (want < minLoc) {
  1201. max = current;
  1202. if (min == max) {
  1203. return Math.max(0, current - 1);
  1204. }
  1205. }
  1206. else if (want >= maxLoc) {
  1207. min = current;
  1208. if (max - min <= 1) {
  1209. return Math.max(current + 1, tabCount - 1);
  1210. }
  1211. }
  1212. else {
  1213. return current;
  1214. }
  1215. }
  1216. return min;
  1217. }
  1218. /**
  1219. * Returns a point which is translated from the specified point in the
  1220. * JTabbedPane's coordinate space to the coordinate space of the
  1221. * ScrollableTabPanel. This is used for SCROLL_TAB_LAYOUT ONLY.
  1222. */
  1223. private Point translatePointToTabPanel(int srcx, int srcy, Point dest) {
  1224. Point vpp = tabScroller.viewport.getLocation();
  1225. Point viewp = tabScroller.viewport.getViewPosition();
  1226. dest.x = srcx - vpp.x + viewp.x;
  1227. dest.y = srcy - vpp.y + viewp.y;
  1228. return dest;
  1229. }
  1230. // BasicTabbedPaneUI methods
  1231. protected Component getVisibleComponent() {
  1232. return visibleComponent;
  1233. }
  1234. protected void setVisibleComponent(Component component) {
  1235. if (visibleComponent != null && visibleComponent != component &&
  1236. visibleComponent.getParent() == tabPane) {
  1237. visibleComponent.setVisible(false);
  1238. }
  1239. if (component != null && !component.isVisible()) {
  1240. component.setVisible(true);
  1241. }
  1242. visibleComponent = component;
  1243. }
  1244. protected void assureRectsCreated(int tabCount) {
  1245. int rectArrayLen = rects.length;
  1246. if (tabCount != rectArrayLen ) {
  1247. Rectangle[] tempRectArray = new Rectangle[tabCount];
  1248. System.arraycopy(rects, 0, tempRectArray, 0,
  1249. Math.min(rectArrayLen, tabCount));
  1250. rects = tempRectArray;
  1251. for (int rectIndex = rectArrayLen; rectIndex < tabCount; rectIndex++) {
  1252. rects[rectIndex] = new Rectangle();
  1253. }
  1254. }
  1255. }
  1256. protected void expandTabRunsArray() {
  1257. int rectLen = tabRuns.length;
  1258. int[] newArray = new int[rectLen+10];
  1259. System.arraycopy(tabRuns, 0, newArray, 0, runCount);
  1260. tabRuns = newArray;
  1261. }
  1262. protected int getRunForTab(int tabCount, int tabIndex) {
  1263. for (int i = 0; i < runCount; i++) {
  1264. int first = tabRuns[i];
  1265. int last = lastTabInRun(tabCount, i);
  1266. if (tabIndex >= first && tabIndex <= last) {
  1267. return i;
  1268. }
  1269. }
  1270. return 0;
  1271. }
  1272. protected int lastTabInRun(int tabCount, int run) {
  1273. if (runCount == 1) {
  1274. return tabCount - 1;
  1275. }
  1276. int nextRun = (run == runCount - 1? 0 : run + 1);
  1277. if (tabRuns[nextRun] == 0) {
  1278. return tabCount - 1;
  1279. }
  1280. return tabRuns[nextRun]-1;
  1281. }
  1282. protected int getTabRunOverlay(int tabPlacement) {
  1283. return tabRunOverlay;
  1284. }
  1285. protected int getTabRunIndent(int tabPlacement, int run) {
  1286. return 0;
  1287. }
  1288. protected boolean shouldPadTabRun(int tabPlacement, int run) {
  1289. return runCount > 1;
  1290. }
  1291. protected boolean shouldRotateTabRuns(int tabPlacement) {
  1292. return true;
  1293. }
  1294. protected Icon getIconForTab(int tabIndex) {
  1295. return (!tabPane.isEnabled() || !tabPane.isEnabledAt(tabIndex))?
  1296. tabPane.getDisabledIconAt(tabIndex) : tabPane.getIconAt(tabIndex);
  1297. }
  1298. /**
  1299. * Returns the text View object required to render stylized text (HTML) for
  1300. * the specified tab or null if no specialized text rendering is needed
  1301. * for this tab. This is provided to support html rendering inside tabs.
  1302. *
  1303. * @param tabIndex the index of the tab
  1304. * @return the text view to render the tab's text or null if no
  1305. * specialized rendering is required
  1306. *
  1307. * @since 1.4
  1308. */
  1309. protected View getTextViewForTab(int tabIndex) {
  1310. if (htmlViews != null) {
  1311. return (View)htmlViews.elementAt(tabIndex);
  1312. }
  1313. return null;
  1314. }
  1315. protected int calculateTabHeight(int tabPlacement, int tabIndex, int fontHeight) {
  1316. int height = 0;
  1317. View v = getTextViewForTab(tabIndex);
  1318. if (v != null) {
  1319. // html
  1320. height += (int)v.getPreferredSpan(View.Y_AXIS);
  1321. } else {
  1322. // plain text
  1323. height += fontHeight;
  1324. }
  1325. Icon icon = getIconForTab(tabIndex);
  1326. Insets tabInsets = getTabInsets(tabPlacement, tabIndex);
  1327. if (icon != null) {
  1328. height = Math.max(height, icon.getIconHeight());
  1329. }
  1330. height += tabInsets.top + tabInsets.bottom + 2;
  1331. return height;
  1332. }
  1333. protected int calculateMaxTabHeight(int tabPlacement) {
  1334. FontMetrics metrics = getFontMetrics();
  1335. int tabCount = tabPane.getTabCount();
  1336. int result = 0;
  1337. int fontHeight = metrics.getHeight();
  1338. for(int i = 0; i < tabCount; i++) {
  1339. result = Math.max(calculateTabHeight(tabPlacement, i, fontHeight), result);
  1340. }
  1341. return result;
  1342. }
  1343. protected int calculateTabWidth(int tabPlacement, int tabIndex, FontMetrics metrics) {
  1344. Icon icon = getIconForTab(tabIndex);
  1345. Insets tabInsets = getTabInsets(tabPlacement, tabIndex);
  1346. int width = tabInsets.left + tabInsets.right + 3;
  1347. if (icon != null) {
  1348. width += icon.getIconWidth() + textIconGap;
  1349. }
  1350. View v = getTextViewForTab(tabIndex);
  1351. if (v != null) {
  1352. // html
  1353. width += (int)v.getPreferredSpan(View.X_AXIS);
  1354. } else {
  1355. // plain text
  1356. String title = tabPane.getTitleAt(tabIndex);
  1357. width += SwingUtilities2.stringWidth(tabPane, metrics, title);
  1358. }
  1359. return width;
  1360. }
  1361. protected int calculateMaxTabWidth(int tabPlacement) {
  1362. FontMetrics metrics = getFontMetrics();
  1363. int tabCount = tabPane.getTabCount();
  1364. int result = 0;
  1365. for(int i = 0; i < tabCount; i++) {
  1366. result = Math.max(calculateTabWidth(tabPlacement, i, metrics), result);
  1367. }
  1368. return result;
  1369. }
  1370. protected int calculateTabAreaHeight(int tabPlacement, int horizRunCount, int maxTabHeight) {
  1371. Insets tabAreaInsets = getTabAreaInsets(tabPlacement);
  1372. int tabRunOverlay = getTabRunOverlay(tabPlacement);
  1373. return (horizRunCount > 0?
  1374. horizRunCount * (maxTabHeight-tabRunOverlay) + tabRunOverlay +
  1375. tabAreaInsets.top + tabAreaInsets.bottom :
  1376. 0);
  1377. }
  1378. protected int calculateTabAreaWidth(int tabPlacement, int vertRunCount, int maxTabWidth) {
  1379. Insets tabAreaInsets = getTabAreaInsets(tabPlacement);
  1380. int tabRunOverlay = getTabRunOverlay(tabPlacement);
  1381. return (vertRunCount > 0?
  1382. vertRunCount * (maxTabWidth-tabRunOverlay) + tabRunOverlay +
  1383. tabAreaInsets.left + tabAreaInsets.right :
  1384. 0);
  1385. }
  1386. protected Insets getTabInsets(int tabPlacement, int tabIndex) {
  1387. return tabInsets;
  1388. }
  1389. protected Insets getSelectedTabPadInsets(int tabPlacement) {
  1390. rotateInsets(selectedTabPadInsets, currentPadInsets, tabPlacement);
  1391. return currentPadInsets;
  1392. }
  1393. protected Insets getTabAreaInsets(int tabPlacement) {
  1394. rotateInsets(tabAreaInsets, currentTabAreaInsets, tabPlacement);
  1395. return currentTabAreaInsets;
  1396. }
  1397. protected Insets getContentBorderInsets(int tabPlacement) {
  1398. return contentBorderInsets;
  1399. }
  1400. protected FontMetrics getFontMetrics() {
  1401. Font font = tabPane.getFont();
  1402. return tabPane.getFontMetrics(font);
  1403. }
  1404. // Tab Navigation methods
  1405. protected void navigateSelectedTab(int direction) {
  1406. int tabPlacement = tabPane.getTabPlacement();
  1407. int current = DefaultLookup.getBoolean(tabPane, this,
  1408. "TabbedPane.selectionFollowsFocus", true) ?
  1409. tabPane.getSelectedIndex() : getFocusIndex();
  1410. int tabCount = tabPane.getTabCount();
  1411. boolean leftToRight = BasicGraphicsUtils.isLeftToRight(tabPane);
  1412. // If we have no tabs then don't navigate.
  1413. if (tabCount <= 0) {
  1414. return;
  1415. }
  1416. int offset;
  1417. switch(tabPlacement) {
  1418. case LEFT:
  1419. case RIGHT:
  1420. switch(direction) {
  1421. case NEXT:
  1422. selectNextTab(current);
  1423. break;
  1424. case PREVIOUS:
  1425. selectPreviousTab(current);
  1426. break;
  1427. case NORTH:
  1428. selectPreviousTabInRun(current);
  1429. break;
  1430. case SOUTH:
  1431. selectNextTabInRun(current);
  1432. break;
  1433. case WEST:
  1434. offset = getTabRunOffset(tabPlacement, tabCount, current, false);
  1435. selectAdjacentRunTab(tabPlacement, current, offset);
  1436. break;
  1437. case EAST:
  1438. offset = getTabRunOffset(tabPlacement, tabCount, current, true);
  1439. selectAdjacentRunTab(tabPlacement, current, offset);
  1440. break;
  1441. default:
  1442. }
  1443. break;
  1444. case BOTTOM:
  1445. case TOP:
  1446. default:
  1447. switch(direction) {
  1448. case NEXT:
  1449. selectNextTab(current);
  1450. break;
  1451. case PREVIOUS:
  1452. selectPreviousTab(current);
  1453. break;
  1454. case NORTH:
  1455. offset = getTabRunOffset(tabPlacement, tabCount, current, false);
  1456. selectAdjacentRunTab(tabPlacement, current, offset);
  1457. break;
  1458. case SOUTH:
  1459. offset = getTabRunOffset(tabPlacement, tabCount, current, true);
  1460. selectAdjacentRunTab(tabPlacement, current, offset);
  1461. break;
  1462. case EAST:
  1463. if (leftToRight) {
  1464. selectNextTabInRun(current);
  1465. } else {
  1466. selectPreviousTabInRun(current);
  1467. }
  1468. break;
  1469. case WEST:
  1470. if (leftToRight) {
  1471. selectPreviousTabInRun(current);
  1472. } else {
  1473. selectNextTabInRun(current);
  1474. }
  1475. break;
  1476. default:
  1477. }
  1478. }
  1479. }
  1480. protected void selectNextTabInRun(int current) {
  1481. int tabCount = tabPane.getTabCount();
  1482. int tabIndex = getNextTabIndexInRun(tabCount, current);
  1483. while(tabIndex != current && !tabPane.isEnabledAt(tabIndex)) {
  1484. tabIndex = getNextTabIndexInRun(tabCount, tabIndex);
  1485. }
  1486. navigateTo(tabIndex);
  1487. }
  1488. protected void selectPreviousTabInRun(int current) {
  1489. int tabCount = tabPane.getTabCount();
  1490. int tabIndex = getPreviousTabIndexInRun(tabCount, current);
  1491. while(tabIndex != current && !tabPane.isEnabledAt(tabIndex)) {
  1492. tabIndex = getPreviousTabIndexInRun(tabCount, tabIndex);
  1493. }
  1494. navigateTo(tabIndex);
  1495. }
  1496. protected void selectNextTab(int current) {
  1497. int tabIndex = getNextTabIndex(current);
  1498. while (tabIndex != current && !tabPane.isEnabledAt(tabIndex)) {
  1499. tabIndex = getNextTabIndex(tabIndex);
  1500. }
  1501. navigateTo(tabIndex);
  1502. }
  1503. protected void selectPreviousTab(int current) {
  1504. int tabIndex = getPreviousTabIndex(current);
  1505. while (tabIndex != current && !tabPane.isEnabledAt(tabIndex)) {
  1506. tabIndex = getPreviousTabIndex(tabIndex);
  1507. }
  1508. navigateTo(tabIndex);
  1509. }
  1510. protected void selectAdjacentRunTab(int tabPlacement,
  1511. int tabIndex, int offset) {
  1512. if ( runCount < 2 ) {
  1513. return;
  1514. }
  1515. int newIndex;
  1516. Rectangle r = rects[tabIndex];
  1517. switch(tabPlacement) {
  1518. case LEFT:
  1519. case RIGHT:
  1520. newIndex = tabForCoordinate(tabPane, r.x + r.width2 + offset,
  1521. r.y + r.height2);
  1522. break;
  1523. case BOTTOM:
  1524. case TOP:
  1525. default:
  1526. newIndex = tabForCoordinate(tabPane, r.x + r.width2,
  1527. r.y + r.height2 + offset);
  1528. }
  1529. if (newIndex != -1) {
  1530. while (!tabPane.isEnabledAt(newIndex) && newIndex != tabIndex) {
  1531. newIndex = getNextTabIndex(newIndex);
  1532. }
  1533. navigateTo(newIndex);
  1534. }
  1535. }
  1536. private void navigateTo(int index) {
  1537. if (DefaultLookup.getBoolean(tabPane, this,
  1538. "TabbedPane.selectionFollowsFocus", true)) {
  1539. setFocusIndex(index);
  1540. tabPane.setSelectedIndex(index);
  1541. } else {
  1542. // Just move focus (not selection)
  1543. int oldFocusIndex = focusIndex;
  1544. setFocusIndex(index);
  1545. tabPane.repaint(getTabBounds(tabPane, focusIndex));
  1546. if (oldFocusIndex != -1) {
  1547. tabPane.repaint(getTabBounds(tabPane, oldFocusIndex));
  1548. }
  1549. }
  1550. }
  1551. void setFocusIndex(int index) {
  1552. focusIndex = index;
  1553. }
  1554. /**
  1555. * Returns the index of the tab that has focus.
  1556. *
  1557. * @return index of tab that has focus
  1558. * @since 1.5
  1559. */
  1560. protected int getFocusIndex() {
  1561. return focusIndex;
  1562. }
  1563. protected int getTabRunOffset(int tabPlacement, int tabCount,
  1564. int tabIndex, boolean forward) {
  1565. int run = getRunForTab(tabCount, tabIndex);
  1566. int offset;
  1567. switch(tabPlacement) {
  1568. case LEFT: {
  1569. if (run == 0) {
  1570. offset = (forward?
  1571. -(calculateTabAreaWidth(tabPlacement, runCount, maxTabWidth)-maxTabWidth) :
  1572. -maxTabWidth);
  1573. } else if (run == runCount - 1) {
  1574. offset = (forward?
  1575. maxTabWidth :
  1576. calculateTabAreaWidth(tabPlacement, runCount, maxTabWidth)-maxTabWidth);
  1577. } else {
  1578. offset = (forward? maxTabWidth : -maxTabWidth);
  1579. }
  1580. break;
  1581. }
  1582. case RIGHT: {
  1583. if (run == 0) {
  1584. offset = (forward?
  1585. maxTabWidth :
  1586. calculateTabAreaWidth(tabPlacement, runCount, maxTabWidth)-maxTabWidth);
  1587. } else if (run == runCount - 1) {
  1588. offset = (forward?
  1589. -(calculateTabAreaWidth(tabPlacement, runCount, maxTabWidth)-maxTabWidth) :
  1590. -maxTabWidth);
  1591. } else {
  1592. offset = (forward? maxTabWidth : -maxTabWidth);
  1593. }
  1594. break;
  1595. }
  1596. case BOTTOM: {
  1597. if (run == 0) {
  1598. offset = (forward?
  1599. maxTabHeight :
  1600. calculateTabAreaHeight(tabPlacement, runCount, maxTabHeight)-maxTabHeight);
  1601. } else if (run == runCount - 1) {
  1602. offset = (forward?
  1603. -(calculateTabAreaHeight(tabPlacement, runCount, maxTabHeight)-maxTabHeight) :
  1604. -maxTabHeight);
  1605. } else {
  1606. offset = (forward? maxTabHeight : -maxTabHeight);
  1607. }
  1608. break;
  1609. }
  1610. case TOP:
  1611. default: {
  1612. if (run == 0) {
  1613. offset = (forward?
  1614. -(calculateTabAreaHeight(tabPlacement, runCount, maxTabHeight)-maxTabHeight) :
  1615. -maxTabHeight);
  1616. } else if (run == runCount - 1) {
  1617. offset = (forward?
  1618. maxTabHeight :
  1619. calculateTabAreaHeight(tabPlacement, runCount, maxTabHeight)-maxTabHeight);
  1620. } else {
  1621. offset = (forward? maxTabHeight : -maxTabHeight);
  1622. }
  1623. }
  1624. }
  1625. return offset;
  1626. }
  1627. protected int getPreviousTabIndex(int base) {
  1628. int tabIndex = (base - 1 >= 0? base - 1 : tabPane.getTabCount() - 1);
  1629. return (tabIndex >= 0? tabIndex : 0);
  1630. }
  1631. protected int getNextTabIndex(int base) {
  1632. return (base+1)%tabPane.getTabCount();
  1633. }
  1634. protected int getNextTabIndexInRun(int tabCount, int base) {
  1635. if (runCount < 2) {
  1636. return getNextTabIndex(base);
  1637. }
  1638. int currentRun = getRunForTab(tabCount, base);
  1639. int next = getNextTabIndex(base);
  1640. if (next == tabRuns[getNextTabRun(currentRun)]) {
  1641. return tabRuns[currentRun];
  1642. }
  1643. return next;
  1644. }
  1645. protected int getPreviousTabIndexInRun(int tabCount, int base) {
  1646. if (runCount < 2) {
  1647. return getPreviousTabIndex(base);
  1648. }
  1649. int currentRun = getRunForTab(tabCount, base);
  1650. if (base == tabRuns[currentRun]) {
  1651. int previous = tabRuns[getNextTabRun(currentRun)]-1;
  1652. return (previous != -1? previous : tabCount-1);
  1653. }
  1654. return getPreviousTabIndex(base);
  1655. }
  1656. protected int getPreviousTabRun(int baseRun) {
  1657. int runIndex = (baseRun - 1 >= 0? baseRun - 1 : runCount - 1);
  1658. return (runIndex >= 0? runIndex : 0);
  1659. }
  1660. protected int getNextTabRun(int baseRun) {
  1661. return (baseRun+1)%runCount;
  1662. }
  1663. protected static void rotateInsets(Insets topInsets, Insets targetInsets, int targetPlacement) {
  1664. switch(targetPlacement) {
  1665. case LEFT:
  1666. targetInsets.top = topInsets.left;
  1667. targetInsets.left = topInsets.top;
  1668. targetInsets.bottom = topInsets.right;
  1669. targetInsets.right = topInsets.bottom;
  1670. break;
  1671. case BOTTOM:
  1672. targetInsets.top = topInsets.bottom;
  1673. targetInsets.left = topInsets.left;
  1674. targetInsets.bottom = topInsets.top;
  1675. targetInsets.right = topInsets.right;
  1676. break;
  1677. case RIGHT:
  1678. targetInsets.top = topInsets.left;
  1679. targetInsets.left = topInsets.bottom;
  1680. targetInsets.bottom = topInsets.right;
  1681. targetInsets.right = topInsets.top;
  1682. break;
  1683. case TOP:
  1684. default:
  1685. targetInsets.top = topInsets.top;
  1686. targetInsets.left = topInsets.left;
  1687. targetInsets.bottom = topInsets.bottom;
  1688. targetInsets.right = topInsets.right;
  1689. }
  1690. }
  1691. // REMIND(aim,7/29/98): This method should be made
  1692. // protected in the next release where
  1693. // API changes are allowed
  1694. //
  1695. boolean requestFocusForVisibleComponent() {
  1696. Component visibleComponent = getVisibleComponent();
  1697. if (visibleComponent != null && visibleComponent.isFocusTraversable()) {
  1698. BasicLookAndFeel.compositeRequestFocus(visibleComponent);
  1699. return true;
  1700. } else if (visibleComponent instanceof JComponent) {
  1701. if (((JComponent)visibleComponent).requestDefaultFocus()) {
  1702. return true;
  1703. }
  1704. }
  1705. return false;
  1706. }
  1707. private static class Actions extends UIAction {
  1708. final static String NEXT = "navigateNext";
  1709. final static String PREVIOUS = "navigatePrevious";
  1710. final static String RIGHT = "navigateRight";
  1711. final static String LEFT = "navigateLeft";
  1712. final static String UP = "navigateUp";
  1713. final static String DOWN = "navigateDown";
  1714. final static String PAGE_UP = "navigatePageUp";
  1715. final static String PAGE_DOWN = "navigatePageDown";
  1716. final static String REQUEST_FOCUS = "requestFocus";
  1717. final static String REQUEST_FOCUS_FOR_VISIBLE =
  1718. "requestFocusForVisibleComponent";
  1719. final static String SET_SELECTED = "setSelectedIndex";
  1720. final static String SELECT_FOCUSED = "selectTabWithFocus";
  1721. final static String SCROLL_FORWARD = "scrollTabsForwardAction";
  1722. final static String SCROLL_BACKWARD = "scrollTabsBackwardAction";
  1723. Actions(String key) {
  1724. super(key);
  1725. }
  1726. public void actionPerformed(ActionEvent e) {
  1727. String key = getName();
  1728. JTabbedPane pane = (JTabbedPane)e.getSource();
  1729. BasicTabbedPaneUI ui = (BasicTabbedPaneUI)BasicLookAndFeel.
  1730. getUIOfType(pane.getUI(), BasicTabbedPaneUI.class);
  1731. if (ui == null) {
  1732. return;
  1733. }
  1734. if (key == NEXT) {
  1735. ui.navigateSelectedTab(SwingConstants.NEXT);
  1736. }
  1737. else if (key == PREVIOUS) {
  1738. ui.navigateSelectedTab(SwingConstants.PREVIOUS);
  1739. }
  1740. else if (key == RIGHT) {
  1741. ui.navigateSelectedTab(SwingConstants.EAST);
  1742. }
  1743. else if (key == LEFT) {
  1744. ui.navigateSelectedTab(SwingConstants.WEST);
  1745. }
  1746. else if (key == UP) {
  1747. ui.navigateSelectedTab(SwingConstants.NORTH);
  1748. }
  1749. else if (key == DOWN) {
  1750. ui.navigateSelectedTab(SwingConstants.SOUTH);
  1751. }
  1752. else if (key == PAGE_UP) {
  1753. int tabPlacement = pane.getTabPlacement();
  1754. if (tabPlacement == TOP|| tabPlacement == BOTTOM) {
  1755. ui.navigateSelectedTab(SwingConstants.WEST);
  1756. } else {
  1757. ui.navigateSelectedTab(SwingConstants.NORTH);
  1758. }
  1759. }
  1760. else if (key == PAGE_DOWN) {
  1761. int tabPlacement = pane.getTabPlacement();
  1762. if (tabPlacement == TOP || tabPlacement == BOTTOM) {
  1763. ui.navigateSelectedTab(SwingConstants.EAST);
  1764. } else {
  1765. ui.navigateSelectedTab(SwingConstants.SOUTH);
  1766. }
  1767. }
  1768. else if (key == REQUEST_FOCUS) {
  1769. pane.requestFocus();
  1770. }
  1771. else if (key == REQUEST_FOCUS_FOR_VISIBLE) {
  1772. ui.requestFocusForVisibleComponent();
  1773. }
  1774. else if (key == SET_SELECTED) {
  1775. String command = e.getActionCommand();
  1776. if (command != null && command.length() > 0) {
  1777. int mnemonic = (int)e.getActionCommand().charAt(0);
  1778. if (mnemonic >= 'a' && mnemonic <='z') {
  1779. mnemonic -= ('a' - 'A');
  1780. }
  1781. Integer index = (Integer)ui.mnemonicToIndexMap.
  1782. get(new Integer(mnemonic));
  1783. if (index != null && pane.isEnabledAt(index.intValue())) {
  1784. pane.setSelectedIndex(index.intValue());
  1785. }
  1786. }
  1787. }
  1788. else if (key == SELECT_FOCUSED) {
  1789. int focusIndex = ui.getFocusIndex();
  1790. if (focusIndex != -1) {
  1791. pane.setSelectedIndex(focusIndex);
  1792. }
  1793. }
  1794. else if (key == SCROLL_FORWARD) {
  1795. if (ui.scrollableTabLayoutEnabled()) {
  1796. ui.tabScroller.scrollForward(pane.getTabPlacement());
  1797. }
  1798. }
  1799. else if (key == SCROLL_BACKWARD) {
  1800. if (ui.scrollableTabLayoutEnabled()) {
  1801. ui.tabScroller.scrollBackward(pane.getTabPlacement());
  1802. }
  1803. }
  1804. }
  1805. }
  1806. /**
  1807. * This class should be treated as a "protected" inner class.
  1808. * Instantiate it only within subclasses of BasicTabbedPaneUI.
  1809. */
  1810. public class TabbedPaneLayout implements LayoutManager {
  1811. public void addLayoutComponent(String name, Component comp) {}
  1812. public void removeLayoutComponent(Component comp) {}
  1813. public Dimension preferredLayoutSize(Container parent) {
  1814. return calculateSize(false);
  1815. }
  1816. public Dimension minimumLayoutSize(Container parent) {
  1817. return calculateSize(true);
  1818. }
  1819. protected Dimension calculateSize(boolean minimum) {
  1820. int tabPlacement = tabPane.getTabPlacement();
  1821. Insets insets = tabPane.getInsets();
  1822. Insets contentInsets = getContentBorderInsets(tabPlacement);
  1823. Insets tabAreaInsets = getTabAreaInsets(tabPlacement);
  1824. Dimension zeroSize = new Dimension(0,0);
  1825. int height = 0;
  1826. int width = 0;
  1827. int cWidth = 0;
  1828. int cHeight = 0;
  1829. // Determine minimum size required to display largest
  1830. // child in each dimension
  1831. //
  1832. for (int i = 0; i < tabPane.getTabCount(); i++) {
  1833. Component component = tabPane.getComponentAt(i);
  1834. if (component != null) {
  1835. Dimension size = zeroSize;
  1836. size = minimum? component.getMinimumSize() :
  1837. component.getPreferredSize();
  1838. if (size != null) {
  1839. cHeight = Math.max(size.height, cHeight);
  1840. cWidth = Math.max(size.width, cWidth);
  1841. }
  1842. }
  1843. }
  1844. // Add content border insets to minimum size
  1845. width += cWidth;
  1846. height += cHeight;
  1847. int tabExtent = 0;
  1848. // Calculate how much space the tabs will need, based on the
  1849. // minimum size required to display largest child + content border
  1850. //
  1851. switch(tabPlacement) {
  1852. case LEFT:
  1853. case RIGHT:
  1854. height = Math.max(height, calculateMaxTabHeight(tabPlacement));
  1855. tabExtent = preferredTabAreaWidth(tabPlacement, height - tabAreaInsets.top - tabAreaInsets.bottom);
  1856. width += tabExtent;
  1857. break;
  1858. case TOP:
  1859. case BOTTOM:
  1860. default:
  1861. width = Math.max(width, calculateMaxTabWidth(tabPlacement));
  1862. tabExtent = preferredTabAreaHeight(tabPlacement, width - tabAreaInsets.left - tabAreaInsets.right);
  1863. height += tabExtent;
  1864. }
  1865. return new Dimension(width + insets.left + insets.right + contentInsets.left + contentInsets.right,
  1866. height + insets.bottom + insets.top + contentInsets.top + contentInsets.bottom);
  1867. }
  1868. protected int preferredTabAreaHeight(int tabPlacement, int width) {
  1869. FontMetrics metrics = getFontMetrics();
  1870. int tabCount = tabPane.getTabCount();
  1871. int total = 0;
  1872. if (tabCount > 0) {
  1873. int rows = 1;
  1874. int x = 0;
  1875. int maxTabHeight = calculateMaxTabHeight(tabPlacement);
  1876. for (int i = 0; i < tabCount; i++) {
  1877. int tabWidth = calculateTabWidth(tabPlacement, i, metrics);
  1878. if (x != 0 && x + tabWidth > width) {
  1879. rows++;
  1880. x = 0;
  1881. }
  1882. x += tabWidth;
  1883. }
  1884. total = calculateTabAreaHeight(tabPlacement, rows, maxTabHeight);
  1885. }
  1886. return total;
  1887. }
  1888. protected int preferredTabAreaWidth(int tabPlacement, int height) {
  1889. FontMetrics metrics = getFontMetrics();
  1890. int tabCount = tabPane.getTabCount();
  1891. int total = 0;
  1892. if (tabCount > 0) {
  1893. int columns = 1;
  1894. int y = 0;
  1895. int fontHeight = metrics.getHeight();
  1896. maxTabWidth = calculateMaxTabWidth(tabPlacement);
  1897. for (int i = 0; i < tabCount; i++) {
  1898. int tabHeight = calculateTabHeight(tabPlacement, i, fontHeight);
  1899. if (y != 0 && y + tabHeight > height) {
  1900. columns++;
  1901. y = 0;
  1902. }
  1903. y += tabHeight;
  1904. }
  1905. total = calculateTabAreaWidth(tabPlacement, columns, maxTabWidth);
  1906. }
  1907. return total;
  1908. }
  1909. public void layoutContainer(Container parent) {
  1910. setRolloverTab(-1);
  1911. int tabPlacement = tabPane.getTabPlacement();
  1912. Insets insets = tabPane.getInsets();
  1913. int selectedIndex = tabPane.getSelectedIndex();
  1914. Component visibleComponent = getVisibleComponent();
  1915. calculateLayoutInfo();
  1916. if (selectedIndex < 0) {
  1917. if (visibleComponent != null) {
  1918. // The last tab was removed, so remove the component
  1919. setVisibleComponent(null);
  1920. }
  1921. } else {
  1922. int cx, cy, cw, ch;
  1923. int totalTabWidth = 0;
  1924. int totalTabHeight = 0;
  1925. Insets contentInsets = getContentBorderInsets(tabPlacement);
  1926. Component selectedComponent = tabPane.getComponentAt(selectedIndex);
  1927. boolean shouldChangeFocus = false;
  1928. // In order to allow programs to use a single component
  1929. // as the display for multiple tabs, we will not change
  1930. // the visible compnent if the currently selected tab
  1931. // has a null component. This is a bit dicey, as we don't
  1932. // explicitly state we support this in the spec, but since
  1933. // programs are now depending on this, we're making it work.
  1934. //
  1935. if (selectedComponent != null) {
  1936. if (selectedComponent != visibleComponent &&
  1937. visibleComponent != null) {
  1938. if (SwingUtilities.findFocusOwner(visibleComponent) != null) {
  1939. shouldChangeFocus = true;
  1940. }
  1941. }
  1942. setVisibleComponent(selectedComponent);
  1943. }
  1944. Rectangle bounds = tabPane.getBounds();
  1945. int numChildren = tabPane.getComponentCount();
  1946. if (numChildren > 0) {
  1947. switch(tabPlacement) {
  1948. case LEFT:
  1949. totalTabWidth = calculateTabAreaWidth(tabPlacement, runCount, maxTabWidth);
  1950. cx = insets.left + totalTabWidth + contentInsets.left;
  1951. cy = insets.top + contentInsets.top;
  1952. break;
  1953. case RIGHT:
  1954. totalTabWidth = calculateTabAreaWidth(tabPlacement, runCount, maxTabWidth);
  1955. cx = insets.left + contentInsets.left;
  1956. cy = insets.top + contentInsets.top;
  1957. break;
  1958. case BOTTOM:
  1959. totalTabHeight = calculateTabAreaHeight(tabPlacement, runCount, maxTabHeight);
  1960. cx = insets.left + contentInsets.left;
  1961. cy = insets.top + contentInsets.top;
  1962. break;
  1963. case TOP:
  1964. default:
  1965. totalTabHeight = calculateTabAreaHeight(tabPlacement, runCount, maxTabHeight);
  1966. cx = insets.left + contentInsets.left;
  1967. cy = insets.top + totalTabHeight + contentInsets.top;
  1968. }
  1969. cw = bounds.width - totalTabWidth -
  1970. insets.left - insets.right -
  1971. contentInsets.left - contentInsets.right;
  1972. ch = bounds.height - totalTabHeight -
  1973. insets.top - insets.bottom -
  1974. contentInsets.top - contentInsets.bottom;
  1975. for (int i=0; i < numChildren; i++) {
  1976. Component child = tabPane.getComponent(i);
  1977. child.setBounds(cx, cy, cw, ch);
  1978. }
  1979. }
  1980. if (shouldChangeFocus) {
  1981. if (!requestFocusForVisibleComponent()) {
  1982. tabPane.requestFocus();
  1983. }
  1984. }
  1985. }
  1986. }
  1987. public void calculateLayoutInfo() {
  1988. int tabCount = tabPane.getTabCount();
  1989. assureRectsCreated(tabCount);
  1990. calculateTabRects(tabPane.getTabPlacement(), tabCount);
  1991. isRunsDirty = false;
  1992. }
  1993. protected void calculateTabRects(int tabPlacement, int tabCount) {
  1994. FontMetrics metrics = getFontMetrics();
  1995. Dimension size = tabPane.getSize();
  1996. Insets insets = tabPane.getInsets();
  1997. Insets tabAreaInsets = getTabAreaInsets(tabPlacement);
  1998. int fontHeight = metrics.getHeight();
  1999. int selectedIndex = tabPane.getSelectedIndex();
  2000. int tabRunOverlay;
  2001. int i, j;
  2002. int x, y;
  2003. int returnAt;
  2004. boolean verticalTabRuns = (tabPlacement == LEFT || tabPlacement == RIGHT);
  2005. boolean leftToRight = BasicGraphicsUtils.isLeftToRight(tabPane);
  2006. //
  2007. // Calculate bounds within which a tab run must fit
  2008. //
  2009. switch(tabPlacement) {
  2010. case LEFT:
  2011. maxTabWidth = calculateMaxTabWidth(tabPlacement);
  2012. x = insets.left + tabAreaInsets.left;
  2013. y = insets.top + tabAreaInsets.top;
  2014. returnAt = size.height - (insets.bottom + tabAreaInsets.bottom);
  2015. break;
  2016. case RIGHT:
  2017. maxTabWidth = calculateMaxTabWidth(tabPlacement);
  2018. x = size.width - insets.right - tabAreaInsets.right - maxTabWidth;
  2019. y = insets.top + tabAreaInsets.top;
  2020. returnAt = size.height - (insets.bottom + tabAreaInsets.bottom);
  2021. break;
  2022. case BOTTOM:
  2023. maxTabHeight = calculateMaxTabHeight(tabPlacement);
  2024. x = insets.left + tabAreaInsets.left;
  2025. y = size.height - insets.bottom - tabAreaInsets.bottom - maxTabHeight;
  2026. returnAt = size.width - (insets.right + tabAreaInsets.right);
  2027. break;
  2028. case TOP:
  2029. default:
  2030. maxTabHeight = calculateMaxTabHeight(tabPlacement);
  2031. x = insets.left + tabAreaInsets.left;
  2032. y = insets.top + tabAreaInsets.top;
  2033. returnAt = size.width - (insets.right + tabAreaInsets.right);
  2034. break;
  2035. }
  2036. tabRunOverlay = getTabRunOverlay(tabPlacement);
  2037. runCount = 0;
  2038. selectedRun = -1;
  2039. if (tabCount == 0) {
  2040. return;
  2041. }
  2042. // Run through tabs and partition them into runs
  2043. Rectangle rect;
  2044. for (i = 0; i < tabCount; i++) {
  2045. rect = rects[i];
  2046. if (!verticalTabRuns) {
  2047. // Tabs on TOP or BOTTOM....
  2048. if (i > 0) {
  2049. rect.x = rects[i-1].x + rects[i-1].width;
  2050. } else {
  2051. tabRuns[0] = 0;
  2052. runCount = 1;
  2053. maxTabWidth = 0;
  2054. rect.x = x;
  2055. }
  2056. rect.width = calculateTabWidth(tabPlacement, i, metrics);
  2057. maxTabWidth = Math.max(maxTabWidth, rect.width);
  2058. // Never move a TAB down a run if it is in the first column.
  2059. // Even if there isn't enough room, moving it to a fresh
  2060. // line won't help.
  2061. if (rect.x != 2 + insets.left && rect.x + rect.width > returnAt) {
  2062. if (runCount > tabRuns.length - 1) {
  2063. expandTabRunsArray();
  2064. }
  2065. tabRuns[runCount] = i;
  2066. runCount++;
  2067. rect.x = x;
  2068. }
  2069. // Initialize y position in case there's just one run
  2070. rect.y = y;
  2071. rect.height = maxTabHeight/* - 2*/;
  2072. } else {
  2073. // Tabs on LEFT or RIGHT...
  2074. if (i > 0) {
  2075. rect.y = rects[i-1].y + rects[i-1].height;
  2076. } else {
  2077. tabRuns[0] = 0;
  2078. runCount = 1;
  2079. maxTabHeight = 0;
  2080. rect.y = y;
  2081. }
  2082. rect.height = calculateTabHeight(tabPlacement, i, fontHeight);
  2083. maxTabHeight = Math.max(maxTabHeight, rect.height);
  2084. // Never move a TAB over a run if it is in the first run.
  2085. // Even if there isn't enough room, moving it to a fresh
  2086. // column won't help.
  2087. if (rect.y != 2 + insets.top && rect.y + rect.height > returnAt) {
  2088. if (runCount > tabRuns.length - 1) {
  2089. expandTabRunsArray();
  2090. }
  2091. tabRuns[runCount] = i;
  2092. runCount++;
  2093. rect.y = y;
  2094. }
  2095. // Initialize x position in case there's just one column
  2096. rect.x = x;
  2097. rect.width = maxTabWidth/* - 2*/;
  2098. }
  2099. if (i == selectedIndex) {
  2100. selectedRun = runCount - 1;
  2101. }
  2102. }
  2103. if (runCount > 1) {
  2104. // Re-distribute tabs in case last run has leftover space
  2105. normalizeTabRuns(tabPlacement, tabCount, verticalTabRuns? y : x, returnAt);
  2106. selectedRun = getRunForTab(tabCount, selectedIndex);
  2107. // Rotate run array so that selected run is first
  2108. if (shouldRotateTabRuns(tabPlacement)) {
  2109. rotateTabRuns(tabPlacement, selectedRun);
  2110. }
  2111. }
  2112. // Step through runs from back to front to calculate
  2113. // tab y locations and to pad runs appropriately
  2114. for (i = runCount - 1; i >= 0; i--) {
  2115. int start = tabRuns[i];
  2116. int next = tabRuns[i == (runCount - 1)? 0 : i + 1];
  2117. int end = (next != 0? next - 1 : tabCount - 1);
  2118. if (!verticalTabRuns) {
  2119. for (j = start; j <= end; j++) {
  2120. rect = rects[j];
  2121. rect.y = y;
  2122. rect.x += getTabRunIndent(tabPlacement, i);
  2123. }
  2124. if (shouldPadTabRun(tabPlacement, i)) {
  2125. padTabRun(tabPlacement, start, end, returnAt);
  2126. }
  2127. if (tabPlacement == BOTTOM) {
  2128. y -= (maxTabHeight - tabRunOverlay);
  2129. } else {
  2130. y += (maxTabHeight - tabRunOverlay);
  2131. }
  2132. } else {
  2133. for (j = start; j <= end; j++) {
  2134. rect = rects[j];
  2135. rect.x = x;
  2136. rect.y += getTabRunIndent(tabPlacement, i);
  2137. }
  2138. if (shouldPadTabRun(tabPlacement, i)) {
  2139. padTabRun(tabPlacement, start, end, returnAt);
  2140. }
  2141. if (tabPlacement == RIGHT) {
  2142. x -= (maxTabWidth - tabRunOverlay);
  2143. } else {
  2144. x += (maxTabWidth - tabRunOverlay);
  2145. }
  2146. }
  2147. }
  2148. // Pad the selected tab so that it appears raised in front
  2149. padSelectedTab(tabPlacement, selectedIndex);
  2150. // if right to left and tab placement on the top or
  2151. // the bottom, flip x positions and adjust by widths
  2152. if (!leftToRight && !verticalTabRuns) {
  2153. int rightMargin = size.width
  2154. - (insets.right + tabAreaInsets.right);
  2155. for (i = 0; i < tabCount; i++) {
  2156. rects[i].x = rightMargin - rects[i].x - rects[i].width;
  2157. }
  2158. }
  2159. }
  2160. /*
  2161. * Rotates the run-index array so that the selected run is run[0]
  2162. */
  2163. protected void rotateTabRuns(int tabPlacement, int selectedRun) {
  2164. for (int i = 0; i < selectedRun; i++) {
  2165. int save = tabRuns[0];
  2166. for (int j = 1; j < runCount; j++) {
  2167. tabRuns[j - 1] = tabRuns[j];
  2168. }
  2169. tabRuns[runCount-1] = save;
  2170. }
  2171. }
  2172. protected void normalizeTabRuns(int tabPlacement, int tabCount,
  2173. int start, int max) {
  2174. boolean verticalTabRuns = (tabPlacement == LEFT || tabPlacement == RIGHT);
  2175. int run = runCount - 1;
  2176. boolean keepAdjusting = true;
  2177. double weight = 1.25;
  2178. // At this point the tab runs are packed to fit as many
  2179. // tabs as possible, which can leave the last run with a lot
  2180. // of extra space (resulting in very fat tabs on the last run).
  2181. // So we'll attempt to distribute this extra space more evenly
  2182. // across the runs in order to make the runs look more consistent.
  2183. //
  2184. // Starting with the last run, determine whether the last tab in
  2185. // the previous run would fit (generously) in this run; if so,
  2186. // move tab to current run and shift tabs accordingly. Cycle
  2187. // through remaining runs using the same algorithm.
  2188. //
  2189. while (keepAdjusting) {
  2190. int last = lastTabInRun(tabCount, run);
  2191. int prevLast = lastTabInRun(tabCount, run-1);
  2192. int end;
  2193. int prevLastLen;
  2194. if (!verticalTabRuns) {
  2195. end = rects[last].x + rects[last].width;
  2196. prevLastLen = (int)(maxTabWidth*weight);
  2197. } else {
  2198. end = rects[last].y + rects[last].height;
  2199. prevLastLen = (int)(maxTabHeight*weight*2);
  2200. }
  2201. // Check if the run has enough extra space to fit the last tab
  2202. // from the previous row...
  2203. if (max - end > prevLastLen) {
  2204. // Insert tab from previous row and shift rest over
  2205. tabRuns[run] = prevLast;
  2206. if (!verticalTabRuns) {
  2207. rects[prevLast].x = start;
  2208. } else {
  2209. rects[prevLast].y = start;
  2210. }
  2211. for (int i = prevLast+1; i <= last; i++) {
  2212. if (!verticalTabRuns) {
  2213. rects[i].x = rects[i-1].x + rects[i-1].width;
  2214. } else {
  2215. rects[i].y = rects[i-1].y + rects[i-1].height;
  2216. }
  2217. }
  2218. } else if (run == runCount - 1) {
  2219. // no more room left in last run, so we're done!
  2220. keepAdjusting = false;
  2221. }
  2222. if (run - 1 > 0) {
  2223. // check previous run next...
  2224. run -= 1;
  2225. } else {
  2226. // check last run again...but require a higher ratio
  2227. // of extraspace-to-tabsize because we don't want to
  2228. // end up with too many tabs on the last run!
  2229. run = runCount - 1;
  2230. weight += .25;
  2231. }
  2232. }
  2233. }
  2234. protected void padTabRun(int tabPlacement, int start, int end, int max) {
  2235. Rectangle lastRect = rects[end];
  2236. if (tabPlacement == TOP || tabPlacement == BOTTOM) {
  2237. int runWidth = (lastRect.x + lastRect.width) - rects[start].x;
  2238. int deltaWidth = max - (lastRect.x + lastRect.width);
  2239. float factor = (float)deltaWidth / (float)runWidth;
  2240. for (int j = start; j <= end; j++) {
  2241. Rectangle pastRect = rects[j];
  2242. if (j > start) {
  2243. pastRect.x = rects[j-1].x + rects[j-1].width;
  2244. }
  2245. pastRect.width += Math.round((float)pastRect.width * factor);
  2246. }
  2247. lastRect.width = max - lastRect.x;
  2248. } else {
  2249. int runHeight = (lastRect.y + lastRect.height) - rects[start].y;
  2250. int deltaHeight = max - (lastRect.y + lastRect.height);
  2251. float factor = (float)deltaHeight / (float)runHeight;
  2252. for (int j = start; j <= end; j++) {
  2253. Rectangle pastRect = rects[j];
  2254. if (j > start) {
  2255. pastRect.y = rects[j-1].y + rects[j-1].height;
  2256. }
  2257. pastRect.height += Math.round((float)pastRect.height * factor);
  2258. }
  2259. lastRect.height = max - lastRect.y;
  2260. }
  2261. }
  2262. protected void padSelectedTab(int tabPlacement, int selectedIndex) {
  2263. if (selectedIndex >= 0) {
  2264. Rectangle selRect = rects[selectedIndex];
  2265. Insets padInsets = getSelectedTabPadInsets(tabPlacement);
  2266. selRect.x -= padInsets.left;
  2267. selRect.width += (padInsets.left + padInsets.right);
  2268. selRect.y -= padInsets.top;
  2269. selRect.height += (padInsets.top + padInsets.bottom);
  2270. }
  2271. }
  2272. }
  2273. private class TabbedPaneScrollLayout extends TabbedPaneLayout {
  2274. protected int preferredTabAreaHeight(int tabPlacement, int width) {
  2275. return calculateMaxTabHeight(tabPlacement);
  2276. }
  2277. protected int preferredTabAreaWidth(int tabPlacement, int height) {
  2278. return calculateMaxTabWidth(tabPlacement);
  2279. }
  2280. public void layoutContainer(Container parent) {
  2281. setRolloverTab(-1);
  2282. int tabPlacement = tabPane.getTabPlacement();
  2283. int tabCount = tabPane.getTabCount();
  2284. Insets insets = tabPane.getInsets();
  2285. int selectedIndex = tabPane.getSelectedIndex();
  2286. Component visibleComponent = getVisibleComponent();
  2287. calculateLayoutInfo();
  2288. if (selectedIndex < 0) {
  2289. if (visibleComponent != null) {
  2290. // The last tab was removed, so remove the component
  2291. setVisibleComponent(null);
  2292. }
  2293. } else {
  2294. Component selectedComponent = tabPane.getComponentAt(selectedIndex);
  2295. boolean shouldChangeFocus = false;
  2296. // In order to allow programs to use a single component
  2297. // as the display for multiple tabs, we will not change
  2298. // the visible compnent if the currently selected tab
  2299. // has a null component. This is a bit dicey, as we don't
  2300. // explicitly state we support this in the spec, but since
  2301. // programs are now depending on this, we're making it work.
  2302. //
  2303. if (selectedComponent != null) {
  2304. if (selectedComponent != visibleComponent &&
  2305. visibleComponent != null) {
  2306. if (SwingUtilities.findFocusOwner(visibleComponent) != null) {
  2307. shouldChangeFocus = true;
  2308. }
  2309. }
  2310. setVisibleComponent(selectedComponent);
  2311. }
  2312. int tx, ty, tw, th; // tab area bounds
  2313. int cx, cy, cw, ch; // content area bounds
  2314. Insets contentInsets = getContentBorderInsets(tabPlacement);
  2315. Rectangle bounds = tabPane.getBounds();
  2316. int numChildren = tabPane.getComponentCount();
  2317. if (numChildren > 0) {
  2318. switch(tabPlacement) {
  2319. case LEFT:
  2320. // calculate tab area bounds
  2321. tw = calculateTabAreaWidth(tabPlacement, runCount, maxTabWidth);
  2322. th = bounds.height - insets.top - insets.bottom;
  2323. tx = insets.left;
  2324. ty = insets.top;
  2325. // calculate content area bounds
  2326. cx = tx + tw + contentInsets.left;
  2327. cy = ty + contentInsets.top;
  2328. cw = bounds.width - insets.left - insets.right - tw -
  2329. contentInsets.left - contentInsets.right;
  2330. ch = bounds.height - insets.top - insets.bottom -
  2331. contentInsets.top - contentInsets.bottom;
  2332. break;
  2333. case RIGHT:
  2334. // calculate tab area bounds
  2335. tw = calculateTabAreaWidth(tabPlacement, runCount, maxTabWidth);
  2336. th = bounds.height - insets.top - insets.bottom;
  2337. tx = bounds.width - insets.right - tw;
  2338. ty = insets.top;
  2339. // calculate content area bounds
  2340. cx = insets.left + contentInsets.left;
  2341. cy = insets.top + contentInsets.top;
  2342. cw = bounds.width - insets.left - insets.right - tw -
  2343. contentInsets.left - contentInsets.right;
  2344. ch = bounds.height - insets.top - insets.bottom -
  2345. contentInsets.top - contentInsets.bottom;
  2346. break;
  2347. case BOTTOM:
  2348. // calculate tab area bounds
  2349. tw = bounds.width - insets.left - insets.right;
  2350. th = calculateTabAreaHeight(tabPlacement, runCount, maxTabHeight);
  2351. tx = insets.left;
  2352. ty = bounds.height - insets.bottom - th;
  2353. // calculate content area bounds
  2354. cx = insets.left + contentInsets.left;
  2355. cy = insets.top + contentInsets.top;
  2356. cw = bounds.width - insets.left - insets.right -
  2357. contentInsets.left - contentInsets.right;
  2358. ch = bounds.height - insets.top - insets.bottom - th -
  2359. contentInsets.top - contentInsets.bottom;
  2360. break;
  2361. case TOP:
  2362. default:
  2363. // calculate tab area bounds
  2364. tw = bounds.width - insets.left - insets.right;
  2365. th = calculateTabAreaHeight(tabPlacement, runCount, maxTabHeight);
  2366. tx = insets.left;
  2367. ty = insets.top;
  2368. // calculate content area bounds
  2369. cx = tx + contentInsets.left;
  2370. cy = ty + th + contentInsets.top;
  2371. cw = bounds.width - insets.left - insets.right -
  2372. contentInsets.left - contentInsets.right;
  2373. ch = bounds.height - insets.top - insets.bottom - th -
  2374. contentInsets.top - contentInsets.bottom;
  2375. }
  2376. for (int i=0; i < numChildren; i++) {
  2377. Component child = tabPane.getComponent(i);
  2378. if (tabScroller != null && child == tabScroller.viewport) {
  2379. JViewport viewport = (JViewport)child;
  2380. Rectangle viewRect = viewport.getViewRect();
  2381. int vw = tw;
  2382. int vh = th;
  2383. Dimension butSize = tabScroller.scrollForwardButton.getPreferredSize();
  2384. switch(tabPlacement) {
  2385. case LEFT:
  2386. case RIGHT:
  2387. int totalTabHeight = rects[tabCount-1].y + rects[tabCount-1].height;
  2388. if (totalTabHeight > th) {
  2389. // Allow space for scrollbuttons
  2390. vh = (th > 2*butSize.height) ? th - 2*butSize.height : 0;
  2391. if (totalTabHeight - viewRect.y <= vh) {
  2392. // Scrolled to the end, so ensure the viewport size is
  2393. // such that the scroll offset aligns with a tab
  2394. vh = totalTabHeight - viewRect.y;
  2395. }
  2396. }
  2397. break;
  2398. case BOTTOM:
  2399. case TOP:
  2400. default:
  2401. int totalTabWidth = rects[tabCount-1].x + rects[tabCount-1].width;
  2402. if (totalTabWidth > tw) {
  2403. // Need to allow space for scrollbuttons
  2404. vw = (tw > 2*butSize.width) ? tw - 2*butSize.width : 0 ;
  2405. if (totalTabWidth - viewRect.x <= vw) {
  2406. // Scrolled to the end, so ensure the viewport size is
  2407. // such that the scroll offset aligns with a tab
  2408. vw = totalTabWidth - viewRect.x;
  2409. }
  2410. }
  2411. }
  2412. child.setBounds(tx, ty, vw, vh);
  2413. } else if (tabScroller != null &&
  2414. (child == tabScroller.scrollForwardButton ||
  2415. child == tabScroller.scrollBackwardButton)) {
  2416. Component scrollbutton = child;
  2417. Dimension bsize = scrollbutton.getPreferredSize();
  2418. int bx = 0;
  2419. int by = 0;
  2420. int bw = bsize.width;
  2421. int bh = bsize.height;
  2422. boolean visible = false;
  2423. switch(tabPlacement) {
  2424. case LEFT:
  2425. case RIGHT:
  2426. int totalTabHeight = rects[tabCount-1].y + rects[tabCount-1].height;
  2427. if (totalTabHeight > th) {
  2428. visible = true;
  2429. bx = (tabPlacement == LEFT? tx + tw - bsize.width : tx);
  2430. by = (child == tabScroller.scrollForwardButton)?
  2431. bounds.height - insets.bottom - bsize.height :
  2432. bounds.height - insets.bottom - 2*bsize.height;
  2433. }
  2434. break;
  2435. case BOTTOM:
  2436. case TOP:
  2437. default:
  2438. int totalTabWidth = rects[tabCount-1].x + rects[tabCount-1].width;
  2439. if (totalTabWidth > tw) {
  2440. visible = true;
  2441. bx = (child == tabScroller.scrollForwardButton)?
  2442. bounds.width - insets.left - bsize.width :
  2443. bounds.width - insets.left - 2*bsize.width;
  2444. by = (tabPlacement == TOP? ty + th - bsize.height : ty);
  2445. }
  2446. }
  2447. child.setVisible(visible);
  2448. if (visible) {
  2449. child.setBounds(bx, by, bw, bh);
  2450. }
  2451. } else {
  2452. // All content children...
  2453. child.setBounds(cx, cy, cw, ch);
  2454. }
  2455. }
  2456. if (shouldChangeFocus) {
  2457. if (!requestFocusForVisibleComponent()) {
  2458. tabPane.requestFocus();
  2459. }
  2460. }
  2461. }
  2462. }
  2463. }
  2464. protected void calculateTabRects(int tabPlacement, int tabCount) {
  2465. FontMetrics metrics = getFontMetrics();
  2466. Dimension size = tabPane.getSize();
  2467. Insets insets = tabPane.getInsets();
  2468. Insets tabAreaInsets = getTabAreaInsets(tabPlacement);
  2469. int fontHeight = metrics.getHeight();
  2470. int selectedIndex = tabPane.getSelectedIndex();
  2471. int i, j;
  2472. boolean verticalTabRuns = (tabPlacement == LEFT || tabPlacement == RIGHT);
  2473. boolean leftToRight = BasicGraphicsUtils.isLeftToRight(tabPane);
  2474. int x = tabAreaInsets.left;
  2475. int y = tabAreaInsets.top;
  2476. int totalWidth = 0;
  2477. int totalHeight = 0;
  2478. //
  2479. // Calculate bounds within which a tab run must fit
  2480. //
  2481. switch(tabPlacement) {
  2482. case LEFT:
  2483. case RIGHT:
  2484. maxTabWidth = calculateMaxTabWidth(tabPlacement);
  2485. break;
  2486. case BOTTOM:
  2487. case TOP:
  2488. default:
  2489. maxTabHeight = calculateMaxTabHeight(tabPlacement);
  2490. }
  2491. runCount = 0;
  2492. selectedRun = -1;
  2493. if (tabCount == 0) {
  2494. return;
  2495. }
  2496. selectedRun = 0;
  2497. runCount = 1;
  2498. // Run through tabs and lay them out in a single run
  2499. Rectangle rect;
  2500. for (i = 0; i < tabCount; i++) {
  2501. rect = rects[i];
  2502. if (!verticalTabRuns) {
  2503. // Tabs on TOP or BOTTOM....
  2504. if (i > 0) {
  2505. rect.x = rects[i-1].x + rects[i-1].width;
  2506. } else {
  2507. tabRuns[0] = 0;
  2508. maxTabWidth = 0;
  2509. totalHeight += maxTabHeight;
  2510. rect.x = x;
  2511. }
  2512. rect.width = calculateTabWidth(tabPlacement, i, metrics);
  2513. totalWidth = rect.x + rect.width;
  2514. maxTabWidth = Math.max(maxTabWidth, rect.width);
  2515. rect.y = y;
  2516. rect.height = maxTabHeight/* - 2*/;
  2517. } else {
  2518. // Tabs on LEFT or RIGHT...
  2519. if (i > 0) {
  2520. rect.y = rects[i-1].y + rects[i-1].height;
  2521. } else {
  2522. tabRuns[0] = 0;
  2523. maxTabHeight = 0;
  2524. totalWidth = maxTabWidth;
  2525. rect.y = y;
  2526. }
  2527. rect.height = calculateTabHeight(tabPlacement, i, fontHeight);
  2528. totalHeight = rect.y + rect.height;
  2529. maxTabHeight = Math.max(maxTabHeight, rect.height);
  2530. rect.x = x;
  2531. rect.width = maxTabWidth/* - 2*/;
  2532. }
  2533. }
  2534. if (tabsOverlapBorder) {
  2535. // Pad the selected tab so that it appears raised in front
  2536. padSelectedTab(tabPlacement, selectedIndex);
  2537. }
  2538. // if right to left and tab placement on the top or
  2539. // the bottom, flip x positions and adjust by widths
  2540. if (!leftToRight && !verticalTabRuns) {
  2541. int rightMargin = size.width
  2542. - (insets.right + tabAreaInsets.right);
  2543. for (i = 0; i < tabCount; i++) {
  2544. rects[i].x = rightMargin - rects[i].x - rects[i].width;
  2545. }
  2546. }
  2547. //tabPanel.setSize(totalWidth, totalHeight);
  2548. tabScroller.tabPanel.setPreferredSize(new Dimension(totalWidth, totalHeight));
  2549. }
  2550. }
  2551. private class ScrollableTabSupport implements ActionListener,
  2552. ChangeListener {
  2553. public ScrollableTabViewport viewport;
  2554. public ScrollableTabPanel tabPanel;
  2555. public JButton scrollForwardButton;
  2556. public JButton scrollBackwardButton;
  2557. public int leadingTabIndex;
  2558. private Point tabViewPosition = new Point(0,0);
  2559. ScrollableTabSupport(int tabPlacement) {
  2560. viewport = new ScrollableTabViewport();
  2561. tabPanel = new ScrollableTabPanel();
  2562. viewport.setView(tabPanel);
  2563. viewport.addChangeListener(this);
  2564. createButtons();
  2565. }
  2566. /**
  2567. * Recreates the scroll buttons and adds them to the TabbedPane.
  2568. */
  2569. void createButtons() {
  2570. if (scrollForwardButton != null) {
  2571. tabPane.remove(scrollForwardButton);
  2572. scrollForwardButton.removeActionListener(this);
  2573. tabPane.remove(scrollBackwardButton);
  2574. scrollBackwardButton.removeActionListener(this);
  2575. }
  2576. int tabPlacement = tabPane.getTabPlacement();
  2577. if (tabPlacement == TOP || tabPlacement == BOTTOM) {
  2578. scrollForwardButton = createScrollButton(EAST);
  2579. scrollBackwardButton = createScrollButton(WEST);
  2580. } else { // tabPlacement = LEFT || RIGHT
  2581. scrollForwardButton = createScrollButton(SOUTH);
  2582. scrollBackwardButton = createScrollButton(NORTH);
  2583. }
  2584. scrollForwardButton.addActionListener(this);
  2585. scrollBackwardButton.addActionListener(this);
  2586. tabPane.add(scrollForwardButton);
  2587. tabPane.add(scrollBackwardButton);
  2588. }
  2589. public void scrollForward(int tabPlacement) {
  2590. Dimension viewSize = viewport.getViewSize();
  2591. Rectangle viewRect = viewport.getViewRect();
  2592. if (tabPlacement == TOP || tabPlacement == BOTTOM) {
  2593. if (viewRect.width >= viewSize.width - viewRect.x) {
  2594. return; // no room left to scroll
  2595. }
  2596. } else { // tabPlacement == LEFT || tabPlacement == RIGHT
  2597. if (viewRect.height >= viewSize.height - viewRect.y) {
  2598. return;
  2599. }
  2600. }
  2601. setLeadingTabIndex(tabPlacement, leadingTabIndex+1);
  2602. }
  2603. public void scrollBackward(int tabPlacement) {
  2604. if (leadingTabIndex == 0) {
  2605. return; // no room left to scroll
  2606. }
  2607. setLeadingTabIndex(tabPlacement, leadingTabIndex-1);
  2608. }
  2609. public void setLeadingTabIndex(int tabPlacement, int index) {
  2610. leadingTabIndex = index;
  2611. Dimension viewSize = viewport.getViewSize();
  2612. Rectangle viewRect = viewport.getViewRect();
  2613. switch(tabPlacement) {
  2614. case TOP:
  2615. case BOTTOM:
  2616. tabViewPosition.x = leadingTabIndex == 0? 0 : rects[leadingTabIndex].x;
  2617. if ((viewSize.width - tabViewPosition.x) < viewRect.width) {
  2618. // We've scrolled to the end, so adjust the viewport size
  2619. // to ensure the view position remains aligned on a tab boundary
  2620. Dimension extentSize = new Dimension(viewSize.width - tabViewPosition.x,
  2621. viewRect.height);
  2622. viewport.setExtentSize(extentSize);
  2623. }
  2624. break;
  2625. case LEFT:
  2626. case RIGHT:
  2627. tabViewPosition.y = leadingTabIndex == 0? 0 : rects[leadingTabIndex].y;
  2628. if ((viewSize.height - tabViewPosition.y) < viewRect.height) {
  2629. // We've scrolled to the end, so adjust the viewport size
  2630. // to ensure the view position remains aligned on a tab boundary
  2631. Dimension extentSize = new Dimension(viewRect.width,
  2632. viewSize.height - tabViewPosition.y);
  2633. viewport.setExtentSize(extentSize);
  2634. }
  2635. }
  2636. viewport.setViewPosition(tabViewPosition);
  2637. }
  2638. public void stateChanged(ChangeEvent e) {
  2639. JViewport viewport = (JViewport)e.getSource();
  2640. int tabPlacement = tabPane.getTabPlacement();
  2641. int tabCount = tabPane.getTabCount();
  2642. Rectangle vpRect = viewport.getBounds();
  2643. Dimension viewSize = viewport.getViewSize();
  2644. Rectangle viewRect = viewport.getViewRect();
  2645. leadingTabIndex = getClosestTab(viewRect.x, viewRect.y);
  2646. // If the tab isn't right aligned, adjust it.
  2647. if (leadingTabIndex + 1 < tabCount) {
  2648. switch (tabPlacement) {
  2649. case TOP:
  2650. case BOTTOM:
  2651. if (rects[leadingTabIndex].x < viewRect.x) {
  2652. leadingTabIndex++;
  2653. }
  2654. break;
  2655. case LEFT:
  2656. case RIGHT:
  2657. if (rects[leadingTabIndex].y < viewRect.y) {
  2658. leadingTabIndex++;
  2659. }
  2660. break;
  2661. }
  2662. }
  2663. Insets contentInsets = getContentBorderInsets(tabPlacement);
  2664. switch(tabPlacement) {
  2665. case LEFT:
  2666. tabPane.repaint(vpRect.x+vpRect.width, vpRect.y,
  2667. contentInsets.left, vpRect.height);
  2668. scrollBackwardButton.setEnabled(
  2669. viewRect.y > 0 && leadingTabIndex > 0);
  2670. scrollForwardButton.setEnabled(
  2671. leadingTabIndex < tabCount-1 &&
  2672. viewSize.height-viewRect.y > viewRect.height);
  2673. break;
  2674. case RIGHT:
  2675. tabPane.repaint(vpRect.x-contentInsets.right, vpRect.y,
  2676. contentInsets.right, vpRect.height);
  2677. scrollBackwardButton.setEnabled(
  2678. viewRect.y > 0 && leadingTabIndex > 0);
  2679. scrollForwardButton.setEnabled(
  2680. leadingTabIndex < tabCount-1 &&
  2681. viewSize.height-viewRect.y > viewRect.height);
  2682. break;
  2683. case BOTTOM:
  2684. tabPane.repaint(vpRect.x, vpRect.y-contentInsets.bottom,
  2685. vpRect.width, contentInsets.bottom);
  2686. scrollBackwardButton.setEnabled(
  2687. viewRect.x > 0 && leadingTabIndex > 0);
  2688. scrollForwardButton.setEnabled(
  2689. leadingTabIndex < tabCount-1 &&
  2690. viewSize.width-viewRect.x > viewRect.width);
  2691. break;
  2692. case TOP:
  2693. default:
  2694. tabPane.repaint(vpRect.x, vpRect.y+vpRect.height,
  2695. vpRect.width, contentInsets.top);
  2696. scrollBackwardButton.setEnabled(
  2697. viewRect.x > 0 && leadingTabIndex > 0);
  2698. scrollForwardButton.setEnabled(
  2699. leadingTabIndex < tabCount-1 &&
  2700. viewSize.width-viewRect.x > viewRect.width);
  2701. }
  2702. }
  2703. /**
  2704. * ActionListener for the scroll buttons.
  2705. */
  2706. public void actionPerformed(ActionEvent e) {
  2707. ActionMap map = tabPane.getActionMap();
  2708. if (map != null) {
  2709. String actionKey;
  2710. if (e.getSource() == scrollForwardButton) {
  2711. actionKey = "scrollTabsForwardAction";
  2712. }
  2713. else {
  2714. actionKey = "scrollTabsBackwardAction";
  2715. }
  2716. Action action = map.get(actionKey);
  2717. if (action != null && action.isEnabled()) {
  2718. action.actionPerformed(new ActionEvent(tabPane,
  2719. ActionEvent.ACTION_PERFORMED, null, e.getWhen(),
  2720. e.getModifiers()));
  2721. }
  2722. }
  2723. }
  2724. public String toString() {
  2725. return new String("viewport.viewSize="+viewport.getViewSize()+"\n"+
  2726. "viewport.viewRectangle="+viewport.getViewRect()+"\n"+
  2727. "leadingTabIndex="+leadingTabIndex+"\n"+
  2728. "tabViewPosition="+tabViewPosition);
  2729. }
  2730. }
  2731. private class ScrollableTabViewport extends JViewport implements UIResource {
  2732. public ScrollableTabViewport() {
  2733. super();
  2734. setName("TabbedPane.scrollableViewport");
  2735. setScrollMode(SIMPLE_SCROLL_MODE);
  2736. setOpaque(tabPane.isOpaque());
  2737. Color bgColor = UIManager.getColor("TabbedPane.tabAreaBackground");
  2738. if (bgColor == null) {
  2739. bgColor = tabPane.getBackground();
  2740. }
  2741. setBackground(bgColor);
  2742. }
  2743. }
  2744. private class ScrollableTabPanel extends JPanel implements UIResource {
  2745. public ScrollableTabPanel() {
  2746. setLayout(null);
  2747. setOpaque(tabPane.isOpaque());
  2748. Color bgColor = UIManager.getColor("TabbedPane.tabAreaBackground");
  2749. if (bgColor == null) {
  2750. bgColor = tabPane.getBackground();
  2751. }
  2752. setBackground(bgColor);
  2753. }
  2754. public void paintComponent(Graphics g) {
  2755. super.paintComponent(g);
  2756. BasicTabbedPaneUI.this.paintTabArea(g, tabPane.getTabPlacement(),
  2757. tabPane.getSelectedIndex());
  2758. }
  2759. }
  2760. private class ScrollableTabButton extends BasicArrowButton implements UIResource,
  2761. SwingConstants {
  2762. public ScrollableTabButton(int direction) {
  2763. super(direction,
  2764. UIManager.getColor("TabbedPane.selected"),
  2765. UIManager.getColor("TabbedPane.shadow"),
  2766. UIManager.getColor("TabbedPane.darkShadow"),
  2767. UIManager.getColor("TabbedPane.highlight"));
  2768. }
  2769. }
  2770. // Controller: event listeners
  2771. private class Handler implements ChangeListener, ContainerListener,
  2772. FocusListener, MouseListener, MouseMotionListener,
  2773. PropertyChangeListener {
  2774. //
  2775. // PropertyChangeListener
  2776. //
  2777. public void propertyChange(PropertyChangeEvent e) {
  2778. JTabbedPane pane = (JTabbedPane)e.getSource();
  2779. String name = e.getPropertyName();
  2780. boolean isScrollLayout = scrollableTabLayoutEnabled();
  2781. if (name == "mnemonicAt") {
  2782. updateMnemonics();
  2783. pane.repaint();
  2784. }
  2785. else if (name == "displayedMnemonicIndexAt") {
  2786. pane.repaint();
  2787. }
  2788. else if (name =="indexForTitle") {
  2789. int index = ((Integer)e.getNewValue()).intValue();
  2790. String title = tabPane.getTitleAt(index);
  2791. if (BasicHTML.isHTMLString(title)) {
  2792. if (htmlViews==null) { // Initialize vector
  2793. htmlViews = createHTMLVector();
  2794. } else { // Vector already exists
  2795. View v = BasicHTML.createHTMLView(tabPane, title);
  2796. htmlViews.setElementAt(v, index);
  2797. }
  2798. } else {
  2799. if (htmlViews != null && htmlViews.elementAt(index) != null) {
  2800. htmlViews.setElementAt(null, index);
  2801. }
  2802. }
  2803. updateMnemonics();
  2804. } else if (name == "tabLayoutPolicy") {
  2805. BasicTabbedPaneUI.this.uninstallUI(pane);
  2806. BasicTabbedPaneUI.this.installUI(pane);
  2807. } else if (name == "tabPlacement") {
  2808. if (scrollableTabLayoutEnabled()) {
  2809. tabScroller.createButtons();
  2810. }
  2811. } else if (name == "opaque" && isScrollLayout) {
  2812. boolean newVal = ((Boolean)e.getNewValue()).booleanValue();
  2813. tabScroller.tabPanel.setOpaque(newVal);
  2814. tabScroller.viewport.setOpaque(newVal);
  2815. } else if (name == "background" && isScrollLayout) {
  2816. Color newVal = (Color)e.getNewValue();
  2817. tabScroller.tabPanel.setBackground(newVal);
  2818. tabScroller.viewport.setBackground(newVal);
  2819. Color newColor = selectedColor == null ? newVal : selectedColor;
  2820. tabScroller.scrollForwardButton.setBackground(newColor);
  2821. tabScroller.scrollBackwardButton.setBackground(newColor);
  2822. }
  2823. }
  2824. //
  2825. // ChangeListener
  2826. //
  2827. public void stateChanged(ChangeEvent e) {
  2828. JTabbedPane tabPane = (JTabbedPane)e.getSource();
  2829. tabPane.revalidate();
  2830. tabPane.repaint();
  2831. if (scrollableTabLayoutEnabled()) {
  2832. int index = tabPane.getSelectedIndex();
  2833. if (index < rects.length && index != -1) {
  2834. tabScroller.tabPanel.scrollRectToVisible(
  2835. (Rectangle)rects[index].clone());
  2836. }
  2837. }
  2838. }
  2839. //
  2840. // MouseListener
  2841. //
  2842. public void mouseClicked(MouseEvent e) {
  2843. }
  2844. public void mouseReleased(MouseEvent e) {
  2845. }
  2846. public void mouseEntered(MouseEvent e) {
  2847. setRolloverTab(e.getX(), e.getY());
  2848. }
  2849. public void mouseExited(MouseEvent e) {
  2850. setRolloverTab(-1);
  2851. }
  2852. public void mousePressed(MouseEvent e) {
  2853. if (!tabPane.isEnabled()) {
  2854. return;
  2855. }
  2856. int tabIndex = tabForCoordinate(tabPane, e.getX(), e.getY());
  2857. if (tabIndex >= 0 && tabPane.isEnabledAt(tabIndex)) {
  2858. if (tabIndex != tabPane.getSelectedIndex()) {
  2859. tabPane.setSelectedIndex(tabIndex);
  2860. }
  2861. if (tabPane.isRequestFocusEnabled() && tabIndex != focusIndex) {
  2862. tabPane.requestFocus();
  2863. int oldFocusIndex = focusIndex;
  2864. setFocusIndex(tabIndex);
  2865. tabPane.repaint(getTabBounds(tabPane, focusIndex));
  2866. if (oldFocusIndex != -1) {
  2867. tabPane.repaint(getTabBounds(tabPane, oldFocusIndex));
  2868. }
  2869. }
  2870. }
  2871. }
  2872. //
  2873. // MouseMotionListener
  2874. //
  2875. public void mouseDragged(MouseEvent e) {
  2876. }
  2877. public void mouseMoved(MouseEvent e) {
  2878. setRolloverTab(e.getX(), e.getY());
  2879. }
  2880. //
  2881. // FocusListener
  2882. //
  2883. public void focusGained(FocusEvent e) {
  2884. JTabbedPane tabPane = (JTabbedPane)e.getSource();
  2885. int tabCount = tabPane.getTabCount();
  2886. int focusIndex = getFocusIndex();
  2887. if (focusIndex == -1) {
  2888. focusIndex = tabPane.getSelectedIndex();
  2889. setFocusIndex(focusIndex);
  2890. }
  2891. if (focusIndex != -1 && tabCount > 0
  2892. && tabCount == rects.length) {
  2893. tabPane.repaint(getTabBounds(tabPane, focusIndex));
  2894. }
  2895. }
  2896. public void focusLost(FocusEvent e) {
  2897. JTabbedPane tabPane = (JTabbedPane)e.getSource();
  2898. int tabCount = tabPane.getTabCount();
  2899. int focusIndex = getFocusIndex();
  2900. if (focusIndex != -1 && tabCount > 0
  2901. && tabCount == rects.length) {
  2902. //PENDING(aim): this gets called unexplicably when an unselected
  2903. // tab is clicked when the tabbedpane doesn't have focus.
  2904. // need to investigate further!
  2905. tabPane.repaint(getTabBounds(tabPane, focusIndex));
  2906. setFocusIndex(-1);
  2907. }
  2908. }
  2909. //
  2910. // ContainerListener
  2911. //
  2912. /* GES 2/3/99:
  2913. The container listener code was added to support HTML
  2914. rendering of tab titles.
  2915. Ideally, we would be able to listen for property changes
  2916. when a tab is added or its text modified. At the moment
  2917. there are no such events because the Beans spec doesn't
  2918. allow 'indexed' property changes (i.e. tab 2's text changed
  2919. from A to B).
  2920. In order to get around this, we listen for tabs to be added
  2921. or removed by listening for the container events. we then
  2922. queue up a runnable (so the component has a chance to complete
  2923. the add) which checks the tab title of the new component to see
  2924. if it requires HTML rendering.
  2925. The Views (one per tab title requiring HTML rendering) are
  2926. stored in the htmlViews Vector, which is only allocated after
  2927. the first time we run into an HTML tab. Note that this vector
  2928. is kept in step with the number of pages, and nulls are added
  2929. for those pages whose tab title do not require HTML rendering.
  2930. This makes it easy for the paint and layout code to tell
  2931. whether to invoke the HTML engine without having to check
  2932. the string during time-sensitive operations.
  2933. When we have added a way to listen for tab additions and
  2934. changes to tab text, this code should be removed and
  2935. replaced by something which uses that. */
  2936. public void componentAdded(ContainerEvent e) {
  2937. JTabbedPane tp = (JTabbedPane)e.getContainer();
  2938. Component child = e.getChild();
  2939. if (child instanceof UIResource) {
  2940. return;
  2941. }
  2942. int index = tp.indexOfComponent(child);
  2943. String title = tp.getTitleAt(index);
  2944. boolean isHTML = BasicHTML.isHTMLString(title);
  2945. if (isHTML) {
  2946. if (htmlViews==null) { // Initialize vector
  2947. htmlViews = createHTMLVector();
  2948. } else { // Vector already exists
  2949. View v = BasicHTML.createHTMLView(tp, title);
  2950. htmlViews.insertElementAt(v, index);
  2951. }
  2952. } else { // Not HTML
  2953. if (htmlViews!=null) { // Add placeholder
  2954. htmlViews.insertElementAt(null, index);
  2955. } // else nada!
  2956. }
  2957. isRunsDirty = true;
  2958. updateMnemonics();
  2959. }
  2960. public void componentRemoved(ContainerEvent e) {
  2961. JTabbedPane tp = (JTabbedPane)e.getContainer();
  2962. Component child = e.getChild();
  2963. if (child instanceof UIResource) {
  2964. return;
  2965. }
  2966. // NOTE 4/15/2002 (joutwate):
  2967. // This fix is implemented using client properties since there is
  2968. // currently no IndexPropertyChangeEvent. Once
  2969. // IndexPropertyChangeEvents have been added this code should be
  2970. // modified to use it.
  2971. Integer indexObj =
  2972. (Integer)tp.getClientProperty("__index_to_remove__");
  2973. if (indexObj != null) {
  2974. int index = indexObj.intValue();
  2975. if (htmlViews != null && htmlViews.size() > index) {
  2976. htmlViews.removeElementAt(index);
  2977. }
  2978. tp.putClientProperty("__index_to_remove__", null);
  2979. }
  2980. isRunsDirty = true;
  2981. updateMnemonics();
  2982. }
  2983. }
  2984. /**
  2985. * This class should be treated as a "protected" inner class.
  2986. * Instantiate it only within subclasses of BasicTabbedPaneUI.
  2987. */
  2988. public class PropertyChangeHandler implements PropertyChangeListener {
  2989. // NOTE: This class exists only for backward compatability. All
  2990. // its functionality has been moved into Handler. If you need to add
  2991. // new functionality add it to the Handler, but make sure this
  2992. // class calls into the Handler.
  2993. public void propertyChange(PropertyChangeEvent e) {
  2994. getHandler().propertyChange(e);
  2995. }
  2996. }
  2997. /**
  2998. * This class should be treated as a "protected" inner class.
  2999. * Instantiate it only within subclasses of BasicTabbedPaneUI.
  3000. */
  3001. public class TabSelectionHandler implements ChangeListener {
  3002. // NOTE: This class exists only for backward compatability. All
  3003. // its functionality has been moved into Handler. If you need to add
  3004. // new functionality add it to the Handler, but make sure this
  3005. // class calls into the Handler.
  3006. public void stateChanged(ChangeEvent e) {
  3007. getHandler().stateChanged(e);
  3008. }
  3009. }
  3010. /**
  3011. * This class should be treated as a "protected" inner class.
  3012. * Instantiate it only within subclasses of BasicTabbedPaneUI.
  3013. */
  3014. public class MouseHandler extends MouseAdapter {
  3015. // NOTE: This class exists only for backward compatability. All
  3016. // its functionality has been moved into Handler. If you need to add
  3017. // new functionality add it to the Handler, but make sure this
  3018. // class calls into the Handler.
  3019. public void mousePressed(MouseEvent e) {
  3020. getHandler().mousePressed(e);
  3021. }
  3022. }
  3023. /**
  3024. * This class should be treated as a "protected" inner class.
  3025. * Instantiate it only within subclasses of BasicTabbedPaneUI.
  3026. */
  3027. public class FocusHandler extends FocusAdapter {
  3028. // NOTE: This class exists only for backward compatability. All
  3029. // its functionality has been moved into Handler. If you need to add
  3030. // new functionality add it to the Handler, but make sure this
  3031. // class calls into the Handler.
  3032. public void focusGained(FocusEvent e) {
  3033. getHandler().focusGained(e);
  3034. }
  3035. public void focusLost(FocusEvent e) {
  3036. getHandler().focusLost(e);
  3037. }
  3038. }
  3039. private Vector createHTMLVector() {
  3040. Vector htmlViews = new Vector();
  3041. int count = tabPane.getTabCount();
  3042. if (count>0) {
  3043. for (int i=0 ; i<count; i++) {
  3044. String title = tabPane.getTitleAt(i);
  3045. if (BasicHTML.isHTMLString(title)) {
  3046. htmlViews.addElement(BasicHTML.createHTMLView(tabPane, title));
  3047. } else {
  3048. htmlViews.addElement(null);
  3049. }
  3050. }
  3051. }
  3052. return htmlViews;
  3053. }
  3054. }