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