J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 1998-2004 Sun Microsystems, Inc. All Rights Reserved. |
| 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | * |
| 5 | * This code is free software; you can redistribute it and/or modify it |
| 6 | * under the terms of the GNU General Public License version 2 only, as |
| 7 | * published by the Free Software Foundation. Sun designates this |
| 8 | * particular file as subject to the "Classpath" exception as provided |
| 9 | * by Sun in the LICENSE file that accompanied this code. |
| 10 | * |
| 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
| 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 14 | * version 2 for more details (a copy is included in the LICENSE file that |
| 15 | * accompanied this code). |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License version |
| 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
| 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | * |
| 21 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 22 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 23 | * have any questions. |
| 24 | */ |
| 25 | |
| 26 | package javax.swing.plaf.metal; |
| 27 | |
| 28 | import javax.swing.*; |
| 29 | import javax.swing.event.*; |
| 30 | import javax.swing.border.*; |
| 31 | import javax.swing.plaf.*; |
| 32 | import javax.swing.plaf.basic.*; |
| 33 | |
| 34 | import java.awt.*; |
| 35 | import java.beans.*; |
| 36 | import java.awt.event.*; |
| 37 | |
| 38 | |
| 39 | /** |
| 40 | * A Metal L&F implementation of ScrollPaneUI. |
| 41 | * <p> |
| 42 | * <strong>Warning:</strong> |
| 43 | * Serialized objects of this class will not be compatible with |
| 44 | * future Swing releases. The current serialization support is |
| 45 | * appropriate for short term storage or RMI between applications running |
| 46 | * the same version of Swing. As of 1.4, support for long term storage |
| 47 | * of all JavaBeans<sup><font size="-2">TM</font></sup> |
| 48 | * has been added to the <code>java.beans</code> package. |
| 49 | * Please see {@link java.beans.XMLEncoder}. |
| 50 | * |
| 51 | * @author Steve Wilson |
| 52 | */ |
| 53 | public class MetalScrollPaneUI extends BasicScrollPaneUI |
| 54 | { |
| 55 | |
| 56 | private PropertyChangeListener scrollBarSwapListener; |
| 57 | |
| 58 | public static ComponentUI createUI(JComponent x) { |
| 59 | return new MetalScrollPaneUI(); |
| 60 | } |
| 61 | |
| 62 | public void installUI(JComponent c) { |
| 63 | |
| 64 | super.installUI(c); |
| 65 | |
| 66 | JScrollPane sp = (JScrollPane)c; |
| 67 | updateScrollbarsFreeStanding(); |
| 68 | } |
| 69 | |
| 70 | public void uninstallUI(JComponent c) { |
| 71 | super.uninstallUI(c); |
| 72 | |
| 73 | JScrollPane sp = (JScrollPane)c; |
| 74 | JScrollBar hsb = sp.getHorizontalScrollBar(); |
| 75 | JScrollBar vsb = sp.getVerticalScrollBar(); |
| 76 | if (hsb != null) { |
| 77 | hsb.putClientProperty( MetalScrollBarUI.FREE_STANDING_PROP, null); |
| 78 | } |
| 79 | if (vsb != null) { |
| 80 | vsb.putClientProperty( MetalScrollBarUI.FREE_STANDING_PROP, null); |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | |
| 85 | public void installListeners(JScrollPane scrollPane) { |
| 86 | super.installListeners(scrollPane); |
| 87 | scrollBarSwapListener = createScrollBarSwapListener(); |
| 88 | scrollPane.addPropertyChangeListener(scrollBarSwapListener); |
| 89 | } |
| 90 | |
| 91 | |
| 92 | public void uninstallListeners(JScrollPane scrollPane) { |
| 93 | super.uninstallListeners(scrollPane); |
| 94 | |
| 95 | scrollPane.removePropertyChangeListener(scrollBarSwapListener); |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * If the border of the scrollpane is an instance of |
| 100 | * <code>MetalBorders.ScrollPaneBorder</code>, the client property |
| 101 | * <code>FREE_STANDING_PROP</code> of the scrollbars |
| 102 | * is set to false, otherwise it is set to true. |
| 103 | */ |
| 104 | private void updateScrollbarsFreeStanding() { |
| 105 | if (scrollpane == null) { |
| 106 | return; |
| 107 | } |
| 108 | Border border = scrollpane.getBorder(); |
| 109 | Object value; |
| 110 | |
| 111 | if (border instanceof MetalBorders.ScrollPaneBorder) { |
| 112 | value = Boolean.FALSE; |
| 113 | } |
| 114 | else { |
| 115 | value = Boolean.TRUE; |
| 116 | } |
| 117 | JScrollBar sb = scrollpane.getHorizontalScrollBar(); |
| 118 | if (sb != null) { |
| 119 | sb.putClientProperty |
| 120 | (MetalScrollBarUI.FREE_STANDING_PROP, value); |
| 121 | } |
| 122 | sb = scrollpane.getVerticalScrollBar(); |
| 123 | if (sb != null) { |
| 124 | sb.putClientProperty |
| 125 | (MetalScrollBarUI.FREE_STANDING_PROP, value); |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | protected PropertyChangeListener createScrollBarSwapListener() { |
| 130 | return new PropertyChangeListener() { |
| 131 | public void propertyChange(PropertyChangeEvent e) { |
| 132 | String propertyName = e.getPropertyName(); |
| 133 | if (propertyName.equals("verticalScrollBar") || |
| 134 | propertyName.equals("horizontalScrollBar")) { |
| 135 | JScrollBar oldSB = (JScrollBar)e.getOldValue(); |
| 136 | if (oldSB != null) { |
| 137 | oldSB.putClientProperty( |
| 138 | MetalScrollBarUI.FREE_STANDING_PROP, null); |
| 139 | } |
| 140 | JScrollBar newSB = (JScrollBar)e.getNewValue(); |
| 141 | if (newSB != null) { |
| 142 | newSB.putClientProperty( |
| 143 | MetalScrollBarUI.FREE_STANDING_PROP, |
| 144 | Boolean.FALSE); |
| 145 | } |
| 146 | } |
| 147 | else if ("border".equals(propertyName)) { |
| 148 | updateScrollbarsFreeStanding(); |
| 149 | } |
| 150 | }}; |
| 151 | } |
| 152 | |
| 153 | } |