📄 basiclistui.java
字号:
< Point2D.distance(me.x + me.width, me.y + (int)(prev.height / 2.0), p.x, p.y); } } if (decr) { index--; rect = getCellBounds(list, index); if (isLeftToRight) { rect.x += rect.width; } else { rect.x -= DROP_LINE_THICKNESS; } } else { rect = getCellBounds(list, index); if (!isLeftToRight) { rect.x += rect.width - DROP_LINE_THICKNESS; } } if (rect.x >= list.getWidth()) { rect.x = list.getWidth() - DROP_LINE_THICKNESS; } else if (rect.x < 0) { rect.x = 0; } rect.width = DROP_LINE_THICKNESS; } else if (layoutOrientation == JList.VERTICAL_WRAP) { if (index == size) { index--; rect = getCellBounds(list, index); rect.y += rect.height; } else if (index != 0 && convertModelToColumn(index) != convertModelToColumn(index - 1)) { Rectangle prev = getCellBounds(list, index - 1); Rectangle me = getCellBounds(list, index); Point p = loc.getDropPoint(); if (Point2D.distance(prev.x + (int)(prev.width / 2.0), prev.y + prev.height, p.x, p.y) < Point2D.distance(me.x + (int)(me.width / 2.0), me.y, p.x, p.y)) { index--; rect = getCellBounds(list, index); rect.y += rect.height; } else { rect = getCellBounds(list, index); } } else { rect = getCellBounds(list, index); } if (rect.y >= list.getHeight()) { rect.y = list.getHeight() - DROP_LINE_THICKNESS; } rect.height = DROP_LINE_THICKNESS; } else { if (index == size) { index--; rect = getCellBounds(list, index); rect.y += rect.height; } else { rect = getCellBounds(list, index); } if (rect.y >= list.getHeight()) { rect.y = list.getHeight() - DROP_LINE_THICKNESS; } rect.height = DROP_LINE_THICKNESS; } return rect; } /** * Returns the baseline. * * @throws NullPointerException {@inheritDoc} * @throws IllegalArgumentException {@inheritDoc} * @see javax.swing.JComponent#getBaseline(int, int) * @since 1.6 */ public int getBaseline(JComponent c, int width, int height) { super.getBaseline(c, width, height); int rowHeight = list.getFixedCellHeight(); UIDefaults lafDefaults = UIManager.getLookAndFeelDefaults(); Component renderer = (Component)lafDefaults.get( BASELINE_COMPONENT_KEY); if (renderer == null) { ListCellRenderer lcr = (ListCellRenderer)UIManager.get( "List.cellRenderer"); renderer = lcr.getListCellRendererComponent( list, "a", -1, false, false); lafDefaults.put(BASELINE_COMPONENT_KEY, renderer); } renderer.setFont(list.getFont()); // JList actually has much more complex behavior here. // If rowHeight != -1 the rowHeight is either the max of all cell // heights (layout orientation != VERTICAL), or is variable depending // upon the cell. We assume a default size. // We could theoretically query the real renderer, but that would // not work for an empty model and the results may vary with // the content. if (rowHeight == -1) { rowHeight = renderer.getPreferredSize().height; } return renderer.getBaseline(Integer.MAX_VALUE, rowHeight) + list.getInsets().top; } /** * Returns an enum indicating how the baseline of the component * changes as the size changes. * * @throws NullPointerException {@inheritDoc} * @see javax.swing.JComponent#getBaseline(int, int) * @since 1.6 */ public Component.BaselineResizeBehavior getBaselineResizeBehavior( JComponent c) { super.getBaselineResizeBehavior(c); return Component.BaselineResizeBehavior.CONSTANT_ASCENT; } /** * The preferredSize of the list depends upon the layout orientation. * <table summary="Describes the preferred size for each layout orientation"> * <tr><th>Layout Orientation</th><th>Preferred Size</th></tr> * <tr> * <td>JList.VERTICAL * <td>The preferredSize of the list is total height of the rows * and the maximum width of the cells. If JList.fixedCellHeight * is specified then the total height of the rows is just * (cellVerticalMargins + fixedCellHeight) * model.getSize() where * rowVerticalMargins is the space we allocate for drawing * the yellow focus outline. Similarly if fixedCellWidth is * specified then we just use that. * </td> * <tr> * <td>JList.VERTICAL_WRAP * <td>If the visible row count is greater than zero, the preferredHeight * is the maximum cell height * visibleRowCount. If the visible row * count is <= 0, the preferred height is either the current height * of the list, or the maximum cell height, whichever is * bigger. The preferred width is than the maximum cell width * * number of columns needed. Where the number of columns needs is * list.height / max cell height. Max cell height is either the fixed * cell height, or is determined by iterating through all the cells * to find the maximum height from the ListCellRenderer. * <tr> * <td>JList.HORIZONTAL_WRAP * <td>If the visible row count is greater than zero, the preferredHeight * is the maximum cell height * adjustedRowCount. Where * visibleRowCount is used to determine the number of columns. * Because this lays out horizontally the number of rows is * then determined from the column count. For example, lets say * you have a model with 10 items and the visible row count is 8. * The number of columns needed to display this is 2, but you no * longer need 8 rows to display this, you only need 5, thus * the adjustedRowCount is 5. * <p>If the visible row * count is <= 0, the preferred height is dictated by the * number of columns, which will be as many as can fit in the width * of the <code>JList</code> (width / max cell width), with at * least one column. The preferred height then becomes the * model size / number of columns * maximum cell height. * Max cell height is either the fixed * cell height, or is determined by iterating through all the cells * to find the maximum height from the ListCellRenderer. * </table> * The above specifies the raw preferred width and height. The resulting * preferred width is the above width + insets.left + insets.right and * the resulting preferred height is the above height + insets.top + * insets.bottom. Where the <code>Insets</code> are determined from * <code>list.getInsets()</code>. * * @param c The JList component. * @return The total size of the list. */ public Dimension getPreferredSize(JComponent c) { maybeUpdateLayoutState(); int lastRow = list.getModel().getSize() - 1; if (lastRow < 0) { return new Dimension(0, 0); } Insets insets = list.getInsets(); int width = cellWidth * columnCount + insets.left + insets.right; int height; if (layoutOrientation != JList.VERTICAL) { height = preferredHeight; } else { Rectangle bounds = getCellBounds(list, lastRow); if (bounds != null) { height = bounds.y + bounds.height + insets.bottom; } else { height = 0; } } return new Dimension(width, height); } /** * Selected the previous row and force it to be visible. * * @see JList#ensureIndexIsVisible */ protected void selectPreviousIndex() { int s = list.getSelectedIndex(); if(s > 0) { s -= 1; list.setSelectedIndex(s); list.ensureIndexIsVisible(s); } } /** * Selected the previous row and force it to be visible. * * @see JList#ensureIndexIsVisible */ protected void selectNextIndex() { int s = list.getSelectedIndex(); if((s + 1) < list.getModel().getSize()) { s += 1; list.setSelectedIndex(s); list.ensureIndexIsVisible(s); } } /** * Registers the keyboard bindings on the <code>JList</code> that the * <code>BasicListUI</code> is associated with. This method is called at * installUI() time. * * @see #installUI */ protected void installKeyboardActions() { InputMap inputMap = getInputMap(JComponent.WHEN_FOCUSED); SwingUtilities.replaceUIInputMap(list, JComponent.WHEN_FOCUSED, inputMap); LazyActionMap.installLazyActionMap(list, BasicListUI.class, "List.actionMap"); } InputMap getInputMap(int condition) { if (condition == JComponent.WHEN_FOCUSED) { InputMap keyMap = (InputMap)DefaultLookup.get( list, this, "List.focusInputMap"); InputMap rtlKeyMap; if (isLeftToRight || ((rtlKeyMap = (InputMap)DefaultLookup.get(list, this, "List.focusInputMap.RightToLeft")) == null)) { return keyMap; } else { rtlKeyMap.setParent(keyMap); return rtlKeyMap; } } return null; } /** * Unregisters keyboard actions installed from * <code>installKeyboardActions</code>. * This method is called at uninstallUI() time - subclassess should * ensure that all of the keyboard actions registered at installUI * time are removed here. * * @see #installUI */ protected void uninstallKeyboardActions() { SwingUtilities.replaceUIActionMap(list, null); SwingUtilities.replaceUIInputMap(list, JComponent.WHEN_FOCUSED, null); } /** * Create and install the listeners for the JList, its model, and its * selectionModel. This method is called at installUI() time. * * @see #installUI * @see #uninstallListeners */ protected void installListeners() { TransferHandler th = list.getTransferHandler(); if (th == null || th instanceof UIResource) { list.setTransferHandler(defaultTransferHandler); } focusListener = createFocusListener(); mouseInputListener = createMouseInputListener(); propertyChangeListener = createPropertyChangeListener(); listSelectionListener = createListSelectionListener(); listDataListener = createListDataListener(); list.addFocusListener(focusListener); list.addMouseListener(mouseInputListener); list.addMouseMotionListener(mouseInputListener); list.addPropertyChangeListener(propertyChangeListener); list.addKeyListener(getHandler()); ListModel model = list.getModel(); if (model != null) { model.addListDataListener(listDataListener); } ListSelectionModel selectionModel = list.getSelectionModel(); if (selectionModel != null) { selectionModel.addListSelectionListener(listSelectionListener); } } /** * Remove the listeners for the JList, its model, and its * selectionModel. All of the listener fields, are reset to * null here. This method is called at uninstallUI() time, * it should be kept in sync with installListeners. * * @see #uninstallUI * @see #installListeners */ protected void uninstallListeners() { list.removeFocusListener(focusListener); list.removeMouseListener(mouseInputListener); list.removeMouseMotionListener(mouseInputListener); list.removePropertyChangeListener(propertyChangeListener); list.removeKeyListener(getHandler()); ListModel model = list.getModel(); if (model != null) { model.removeListDataListener(listDataListener); } ListSelectionModel selectionModel = list.getSelectionModel(); if (selectionModel != null) { selectionModel.removeListSelectionListener(listSelectionListener); } focusListener = null; mouseInputListener = null; listSelectionListener = null; listDataListener = null; propertyChangeListener = null; handler = null; } /** * Initialize JList properties, e.g. font, foreground, and background, * and add the CellRendererPane. The font, foreground, and background
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -