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.Point;
  28 import org.jemmy.Rectangle;
  29 import org.jemmy.control.Wrap;
  30 import org.jemmy.env.Timeout;
  31 import org.jemmy.interfaces.Caret;
  32 import org.jemmy.interfaces.CaretOwner;
  33 import org.jemmy.interfaces.Scroll;
  34 
  35 /**
  36  * Performs scrolling by clicking at a certain position.
  37  * @author shura
  38  */
  39 public abstract class ScrollerImpl extends CaretImpl {
  40 
  41     /**
  42      * @deprecated Use AdvancedScroller.SCROLL_TIMEOUT
  43      */
  44     public static final Timeout SCROLL_TIMEOUT = CaretImpl.SCROLL_TIMEOUT;
  45 
  46     Scroll scroll;
  47 
  48     /**
  49      *
  50      * @param target
  51      * @param caret
  52      */
  53     public ScrollerImpl(Wrap target, CaretOwner caret) {
  54         super(target, caret);
  55         scroll = new CaretScroll(caret);
  56         addScrollAction(new ScrollAction() {
  57 
  58             public void scrollTo(int direction) {
  59                 getWrap().mouse().click(1, getScrollClickPoint(direction > 0));
  60             }
  61         });
  62     }
  63 
  64     /**
  65      * @param increase
  66      * @return  a point to click in order to decrease/increase the value
  67      */
  68     protected abstract Point getScrollClickPoint(boolean increase);
  69 
  70     /**
  71      * An auxiliary function to calculate click point, on the appropriate side
  72      * of the control depending on the parameters.
  73      * @param c
  74      * @param horizontal - horizontal or vertical
  75      * @param increase - increase or decrease
  76      * @param offset distance from the border
  77      * @return
  78      */
  79     public static Point createScrollPoint(Wrap c, boolean horizontal, boolean increase, int offset) {
  80         return createScrollPoint(c.getScreenBounds(), horizontal, increase, offset);
  81     }
  82 
  83     /**
  84      *
  85      * @param bounds
  86      * @param horizontal
  87      * @param increase
  88      * @param offset
  89      * @return
  90      */
  91     public static Point createScrollPoint(Rectangle bounds, boolean horizontal, boolean increase, int offset) {
  92         if(horizontal) {
  93             return new Point(increase ? (bounds.width - 1 - offset) : offset, bounds.height / 2);
  94         } else {
  95             return new Point(bounds.width / 2, increase ? (bounds.height - 1 - offset) : offset);
  96         }
  97     }
  98 
  99     //only the value is used from it
 100     /**
 101      *
 102      */
 103     public static class CaretScroll implements Scroll {
 104 
 105         CaretOwner co;
 106 
 107         /**
 108          *
 109          * @param co
 110          */
 111         public CaretScroll(CaretOwner co) {
 112             this.co = co;
 113         }
 114 
 115         public double maximum() {
 116             throw new UnsupportedOperationException("Not supported yet.");
 117         }
 118 
 119         public double minimum() {
 120             throw new UnsupportedOperationException("Not supported yet.");
 121         }
 122 
 123         public double position() {
 124             return co.position();
 125         }
 126 
 127         public Caret caret() {
 128             throw new UnsupportedOperationException("Not supported yet.");
 129         }
 130 
 131         public void to(double position) {
 132             throw new UnsupportedOperationException("Not supported yet.");
 133         }
 134     }
 135 
 136 }