@Override
public void run(Object... parameters) {
int toOrig;
int dragAttempt = 0;
Timeout moveTimeout = wrap.getEnvironment().getTimeout(Drag.IN_DRAG_TIMEOUT).clone();
while((toOrig = condition.to()) != 0) {
Vector axis = getScrollVector().
multiply(toOrig).setLenght(dragDelta);
if (reverse) {
axis.multiply(-1);
}
Vector shift = axis.clone();
Wrap<?> knob = getKnob();
Point orig = new Point(knob.getScreenBounds().getX() + knob.getScreenBounds().getWidth()/2, knob.getScreenBounds().getY() + knob.getScreenBounds().getHeight()/2);
knob.mouse().move(knob.toLocal(orig.getLocation()));
knob.mouse().press();
wrap.getEnvironment().getTimeout(Drag.BEFORE_DRAG_TIMEOUT).sleep();
try {
while (condition.to() == toOrig) {
wrap.getEnvironment().getTimeout(Drag.IN_DRAG_TIMEOUT).sleep();
// Rectangle old_pos = knob.getScreenBounds();
knob.mouse().move(knob.toLocal(orig.getLocation().translate(shift)));
// if (old_pos.equals(knob.getScreenBounds())) { // TODO: it would be better to check if we achieve maximum position
// break;
// }
if(scroll.position() == scroll.minimum() || scroll.position() == scroll.maximum()) {
break;
}
shift.add(axis);
}
} finally {
wrap.getEnvironment().getTimeout(Drag.BEFORE_DROP_TIMEOUT).sleep();
knob.mouse().release();
}
dragAttempt++;
if(dragAttempt >= MAX_SCROLL_ATTEMPTS) {
//did what we could
return;
}
//slow dow the scrolling
moveTimeout.setValue((long) (moveTimeout.getValue() * 1.5));
}
}
});
}