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