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     public ScrollerImpl(Wrap target, CaretOwner caret) {
  49         super(target, caret);
  50         scroll = new CaretScroll(caret);
  51         addScrollAction(new ScrollAction() {
  52 
  53             public void scrollTo(int direction) {
  54                 getWrap().mouse().click(1, getScrollClickPoint(direction > 0));
  55             }
  56         });
  57     }
  58 
  59     /**
  60      * @param increase <code>true</code> to increase, <code>false</code> to decrease the value
  61      * @return  a point to click in order to decrease/increase the value
  62      */
  63     protected abstract Point getScrollClickPoint(boolean increase);
  64 
  65     /**
  66      * An auxiliary function to calculate click point, on the appropriate side
  67      * of the control depending on the parameters.
  68      * @param c the control wrapper
  69      * @param horizontal - horizontal or vertical
  70      * @param increase - increase or decrease
  71      * @param offset distance from the border
  72      * @return the point instance
  73      */
  74     public static Point createScrollPoint(Wrap c, boolean horizontal, boolean increase, int offset) {
  75         return createScrollPoint(c.getScreenBounds(), horizontal, increase, offset);
  76     }
  77 
  78     public static Point createScrollPoint(Rectangle bounds, boolean horizontal, boolean increase, int offset) {
  79         if(horizontal) {
  80             return new Point(increase ? (bounds.width - 1 - offset) : offset, bounds.height / 2);
  81         } else {
  82             return new Point(bounds.width / 2, increase ? (bounds.height - 1 - offset) : offset);
  83         }
  84     }
  85 
  86     public static class CaretScroll implements Scroll {
  87 
  88         CaretOwner co;
  89 
  90         public CaretScroll(CaretOwner co) {
  91             this.co = co;
  92         }
  93 
  94         public double maximum() {
  95             throw new UnsupportedOperationException("Not supported yet.");
  96         }
  97 
  98         public double minimum() {
  99             throw new UnsupportedOperationException("Not supported yet.");
 100         }
 101 
 102         public double position() {
 103             return co.position();
 104         }
 105 
 106         public Caret caret() {
 107             throw new UnsupportedOperationException("Not supported yet.");
 108         }
 109 
 110         public void to(double position) {
 111             throw new UnsupportedOperationException("Not supported yet.");
 112         }
 113     }
 114 
 115 }