src/share/classes/sun/awt/ScrollPaneWheelScroller.java

Print this page




  30 import java.awt.Adjustable;
  31 import java.awt.event.MouseWheelEvent;
  32 
  33 import sun.util.logging.PlatformLogger;
  34 
  35 /*
  36  * ScrollPaneWheelScroller is a helper class for implmenenting mouse wheel
  37  * scrolling on a java.awt.ScrollPane.  It contains only static methods.
  38  * No objects of this class may be instantiated, thus it is declared abstract.
  39  */
  40 public abstract class ScrollPaneWheelScroller {
  41 
  42     private static final PlatformLogger log = PlatformLogger.getLogger("sun.awt.ScrollPaneWheelScroller");
  43 
  44     private ScrollPaneWheelScroller() {}
  45 
  46     /*
  47      * Called from ScrollPane.processMouseWheelEvent()
  48      */
  49     public static void handleWheelScrolling(ScrollPane sp, MouseWheelEvent e) {
  50         if (log.isLoggable(PlatformLogger.FINER)) {
  51             log.finer("x = " + e.getX() + ", y = " + e.getY() + ", src is " + e.getSource());
  52         }
  53         int increment = 0;
  54 
  55         if (sp != null && e.getScrollAmount() != 0) {
  56             Adjustable adj = getAdjustableToScroll(sp);
  57             if (adj != null) {
  58                 increment = getIncrementFromAdjustable(adj, e);
  59                 if (log.isLoggable(PlatformLogger.FINER)) {
  60                     log.finer("increment from adjustable(" + adj.getClass() + ") : " + increment);
  61                 }
  62                 scrollAdjustable(adj, increment);
  63             }
  64         }
  65     }
  66 
  67     /*
  68      * Given a ScrollPane, determine which Scrollbar should be scrolled by the
  69      * mouse wheel, if any.
  70      */
  71     public static Adjustable getAdjustableToScroll(ScrollPane sp) {
  72         int policy = sp.getScrollbarDisplayPolicy();
  73 
  74         // if policy is display always or never, use vert
  75         if (policy == ScrollPane.SCROLLBARS_ALWAYS ||
  76             policy == ScrollPane.SCROLLBARS_NEVER) {
  77             if (log.isLoggable(PlatformLogger.FINER)) {
  78                 log.finer("using vertical scrolling due to scrollbar policy");
  79             }
  80             return sp.getVAdjustable();
  81 
  82         }
  83         else {
  84 
  85             Insets ins = sp.getInsets();
  86             int vertScrollWidth = sp.getVScrollbarWidth();
  87 
  88             if (log.isLoggable(PlatformLogger.FINER)) {
  89                 log.finer("insets: l = " + ins.left + ", r = " + ins.right +
  90                  ", t = " + ins.top + ", b = " + ins.bottom);
  91                 log.finer("vertScrollWidth = " + vertScrollWidth);
  92             }
  93 
  94             // Check if scrollbar is showing by examining insets of the
  95             // ScrollPane
  96             if (ins.right >= vertScrollWidth) {
  97                 if (log.isLoggable(PlatformLogger.FINER)) {
  98                     log.finer("using vertical scrolling because scrollbar is present");
  99                 }
 100                 return sp.getVAdjustable();
 101             }
 102             else {
 103                 int horizScrollHeight = sp.getHScrollbarHeight();
 104                 if (ins.bottom >= horizScrollHeight) {
 105                     if (log.isLoggable(PlatformLogger.FINER)) {
 106                         log.finer("using horiz scrolling because scrollbar is present");
 107                     }
 108                     return sp.getHAdjustable();
 109                 }
 110                 else {
 111                     if (log.isLoggable(PlatformLogger.FINER)) {
 112                         log.finer("using NO scrollbar becsause neither is present");
 113                     }
 114                     return null;
 115                 }
 116             }
 117         }
 118     }
 119 
 120     /*
 121      * Given the info in a MouseWheelEvent and an Adjustable to scroll, return
 122      * the amount by which the Adjustable should be adjusted.  This value may
 123      * be positive or negative.
 124      */
 125     public static int getIncrementFromAdjustable(Adjustable adj,
 126                                                  MouseWheelEvent e) {
 127         if (log.isLoggable(PlatformLogger.FINE)) {
 128             if (adj == null) {
 129                 log.fine("Assertion (adj != null) failed");
 130             }
 131         }
 132 
 133         int increment = 0;
 134 
 135         if (e.getScrollType() == MouseWheelEvent.WHEEL_UNIT_SCROLL) {
 136             increment = e.getUnitsToScroll() * adj.getUnitIncrement();
 137         }
 138         else if (e.getScrollType() == MouseWheelEvent.WHEEL_BLOCK_SCROLL) {
 139             increment = adj.getBlockIncrement() * e.getWheelRotation();
 140         }
 141         return increment;
 142     }
 143 
 144     /*
 145      * Scroll the given Adjustable by the given amount.  Checks the Adjustable's
 146      * bounds and sets the new value to the Adjustable.
 147      */
 148     public static void scrollAdjustable(Adjustable adj, int amount) {
 149         if (log.isLoggable(PlatformLogger.FINE)) {
 150             if (adj == null) {
 151                 log.fine("Assertion (adj != null) failed");
 152             }
 153             if (amount == 0) {
 154                 log.fine("Assertion (amount != 0) failed");
 155             }
 156         }
 157 
 158         int current = adj.getValue();
 159         int upperLimit = adj.getMaximum() - adj.getVisibleAmount();
 160         if (log.isLoggable(PlatformLogger.FINER)) {
 161             log.finer("doScrolling by " + amount);
 162         }
 163 
 164         if (amount > 0 && current < upperLimit) { // still some room to scroll
 165                                                   // down
 166             if (current + amount < upperLimit) {
 167                 adj.setValue(current + amount);
 168                 return;
 169             }
 170             else {
 171                 adj.setValue(upperLimit);
 172                 return;
 173             }
 174         }
 175         else if (amount < 0 && current > adj.getMinimum()) { // still some room
 176                                                              // to scroll up
 177             if (current + amount > adj.getMinimum()) {
 178                 adj.setValue(current + amount);
 179                 return;
 180             }


  30 import java.awt.Adjustable;
  31 import java.awt.event.MouseWheelEvent;
  32 
  33 import sun.util.logging.PlatformLogger;
  34 
  35 /*
  36  * ScrollPaneWheelScroller is a helper class for implmenenting mouse wheel
  37  * scrolling on a java.awt.ScrollPane.  It contains only static methods.
  38  * No objects of this class may be instantiated, thus it is declared abstract.
  39  */
  40 public abstract class ScrollPaneWheelScroller {
  41 
  42     private static final PlatformLogger log = PlatformLogger.getLogger("sun.awt.ScrollPaneWheelScroller");
  43 
  44     private ScrollPaneWheelScroller() {}
  45 
  46     /*
  47      * Called from ScrollPane.processMouseWheelEvent()
  48      */
  49     public static void handleWheelScrolling(ScrollPane sp, MouseWheelEvent e) {
  50         if (log.isLoggable(PlatformLogger.Level.FINER)) {
  51             log.finer("x = " + e.getX() + ", y = " + e.getY() + ", src is " + e.getSource());
  52         }
  53         int increment = 0;
  54 
  55         if (sp != null && e.getScrollAmount() != 0) {
  56             Adjustable adj = getAdjustableToScroll(sp);
  57             if (adj != null) {
  58                 increment = getIncrementFromAdjustable(adj, e);
  59                 if (log.isLoggable(PlatformLogger.Level.FINER)) {
  60                     log.finer("increment from adjustable(" + adj.getClass() + ") : " + increment);
  61                 }
  62                 scrollAdjustable(adj, increment);
  63             }
  64         }
  65     }
  66 
  67     /*
  68      * Given a ScrollPane, determine which Scrollbar should be scrolled by the
  69      * mouse wheel, if any.
  70      */
  71     public static Adjustable getAdjustableToScroll(ScrollPane sp) {
  72         int policy = sp.getScrollbarDisplayPolicy();
  73 
  74         // if policy is display always or never, use vert
  75         if (policy == ScrollPane.SCROLLBARS_ALWAYS ||
  76             policy == ScrollPane.SCROLLBARS_NEVER) {
  77             if (log.isLoggable(PlatformLogger.Level.FINER)) {
  78                 log.finer("using vertical scrolling due to scrollbar policy");
  79             }
  80             return sp.getVAdjustable();
  81 
  82         }
  83         else {
  84 
  85             Insets ins = sp.getInsets();
  86             int vertScrollWidth = sp.getVScrollbarWidth();
  87 
  88             if (log.isLoggable(PlatformLogger.Level.FINER)) {
  89                 log.finer("insets: l = " + ins.left + ", r = " + ins.right +
  90                  ", t = " + ins.top + ", b = " + ins.bottom);
  91                 log.finer("vertScrollWidth = " + vertScrollWidth);
  92             }
  93 
  94             // Check if scrollbar is showing by examining insets of the
  95             // ScrollPane
  96             if (ins.right >= vertScrollWidth) {
  97                 if (log.isLoggable(PlatformLogger.Level.FINER)) {
  98                     log.finer("using vertical scrolling because scrollbar is present");
  99                 }
 100                 return sp.getVAdjustable();
 101             }
 102             else {
 103                 int horizScrollHeight = sp.getHScrollbarHeight();
 104                 if (ins.bottom >= horizScrollHeight) {
 105                     if (log.isLoggable(PlatformLogger.Level.FINER)) {
 106                         log.finer("using horiz scrolling because scrollbar is present");
 107                     }
 108                     return sp.getHAdjustable();
 109                 }
 110                 else {
 111                     if (log.isLoggable(PlatformLogger.Level.FINER)) {
 112                         log.finer("using NO scrollbar becsause neither is present");
 113                     }
 114                     return null;
 115                 }
 116             }
 117         }
 118     }
 119 
 120     /*
 121      * Given the info in a MouseWheelEvent and an Adjustable to scroll, return
 122      * the amount by which the Adjustable should be adjusted.  This value may
 123      * be positive or negative.
 124      */
 125     public static int getIncrementFromAdjustable(Adjustable adj,
 126                                                  MouseWheelEvent e) {
 127         if (log.isLoggable(PlatformLogger.Level.FINE)) {
 128             if (adj == null) {
 129                 log.fine("Assertion (adj != null) failed");
 130             }
 131         }
 132 
 133         int increment = 0;
 134 
 135         if (e.getScrollType() == MouseWheelEvent.WHEEL_UNIT_SCROLL) {
 136             increment = e.getUnitsToScroll() * adj.getUnitIncrement();
 137         }
 138         else if (e.getScrollType() == MouseWheelEvent.WHEEL_BLOCK_SCROLL) {
 139             increment = adj.getBlockIncrement() * e.getWheelRotation();
 140         }
 141         return increment;
 142     }
 143 
 144     /*
 145      * Scroll the given Adjustable by the given amount.  Checks the Adjustable's
 146      * bounds and sets the new value to the Adjustable.
 147      */
 148     public static void scrollAdjustable(Adjustable adj, int amount) {
 149         if (log.isLoggable(PlatformLogger.Level.FINE)) {
 150             if (adj == null) {
 151                 log.fine("Assertion (adj != null) failed");
 152             }
 153             if (amount == 0) {
 154                 log.fine("Assertion (amount != 0) failed");
 155             }
 156         }
 157 
 158         int current = adj.getValue();
 159         int upperLimit = adj.getMaximum() - adj.getVisibleAmount();
 160         if (log.isLoggable(PlatformLogger.Level.FINER)) {
 161             log.finer("doScrolling by " + amount);
 162         }
 163 
 164         if (amount > 0 && current < upperLimit) { // still some room to scroll
 165                                                   // down
 166             if (current + amount < upperLimit) {
 167                 adj.setValue(current + amount);
 168                 return;
 169             }
 170             else {
 171                 adj.setValue(upperLimit);
 172                 return;
 173             }
 174         }
 175         else if (amount < 0 && current > adj.getMinimum()) { // still some room
 176                                                              // to scroll up
 177             if (current + amount > adj.getMinimum()) {
 178                 adj.setValue(current + amount);
 179                 return;
 180             }