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