📄 basiclistui.java
字号:
* properties are only set if their current value is either null * or a UIResource, other properties are set if the current * value is null. * * @see #uninstallDefaults * @see #installUI * @see CellRendererPane */ protected void installDefaults() { list.setLayout(null); LookAndFeel.installBorder(list, "List.border"); LookAndFeel.installColorsAndFont(list, "List.background", "List.foreground", "List.font"); LookAndFeel.installProperty(list, "opaque", Boolean.TRUE); if (list.getCellRenderer() == null) { list.setCellRenderer((ListCellRenderer)(UIManager.get("List.cellRenderer"))); } Color sbg = list.getSelectionBackground(); if (sbg == null || sbg instanceof UIResource) { list.setSelectionBackground(UIManager.getColor("List.selectionBackground")); } Color sfg = list.getSelectionForeground(); if (sfg == null || sfg instanceof UIResource) { list.setSelectionForeground(UIManager.getColor("List.selectionForeground")); } Long l = (Long)UIManager.get("List.timeFactor"); timeFactor = (l!=null) ? l.longValue() : 1000L; updateIsFileList(); } private void updateIsFileList() { boolean b = Boolean.TRUE.equals(list.getClientProperty("List.isFileList")); if (b != isFileList) { isFileList = b; Font oldFont = list.getFont(); if (oldFont == null || oldFont instanceof UIResource) { Font newFont = UIManager.getFont(b ? "FileChooser.listFont" : "List.font"); if (newFont != null && newFont != oldFont) { list.setFont(newFont); } } } } /** * Set the JList properties that haven't been explicitly overridden to * null. A property is considered overridden if its current value * is not a UIResource. * * @see #installDefaults * @see #uninstallUI * @see CellRendererPane */ protected void uninstallDefaults() { LookAndFeel.uninstallBorder(list); if (list.getFont() instanceof UIResource) { list.setFont(null); } if (list.getForeground() instanceof UIResource) { list.setForeground(null); } if (list.getBackground() instanceof UIResource) { list.setBackground(null); } if (list.getSelectionBackground() instanceof UIResource) { list.setSelectionBackground(null); } if (list.getSelectionForeground() instanceof UIResource) { list.setSelectionForeground(null); } if (list.getCellRenderer() instanceof UIResource) { list.setCellRenderer(null); } if (list.getTransferHandler() instanceof UIResource) { list.setTransferHandler(null); } } /** * Initializes <code>this.list</code> by calling <code>installDefaults()</code>, * <code>installListeners()</code>, and <code>installKeyboardActions()</code> * in order. * * @see #installDefaults * @see #installListeners * @see #installKeyboardActions */ public void installUI(JComponent c) { list = (JList)c; layoutOrientation = list.getLayoutOrientation(); rendererPane = new CellRendererPane(); list.add(rendererPane); columnCount = 1; updateLayoutStateNeeded = modelChanged; isLeftToRight = list.getComponentOrientation().isLeftToRight(); installDefaults(); installListeners(); installKeyboardActions(); } /** * Uninitializes <code>this.list</code> by calling <code>uninstallListeners()</code>, * <code>uninstallKeyboardActions()</code>, and <code>uninstallDefaults()</code> * in order. Sets this.list to null. * * @see #uninstallListeners * @see #uninstallKeyboardActions * @see #uninstallDefaults */ public void uninstallUI(JComponent c) { uninstallListeners(); uninstallDefaults(); uninstallKeyboardActions(); cellWidth = cellHeight = -1; cellHeights = null; listWidth = listHeight = -1; list.remove(rendererPane); rendererPane = null; list = null; } /** * Returns a new instance of BasicListUI. BasicListUI delegates are * allocated one per JList. * * @return A new ListUI implementation for the Windows look and feel. */ public static ComponentUI createUI(JComponent list) { return new BasicListUI(); } /** * {@inheritDoc} * @throws NullPointerException {@inheritDoc} */ public int locationToIndex(JList list, Point location) { maybeUpdateLayoutState(); return convertLocationToModel(location.x, location.y); } /** * {@inheritDoc} */ public Point indexToLocation(JList list, int index) { maybeUpdateLayoutState(); Rectangle rect = getCellBounds(list, index, index); if (rect != null) { return new Point(rect.x, rect.y); } return null; } /** * {@inheritDoc} */ public Rectangle getCellBounds(JList list, int index1, int index2) { maybeUpdateLayoutState(); int minIndex = Math.min(index1, index2); int maxIndex = Math.max(index1, index2); if (minIndex >= list.getModel().getSize()) { return null; } Rectangle minBounds = getCellBounds(list, minIndex); if (minBounds == null) { return null; } if (minIndex == maxIndex) { return minBounds; } Rectangle maxBounds = getCellBounds(list, maxIndex); if (maxBounds != null) { if (layoutOrientation == JList.HORIZONTAL_WRAP) { int minRow = convertModelToRow(minIndex); int maxRow = convertModelToRow(maxIndex); if (minRow != maxRow) { minBounds.x = 0; minBounds.width = list.getWidth(); } } else if (minBounds.x != maxBounds.x) { // Different columns minBounds.y = 0; minBounds.height = list.getHeight(); } minBounds.add(maxBounds); } return minBounds; } /** * Gets the bounds of the specified model index, returning the resulting * bounds, or null if <code>index</code> is not valid. */ private Rectangle getCellBounds(JList list, int index) { maybeUpdateLayoutState(); int row = convertModelToRow(index); int column = convertModelToColumn(index); if (row == -1 || column == -1) { return null; } Insets insets = list.getInsets(); int x; int w = cellWidth; int y = insets.top; int h; switch (layoutOrientation) { case JList.VERTICAL_WRAP: case JList.HORIZONTAL_WRAP: if (isLeftToRight) { x = insets.left + column * cellWidth; } else { x = list.getWidth() - insets.right - (column+1) * cellWidth; } y += cellHeight * row; h = cellHeight; break; default: x = insets.left; if (cellHeights == null) { y += (cellHeight * row); } else if (row >= cellHeights.length) { y = 0; } else { for(int i = 0; i < row; i++) { y += cellHeights[i]; } } w = list.getWidth() - (insets.left + insets.right); h = getRowHeight(index); break; } return new Rectangle(x, y, w, h); } /** * Returns the height of the specified row based on the current layout. * * @return The specified row height or -1 if row isn't valid. * @see #convertYToRow * @see #convertRowToY * @see #updateLayoutState */ protected int getRowHeight(int row) { return getHeight(0, row); } /** * Convert the JList relative coordinate to the row that contains it, * based on the current layout. If y0 doesn't fall within any row, * return -1. * * @return The row that contains y0, or -1. * @see #getRowHeight * @see #updateLayoutState */ protected int convertYToRow(int y0) { return convertLocationToRow(0, y0, false); } /** * Return the JList relative Y coordinate of the origin of the specified * row or -1 if row isn't valid. * * @return The Y coordinate of the origin of row, or -1. * @see #getRowHeight * @see #updateLayoutState */ protected int convertRowToY(int row) { if (row >= getRowCount(0) || row < 0) { return -1; } Rectangle bounds = getCellBounds(list, row, row); return bounds.y; } /** * Returns the height of the cell at the passed in location. */ private int getHeight(int column, int row) { if (column < 0 || column > columnCount || row < 0) { return -1; } if (layoutOrientation != JList.VERTICAL) { return cellHeight; } if (row >= list.getModel().getSize()) { return -1; } return (cellHeights == null) ? cellHeight : ((row < cellHeights.length) ? cellHeights[row] : -1); } /** * Returns the row at location x/y. * * @param closest If true and the location doesn't exactly match a * particular location, this will return the closest row. */ private int convertLocationToRow(int x, int y0, boolean closest) { int size = list.getModel().getSize(); if (size <= 0) { return -1; } Insets insets = list.getInsets(); if (cellHeights == null) { int row = (cellHeight == 0) ? 0 : ((y0 - insets.top) / cellHeight); if (closest) { if (row < 0) { row = 0; } else if (row >= size) { row = size - 1; } } return row; } else if (size > cellHeights.length) { return -1; } else { int y = insets.top; int row = 0; if (closest && y0 < y) { return 0; } int i;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -