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