basiclistui.java

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

JAVA
703
字号
  {    maybeUpdateLayoutState();    if (l != list || cellWidth == -1)      return null;    int lo = Math.min(index1, index2);    int hi = Math.max(index1, index2);    Rectangle lobounds = new Rectangle(0, convertRowToY(lo), cellWidth,                                       getRowHeight(lo));    Rectangle hibounds = new Rectangle(0, convertRowToY(hi), cellWidth,                                       getRowHeight(hi));    return lobounds.union(hibounds);  }  /**   * Calculate the Y coordinate of the upper edge of a particular row,   * considering the Y coordinate <code>0</code> to occur at the top of the   * list.   *   * @param row The row to calculate the Y coordinate of   *   * @return The Y coordinate of the specified row, or <code>-1</code> if   * the specified row number is invalid   */  int convertRowToY(int row)  {    int y = 0;    for (int i = 0; i < row; ++i)      {        int h = getRowHeight(i);        if (h == -1)          return -1;        y += h;      }    return y;  }  /**   * Calculate the row number containing a particular Y coordinate,   * considering the Y coodrinate <code>0</code> to occur at the top of the   * list.   *   * @param y0 The Y coordinate to calculate the row number for   *   * @return The row number containing the specified Y value, or <code>-1</code>   * if the specified Y coordinate is invalid   */  int convertYToRow(int y0)  {    for (int row = 0; row < cellHeights.length; ++row)      {        int h = getRowHeight(row);        if (y0 < h)          return row;        y0 -= h;      }    return -1;  }  /**   * Recomputes the {@link #cellHeights}, {@link #cellHeight}, and {@link   * #cellWidth} properties by examining the variouis properties of the   * {@link JList}.   */  void updateLayoutState()  {    int nrows = list.getModel().getSize();    cellHeight = -1;    cellWidth = -1;    if (cellHeights == null || cellHeights.length != nrows)      cellHeights = new int[nrows];    if (list.getFixedCellHeight() == -1 || list.getFixedCellWidth() == -1)      {        ListCellRenderer rend = list.getCellRenderer();        for (int i = 0; i < nrows; ++i)          {            Component flyweight = rend.getListCellRendererComponent(list,                                                                    list.getModel()                                                                        .getElementAt(i),                                                                    0, false,                                                                    false);            Dimension dim = flyweight.getPreferredSize();            cellHeights[i] = dim.height;            cellWidth = Math.max(cellWidth, dim.width);          }      }    else      {        cellHeight = list.getFixedCellHeight();        cellWidth = list.getFixedCellWidth();      }  }  /**   * Marks the current layout as damaged and requests revalidation from the   * JList.   *   * @see #updateLayoutStateNeeded   */  void damageLayout()  {    updateLayoutStateNeeded = 1;    list.revalidate();  }  /**   * Calls {@link #updateLayoutState} if {@link #updateLayoutStateNeeded}   * is nonzero, then resets {@link #updateLayoutStateNeeded} to zero.   */  void maybeUpdateLayoutState()  {    if (updateLayoutStateNeeded != 0)      {        updateLayoutState();        updateLayoutStateNeeded = 0;      }  }  /**   * Creates a new BasicListUI object.   */  public BasicListUI()  {    focusListener = new FocusHandler();    listDataListener = new ListDataHandler();    listSelectionListener = new ListSelectionHandler();    mouseInputListener = new MouseInputHandler();    propertyChangeListener = new PropertyChangeHandler();    updateLayoutStateNeeded = 1;  }  /**   * Installs various default settings (mostly colors) from the {@link   * UIDefaults} into the {@link JList}   *   * @see #uninstallDefaults   */  void installDefaults()  {    UIDefaults defaults = UIManager.getLookAndFeelDefaults();    list.setForeground(defaults.getColor("List.foreground"));    list.setBackground(defaults.getColor("List.background"));    list.setSelectionForeground(defaults.getColor("List.selectionForeground"));    list.setSelectionBackground(defaults.getColor("List.selectionBackground"));    list.setOpaque(true);  }  /**   * Resets to <code>null</code> those defaults which were installed in    * {@link #installDefaults}   */  void uninstallDefaults()  {    UIDefaults defaults = UIManager.getLookAndFeelDefaults();    list.setForeground(null);    list.setBackground(null);    list.setSelectionForeground(null);    list.setSelectionBackground(null);  }  /**   * Attaches all the listeners we have in the UI class to the {@link   * JList}, its model and its selection model.   *   * @see #uninstallListeners   */  void installListeners()  {    list.addFocusListener(focusListener);    list.getModel().addListDataListener(listDataListener);    list.addListSelectionListener(listSelectionListener);    list.addMouseListener(mouseInputListener);    list.addMouseMotionListener(mouseInputListener);    list.addPropertyChangeListener(propertyChangeListener);  }  /**   * Detaches all the listeners we attached in {@link #installListeners}.   */  void uninstallListeners()  {    list.removeFocusListener(focusListener);    list.getModel().removeListDataListener(listDataListener);    list.removeListSelectionListener(listSelectionListener);    list.removeMouseListener(mouseInputListener);    list.removeMouseMotionListener(mouseInputListener);    list.removePropertyChangeListener(propertyChangeListener);  }  /**   * Installs keyboard actions for this UI in the {@link JList}.   */  void installKeyboardActions()  {  }  /**   * Uninstalls keyboard actions for this UI in the {@link JList}.   */  void uninstallKeyboardActions()  {  }  /**   * Installs the various aspects of the UI in the {@link JList}. In   * particular, calls {@link #installDefaults}, {@link #installListeners}   * and {@link #installKeyboardActions}. Also saves a reference to the   * provided component, cast to a {@link JList}.   *   * @param c The {@link JList} to install the UI into   */  public void installUI(final JComponent c)  {    super.installUI(c);    list = (JList) c;    installDefaults();    installListeners();    installKeyboardActions();    maybeUpdateLayoutState();  }  /**   * Uninstalls all the aspects of the UI which were installed in {@link   * #installUI}. When finished uninstalling, drops the saved reference to   * the {@link JList}.   *   * @param c Ignored; the UI is uninstalled from the {@link JList}   * reference saved during the call to {@link #installUI}   */  public void uninstallUI(final JComponent c)  {    uninstallKeyboardActions();    uninstallListeners();    uninstallDefaults();    list = null;  }  /**   * Gets the maximum size this list can assume.   *   * @param c The component to measure the size of   *   * @return A new Dimension representing the component's maximum size   */  public Dimension getMaximumSize(JComponent c)  {    return new Dimension(Integer.MAX_VALUE, Integer.MAX_VALUE);  }  /**   * Gets the size this list would prefer to assume. This is calculated by   * calling {@link #getCellBounds} over the entire list.   *   * @param c Ignored; uses the saved {@link JList} reference    *   * @return DOCUMENT ME!   */  public Dimension getPreferredSize(JComponent c)  {    if (list.getModel().getSize() == 0)      return new Dimension(0, 0);    Rectangle bounds = getCellBounds(list, 0, list.getModel().getSize() - 1);    return bounds.getSize();  }  /**   * Paints the packground of the list using the background color   * of the specified component.   *   * @param g The graphics context to paint in   * @param c The component to paint the background of   */  public void paintBackground(Graphics g, JComponent c)  {    Dimension size = getPreferredSize(c);    Color save = g.getColor();    g.setColor(c.getBackground());    g.fillRect(0, 0, size.width, size.height);    g.setColor(save);  }  /**   * Paints a single cell in the list.   *   * @param g The graphics context to paint in   * @param row The row number to paint   * @param bounds The bounds of the cell to paint, assuming a coordinate   * system beginning at <code>(0,0)</code> in the upper left corner of the   * list   * @param rend A cell renderer to paint with   * @param data The data to provide to the cell renderer   * @param sel A selection model to provide to the cell renderer   * @param lead The lead selection index of the list   */  void paintCell(Graphics g, int row, Rectangle bounds, ListCellRenderer rend,                 ListModel data, ListSelectionModel sel, int lead)  {    boolean is_sel = list.isSelectedIndex(row);    boolean has_focus = false;    Component comp = rend.getListCellRendererComponent(list,                                                       data.getElementAt(row),                                                       0, is_sel, has_focus);    g.translate(bounds.x, bounds.y);    comp.setBounds(new Rectangle(0, 0, bounds.width, bounds.height));    comp.paint(g);    g.translate(-bounds.x, -bounds.y);  }  /**   * Paints the list by calling {@link #paintBackground} and then repeatedly   * calling {@link #paintCell} for each visible cell in the list.   *   * @param g The graphics context to paint with   * @param c Ignored; uses the saved {@link JList} reference    */  public void paint(Graphics g, JComponent c)  {    int nrows = list.getModel().getSize();    if (nrows == 0)      return;    maybeUpdateLayoutState();    ListCellRenderer render = list.getCellRenderer();    ListModel model = list.getModel();    ListSelectionModel sel = list.getSelectionModel();    int lead = sel.getLeadSelectionIndex();    Rectangle clip = g.getClipBounds();    paintBackground(g, list);    for (int row = 0; row < nrows; ++row)      {        Rectangle bounds = getCellBounds(list, row, row);        if (bounds.intersects(clip))          paintCell(g, row, bounds, render, model, sel, lead);      }  }  public int locationToIndex(JList list, Point location)  {    return convertYToRow(location.y);  }  public Point indexToLocation(JList list, int index)  {    return new Point(0, convertRowToY(index));  }}

⌨️ 快捷键说明

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