1 /*
   2  * Copyright (c) 2009, 2016, 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 
  26 package com.sun.javafx.robot.impl;
  27 
  28 import javafx.collections.ObservableList;
  29 import javafx.event.EventType;
  30 import javafx.scene.Node;
  31 import javafx.scene.Parent;
  32 import javafx.scene.Scene;
  33 import javafx.scene.input.KeyCode;
  34 import javafx.scene.input.KeyEvent;
  35 import javafx.scene.input.MouseButton;
  36 import javafx.scene.input.MouseEvent;
  37 import javafx.scene.paint.Color;
  38 
  39 import com.sun.javafx.robot.FXRobotImage;
  40 import javafx.scene.input.ScrollEvent;
  41 
  42 /**
  43  * Utility class class used for accessing certain implementation-specific
  44  * runtime functionality.
  45  *
  46  */
  47 public class FXRobotHelper {
  48 
  49     static FXRobotInputAccessor inputAccessor;
  50     static FXRobotSceneAccessor sceneAccessor;
  51     static FXRobotImageConvertor imageConvertor;
  52 
  53     /**
  54      * Returns a ObservableList containing this passed parent's children.
  55      *
  56      * Note that application must use/reference javafx.scene.Scene class prior to
  57      * using this method (for example, by creating a scene).
  58      *
  59      * @param p Parent subclass to get children for
  60      * @return ObservableList containing this parent's children
  61      */
  62     public static ObservableList<Node> getChildren(Parent p) {
  63         if (sceneAccessor == null) {
  64             // TODO: force scene initialization
  65         }
  66         return sceneAccessor.getChildren(p);
  67     }
  68 
  69     /**
  70      * Converts passed integer in IntArgb pixel format to Color.
  71      * @return Color object
  72      */
  73     public static Color argbToColor(int argb) {
  74         int a = argb >> 24;
  75         a = a & 0xff;
  76         float aa = ((float)a) / 255f;
  77 
  78         int r = argb >> 16;
  79         r = r & 0xff;
  80 
  81         int g = argb >> 8;
  82         g = g & 0xff;
  83 
  84         int b = argb;
  85         b = b & 0xff;
  86 
  87         return Color.rgb(r, g, b, aa);
  88     }
  89 
  90     /**
  91      * @treatAsPrivate implementation detail
  92      */
  93     public static void setInputAccessor(FXRobotInputAccessor a) {
  94         if (inputAccessor != null) {
  95             System.out.println("Warning: Input accessor is already set: " + inputAccessor);
  96             Thread.dumpStack();
  97         }
  98         inputAccessor = a;
  99     }
 100 
 101     /**
 102      * @treatAsPrivate implementation detail
 103      */
 104     public static void setSceneAccessor(FXRobotSceneAccessor a) {
 105         if (sceneAccessor != null) {
 106             System.out.println("Warning: Scene accessor is already set: " + sceneAccessor);
 107             Thread.dumpStack();
 108         }
 109         sceneAccessor = a;
 110     }
 111 
 112     /**
 113      * @treatAsPrivate implementation detail
 114      */
 115     public static void setImageConvertor(FXRobotImageConvertor ic) {
 116         if (imageConvertor != null) {
 117             System.out.println("Warning: Image convertor is already set: " + imageConvertor);
 118             Thread.dumpStack();
 119         }
 120         imageConvertor = ic;
 121     }
 122 
 123     /**
 124      * @treatAsPrivate implementation detail
 125      */
 126     public static abstract class FXRobotImageConvertor {
 127           public abstract FXRobotImage
 128               convertToFXRobotImage(Object platformImage);
 129     }
 130 
 131     /**
 132      * @treatAsPrivate implementation detail
 133      */
 134     public static abstract class FXRobotInputAccessor {
 135         public abstract int getCodeForKeyCode(KeyCode keyCode);
 136         public abstract KeyCode getKeyCodeForCode(int code);
 137         public abstract KeyEvent createKeyEvent(
 138                                 EventType<? extends KeyEvent> eventType,
 139                                 KeyCode keyCode, String keyChar, String keyText,
 140                                 boolean shiftDown, boolean controlDown,
 141                                 boolean altDown, boolean metaDown);
 142         public abstract MouseEvent createMouseEvent(
 143                                 EventType<? extends MouseEvent> eventType,
 144                                 int x, int y,
 145                                 int screenX, int screenY,
 146                                 MouseButton button,
 147                                 int clickCount,
 148                                 boolean shiftDown,
 149                                 boolean controlDown,
 150                                 boolean altDown,
 151                                 boolean metaDown,
 152                                 boolean popupTrigger,
 153                                 boolean primaryButtonDown,
 154                                 boolean middleButtonDown,
 155                                 boolean secondaryButtonDown);
 156         public abstract ScrollEvent createScrollEvent(
 157                                 EventType<? extends ScrollEvent> eventType,
 158                                 int scrollX, int scrollY,
 159                                 ScrollEvent.HorizontalTextScrollUnits xTextUnits,
 160                                 int xText,
 161                                 ScrollEvent.VerticalTextScrollUnits yTextUnits,
 162                                 int yText,
 163                                 int x, int y,
 164                                 int screenX, int screenY,
 165                                 boolean shiftDown,
 166                                 boolean controlDown,
 167                                 boolean altDown,
 168                                 boolean metaDown);
 169     }
 170 
 171     /**
 172      * @treatAsPrivate implementation detail
 173      */
 174     public static abstract class FXRobotSceneAccessor {
 175         public abstract void processKeyEvent(Scene scene, KeyEvent keyEvent);
 176         public abstract void processMouseEvent(Scene scene, MouseEvent mouseEvent);
 177         public abstract void processScrollEvent(Scene scene, ScrollEvent scrollEvent);
 178         public abstract ObservableList<Node> getChildren(Parent parent);
 179         public abstract Object renderToImage(Scene scene, Object platformImage);
 180     }
 181 }