1 /*
   2  * Copyright (c) 2008, 2013, Oracle and/or its affiliates.
   3  * All rights reserved. Use is subject to license terms.
   4  *
   5  * This file is available and licensed under the following license:
   6  *
   7  * Redistribution and use in source and binary forms, with or without
   8  * modification, are permitted provided that the following conditions
   9  * are met:
  10  *
  11  *  - Redistributions of source code must retain the above copyright
  12  *    notice, this list of conditions and the following disclaimer.
  13  *  - Redistributions in binary form must reproduce the above copyright
  14  *    notice, this list of conditions and the following disclaimer in
  15  *    the documentation and/or other materials provided with the distribution.
  16  *  - Neither the name of Oracle Corporation nor the names of its
  17  *    contributors may be used to endorse or promote products derived
  18  *    from this software without specific prior written permission.
  19  *
  20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31  */
  32 package com.javafx.experiments.scheduleapp.control;
  33 
  34 import com.sun.javafx.scene.control.skin.ScrollPaneSkin;
  35 import java.lang.reflect.Field;
  36 import javafx.event.EventHandler;
  37 import javafx.scene.control.ScrollPane;
  38 import javafx.scene.input.MouseEvent;
  39 import javafx.scene.input.ScrollEvent;
  40 
  41 public class ScrollPaneSkin3 extends ScrollPaneSkin {
  42     private static final String os = System.getProperty("os.name");
  43     public static final boolean IS_BEAGLE = "Linux".equals(os);
  44     public ScrollPaneSkin3(final ScrollPane scrollpane) {
  45         super(scrollpane);
  46         if (IS_BEAGLE) {
  47             scrollpane.addEventFilter(MouseEvent.ANY, new EventHandler<MouseEvent>() {
  48                 public void handle(MouseEvent event) {
  49                     if (event.getEventType() == MouseEvent.MOUSE_DRAGGED) {
  50                         event.consume();
  51                     }
  52                     scrollpane.setVbarPolicy(ScrollPane.ScrollBarPolicy.NEVER);
  53                 }
  54             });
  55             scrollpane.addEventFilter(ScrollEvent.SCROLL, new EventHandler<ScrollEvent>() {
  56                 public void handle(ScrollEvent event) {
  57                     if (vsb.getVisibleAmount() < vsb.getMax()) {
  58                         double nodeHeight = getNodeHeight();
  59                         double vRange = getSkinnable().getVmax()-getSkinnable().getVmin();
  60                         double vPixelValue;
  61                         if (nodeHeight > 0.0) {
  62                             vPixelValue = vRange / nodeHeight;
  63                         }
  64                         else {
  65                             vPixelValue = 0.0;
  66                         }
  67                         double newValue = vsb.getValue()+(-event.getDeltaY())*vPixelValue;
  68                         if (true /*!PlatformUtil.isEmbedded()*/) {
  69                             if ((event.getDeltaY() > 0.0 && vsb.getValue() > vsb.getMin()) ||
  70                                 (event.getDeltaY() < 0.0 && vsb.getValue() < vsb.getMax())) {
  71                                 vsb.setValue(newValue);
  72                                 event.consume();
  73                             }
  74                         }
  75                         // eat all scroll events to avoid white space scrolling past start or end
  76                         event.consume();
  77                     }
  78                 }
  79             });
  80         }
  81     }
  82     double getNodeHeight() {
  83         try {
  84             Field field = ScrollPaneSkin.class.getDeclaredField("nodeHeight");
  85             field.setAccessible(true);
  86             return field.getDouble(this);
  87         } catch (NoSuchFieldException e) {
  88             // TODO Auto-generated catch block
  89             e.printStackTrace();
  90         } catch (SecurityException e) {
  91             // TODO Auto-generated catch block
  92             e.printStackTrace();
  93         } catch (IllegalArgumentException e) {
  94             // TODO Auto-generated catch block
  95             e.printStackTrace();
  96         } catch (IllegalAccessException e) {
  97             // TODO Auto-generated catch block
  98             e.printStackTrace();
  99         }
 100         return 0;
 101     }
 102 
 103     protected void startContentsToViewport() {
 104         if (IS_BEAGLE) return;
 105         super.startContentsToViewport();
 106     }
 107 
 108     protected void startSBReleasedAnimation() {
 109         if (IS_BEAGLE) return;
 110         super.startSBReleasedAnimation();
 111     }
 112 }