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