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