IDEA-162490 Fix for unexpected jumping on the end of scrolling
diff --git a/src/macosx/classes/sun/lwawt/macosx/CPlatformResponder.java b/src/macosx/classes/sun/lwawt/macosx/CPlatformResponder.java
index 42fdbb7..185950b 100644
--- a/src/macosx/classes/sun/lwawt/macosx/CPlatformResponder.java
+++ b/src/macosx/classes/sun/lwawt/macosx/CPlatformResponder.java
@@ -281,8 +281,6 @@
 
     static class DeltaAccumulator {
 
-        static final double MIN_THRESHOLD = 0.1;
-        static final double MAX_THRESHOLD = 0.5;
         double accumulatedDelta;
         boolean accumulate;
 
@@ -299,27 +297,19 @@
                     accumulatedDelta = 0;
                     accumulate = true;
                 }
+                else if (scrollPhase == NSEvent.SCROLL_PHASE_MOMENTUM_BEGAN) {
+                    accumulate = true;
+                }
                 if (accumulate) {
 
                     accumulatedDelta += delta;
 
-                    if (accumulatedDelta > MAX_THRESHOLD) {
-                        roundDelta = (int) (0.5 + accumulatedDelta);
-                    } else if (accumulatedDelta < -MAX_THRESHOLD) {
-                        roundDelta = -(int) (0.5 - accumulatedDelta);
-                    }
+                    roundDelta = (int) Math.round(accumulatedDelta);
 
                     accumulatedDelta -= roundDelta;
 
-                    if (scrollPhase == NSEvent.SCROLL_PHASE_ENDED || scrollPhase == NSEvent.SCROLL_PHASE_CANCELLED) {
+                    if (scrollPhase == NSEvent.SCROLL_PHASE_ENDED) {
                         accumulate = false;
-                        if (roundDelta == 0) {
-                            if (accumulatedDelta > MIN_THRESHOLD) {
-                                roundDelta = 1;
-                            } else if (accumulatedDelta < -MIN_THRESHOLD) {
-                                roundDelta = -1;
-                            }
-                        }
                     }
                 }
             }
diff --git a/src/macosx/classes/sun/lwawt/macosx/NSEvent.java b/src/macosx/classes/sun/lwawt/macosx/NSEvent.java
index b547914..3a8508d 100644
--- a/src/macosx/classes/sun/lwawt/macosx/NSEvent.java
+++ b/src/macosx/classes/sun/lwawt/macosx/NSEvent.java
@@ -36,7 +36,7 @@
     static final int SCROLL_PHASE_UNSUPPORTED = 1;
     static final int SCROLL_PHASE_BEGAN = 2;
     static final int SCROLL_PHASE_CONTINUED = 3;
-    static final int SCROLL_PHASE_CANCELLED = 4;
+    static final int SCROLL_PHASE_MOMENTUM_BEGAN = 4;
     static final int SCROLL_PHASE_ENDED = 5;
 
     private int type;
diff --git a/src/macosx/native/sun/awt/LWCToolkit.m b/src/macosx/native/sun/awt/LWCToolkit.m
index 8a80d63..e730b5c 100644
--- a/src/macosx/native/sun/awt/LWCToolkit.m
+++ b/src/macosx/native/sun/awt/LWCToolkit.m
@@ -43,7 +43,7 @@
 #define SCROLL_PHASE_UNSUPPORTED 1
 #define SCROLL_PHASE_BEGAN 2
 #define SCROLL_PHASE_CONTINUED 3
-#define SCROLL_PHASE_CANCELLED 4
+#define SCROLL_PHASE_MOMENTUM_BEGAN 4
 #define SCROLL_PHASE_ENDED 5
 
 int gNumberOfButtons;
@@ -63,17 +63,29 @@
 
 + (jint) scrollStateWithEvent: (NSEvent*) event {
 
-    if ([event type] != NSScrollWheel) return 0;
-
-    NSEventPhase phase = [event phase];
-    if (!phase) phase = [event momentumPhase];
-    if (!phase) return SCROLL_PHASE_UNSUPPORTED;
-    switch (phase) {
-        case NSEventPhaseBegan: return SCROLL_PHASE_BEGAN;
-        case NSEventPhaseCancelled: return SCROLL_PHASE_CANCELLED;
-        case NSEventPhaseEnded: return SCROLL_PHASE_ENDED;
-        default: return SCROLL_PHASE_CONTINUED;
+    if ([event type] != NSScrollWheel) {
+        return 0;
     }
+    if ([event phase]) {
+        // process a phase of manual scrolling
+        switch ([event phase]) {
+            case NSEventPhaseBegan: return SCROLL_PHASE_BEGAN;
+            case NSEventPhaseCancelled: return SCROLL_PHASE_ENDED;
+            case NSEventPhaseEnded: return SCROLL_PHASE_ENDED;
+            default: return SCROLL_PHASE_CONTINUED;
+        }
+    }
+    if ([event momentumPhase]) {
+        // process a phase of automatic scrolling
+        switch ([event momentumPhase]) {
+            case NSEventPhaseBegan: return SCROLL_PHASE_MOMENTUM_BEGAN;
+            case NSEventPhaseCancelled: return SCROLL_PHASE_ENDED;
+            case NSEventPhaseEnded: return SCROLL_PHASE_ENDED;
+            default: return SCROLL_PHASE_CONTINUED;
+        }
+    }
+    // phase and momentum phase both are not set
+    return SCROLL_PHASE_UNSUPPORTED;
 }
 @end