1 /*
   2  * Copyright (c) 2011, 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 sun.lwawt;
  27 
  28 import javax.swing.*;
  29 import javax.swing.event.ChangeListener;
  30 import javax.swing.event.ChangeEvent;
  31 import java.awt.*;
  32 import java.awt.peer.ScrollPanePeer;
  33 import java.util.List;
  34 
  35 final class LWScrollPanePeer extends LWContainerPeer<ScrollPane, JScrollPane>
  36         implements ScrollPanePeer, ChangeListener {
  37 
  38     LWScrollPanePeer(final ScrollPane target,
  39                      final PlatformComponent platformComponent) {
  40         super(target, platformComponent);
  41     }
  42 
  43     protected JScrollPane createDelegate() {
  44         final JScrollPane sp = new JScrollPane();
  45         final JPanel panel = new JPanel();
  46         panel.setOpaque(false);
  47         panel.setVisible(false);
  48         sp.getViewport().setView(panel);
  49         sp.setBorder(BorderFactory.createEmptyBorder());
  50         sp.getViewport().addChangeListener(this);
  51         return sp;
  52     }
  53 
  54     @Override
  55     public void stateChanged(final ChangeEvent e) {
  56         SwingUtilities.invokeLater(new Runnable() {
  57             @Override
  58             public void run() {
  59                 final LWComponentPeer viewPeer = getViewPeer();
  60                 if (viewPeer != null) {
  61                     final Rectangle r;
  62                     synchronized (getDelegateLock()) {
  63                         r = getDelegate().getViewport().getView().getBounds();
  64                     }
  65                     viewPeer.setBounds(r.x, r.y, r.width, r.height, SET_BOUNDS,
  66                                        true, true);
  67                 }
  68             }
  69         });
  70     }
  71 
  72     @Override
  73     public void initialize() {
  74         super.initialize();
  75         final int policy = getTarget().getScrollbarDisplayPolicy();
  76         synchronized (getDelegateLock()) {
  77             getDelegate().getViewport().setScrollMode(JViewport.SIMPLE_SCROLL_MODE);
  78             getDelegate().setVerticalScrollBarPolicy(convertVPolicy(policy));
  79             getDelegate().setHorizontalScrollBarPolicy(convertHPolicy(policy));
  80         }
  81     }
  82 
  83     LWComponentPeer getViewPeer() {
  84         List<LWComponentPeer> peerList = getChildren();
  85         return peerList.isEmpty() ? null : peerList.get(0);
  86     }
  87 
  88 
  89     @Override
  90     protected Rectangle getContentSize() {
  91         Rectangle viewRect = getDelegate().getViewport().getViewRect();
  92         return new Rectangle(viewRect.width, viewRect.height);
  93     }
  94 
  95     @Override
  96     public void layout() {
  97         super.layout();
  98         synchronized (getDelegateLock()) {
  99             LWComponentPeer viewPeer = getViewPeer();
 100             if (viewPeer != null) {
 101                 Component view = getDelegate().getViewport().getView();
 102                 view.setBounds(viewPeer.getBounds());
 103                 view.setPreferredSize(viewPeer.getPreferredSize());
 104                 view.setMinimumSize(viewPeer.getMinimumSize());
 105                 getDelegate().invalidate();
 106                 getDelegate().validate();
 107                 viewPeer.setBounds(view.getBounds());
 108             }
 109         }
 110     }
 111 
 112     @Override
 113     public void setScrollPosition(int x, int y) {
 114     }
 115 
 116     @Override
 117     public int getHScrollbarHeight() {
 118         synchronized (getDelegateLock()) {
 119             return getDelegate().getHorizontalScrollBar().getHeight();
 120         }
 121     }
 122 
 123     @Override
 124     public int getVScrollbarWidth() {
 125         synchronized (getDelegateLock()) {
 126             return getDelegate().getVerticalScrollBar().getWidth();
 127         }
 128     }
 129 
 130     @Override
 131     public void childResized(int w, int h) {
 132         synchronized (getDelegateLock()) {
 133             getDelegate().invalidate();
 134             getDelegate().validate();
 135         }
 136     }
 137 
 138     @Override
 139     public void setUnitIncrement(Adjustable adj, int u) {
 140     }
 141 
 142     @Override
 143     public void setValue(Adjustable adj, int v) {
 144     }
 145 
 146     private static int convertHPolicy(final int policy) {
 147         switch (policy) {
 148             case ScrollPane.SCROLLBARS_NEVER:
 149                 return ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER;
 150             case ScrollPane.SCROLLBARS_ALWAYS:
 151                 return ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS;
 152             default:
 153                 return ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED;
 154         }
 155     }
 156 
 157     private static int convertVPolicy(final int policy) {
 158         switch (policy) {
 159             case ScrollPane.SCROLLBARS_NEVER:
 160                 return ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER;
 161             case ScrollPane.SCROLLBARS_ALWAYS:
 162                 return ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS;
 163             default:
 164                 return ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED;
 165         }
 166     }
 167 }