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     @Override public void setTooltip(final String tooltipText) {
  87         WebView view = accessor.getView();
  88         if (tooltipText != null) {
  89             if (tooltip == null) {
  90                 tooltip = new Tooltip(tooltipText);
  91             } else {
  92                 tooltip.setText(tooltipText);
  93             }
  94             if (!isTooltipRegistered) {
  95                 Tooltip.install(view, tooltip);
  96                 isTooltipRegistered = true;
  97             }
  98         } else if (isTooltipRegistered) {
  99             Tooltip.uninstall(view, tooltip);
 100             isTooltipRegistered = false;
 101         }
 102     }
 103 
 104     @Override public void transferFocus(boolean forward) {
 105         NodeHelper.traverse(accessor.getView(), forward ? Direction.NEXT : Direction.PREVIOUS);
 106     }
 107 
 108     @Override public WCRectangle getScreenBounds(boolean available) {
 109         WebView view = accessor.getView();
 110 
 111         Screen screen = Utils.getScreen(view);
 112         if (screen != null) {
 113             Rectangle2D r = available
 114                     ? screen.getVisualBounds()
 115                     : screen.getBounds();
 116             return new WCRectangle(
 117                     (float)r.getMinX(),  (float)r.getMinY(),
 118                     (float)r.getWidth(), (float)r.getHeight());
 119         }
 120         return null;
 121     }
 122 
 123     @Override public int getScreenDepth() {
 124         // no way to determine screen color depth, return a default
 125         return 24;
 126     }
 127 
 128     @Override public WebView getContainer() {
 129         return accessor.getView();
 130     }
 131 
 132     @Override public WCPoint screenToWindow(WCPoint ptScreen) {
 133         WebView view = accessor.getView();
 134         Scene scene = view.getScene();
 135         Window window = null;
 136 
 137         if (scene != null &&
 138             (window = scene.getWindow()) != null)
 139         {
 140             Point2D pt = view.sceneToLocal(
 141                     ptScreen.getX() - window.getX() - scene.getX(),
 142                     ptScreen.getY() - window.getY() - scene.getY());
 143             return new WCPoint((float)pt.getX(), (float)pt.getY());
 144         } else {
 145             return new WCPoint(0f, 0f);
 146         }
 147     }
 148 
 149     @Override public WCPoint windowToScreen(WCPoint ptWindow) {
 150         WebView view = accessor.getView();
 151         Scene scene = view.getScene();
 152         Window window = null;
 153 
 154         if (scene != null &&
 155             (window = scene.getWindow()) != null)
 156         {
 157             Point2D pt = view.localToScene(ptWindow.getX(), ptWindow.getY());
 158             return new WCPoint((float)(pt.getX() + scene.getX() + window.getX()),
 159                                (float)(pt.getY() + scene.getY() + window.getY()));
 160         } else {
 161             return new WCPoint(0f, 0f);
 162         }
 163     }
 164 
 165     @Override public WCPageBackBuffer createBackBuffer() {
 166         if (isBackBufferSupported()) {
 167             return WCGraphicsManager.getGraphicsManager().createPageBackBuffer();
 168         }
 169         return null;
 170     }
 171 
 172     @Override public boolean isBackBufferSupported() {
 173         return backBufferSupported;
 174     }
 175 
 176     @Override public void addMessageToConsole(String message, int lineNumber,
 177                                               String sourceId)
 178     {
 179         if (consoleListener != null) {
 180             try {
 181                 consoleListener.messageAdded(accessor.getView(), message, lineNumber, sourceId);
 182             } catch (Exception e) {
 183                 e.printStackTrace();
 184             }
 185         }
 186     }
 187 
 188     @Override public void didClearWindowObject(long context,
 189                                                long windowObject)
 190     {
 191     }
 192 }