📄 basiclistui.java
字号:
columnCount = 1; if (layoutOrientation != JList.VERTICAL) { updateHorizontalLayoutState(fixedCellWidth, fixedCellHeight); } } /** * Invoked when the list is layed out horizontally to determine how * many columns to create. * <p> * This updates the <code>rowsPerColumn, </code><code>columnCount</code>, * <code>preferredHeight</code> and potentially <code>cellHeight</code> * instance variables. */ private void updateHorizontalLayoutState(int fixedCellWidth, int fixedCellHeight) { int visRows = list.getVisibleRowCount(); int dataModelSize = list.getModel().getSize(); Insets insets = list.getInsets(); listHeight = list.getHeight(); listWidth = list.getWidth(); if (dataModelSize == 0) { rowsPerColumn = columnCount = 0; preferredHeight = insets.top + insets.bottom; return; } int height; if (fixedCellHeight != -1) { height = fixedCellHeight; } else { // Determine the max of the renderer heights. int maxHeight = 0; if (cellHeights.length > 0) { maxHeight = cellHeights[cellHeights.length - 1]; for (int counter = cellHeights.length - 2; counter >= 0; counter--) { maxHeight = Math.max(maxHeight, cellHeights[counter]); } } height = cellHeight = maxHeight; cellHeights = null; } // The number of rows is either determined by the visible row // count, or by the height of the list. rowsPerColumn = dataModelSize; if (visRows > 0) { rowsPerColumn = visRows; columnCount = Math.max(1, dataModelSize / rowsPerColumn); if (dataModelSize > 0 && dataModelSize > rowsPerColumn && dataModelSize % rowsPerColumn != 0) { columnCount++; } if (layoutOrientation == JList.HORIZONTAL_WRAP) { // Because HORIZONTAL_WRAP flows differently, the // rowsPerColumn needs to be adjusted. rowsPerColumn = (dataModelSize / columnCount); if (dataModelSize % columnCount > 0) { rowsPerColumn++; } } } else if (layoutOrientation == JList.VERTICAL_WRAP && height != 0) { rowsPerColumn = Math.max(1, (listHeight - insets.top - insets.bottom) / height); columnCount = Math.max(1, dataModelSize / rowsPerColumn); if (dataModelSize > 0 && dataModelSize > rowsPerColumn && dataModelSize % rowsPerColumn != 0) { columnCount++; } } else if (layoutOrientation == JList.HORIZONTAL_WRAP && cellWidth > 0 && listWidth > 0) { columnCount = Math.max(1, (listWidth - insets.left - insets.right) / cellWidth); rowsPerColumn = dataModelSize / columnCount; if (dataModelSize % columnCount > 0) { rowsPerColumn++; } } preferredHeight = rowsPerColumn * cellHeight + insets.top + insets.bottom; } /** * Mouse input, and focus handling for JList. An instance of this * class is added to the appropriate java.awt.Component lists * at installUI() time. Note keyboard input is handled with JComponent * KeyboardActions, see installKeyboardActions(). * <p> * <strong>Warning:</strong> * Serialized objects of this class will not be compatible with * future Swing releases. The current serialization support is * appropriate for short term storage or RMI between applications running * the same version of Swing. As of 1.4, support for long term storage * of all JavaBeans<sup><font size="-2">TM</font></sup> * has been added to the <code>java.beans</code> package. * Please see {@link java.beans.XMLEncoder}. * * @see #createMouseInputListener * @see #installKeyboardActions * @see #installUI */ public class MouseInputHandler implements MouseInputListener { public void mouseClicked(MouseEvent e) {} public void mouseEntered(MouseEvent e) {} public void mouseExited(MouseEvent e) {} public void mousePressed(MouseEvent e) { if (e.isConsumed()) { selectedOnPress = false; return; } selectedOnPress = true; adjustFocusAndSelection(e); } void adjustFocusAndSelection(MouseEvent e) { if (!SwingUtilities.isLeftMouseButton(e)) { return; } if (!list.isEnabled()) { return; } /* Request focus before updating the list selection. This implies * that the current focus owner will see a focusLost() event * before the lists selection is updated IF requestFocus() is * synchronous (it is on Windows). See bug 4122345 */ if (!list.hasFocus() && list.isRequestFocusEnabled()) { list.requestFocus(); } int row = convertLocationToModel(e.getX(), e.getY()); if (row != -1) { boolean adjusting = (e.getID() == MouseEvent.MOUSE_PRESSED) ? true : false; list.setValueIsAdjusting(adjusting); int anchorIndex = list.getAnchorSelectionIndex(); if (e.isControlDown()) { if (list.isSelectedIndex(row)) { list.removeSelectionInterval(row, row); } else { list.addSelectionInterval(row, row); } } else if (e.isShiftDown() && (anchorIndex != -1)) { list.setSelectionInterval(anchorIndex, row); } else { list.setSelectionInterval(row, row); } } } public void mouseDragged(MouseEvent e) { if (e.isConsumed()) { return; } if (!SwingUtilities.isLeftMouseButton(e)) { return; } if (!list.isEnabled()) { return; } if (e.isShiftDown() || e.isControlDown()) { return; } int row = convertLocationToModel(e.getX(), e.getY()); if (row != -1) { Rectangle cellBounds = getCellBounds(list, row, row); if (cellBounds != null) { list.scrollRectToVisible(cellBounds); list.setSelectionInterval(row, row); } } } public void mouseMoved(MouseEvent e) { } public void mouseReleased(MouseEvent e) { if (selectedOnPress) { if (!SwingUtilities.isLeftMouseButton(e)) { return; } list.setValueIsAdjusting(false); } else { adjustFocusAndSelection(e); } } private boolean selectedOnPress; } /** * Creates a delegate that implements MouseInputListener. * The delegate is added to the corresponding java.awt.Component listener * lists at installUI() time. Subclasses can override this method to return * a custom MouseInputListener, e.g. * <pre> * class MyListUI extends BasicListUI { * protected MouseInputListener <b>createMouseInputListener</b>() { * return new MyMouseInputHandler(); * } * public class MyMouseInputHandler extends MouseInputHandler { * public void mouseMoved(MouseEvent e) { * // do some extra work when the mouse moves * super.mouseMoved(e); * } * } * } * </pre> * * @see MouseInputHandler * @see #installUI */ protected MouseInputListener createMouseInputListener() { return new MouseInputHandler(); } /** * This inner class is marked "public" due to a compiler bug. * This class should be treated as a "protected" inner class. * Instantiate it only within subclasses of BasicTableUI. */ public class FocusHandler implements FocusListener { protected void repaintCellFocus() { int leadIndex = list.getLeadSelectionIndex(); if (leadIndex != -1) { Rectangle r = getCellBounds(list, leadIndex, leadIndex); if (r != null) { list.repaint(r.x, r.y, r.width, r.height); } } } /* The focusGained() focusLost() methods run when the JList * focus changes. */ public void focusGained(FocusEvent e) { // hasFocus = true; repaintCellFocus(); } public void focusLost(FocusEvent e) { // hasFocus = false; repaintCellFocus(); } } protected FocusListener createFocusListener() { return new FocusHandler(); } /** * The ListSelectionListener that's added to the JLists selection * model at installUI time, and whenever the JList.selectionModel property * changes. When the selection changes we repaint the affected rows. * <p> * <strong>Warning:</strong> * Serialized objects of this class will not be compatible with * future Swing releases. The current serialization support is * appropriate for short term storage or RMI between applications running * the same version of Swing. As of 1.4, support for long term storage * of all JavaBeans<sup><font size="-2">TM</font></sup> * has been added to the <code>java.beans</code> package. * Please see {@link java.beans.XMLEncoder}. * * @see #createListSelectionListener * @see #getCellBounds * @see #installUI */ public class ListSelectionHandler implements ListSelectionListener { public void valueChanged(ListSelectionEvent e) { maybeUpdateLayoutState(); Rectangle bounds = getCellBounds(list, e.getFirstIndex(), e.getLastIndex()); if (bounds != null) { list.repaint(bounds.x, bounds.y, bounds.width, bounds.height); } } } /** * Creates an instance of ListSelectionHandler that's added to * the JLists by selectionModel as needed. Subclasses can override * this method to return a custom ListSelectionListener, e.g. * <pre> * class MyListUI extends BasicListUI { * protected ListSelectionListener <b>createListSelectionListener</b>() { * return new MySelectionListener(); * } * public class MySelectionListener extends ListSelectionHandler { * public void valueChanged(ListSelectionEvent e) { * // do some extra work when the selection changes * super.valueChange(e); * } * } * } * </pre> * * @see ListSelectionHandler * @see #installUI */ protected ListSelectionListener createListSelectionListener() { return new ListSelectionHandler(); } private void redrawList() { list.revalidate(); list.repaint(); } /** * The ListDataListener that's added to the JLists model at * installUI time, and whenever the JList.model property changes. * <p> * <strong>Warning:</strong> * Serialized objects of this class will not be compatible with * future Swing releases. The current serialization support is * appropriate for short term storage or RMI between applications running * the same version of Swing. As of 1.4, support for long term storage * of all JavaBeans<sup><font size="-2">TM</font></sup> * has been added to the <code>java.beans</code> package. * Please see {@link java.beans.XMLEncoder}. * * @see JList#getModel * @see #maybeUpdateLayoutState * @see #createListDataListener * @see #installUI */ public class ListDataHandler implements ListDataListener { public void intervalAdded(ListDataEvent e) { updateLayoutStateNeeded = modelChanged; int minIndex = Math.min(e.getIndex0(), e.getIndex1()); int maxIndex = Math.max(e.getIndex0(), e.getIndex1()); /* Sync the SelectionModel with the DataModel. */ ListSelectionModel sm = list.getSelectionModel(); if (sm != null) { sm.insertIndexInterval(minIndex, maxIndex - minIndex+1, true); } /* Repaint the entire list, from the origin of * the first added cell, to the bottom of the * component. */ redrawList(); } public void intervalRemoved(ListDataEvent e) { updateLayoutStateNeeded = modelChanged; /* Sync the SelectionModel with the DataModel. */ ListSelectionModel sm = list.getSelectionModel(); if (sm != null) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -