1 /*
   2  * Copyright (c) 2011, 2015, 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.webkit;
  27 
  28 import com.sun.javafx.scene.NodeHelper;
  29 import java.security.AccessController;
  30 import java.security.PrivilegedAction;
  31 
  32 import com.sun.javafx.scene.traversal.Direction;
  33 import javafx.geometry.Point2D;
  34 import javafx.geometry.Rectangle2D;
  35 import javafx.scene.Cursor;
  36 import javafx.scene.Scene;
  37 import javafx.scene.control.Tooltip;
  38 import javafx.scene.web.WebView;
  39 import javafx.stage.Screen;
  40 import javafx.stage.Window;
  41 
  42 import com.sun.javafx.util.Utils;
  43 import com.sun.webkit.CursorManager;
  44 import com.sun.webkit.WebPageClient;
  45 import com.sun.webkit.graphics.WCGraphicsManager;
  46 import com.sun.webkit.graphics.WCPageBackBuffer;
  47 import com.sun.webkit.graphics.WCPoint;
  48 import com.sun.webkit.graphics.WCRectangle;
  49 
  50 public final class WebPageClientImpl implements WebPageClient<WebView> {
  51     private static final boolean backBufferSupported;
  52     private static WebConsoleListener consoleListener = null;
  53     private final Accessor accessor;
  54 
  55     static {
  56         backBufferSupported = Boolean.valueOf(
  57                 AccessController.doPrivileged((PrivilegedAction<String>) () -> System.getProperty(
  58                         "com.sun.webkit.pagebackbuffer", "true")));
  59     }
  60 
  61     static void setConsoleListener(WebConsoleListener consoleListener) {
  62         WebPageClientImpl.consoleListener = consoleListener;
  63     }
  64 
  65     public WebPageClientImpl(Accessor accessor) {
  66         this.accessor = accessor;
  67     }
  68 
  69     @Override public void setFocus(boolean focus) {
  70         WebView view = accessor.getView();
  71         if (view != null && focus) {
  72             view.requestFocus();
  73         }
  74     }
  75 
  76     @Override public void setCursor(long cursorID) {
  77         WebView view = accessor.getView();
  78         if (view != null) {
  79             Object cursor = CursorManager.getCursorManager().getCursor(cursorID);
  80             view.setCursor((cursor instanceof Cursor) ? (Cursor) cursor : Cursor.DEFAULT);
  81         }
  82     }
  83 
  84     private Tooltip  tooltip;
  85     private boolean  isTooltipRegistered = false;
  86     String oldTooltipText = "";
  87     @Override public void setTooltip(final String tooltipText) {
  88         WebView view = accessor.getView();
  89         if (tooltipText != null) {
  90             if (tooltip == null) {
  91                 tooltip = new Tooltip(tooltipText);
  92             } else {
  93                 tooltip.setText(tooltipText);
  94                 if (!oldTooltipText.equals(tooltipText)) {
  95                     Tooltip.uninstall(view, tooltip);
  96                     isTooltipRegistered = false;
  97                 }
  98             }
  99             oldTooltipText = tooltipText;
 100             if (!isTooltipRegistered) {
 101                 Tooltip.install(view, tooltip);
 102                 isTooltipRegistered = true;
 103             }
 104         } else if (isTooltipRegistered) {
 105             Tooltip.uninstall(view, tooltip);
 106             isTooltipRegistered = false;
 107         }
 108     }
 109 
 110     @Override public void transferFocus(boolean forward) {
 111         NodeHelper.traverse(accessor.getView(), forward ? Direction.NEXT : Direction.PREVIOUS);
 112     }
 113 
 114     @Override public WCRectangle getScreenBounds(boolean available) {
 115         WebView view = accessor.getView();
 116 
 117         Screen screen = Utils.getScreen(view);
 118         if (screen != null) {
 119             Rectangle2D r = available
 120                     ? screen.getVisualBounds()
 121                     : screen.getBounds();
 122             return new WCRectangle(
 123                     (float)r.getMinX(),  (float)r.getMinY(),
 124                     (float)r.getWidth(), (float)r.getHeight());
 125         }
 126         return null;
 127     }
 128 
 129     @Override public int getScreenDepth() {
 130         // no way to determine screen color depth, return a default
 131         return 24;
 132     }
 133 
 134     @Override public WebView getContainer() {
 135         return accessor.getView();
 136     }
 137 
 138     @Override public WCPoint screenToWindow(WCPoint ptScreen) {
 139         WebView view = accessor.getView();
 140         Scene scene = view.getScene();
 141         Window window = null;
 142 
 143         if (scene != null &&
 144             (window = scene.getWindow()) != null)
 145         {
 146             Point2D pt = view.sceneToLocal(
 147                     ptScreen.getX() - window.getX() - scene.getX(),
 148                     ptScreen.getY() - window.getY() - scene.getY());
 149             return new WCPoint((float)pt.getX(), (float)pt.getY());
 150         } else {
 151             return new WCPoint(0f, 0f);
 152         }
 153     }
 154 
 155     @Override public WCPoint windowToScreen(WCPoint ptWindow) {
 156         WebView view = accessor.getView();
 157         Scene scene = view.getScene();
 158         Window window = null;
 159 
 160         if (scene != null &&
 161             (window = scene.getWindow()) != null)
 162         {
 163             Point2D pt = view.localToScene(ptWindow.getX(), ptWindow.getY());
 164             return new WCPoint((float)(pt.getX() + scene.getX() + window.getX()),
 165                                (float)(pt.getY() + scene.getY() + window.getY()));
 166         } else {
 167             return new WCPoint(0f, 0f);
 168         }
 169     }
 170 
 171     @Override public WCPageBackBuffer createBackBuffer() {
 172         if (isBackBufferSupported()) {
 173             return WCGraphicsManager.getGraphicsManager().createPageBackBuffer();
 174         }
 175         return null;
 176     }
 177 
 178     @Override public boolean isBackBufferSupported() {
 179         return backBufferSupported;
 180     }
 181 
 182     @Override public void addMessageToConsole(String message, int lineNumber,
 183                                               String sourceId)
 184     {
 185         if (consoleListener != null) {
 186             try {
 187                 consoleListener.messageAdded(accessor.getView(), message, lineNumber, sourceId);
 188             } catch (Exception e) {
 189                 e.printStackTrace();
 190             }
 191         }
 192     }
 193 
 194     @Override public void didClearWindowObject(long context,
 195                                                long windowObject)
 196     {
 197     }
 198 }