basictabbedpaneui.java

来自「Mac OS X 10.4.9 for x86 Source Code gcc」· Java 代码 · 共 2,129 行 · 第 1/5 页

JAVA
2,129
字号
  {    /**     * This method returns the preferred layout size for the given container.     *     * @param parent The container to calculate a size for.     *     * @return The preferred layout size.     */    public Dimension preferredLayoutSize(Container parent)    {      return super.calculateSize(true);    }    /**     * This method returns the minimum layout size for the given container.     *     * @param parent The container to calculate a size for.     *     * @return The minimum layout size.     */    public Dimension minimumLayoutSize(Container parent)    {      return super.calculateSize(true);    }    /**     * This method calculates the tab area height given  a desired width.     *     * @param tabPlacement The JTabbedPane's tab placement.     * @param width The expected width.     *     * @return The tab area height given the width.     */    protected int preferredTabAreaHeight(int tabPlacement, int width)    {      if (tabPane.getTabCount() == 0)	return calculateTabAreaHeight(tabPlacement, 0, 0);      int runs = 1;      int maxTabHeight = calculateMaxTabHeight(tabPlacement);      int tabAreaHeight = calculateTabAreaHeight(tabPlacement, runs,                                                 maxTabHeight);      return tabAreaHeight;    }    /**     * This method calculates the tab area width given a desired height.     *     * @param tabPlacement The JTabbedPane's tab placement.     * @param height The expected height.     *     * @return The tab area width given the height.     */    protected int preferredTabAreaWidth(int tabPlacement, int height)    {      if (tabPane.getTabCount() == 0)	return calculateTabAreaHeight(tabPlacement, 0, 0);      int runs = 1;      int maxTabWidth = calculateMaxTabWidth(tabPlacement);      int tabAreaWidth = calculateTabAreaWidth(tabPlacement, runs, maxTabWidth);      return tabAreaWidth;    }    /**     * This method is called to calculate the tab rectangles.  This method     * will calculate the size and position of all  rectangles (taking into     * account which ones should be in which tab run). It will pad them and     * normalize them  as necessary.     *     * @param tabPlacement The JTabbedPane's tab placement.     * @param tabCount The number of tabs.     */    protected void calculateTabRects(int tabPlacement, int tabCount)    {      if (tabCount == 0)	return;      assureRectsCreated(tabCount);      FontMetrics fm = getFontMetrics();      SwingUtilities.calculateInnerArea(tabPane, calcRect);      Insets tabAreaInsets = getTabAreaInsets(tabPlacement);      Insets insets = tabPane.getInsets();      int max = 0;      int runs = 1;      int start = 0;      int top = 0;      if (tabPlacement == SwingConstants.TOP          || tabPlacement == SwingConstants.BOTTOM)        {	  int maxHeight = calculateMaxTabHeight(tabPlacement);	  calcRect.width -= tabAreaInsets.left + tabAreaInsets.right;	  max = calcRect.width + tabAreaInsets.left + insets.left;	  start = tabAreaInsets.left + insets.left;	  int width = 0;	  int runWidth = start;	  top = insets.top + tabAreaInsets.top;	  for (int i = 0; i < tabCount; i++)	    {	      width = calculateTabWidth(tabPlacement, i, fm);	      rects[i] = new Rectangle(runWidth, top, width, maxHeight);	      runWidth += width;	    }	  tabAreaRect.width = tabPane.getWidth() - insets.left - insets.right;	  tabAreaRect.height = runs * maxTabHeight	                       - (runs - 1) * tabRunOverlay	                       + tabAreaInsets.top + tabAreaInsets.bottom;	  contentRect.width = tabAreaRect.width;	  contentRect.height = tabPane.getHeight() - insets.top	                       - insets.bottom - tabAreaRect.height;	  contentRect.x = insets.left;	  tabAreaRect.x = insets.left;	  if (tabPlacement == SwingConstants.BOTTOM)	    {	      contentRect.y = insets.top;	      tabAreaRect.y = contentRect.y + contentRect.height;	    }	  else	    {	      tabAreaRect.y = insets.top;	      contentRect.y = tabAreaRect.y + tabAreaRect.height;	    }        }      else        {	  int maxWidth = calculateMaxTabWidth(tabPlacement);	  calcRect.height -= tabAreaInsets.top + tabAreaInsets.bottom;	  max = calcRect.height + tabAreaInsets.top;	  int height = 0;	  start = tabAreaInsets.top + insets.top;	  int runHeight = start;	  int fontHeight = fm.getHeight();	  top = insets.left + tabAreaInsets.left;	  for (int i = 0; i < tabCount; i++)	    {	      height = calculateTabHeight(tabPlacement, i, fontHeight);	      rects[i] = new Rectangle(top, runHeight, maxWidth, height);	      runHeight += height;	    }	  tabAreaRect.width = runs * maxTabWidth - (runs - 1) * tabRunOverlay	                      + tabAreaInsets.left + tabAreaInsets.right;	  tabAreaRect.height = tabPane.getHeight() - insets.top	                       - insets.bottom;	  tabAreaRect.y = insets.top;	  contentRect.width = tabPane.getWidth() - insets.left - insets.right	                      - tabAreaRect.width;	  contentRect.height = tabAreaRect.height;	  contentRect.y = insets.top;	  if (tabPlacement == SwingConstants.LEFT)	    {	      tabAreaRect.x = insets.left;	      contentRect.x = tabAreaRect.x + tabAreaRect.width;	    }	  else	    {	      contentRect.x = insets.left;	      tabAreaRect.x = contentRect.x + contentRect.width;	    }        }      runCount = runs;      padSelectedTab(tabPlacement, tabPane.getSelectedIndex());    }    /**     * This method is called when the JTabbedPane is laid out in     * SCROLL_TAB_LAYOUT. It finds the position for all components in the     * JTabbedPane.     *     * @param pane The JTabbedPane to be laid out.     */    public void layoutContainer(Container pane)    {      super.layoutContainer(pane);      int tabCount = tabPane.getTabCount();      Point p = null;      if (tabCount == 0)	return;      int tabPlacement = tabPane.getTabPlacement();      incrButton.hide();      decrButton.hide();      if (tabPlacement == SwingConstants.TOP          || tabPlacement == SwingConstants.BOTTOM)        {	  if (tabAreaRect.x + tabAreaRect.width < rects[tabCount - 1].x	      + rects[tabCount - 1].width)	    {	      Dimension incrDims = incrButton.getPreferredSize();	      Dimension decrDims = decrButton.getPreferredSize();	      decrButton.setBounds(tabAreaRect.x + tabAreaRect.width	                           - incrDims.width - decrDims.width,	                           tabAreaRect.y, decrDims.width,	                           tabAreaRect.height);	      incrButton.setBounds(tabAreaRect.x + tabAreaRect.width	                           - incrDims.width, tabAreaRect.y,	                           decrDims.width, tabAreaRect.height);	      tabAreaRect.width -= decrDims.width + incrDims.width;	      incrButton.show();	      decrButton.show();	    }        }      if (tabPlacement == SwingConstants.LEFT          || tabPlacement == SwingConstants.RIGHT)        {	  if (tabAreaRect.y + tabAreaRect.height < rects[tabCount - 1].y	      + rects[tabCount - 1].height)	    {	      Dimension incrDims = incrButton.getPreferredSize();	      Dimension decrDims = decrButton.getPreferredSize();	      decrButton.setBounds(tabAreaRect.x,	                           tabAreaRect.y + tabAreaRect.height	                           - incrDims.height - decrDims.height,	                           tabAreaRect.width, decrDims.height);	      incrButton.setBounds(tabAreaRect.x,	                           tabAreaRect.y + tabAreaRect.height	                           - incrDims.height, tabAreaRect.width,	                           incrDims.height);	      tabAreaRect.height -= decrDims.height + incrDims.height;	      incrButton.show();	      decrButton.show();	    }        }      viewport.setBounds(tabAreaRect.x, tabAreaRect.y, tabAreaRect.width,                         tabAreaRect.height);      int tabC = tabPane.getTabCount() - 1;      if (tabCount > 0)        {	  int w = Math.max(rects[tabC].width + rects[tabC].x, tabAreaRect.width);	  int h = Math.max(rects[tabC].height, tabAreaRect.height);	  p = findPointForIndex(currentScrollLocation);	  // we want to cover that entire space so that borders that run under	  // the tab area don't show up when we move the viewport around.	  panel.setSize(w + p.x, h + p.y);        }      viewport.setViewPosition(p);      viewport.repaint();    }  }  /**   * This class handles ChangeEvents from the JTabbedPane.   */  public class TabSelectionHandler implements ChangeListener  {    /**     * This method is called whenever a ChangeEvent is fired from the     * JTabbedPane.     *     * @param e The ChangeEvent fired.     */    public void stateChanged(ChangeEvent e)    {      selectedRun = getRunForTab(tabPane.getTabCount(),                                 tabPane.getSelectedIndex());      tabPane.revalidate();      tabPane.repaint();    }  }  /**   * This helper class is a JPanel that fits inside the ScrollViewport. This   * panel's sole job is to paint the tab rectangles inside the  viewport so   * that it's clipped correctly.   */  private class ScrollingPanel extends JPanel  {    /**     * This is a private UI class for our panel.     */    private class ScrollingPanelUI extends BasicPanelUI    {      /**       * This method overrides the default paint method. It paints the tab       * rectangles for the JTabbedPane in the panel.       *       * @param g The Graphics object to paint with.       * @param c The JComponent to paint.       */      public void paint(Graphics g, JComponent c)      {	paintTabArea(g, tabPane.getTabPlacement(), tabPane.getSelectedIndex());      }    }    /**     * This method overrides the updateUI method. It makes the default UI for     * this ScrollingPanel to be  a ScrollingPanelUI.     */    public void updateUI()    {      setUI((PanelUI) new ScrollingPanelUI());    }  }  /**   * This is a helper class that paints the panel that paints tabs. This   * custom JViewport is used so that the tabs painted in the panel will be   * clipped. This class implements UIResource so tabs are not added when   * this objects of this class are added to the  JTabbedPane.   */  private class ScrollingViewport extends JViewport implements UIResource  {  }  /**   * This is a helper class that implements UIResource so it is not added as a   * tab when an object of this class is added to the JTabbedPane.   */  private static class ScrollingButton extends BasicArrowButton    implements UIResource  {    /**     * Creates a ScrollingButton given the direction.     *     * @param dir The direction to point in.     */    public ScrollingButton(int dir)    {      super(dir);    }  }  /** The button that increments the current scroll location. */  private transient ScrollingButton incrButton;  /** The button that decrements the current scroll location. */  private transient ScrollingButton decrButton;  /** The viewport used to display the tabs. */  private transient ScrollingViewport viewport;  /** The panel inside the viewport that paints the tabs. */  private transient ScrollingPanel panel;  /** The starting visible tab in the run in SCROLL_TAB_MODE. */  private transient int currentScrollLocation;  /** A reusable rectangle. */  protected Rectangle calcRect;  /** An array of Rectangles keeping track of the tabs' area and position. */  protected Rectangle[] rects;  /** The insets around the content area. */  protected Insets contentBorderInsets;  /** The extra insets around the selected tab. */  protected Insets selectedTabPadInsets;  /** The insets around the tab area. */  protected Insets tabAreaInsets;  /** The insets around each and every tab. */  protected Insets tabInsets;  /**   * The outer bottom and right edge color for both the tab and content   * border.   */  protected Color darkShadow;  /** The color of the focus outline on the selected tab. */  protected Color focus;  /** FIXME: find a use for this. */  protected Color highlight;  /** The top and left edge color for both the tab and content border. */  protected Color lightHighlight;  /** The inner bottom and right edge color for the tab and content border. */  protected Color shadow;  /** The maximum tab height. */  protected int maxTabHeight;  /** The maximum tab width. */  protected int maxTabWidth;  /** The number of runs in the JTabbedPane. */  protected int runCount;  /** The index of the run that the selected index is in. */  protected int selectedRun;  /** The amount of space each run overlaps the previous by. */  protected int tabRunOverlay;  /** The gap between text and label */  protected int textIconGap;  // Keeps track of tab runs.  // The organization of this array is as follows (lots of experimentation to  // figure this out)  // index 0 = furthest away from the component area (aka outer run)  // index 1 = closest to component area (aka selected run)  // index > 1 = listed in order leading from selected run to outer run.  // each int in the array is the tab index + 1 (counting starts at 1)  // for the last tab in the run. (same as the rects array)  /** This array keeps track of which tabs are in which run. See above. */  protected int[] tabRuns;  /**   * This is the keystroke for moving down.   *   * @deprecated 1.3   */  protected KeyStroke downKey;  /**   * This is the keystroke for moving left.   *   * @deprecated 1.3   */  protected KeyStroke leftKey;

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?