1 /*
2  * Copyright (c) 2011, 2013, 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 
27 package sun.lwawt;
28 
29 import java.awt.Adjustable;
30 import java.awt.Scrollbar;
31 import java.awt.event.AdjustmentEvent;
32 import java.awt.event.AdjustmentListener;
33 import java.awt.peer.ScrollbarPeer;
34 
35 import javax.swing.JScrollBar;
36 
37 /**
38  * Lightweight implementation of {@link ScrollbarPeer}. Delegates most of the
39  * work to the {@link JScrollBar}.
40  */
41 final class LWScrollBarPeer extends LWComponentPeer<Scrollbar, JScrollBar>
42         implements ScrollbarPeer, AdjustmentListener {
43 
44     // JScrollBar fires two changes with firePropertyChange (one for old value
45     // and one for new one.
46     // We save the last value and don't fire event if not changed.
47     private int currentValue;
48 
LWScrollBarPeer(final Scrollbar target, final PlatformComponent platformComponent)49     LWScrollBarPeer(final Scrollbar target,
50                     final PlatformComponent platformComponent) {
51         super(target, platformComponent);
52     }
53 
54     @Override
createDelegate()55     JScrollBar createDelegate() {
56         return new JScrollBar();
57     }
58 
59     @Override
initializeImpl()60     void initializeImpl() {
61         super.initializeImpl();
62         final Scrollbar target = getTarget();
63         setLineIncrement(target.getUnitIncrement());
64         setPageIncrement(target.getBlockIncrement());
65         setValues(target.getValue(), target.getVisibleAmount(),
66                   target.getMinimum(), target.getMaximum());
67 
68         final int orientation = target.getOrientation();
69         final JScrollBar delegate = getDelegate();
70         synchronized (getDelegateLock()) {
71             delegate.setOrientation(orientation == Scrollbar.HORIZONTAL
72                                     ? Adjustable.HORIZONTAL
73                                     : Adjustable.VERTICAL);
74             delegate.addAdjustmentListener(this);
75         }
76     }
77 
78     @Override
setValues(final int value, final int visible, final int minimum, final int maximum)79     public void setValues(final int value, final int visible, final int minimum,
80                           final int maximum) {
81         synchronized (getDelegateLock()) {
82             currentValue = value;
83             getDelegate().setValues(value, visible, minimum, maximum);
84         }
85     }
86 
87     @Override
setLineIncrement(final int l)88     public void setLineIncrement(final int l) {
89         synchronized (getDelegateLock()) {
90             getDelegate().setUnitIncrement(l);
91         }
92     }
93 
94     @Override
setPageIncrement(final int l)95     public void setPageIncrement(final int l) {
96         synchronized (getDelegateLock()) {
97             getDelegate().setBlockIncrement(l);
98         }
99     }
100 
101     // Peer also registered as a listener for ComponentDelegate component
102     @Override
adjustmentValueChanged(final AdjustmentEvent e)103     public void adjustmentValueChanged(final AdjustmentEvent e) {
104         final int value = e.getValue();
105         synchronized (getDelegateLock()) {
106             if (currentValue == value) {
107                 return;
108             }
109             currentValue = value;
110         }
111         getTarget().setValueIsAdjusting(e.getValueIsAdjusting());
112         getTarget().setValue(value);
113         postEvent(new AdjustmentEvent(getTarget(), e.getID(),
114                 e.getAdjustmentType(), value,
115                 e.getValueIsAdjusting()));
116     }
117 }
118