1 /*
2 * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
566 return renderer.getBaseline(Integer.MAX_VALUE, rowHeight) +
567 list.getInsets().top;
568 }
569
570 /**
571 * Returns an enum indicating how the baseline of the component
572 * changes as the size changes.
573 *
574 * @throws NullPointerException {@inheritDoc}
575 * @see javax.swing.JComponent#getBaseline(int, int)
576 * @since 1.6
577 */
578 public Component.BaselineResizeBehavior getBaselineResizeBehavior(
579 JComponent c) {
580 super.getBaselineResizeBehavior(c);
581 return Component.BaselineResizeBehavior.CONSTANT_ASCENT;
582 }
583
584 /**
585 * The preferredSize of the list depends upon the layout orientation.
586 * <table summary="Describes the preferred size for each layout orientation">
587 * <tr><th>Layout Orientation</th><th>Preferred Size</th></tr>
588 * <tr>
589 * <td>JList.VERTICAL
590 * <td>The preferredSize of the list is total height of the rows
591 * and the maximum width of the cells. If JList.fixedCellHeight
592 * is specified then the total height of the rows is just
593 * (cellVerticalMargins + fixedCellHeight) * model.getSize() where
594 * rowVerticalMargins is the space we allocate for drawing
595 * the yellow focus outline. Similarly if fixedCellWidth is
596 * specified then we just use that.
597 * </td>
598 * <tr>
599 * <td>JList.VERTICAL_WRAP
600 * <td>If the visible row count is greater than zero, the preferredHeight
601 * is the maximum cell height * visibleRowCount. If the visible row
602 * count is <= 0, the preferred height is either the current height
603 * of the list, or the maximum cell height, whichever is
604 * bigger. The preferred width is than the maximum cell width *
605 * number of columns needed. Where the number of columns needs is
606 * list.height / max cell height. Max cell height is either the fixed
607 * cell height, or is determined by iterating through all the cells
609 * <tr>
610 * <td>JList.HORIZONTAL_WRAP
611 * <td>If the visible row count is greater than zero, the preferredHeight
612 * is the maximum cell height * adjustedRowCount. Where
613 * visibleRowCount is used to determine the number of columns.
614 * Because this lays out horizontally the number of rows is
615 * then determined from the column count. For example, lets say
616 * you have a model with 10 items and the visible row count is 8.
617 * The number of columns needed to display this is 2, but you no
618 * longer need 8 rows to display this, you only need 5, thus
619 * the adjustedRowCount is 5.
620 * <p>If the visible row
621 * count is <= 0, the preferred height is dictated by the
622 * number of columns, which will be as many as can fit in the width
623 * of the <code>JList</code> (width / max cell width), with at
624 * least one column. The preferred height then becomes the
625 * model size / number of columns * maximum cell height.
626 * Max cell height is either the fixed
627 * cell height, or is determined by iterating through all the cells
628 * to find the maximum height from the ListCellRenderer.
629 * </table>
630 * The above specifies the raw preferred width and height. The resulting
631 * preferred width is the above width + insets.left + insets.right and
632 * the resulting preferred height is the above height + insets.top +
633 * insets.bottom. Where the <code>Insets</code> are determined from
634 * <code>list.getInsets()</code>.
635 *
636 * @param c The JList component.
637 * @return The total size of the list.
638 */
639 public Dimension getPreferredSize(JComponent c) {
640 maybeUpdateLayoutState();
641
642 int lastRow = list.getModel().getSize() - 1;
643 if (lastRow < 0) {
644 return new Dimension(0, 0);
645 }
646
647 Insets insets = list.getInsets();
648 int width = cellWidth * columnCount + insets.left + insets.right;
|
1 /*
2 * Copyright (c) 1997, 2017, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
566 return renderer.getBaseline(Integer.MAX_VALUE, rowHeight) +
567 list.getInsets().top;
568 }
569
570 /**
571 * Returns an enum indicating how the baseline of the component
572 * changes as the size changes.
573 *
574 * @throws NullPointerException {@inheritDoc}
575 * @see javax.swing.JComponent#getBaseline(int, int)
576 * @since 1.6
577 */
578 public Component.BaselineResizeBehavior getBaselineResizeBehavior(
579 JComponent c) {
580 super.getBaselineResizeBehavior(c);
581 return Component.BaselineResizeBehavior.CONSTANT_ASCENT;
582 }
583
584 /**
585 * The preferredSize of the list depends upon the layout orientation.
586 *
587 * <table class="striped">
588 * <caption>Describes the preferred size for each layout orientation
589 * </caption>
590 * <thead>
591 * <tr><th>Layout Orientation</th><th>Preferred Size</th></tr>
592 * </thead>
593 * <tbody>
594 * <tr>
595 * <td>JList.VERTICAL
596 * <td>The preferredSize of the list is total height of the rows
597 * and the maximum width of the cells. If JList.fixedCellHeight
598 * is specified then the total height of the rows is just
599 * (cellVerticalMargins + fixedCellHeight) * model.getSize() where
600 * rowVerticalMargins is the space we allocate for drawing
601 * the yellow focus outline. Similarly if fixedCellWidth is
602 * specified then we just use that.
603 * </td>
604 * <tr>
605 * <td>JList.VERTICAL_WRAP
606 * <td>If the visible row count is greater than zero, the preferredHeight
607 * is the maximum cell height * visibleRowCount. If the visible row
608 * count is <= 0, the preferred height is either the current height
609 * of the list, or the maximum cell height, whichever is
610 * bigger. The preferred width is than the maximum cell width *
611 * number of columns needed. Where the number of columns needs is
612 * list.height / max cell height. Max cell height is either the fixed
613 * cell height, or is determined by iterating through all the cells
615 * <tr>
616 * <td>JList.HORIZONTAL_WRAP
617 * <td>If the visible row count is greater than zero, the preferredHeight
618 * is the maximum cell height * adjustedRowCount. Where
619 * visibleRowCount is used to determine the number of columns.
620 * Because this lays out horizontally the number of rows is
621 * then determined from the column count. For example, lets say
622 * you have a model with 10 items and the visible row count is 8.
623 * The number of columns needed to display this is 2, but you no
624 * longer need 8 rows to display this, you only need 5, thus
625 * the adjustedRowCount is 5.
626 * <p>If the visible row
627 * count is <= 0, the preferred height is dictated by the
628 * number of columns, which will be as many as can fit in the width
629 * of the <code>JList</code> (width / max cell width), with at
630 * least one column. The preferred height then becomes the
631 * model size / number of columns * maximum cell height.
632 * Max cell height is either the fixed
633 * cell height, or is determined by iterating through all the cells
634 * to find the maximum height from the ListCellRenderer.
635 * </tbody>
636 * </table>
637 * The above specifies the raw preferred width and height. The resulting
638 * preferred width is the above width + insets.left + insets.right and
639 * the resulting preferred height is the above height + insets.top +
640 * insets.bottom. Where the <code>Insets</code> are determined from
641 * <code>list.getInsets()</code>.
642 *
643 * @param c The JList component.
644 * @return The total size of the list.
645 */
646 public Dimension getPreferredSize(JComponent c) {
647 maybeUpdateLayoutState();
648
649 int lastRow = list.getModel().getSize() - 1;
650 if (lastRow < 0) {
651 return new Dimension(0, 0);
652 }
653
654 Insets insets = list.getInsets();
655 int width = cellWidth * columnCount + insets.left + insets.right;
|