synthlistui.java

来自「java jdk 1.4的源码」· Java 代码 · 共 1,949 行 · 第 1/5 页

JAVA
1,949
字号
    }    /**     * @return the preferred size     * @see #getPreferredSize     */    public Dimension getMaximumSize(JComponent c) {        return getPreferredSize(c);    }    /**     * 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, SynthListUI.class,                                           "List.actionMap");    }    InputMap getInputMap(int condition) {	if (condition == JComponent.WHEN_FOCUSED) {            SynthContext context = getContext(list, ENABLED);	    InputMap keyMap = (InputMap)context.getStyle().get(context,                                                        "List.focusInputMap");	    InputMap rtlKeyMap;	    if (!SynthLookAndFeel.isLeftToRight(list) &&                       (rtlKeyMap = (InputMap)context.getStyle().get(context,                      "List.focusInputMap.RightToLeft")) != null) {                rtlKeyMap.setParent(keyMap);                keyMap = rtlKeyMap;	    }            context.dispose();            return keyMap;	}	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()    {        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);        fetchStyle(list);        if (list.getCellRenderer() == null) {            list.setCellRenderer(new DefaultListCellRenderer.UIResource());        }	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	    }	}    }    private void fetchStyle(JComponent c) {        SynthContext context = getContext(list, ENABLED);        SynthStyle oldStyle = style;        style = SynthLookAndFeel.updateStyle(context, this);        if (style != oldStyle) {            context.setComponentState(SELECTED);            Color sbg = list.getSelectionBackground();            if (sbg == null || sbg instanceof UIResource) {                list.setSelectionBackground(style.getColor(                                 context, ColorType.TEXT_BACKGROUND));            }            Color sfg = list.getSelectionForeground();            if (sfg == null || sfg instanceof UIResource) {                list.setSelectionForeground(style.getColor(                                 context, ColorType.TEXT_FOREGROUND));            }        }        context.dispose();            }    /**     * 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() {        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);	}        SynthContext context = getContext(list, ENABLED);        style.uninstallDefaults(context);        // Uninstall any class specific state that was installed in        // fetchStyle here.        context.dispose();        style = 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;    }    public SynthContext getContext(JComponent c) {        return getContext(c, getComponentState(c));    }    private SynthContext getContext(JComponent c, int state) {        return SynthContext.getContext(SynthContext.class, c,                    SynthLookAndFeel.getRegion(c), style, state);    }    private Region getRegion(JComponent c) {        return SynthLookAndFeel.getRegion(c);    }    private int getComponentState(JComponent c) {        return SynthLookAndFeel.getComponentState(c);    }    /**     * 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 SynthListUI();    }    /**     * 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;

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?