📄 basiclistui.java
字号:
} ActionMap getActionMap() { ActionMap map = (ActionMap)UIManager.get("List.actionMap"); if (map == null) { map = createActionMap(); if (map != null) { UIManager.getLookAndFeelDefaults().put("List.actionMap", map); } } return map; } ActionMap createActionMap() { ActionMap map = new ActionMapUIResource(); map.put("selectPreviousColumn", new IncrementLeadByColumnAction("selectPreviousColumn", CHANGE_SELECTION, -1)); map.put("selectPreviousColumnExtendSelection", new IncrementLeadByColumnAction ("selectPreviousColumnExtendSelection", EXTEND_SELECTION, -1)); map.put("selectNextColumn", new IncrementLeadByColumnAction("selectNextColumn", CHANGE_SELECTION, 1)); map.put("selectNextColumnExtendSelection", new IncrementLeadByColumnAction ("selectNextColumnExtendSelection", EXTEND_SELECTION, 1)); map.put("selectPreviousRow", new IncrementLeadSelectionAction("selectPreviousRow", CHANGE_SELECTION, -1)); map.put("selectPreviousRowExtendSelection", new IncrementLeadSelectionAction ("selectPreviousRowExtendSelection",EXTEND_SELECTION, -1)); map.put("selectNextRow", new IncrementLeadSelectionAction("selectNextRow", CHANGE_SELECTION, 1)); map.put("selectNextRowExtendSelection", new IncrementLeadSelectionAction ("selectNextRowExtendSelection", EXTEND_SELECTION, 1)); map.put("selectFirstRow", new HomeAction("selectFirstRow", CHANGE_SELECTION)); map.put("selectFirstRowExtendSelection", new HomeAction("selectFirstRowExtendSelection", EXTEND_SELECTION)); map.put("selectLastRow", new EndAction("selctLastRow", CHANGE_SELECTION)); map.put("selectLastRowExtendSelection", new EndAction("selectLastRowExtendSelection", EXTEND_SELECTION)); map.put("scrollUp", new PageUpAction("scrollUp", CHANGE_SELECTION)); map.put("scrollUpExtendSelection", new PageUpAction("scrollUpExtendSelection", EXTEND_SELECTION)); map.put("scrollDown", new PageDownAction("scrollDown", CHANGE_SELECTION)); map.put("scrollDownExtendSelection", new PageDownAction("scrollDownExtendSelection", EXTEND_SELECTION)); map.put("selectAll", new SelectAllAction("selectAll")); map.put("clearSelection", new ClearSelectionAction("clearSelection")); map.put(TransferHandler.getCutAction().getValue(Action.NAME), TransferHandler.getCutAction()); map.put(TransferHandler.getCopyAction().getValue(Action.NAME), TransferHandler.getCopyAction()); map.put(TransferHandler.getPasteAction().getValue(Action.NAME), TransferHandler.getPasteAction()); return map; } /** * 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() { focusListener = createFocusListener(); mouseInputListener = createMouseInputListener(); propertyChangeListener = createPropertyChangeListener(); listSelectionListener = createListSelectionListener(); listDataListener = createListDataListener(); keyListener = createKeyListener(); list.addFocusListener(focusListener); list.addMouseListener(defaultDragRecognizer); list.addMouseMotionListener(defaultDragRecognizer); list.addMouseListener(mouseInputListener); list.addMouseMotionListener(mouseInputListener); list.addPropertyChangeListener(propertyChangeListener); list.addKeyListener(keyListener); 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(defaultDragRecognizer); list.removeMouseMotionListener(defaultDragRecognizer); list.removeMouseListener(mouseInputListener); list.removeMouseMotionListener(mouseInputListener); list.removePropertyChangeListener(propertyChangeListener); list.removeKeyListener(keyListener); 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; keyListener = null; } /** * Initialize JList properties, e.g. font, foreground, and background, * and add the CellRendererPane. The font, foreground, and background * 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() { columnCount = 1; list.setLayout(null); LookAndFeel.installBorder(list, "List.border"); LookAndFeel.installColorsAndFont(list, "List.background", "List.foreground", "List.font"); 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")); } TransferHandler th = list.getTransferHandler(); if (th == null || th instanceof UIResource) { list.setTransferHandler(defaultTransferHandler); } DropTarget dropTarget = list.getDropTarget(); if (dropTarget instanceof UIResource) { try { dropTarget.addDropTargetListener(new ListDropTargetListener()); } catch (TooManyListenersException tmle) { // should not happen... swing drop target is multicast } } } /** * 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); 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(); } /** * Convert a point in <code>JList</code> coordinates to the closest index * of the cell at that location. To determine if the cell actually * contains the specified location use a combination of this method and * <code>getCellBounds</code>. Returns -1 if the model is empty. * * @return The index of the cell at location, or -1. * @see ListUI#locationToIndex */ public int locationToIndex(JList list, Point location) { maybeUpdateLayoutState(); return convertLocationToModel(location.x, location.y); } /** * @return The origin of the index'th cell, null if index is invalid. * @see ListUI#indexToLocation */ 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; } /** * @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();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -