Path: blob/master/src/java.desktop/macosx/classes/sun/lwawt/LWScrollPanePeer.java
41153 views
/*1* Copyright (c) 2011, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.lwawt;2627import javax.swing.*;28import javax.swing.event.ChangeListener;29import javax.swing.event.ChangeEvent;30import java.awt.*;31import java.awt.event.MouseWheelEvent;32import java.awt.peer.ScrollPanePeer;33import java.util.List;3435/**36* Lightweight implementation of {@link ScrollPanePeer}. Delegates most of the37* work to the {@link JScrollPane}.38*/39final class LWScrollPanePeer extends LWContainerPeer<ScrollPane, JScrollPane>40implements ScrollPanePeer, ChangeListener {4142LWScrollPanePeer(final ScrollPane target,43final PlatformComponent platformComponent) {44super(target, platformComponent);45}4647@Override48JScrollPane createDelegate() {49final JScrollPane sp = new JScrollPane();50final JPanel panel = new JPanel();51panel.setOpaque(false);52panel.setVisible(false);53sp.getViewport().setView(panel);54sp.setBorder(BorderFactory.createEmptyBorder());55sp.getViewport().addChangeListener(this);56return sp;57}5859@Override60public void handleEvent(AWTEvent e) {61if (e instanceof MouseWheelEvent) {62MouseWheelEvent wheelEvent = (MouseWheelEvent) e;63//java.awt.ScrollPane consumes the event64// in case isWheelScrollingEnabled() is true,65// forcibly send the consumed event to the delegate66if (getTarget().isWheelScrollingEnabled() && wheelEvent.isConsumed()) {67sendEventToDelegate(wheelEvent);68}69} else {70super.handleEvent(e);71}72}7374@Override75public void stateChanged(final ChangeEvent e) {76SwingUtilities.invokeLater(new Runnable() {77@Override78public void run() {79final LWComponentPeer<?, ?> viewPeer = getViewPeer();80if (viewPeer != null) {81final Rectangle r;82synchronized (getDelegateLock()) {83r = getDelegate().getViewport().getView().getBounds();84}85viewPeer.setBounds(r.x, r.y, r.width, r.height, SET_BOUNDS,86true, true);87}88}89});90}9192@Override93void initializeImpl() {94super.initializeImpl();95final int policy = getTarget().getScrollbarDisplayPolicy();96synchronized (getDelegateLock()) {97getDelegate().getViewport().setScrollMode(JViewport.SIMPLE_SCROLL_MODE);98getDelegate().setVerticalScrollBarPolicy(convertVPolicy(policy));99getDelegate().setHorizontalScrollBarPolicy(convertHPolicy(policy));100}101}102103LWComponentPeer<?, ?> getViewPeer() {104final List<LWComponentPeer<?, ?>> peerList = getChildren();105return peerList.isEmpty() ? null : peerList.get(0);106}107108@Override109Rectangle getContentSize() {110Rectangle viewRect = getDelegate().getViewport().getViewRect();111return new Rectangle(viewRect.width, viewRect.height);112}113114@Override115public void layout() {116super.layout();117synchronized (getDelegateLock()) {118final LWComponentPeer<?, ?> viewPeer = getViewPeer();119if (viewPeer != null) {120Component view = getDelegate().getViewport().getView();121view.setBounds(viewPeer.getBounds());122view.setPreferredSize(viewPeer.getPreferredSize());123view.setMinimumSize(viewPeer.getMinimumSize());124getDelegate().invalidate();125getDelegate().validate();126viewPeer.setBounds(view.getBounds());127}128}129}130131@Override132public void setScrollPosition(int x, int y) {133}134135@Override136public int getHScrollbarHeight() {137synchronized (getDelegateLock()) {138return getDelegate().getHorizontalScrollBar().getHeight();139}140}141142@Override143public int getVScrollbarWidth() {144synchronized (getDelegateLock()) {145return getDelegate().getVerticalScrollBar().getWidth();146}147}148149@Override150public void childResized(int w, int h) {151synchronized (getDelegateLock()) {152getDelegate().invalidate();153getDelegate().validate();154}155}156157@Override158public void setUnitIncrement(Adjustable adj, int u) {159}160161@Override162public void setValue(Adjustable adj, int v) {163}164165private static int convertHPolicy(final int policy) {166switch (policy) {167case ScrollPane.SCROLLBARS_NEVER:168return ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER;169case ScrollPane.SCROLLBARS_ALWAYS:170return ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS;171default:172return ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED;173}174}175176private static int convertVPolicy(final int policy) {177switch (policy) {178case ScrollPane.SCROLLBARS_NEVER:179return ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER;180case ScrollPane.SCROLLBARS_ALWAYS:181return ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS;182default:183return ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED;184}185}186}187188189