📄 basiclistui.java
字号:
return new Point(rect.x, rect.y); } return null; } /** * @return The bounds of the index'th cell. * @see ListUI#getCellBounds */ 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; for (i = 0; i < size; i++) { if ((y0 >= y) && (y0 < y + cellHeights[i])) { return row; } y += cellHeights[i]; row += 1; } return i - 1; } } /** * Returns the closest row that starts at the specified y-location * in the passed in column. */ private int convertLocationToRowInColumn(int y, int column) { int x = 0; if (layoutOrientation != JList.VERTICAL) { if (isLeftToRight) { x = column * cellWidth; } else { x = list.getWidth() - (column+1)*cellWidth - list.getInsets().right; } } return convertLocationToRow(x, y, true); } /** * Returns the closest location to the model index of the passed in * location. */ private int convertLocationToModel(int x, int y) { int row = convertLocationToRow(x, y, true); int column = convertLocationToColumn(x, y); if (row >= 0 && column >= 0) { return getModelIndex(column, row); } return -1; } /** * Returns the number of rows in the given column. */ private int getRowCount(int column) { if (column < 0 || column >= columnCount) { return -1; } if (layoutOrientation == JList.VERTICAL || (column == 0 && columnCount == 1)) { return list.getModel().getSize(); } if (column >= columnCount) { return -1; } if (layoutOrientation == JList.VERTICAL_WRAP) { if (column < (columnCount - 1)) { return rowsPerColumn; } return list.getModel().getSize() - (columnCount - 1) * rowsPerColumn; } // JList.HORIZONTAL_WRAP int diff = columnCount - (columnCount * rowsPerColumn - list.getModel().getSize()); if (column >= diff) { return Math.max(0, rowsPerColumn - 1); } return rowsPerColumn; } /** * Returns the model index for the specified display location. * If <code>column</code>x<code>row</code> is beyond the length of the * model, this will return the model size - 1. */ private int getModelIndex(int column, int row) { switch (layoutOrientation) { case JList.VERTICAL_WRAP: return Math.min(list.getModel().getSize() - 1, rowsPerColumn * column + Math.min(row, rowsPerColumn-1)); case JList.HORIZONTAL_WRAP: return Math.min(list.getModel().getSize() - 1, row * columnCount + column); default: return row; } } /** * Returns the closest column to the passed in location. */ private int convertLocationToColumn(int x, int y) { if (cellWidth > 0) { if (layoutOrientation == JList.VERTICAL) { return 0; } Insets insets = list.getInsets(); int col; if (isLeftToRight) { col = (x - insets.left) / cellWidth; } else { col = (list.getWidth() - x - insets.right - 1) / cellWidth; } if (col < 0) { return 0; } else if (col >= columnCount) { return columnCount - 1; } return col; } return 0; } /** * Returns the row that the model index <code>index</code> will be * displayed in.. */ private int convertModelToRow(int index) { int size = list.getModel().getSize(); if ((index < 0) || (index >= size)) { return -1; } if (layoutOrientation != JList.VERTICAL && columnCount > 1 && rowsPerColumn > 0) { if (layoutOrientation == JList.VERTICAL_WRAP) { return index % rowsPerColumn; } return index / columnCount; } return index; } /** * Returns the column that the model index <code>index</code> will be * displayed in. */ private int convertModelToColumn(int index) { int size = list.getModel().getSize(); if ((index < 0) || (index >= size)) { return -1; } if (layoutOrientation != JList.VERTICAL && rowsPerColumn > 0 && columnCount > 1) { if (layoutOrientation == JList.VERTICAL_WRAP) { return index / rowsPerColumn; } return index % columnCount; } return 0; } /** * If updateLayoutStateNeeded is non zero, call updateLayoutState() and reset * updateLayoutStateNeeded. This method should be called by methods * before doing any computation based on the geometry of the list. * For example it's the first call in paint() and getPreferredSize(). * * @see #updateLayoutState */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -