synthscrollpaneui.java

来自「java jdk 1.4的源码」· Java 代码 · 共 994 行 · 第 1/3 页

JAVA
994
字号
		}	    }	    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);            }	}    }    class PropertyChangeHandler implements PropertyChangeListener    {        public void propertyChange(PropertyChangeEvent e)        {            String propertyName = e.getPropertyName();            if (SynthLookAndFeel.shouldUpdateStyle(e)) {                fetchStyle((JScrollPane)e.getSource());            }	    if (propertyName.equals("verticalScrollBarDisplayPolicy")) {		updateScrollBarDisplayPolicy(e);	    }	    else if (propertyName.equals("horizontalScrollBarDisplayPolicy")) {		updateScrollBarDisplayPolicy(e);	    }	    else if (propertyName.equals("viewport")) {		updateViewport(e);	    }	    else if (propertyName.equals("rowHeader")) {		updateRowHeader(e);	    }	    else if (propertyName.equals("columnHeader")) {		updateColumnHeader(e);	    }	    else if (propertyName.equals("verticalScrollBar")) {		updateVerticalScrollBar(e);	    }	    else if (propertyName.equals("horizontalScrollBar")) {		updateHorizontalScrollBar(e);	    }	    else if (propertyName.equals("componentOrientation")) {		scrollpane.revalidate();		scrollpane.repaint();		InputMap inputMap = getInputMap(JComponent.					WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);		SwingUtilities.replaceUIInputMap(scrollpane, JComponent.				WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, inputMap);	    }	}    }    /**     * 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();     *    }     *    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 new PropertyChangeHandler();    }    /**     * Action to scroll left/right/up/down.     */    private static class ScrollAction extends AbstractAction {	/** Direction to scroll. */	protected int orientation;	/** 1 indicates scroll down, -1 up. */	protected int direction;	/** True indicates a block scroll, otherwise a unit scroll. */	private boolean block;	protected ScrollAction(String name, int orientation, int direction,			       boolean block) {	    super(name);	    this.orientation = orientation;	    this.direction = direction;	    this.block = block;	}	public void actionPerformed(ActionEvent e) {	    JScrollPane scrollpane = (JScrollPane)e.getSource();	    JViewport vp = scrollpane.getViewport();	    Component view;	    if (vp != null && (view = vp.getView()) != null) {		Rectangle visRect = vp.getViewRect();		Dimension vSize = view.getSize();		int amount;		if (view instanceof Scrollable) {		    if (block) {			amount = ((Scrollable)view).getScrollableBlockIncrement			         (visRect, orientation, direction);		    }		    else {			amount = ((Scrollable)view).getScrollableUnitIncrement			         (visRect, orientation, direction);		    }		}		else {		    if (block) {			if (orientation == SwingConstants.VERTICAL) {			    amount = visRect.height;			}			else {			    amount = visRect.width;			}		    }		    else {			amount = 10;		    }		}		if (orientation == SwingConstants.VERTICAL) {		    visRect.y += (amount * direction);		    if ((visRect.y + visRect.height) > vSize.height) {			visRect.y = Math.max(0, vSize.height - visRect.height);		    }		    else if (visRect.y < 0) {			visRect.y = 0;		    }		}		else {		    if (scrollpane.getComponentOrientation().isLeftToRight()) {			visRect.x += (amount * direction);			if ((visRect.x + visRect.width) > vSize.width) {			    visRect.x = Math.max(0, vSize.width - visRect.width);			} else if (visRect.x < 0) {			    visRect.x = 0;			}		    } else {			visRect.x -= (amount * direction);                        if (visRect.width > vSize.width) {                            visRect.x = vSize.width - visRect.width;                        } else {                            visRect.x = Math.max(0, Math.min(vSize.width - visRect.width, visRect.x));			}		    }		}		vp.setViewPosition(visRect.getLocation());	    }	}    }    /**     * Action to scroll to x,y location of 0,0.     */    private static class ScrollHomeAction extends AbstractAction {	protected ScrollHomeAction(String name) {	    super(name);	}	public void actionPerformed(ActionEvent e) {	    JScrollPane scrollpane = (JScrollPane)e.getSource();	    JViewport vp = scrollpane.getViewport();	    Component view;	    if (vp != null && (view = vp.getView()) != null) {		if (scrollpane.getComponentOrientation().isLeftToRight()) {		    vp.setViewPosition(new Point(0, 0));		} else {		    Rectangle visRect = vp.getViewRect();		    Rectangle bounds = view.getBounds();		    vp.setViewPosition(new Point(bounds.width - visRect.width, 0));		}	    }	}    }    /**     * Action to scroll to last visible location.     */    private static class ScrollEndAction extends AbstractAction {	protected ScrollEndAction(String name) {	    super(name);	}	public void actionPerformed(ActionEvent e) {	    JScrollPane scrollpane = (JScrollPane)e.getSource();	    JViewport vp = scrollpane.getViewport();	    Component view;	    if (vp != null && (view = vp.getView()) != null) {		Rectangle visRect = vp.getViewRect();		Rectangle bounds = view.getBounds();		if (scrollpane.getComponentOrientation().isLeftToRight()) {		    vp.setViewPosition(new Point(bounds.width - visRect.width,					     bounds.height - visRect.height));		} else {		    vp.setViewPosition(new Point(0, 					     bounds.height - visRect.height));		}	    }	}    }    private class ViewportBorder extends AbstractBorder implements UIResource {        private Insets insets;        ViewportBorder(SynthContext context) {            this.insets = (Insets)context.getStyle().get(context,                                            "ScrollPane.viewportBorderInsets");            if (this.insets == null) {                this.insets = SynthLookAndFeel.EMPTY_UIRESOURCE_INSETS;            }        }        public void paintBorder(Component c, Graphics g, int x, int y,                            int width, int height) {            JComponent jc = (JComponent)c;            SynthContext context = getContext(jc, Region.VIEWPORT);            SynthStyle style = context.getStyle();            if (style == null) {                assert false: "SynthBorder is being used outside after the " +                              " UI has been uninstalled";                return;            }            SynthPainter painter = (SynthPainter)style.get(context,                                          "ScrollPane.viewportBorderPainter");            if (painter != null) {                painter.paint(context, "border", g, x, y, width, height);            }            context.dispose();        }        public Insets getBorderInsets(Component c) {             return getBorderInsets(c, null);        }        public Insets getBorderInsets(Component c, Insets insets) {            if (insets == null) {                return new Insets(this.insets.top, this.insets.left,                                  this.insets.bottom, this.insets.right);            }            insets.top = this.insets.top;            insets.bottom = this.insets.bottom;            insets.left = this.insets.left;            insets.right = this.insets.left;            return insets;        }        public boolean isBorderOpaque() {            return false;        }    }}

⌨️ 快捷键说明

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