basicscrollpaneui.java

来自「JAVA的一些源码 JAVA2 STANDARD EDITION DEVELO」· Java 代码 · 共 948 行 · 第 1/3 页

JAVA
948
字号
	    if (rowHead != null) {		Point p = rowHead.getViewPosition();		p.y = viewport.getViewPosition().y;                p.x = 0;		rowHead.setViewPosition(p);	    }	    if (colHead != null) {		Point p = colHead.getViewPosition();		if (ltr) {		    p.x = viewport.getViewPosition().x;		} else {		    p.x = Math.max(0, viewport.getViewPosition().x);		}                p.y = 0;		colHead.setViewPosition(p);	    }	}    }    /**     * Listener for viewport events.     */    public class ViewportChangeHandler implements ChangeListener    {        // NOTE: This class exists only for backward compatability. All        // its functionality has been moved into Handler. If you need to add        // new functionality add it to the Handler, but make sure this              // class calls into the Handler.	public void stateChanged(ChangeEvent e) {            getHandler().stateChanged(e);	}    }    protected ChangeListener createViewportChangeListener() {	return getHandler();    }    /**     * Horizontal scrollbar listener.     */    public class HSBChangeListener implements ChangeListener    {        // NOTE: This class exists only for backward compatability. All        // its functionality has been moved into Handler. If you need to add        // new functionality add it to the Handler, but make sure this              // class calls into the Handler.	public void stateChanged(ChangeEvent e)	{            getHandler().stateChanged(e);	}    }    /**     * Returns a <code>PropertyChangeListener</code> that will be installed     * on the horizontal <code>JScrollBar</code>.     */    private PropertyChangeListener createHSBPropertyChangeListener() {        return getHandler();    }    protected ChangeListener createHSBChangeListener() {	return getHandler();    }    /**     * Vertical scrollbar listener.     */    public class VSBChangeListener implements ChangeListener    {        // NOTE: This class exists only for backward compatability. All        // its functionality has been moved into Handler. If you need to add        // new functionality add it to the Handler, but make sure this              // class calls into the Handler.	public void stateChanged(ChangeEvent e)	{            getHandler().stateChanged(e);	}    }    /**     * Returns a <code>PropertyChangeListener</code> that will be installed     * on the vertical <code>JScrollBar</code>.     */    private PropertyChangeListener createVSBPropertyChangeListener() {        return getHandler();    }    protected ChangeListener createVSBChangeListener() {	return getHandler();    }    /**     * MouseWheelHandler is an inner class which implements the      * MouseWheelListener interface.  MouseWheelHandler responds to      * MouseWheelEvents by scrolling the JScrollPane appropriately.     * If the scroll pane's     * <code>isWheelScrollingEnabled</code>     * method returns false, no scrolling occurs.     *      * @see javax.swing.JScrollPane#isWheelScrollingEnabled     * @see #createMouseWheelListener     * @see java.awt.event.MouseWheelListener     * @see java.awt.event.MouseWheelEvent     * @since 1.4     */    protected class MouseWheelHandler implements MouseWheelListener {        // NOTE: This class exists only for backward compatability. All        // its functionality has been moved into Handler. If you need to add        // new functionality add it to the Handler, but make sure this              // class calls into the Handler.        /**         * Called when the mouse wheel is rotated while over a         * JScrollPane.         *         * @param e     MouseWheelEvent to be handled         * @since 1.4         */        public void mouseWheelMoved(MouseWheelEvent e) {            getHandler().mouseWheelMoved(e);        }    }    /**     * Creates an instance of MouseWheelListener, which is added to the     * JScrollPane by installUI().  The returned MouseWheelListener is used     * to handle mouse wheel-driven scrolling.     *     * @return      MouseWheelListener which implements wheel-driven scrolling     * @see #installUI     * @see MouseWheelHandler     * @since 1.4     */    protected MouseWheelListener createMouseWheelListener() {        return getHandler();    }    protected void updateScrollBarDisplayPolicy(PropertyChangeEvent e) {	scrollpane.revalidate();	scrollpane.repaint();    }    protected void updateViewport(PropertyChangeEvent e)     {	JViewport oldViewport = (JViewport)(e.getOldValue());	JViewport newViewport = (JViewport)(e.getNewValue());	if (oldViewport != null) {	    oldViewport.removeChangeListener(viewportChangeListener);	}		if (newViewport != null) {	    Point p = newViewport.getViewPosition();	    if (scrollpane.getComponentOrientation().isLeftToRight()) {		p.x = Math.max(p.x, 0);	    } else {		int max = newViewport.getViewSize().width;		int extent = newViewport.getExtentSize().width;		if (extent > max) {		    p.x = max - extent;		} else {		    p.x = Math.max(0, Math.min(max - extent, p.x));		}	    }	    p.y = Math.max(p.y, 0);	    newViewport.setViewPosition(p);	    newViewport.addChangeListener(viewportChangeListener);	}    }    protected void updateRowHeader(PropertyChangeEvent e)     {	JViewport newRowHead = (JViewport)(e.getNewValue());	if (newRowHead != null) {	    JViewport viewport = scrollpane.getViewport();	    Point p = newRowHead.getViewPosition();	    p.y = (viewport != null) ? viewport.getViewPosition().y : 0;	    newRowHead.setViewPosition(p);	}    }    protected void updateColumnHeader(PropertyChangeEvent e)     {	JViewport newColHead = (JViewport)(e.getNewValue());	if (newColHead != null) {	    JViewport viewport = scrollpane.getViewport();	    Point p = newColHead.getViewPosition();	    if (viewport == null) {		p.x = 0;	    } else {		if (scrollpane.getComponentOrientation().isLeftToRight()) {		    p.x = viewport.getViewPosition().x;		} else {		    p.x = Math.max(0, viewport.getViewPosition().x);		}	    }	    newColHead.setViewPosition(p);	    scrollpane.add(newColHead, COLUMN_HEADER);	}    }    private void updateHorizontalScrollBar(PropertyChangeEvent pce) {	updateScrollBar(pce, hsbChangeListener, hsbPropertyChangeListener);    }    private void updateVerticalScrollBar(PropertyChangeEvent pce) {	updateScrollBar(pce, vsbChangeListener, vsbPropertyChangeListener);    }    private void updateScrollBar(PropertyChangeEvent pce, ChangeListener cl,                                 PropertyChangeListener pcl) {        JScrollBar sb = (JScrollBar)pce.getOldValue();        if (sb != null) {            if (cl != null) {                sb.getModel().removeChangeListener(cl);            }            if (pcl != null) {                sb.removePropertyChangeListener(pcl);            }        }        sb = (JScrollBar)pce.getNewValue();        if (sb != null) {            if (cl != null) {                sb.getModel().addChangeListener(cl);            }            if (pcl != null) {                sb.addPropertyChangeListener(pcl);            }	}    }    public class PropertyChangeHandler implements PropertyChangeListener    {        // NOTE: This class exists only for backward compatability. All        // its functionality has been moved into Handler. If you need to add        // new functionality add it to the Handler, but make sure this              // class calls into the Handler.        public void propertyChange(PropertyChangeEvent e)        {            getHandler().propertyChange(e);	}    }    /**     * Creates an instance of PropertyChangeListener that's added to      * the JScrollPane by installUI().  Subclasses can override this method     * to return a custom PropertyChangeListener, e.g.     * <pre>     * class MyScrollPaneUI extends BasicScrollPaneUI {     *    protected PropertyChangeListener <b>createPropertyChangeListener</b>() {     *        return new MyPropertyChangeListener();     *    }     *    public class MyPropertyChangeListener extends PropertyChangeListener {     *        public void propertyChange(PropertyChangeEvent e) {     *            if (e.getPropertyName().equals("viewport")) {     *                // do some extra work when the viewport changes     *            }     *            super.propertyChange(e);     *        }     *    }     * }     * </pre>     *      * @see java.beans.PropertyChangeListener     * @see #installUI     */    protected PropertyChangeListener createPropertyChangeListener() {        return getHandler();    }    private static class Actions extends UIAction {        private static final String SCROLL_UP = "scrollUp";        private static final String SCROLL_DOWN = "scrollDown";        private static final String SCROLL_HOME = "scrollHome";        private static final String SCROLL_END = "scrollEnd";        private static final String UNIT_SCROLL_UP = "unitScrollUp";        private static final String UNIT_SCROLL_DOWN = "unitScrollDown";        private static final String SCROLL_LEFT = "scrollLeft";        private static final String SCROLL_RIGHT = "scrollRight";        private static final String UNIT_SCROLL_LEFT = "unitScrollLeft";        private static final String UNIT_SCROLL_RIGHT = "unitScrollRight";        Actions(String key) {            super(key);        }        public void actionPerformed(ActionEvent e) {            JScrollPane scrollPane = (JScrollPane)e.getSource();            boolean ltr = scrollPane.getComponentOrientation().isLeftToRight();            String key = getName();            if (key == SCROLL_UP) {                scroll(scrollPane, SwingConstants.VERTICAL, -1, true);            }

⌨️ 快捷键说明

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