1 /*
   2  * Copyright (c) 1998, 2014, Oracle and/or its affiliates. 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.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * 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
  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 @SuppressWarnings("serial") // Same-version serialization only
  54 public class MetalScrollPaneUI extends BasicScrollPaneUI
  55 {
  56 
  57     private PropertyChangeListener scrollBarSwapListener;
  58 
  59     /**
  60      * Constructs a new {@code MetalScrollPaneUI}.
  61      *
  62      * @param x a component
  63      * @return a new {@code MetalScrollPaneUI}
  64      */
  65     public static ComponentUI createUI(JComponent x) {
  66         return new MetalScrollPaneUI();
  67     }
  68 
  69     public void installUI(JComponent c) {
  70 
  71         super.installUI(c);
  72 
  73         JScrollPane sp = (JScrollPane)c;
  74         updateScrollbarsFreeStanding();
  75     }
  76 
  77     public void uninstallUI(JComponent c) {
  78         super.uninstallUI(c);
  79 
  80         JScrollPane sp = (JScrollPane)c;
  81         JScrollBar hsb = sp.getHorizontalScrollBar();
  82         JScrollBar vsb = sp.getVerticalScrollBar();
  83         if (hsb != null) {
  84             hsb.putClientProperty( MetalScrollBarUI.FREE_STANDING_PROP, null);
  85         }
  86         if (vsb != null) {
  87             vsb.putClientProperty( MetalScrollBarUI.FREE_STANDING_PROP, null);
  88         }
  89     }
  90 
  91     public void installListeners(JScrollPane scrollPane) {
  92         super.installListeners(scrollPane);
  93         scrollBarSwapListener = createScrollBarSwapListener();
  94         scrollPane.addPropertyChangeListener(scrollBarSwapListener);
  95     }
  96 
  97     /**
  98      * {@inheritDoc}
  99      */
 100     protected void uninstallListeners(JComponent c) {
 101         super.uninstallListeners(c);
 102         c.removePropertyChangeListener(scrollBarSwapListener);
 103     }
 104 
 105     /**
 106      * @param scrollPane an instance of the {@code JScrollPane}
 107      * @deprecated - Replaced by {@link #uninstallListeners(JComponent)}
 108      */
 109     @Deprecated
 110     public void uninstallListeners(JScrollPane scrollPane) {
 111         super.uninstallListeners(scrollPane);
 112         scrollPane.removePropertyChangeListener(scrollBarSwapListener);
 113     }
 114 
 115     /**
 116      * If the border of the scrollpane is an instance of
 117      * <code>MetalBorders.ScrollPaneBorder</code>, the client property
 118      * <code>FREE_STANDING_PROP</code> of the scrollbars
 119      * is set to false, otherwise it is set to true.
 120      */
 121     private void updateScrollbarsFreeStanding() {
 122         if (scrollpane == null) {
 123             return;
 124         }
 125         Border border = scrollpane.getBorder();
 126         Object value;
 127 
 128         if (border instanceof MetalBorders.ScrollPaneBorder) {
 129             value = Boolean.FALSE;
 130         }
 131         else {
 132             value = Boolean.TRUE;
 133         }
 134         JScrollBar sb = scrollpane.getHorizontalScrollBar();
 135         if (sb != null) {
 136             sb.putClientProperty
 137                    (MetalScrollBarUI.FREE_STANDING_PROP, value);
 138         }
 139         sb = scrollpane.getVerticalScrollBar();
 140         if (sb != null) {
 141             sb.putClientProperty
 142                    (MetalScrollBarUI.FREE_STANDING_PROP, value);
 143         }
 144     }
 145 
 146     /**
 147      * Returns a new {@code PropertyChangeListener} for scroll bar swap events.
 148      *
 149      * @return a new {@code PropertyChangeListener} for scroll bar swap events.
 150      */
 151     protected PropertyChangeListener createScrollBarSwapListener() {
 152         return new PropertyChangeListener() {
 153             public void propertyChange(PropertyChangeEvent e) {
 154                   String propertyName = e.getPropertyName();
 155                   if (propertyName.equals("verticalScrollBar") ||
 156                       propertyName.equals("horizontalScrollBar")) {
 157                       JScrollBar oldSB = (JScrollBar)e.getOldValue();
 158                       if (oldSB != null) {
 159                           oldSB.putClientProperty(
 160                               MetalScrollBarUI.FREE_STANDING_PROP, null);
 161                       }
 162                       JScrollBar newSB = (JScrollBar)e.getNewValue();
 163                       if (newSB != null) {
 164                           newSB.putClientProperty(
 165                               MetalScrollBarUI.FREE_STANDING_PROP,
 166                               Boolean.FALSE);
 167                       }
 168                   }
 169                   else if ("border".equals(propertyName)) {
 170                       updateScrollbarsFreeStanding();
 171                   }
 172         }};
 173     }
 174 
 175 }