1 /*
   2  * Copyright (c) 2007, 2017 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 package org.jemmy.input;
  26 
  27 import org.jemmy.JemmyException;
  28 import org.jemmy.Point;
  29 import org.jemmy.Rectangle;
  30 import org.jemmy.Vector;
  31 import org.jemmy.env.Timeout;
  32 import org.jemmy.action.Action;
  33 import org.jemmy.control.Wrap;
  34 import org.jemmy.interfaces.Caret;
  35 import org.jemmy.interfaces.Drag;
  36 import org.jemmy.interfaces.Scroll;
  37 
  38 /**
  39  * Performs scrolling by doing d'n'd of a "knob" wrap. To be used for controls
  40  * like scroll bars, sliders.
  41  * @author shura
  42  */
  43 public abstract class KnobDragScrollerImpl implements Caret {
  44 
  45     public static final int MAX_SCROLL_ATTEMPTS = 5;
  46 
  47     Wrap<?> wrap;
  48     Scroll scroll;
  49     float dragDelta = 1;
  50     boolean reverse;
  51 
  52     /**
  53      *
  54      * @param wrap
  55      * @param scroll
  56      */
  57     public KnobDragScrollerImpl(Wrap<?> wrap, Scroll scroll) {
  58         this.wrap = wrap;
  59         this.scroll = scroll;
  60     }
  61 
  62     public KnobDragScrollerImpl(Wrap<?> wrap, Scroll scroll, boolean reverse) {
  63         this(wrap, scroll);
  64         this.reverse = reverse;
  65     }
  66 
  67     /**
  68      *
  69      * @return
  70      */
  71     public Wrap<?> getWrap() {
  72         return wrap;
  73     }
  74 
  75     /**
  76      *
  77      * @return
  78      */
  79     public abstract Vector getScrollVector();
  80 
  81     /**
  82      *
  83      * @param dragDelta
  84      */
  85     public void setDragDelta(float dragDelta) {
  86         this.dragDelta = dragDelta;
  87     }
  88 
  89     private void toKnob(Wrap<?> knob, Point inWrap) {
  90         inWrap.translate(wrap.getScreenBounds().x, wrap.getScreenBounds().y);
  91         inWrap.translate(-knob.getScreenBounds().x, -knob.getScreenBounds().y);
  92     }
  93 
  94     private void toWrap(Wrap<?> knob, Point inWrap) {
  95         inWrap.translate(knob.getScreenBounds().x, knob.getScreenBounds().y);
  96         inWrap.translate(-wrap.getScreenBounds().x, -wrap.getScreenBounds().y);
  97     }
  98 
  99     public void to(double value) {
 100         scroll.to(value);
 101     }
 102 
 103     public void to(final Direction condition) {
 104         wrap.getEnvironment().getExecutor().execute(wrap.getEnvironment(), false, new Action() {
 105 
 106             @Override
 107             public void run(Object... parameters) {
 108                 int toOrig;
 109                 int dragAttempt = 0;
 110                 Timeout moveTimeout = wrap.getEnvironment().getTimeout(Drag.IN_DRAG_TIMEOUT).clone();
 111                 while((toOrig = condition.to()) != 0) {
 112                     Vector axis = getScrollVector().
 113                             multiply(toOrig).setLenght(dragDelta);
 114                     if (reverse) {
 115                         axis.multiply(-1);
 116                     }
 117                     Vector shift = axis.clone();
 118                     Wrap<?> knob = getKnob();
 119                     Point orig = new Point(knob.getScreenBounds().getX() + knob.getScreenBounds().getWidth()/2, knob.getScreenBounds().getY() + knob.getScreenBounds().getHeight()/2);
 120                     knob.mouse().move(knob.toLocal(orig.getLocation()));
 121                     knob.mouse().press();
 122                     wrap.getEnvironment().getTimeout(Drag.BEFORE_DRAG_TIMEOUT).sleep();
 123                     try {
 124                         while (condition.to() == toOrig) {
 125                             wrap.getEnvironment().getTimeout(Drag.IN_DRAG_TIMEOUT).sleep();
 126 //                            Rectangle old_pos = knob.getScreenBounds();
 127                             knob.mouse().move(knob.toLocal(orig.getLocation().translate(shift)));
 128 //                            if (old_pos.equals(knob.getScreenBounds())) { // TODO: it would be better to check if we achieve maximum position
 129 //                                break;
 130 //                            }
 131                             if(scroll.position() == scroll.minimum() || scroll.position() == scroll.maximum()) {
 132                                 break;
 133                             }
 134                             shift.add(axis);
 135                         }
 136                     } finally {
 137                         wrap.getEnvironment().getTimeout(Drag.BEFORE_DROP_TIMEOUT).sleep();
 138                         knob.mouse().release();
 139                     }
 140                     dragAttempt++;
 141                     if(dragAttempt >= MAX_SCROLL_ATTEMPTS) {
 142                         //did what we could
 143                         return;
 144                     }
 145                     //slow dow the scrolling
 146                     moveTimeout.setValue((long) (moveTimeout.getValue() * 1.5));
 147                 }
 148             }
 149         });
 150     }
 151 
 152     /*
 153      * Create another class for this perhaps
 154     private void initialDrag(double value) {
 155     Point hp = getEndPoint(false);
 156     Point lp = getEndPoint(true);
 157     double xdiff = hp.x - lp.x;
 158     double ydiff = hp.y - lp.y;
 159     double ratio = value / (scroll.maximum() - scroll.minimum());
 160     Wrap<?> knob = getKnob();
 161     Point p = new Point(xdiff * ratio, ydiff * ratio);
 162     toKnob(knob, p);
 163     Point clickPoint = knob.getClickPoint();
 164     p.translate(clickPoint.x, clickPoint.y);
 165     knob.drag().dnd(clickPoint, knob, p);
 166     }
 167      *
 168      */
 169     /**
 170      *
 171      * @return
 172      */
 173     protected abstract Wrap<?> getKnob();
 174 }