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.sg.prism.web;
  27 
  28 import com.sun.javafx.logging.PlatformLogger;
  29 import com.sun.javafx.geom.BaseBounds;
  30 import com.sun.javafx.geom.RectBounds;
  31 import com.sun.javafx.geom.transform.BaseTransform;
  32 import com.sun.javafx.sg.prism.NGGroup;
  33 import com.sun.prism.Graphics;
  34 import com.sun.prism.PrinterGraphics;
  35 import com.sun.webkit.WebPage;
  36 import com.sun.webkit.graphics.WCGraphicsContext;
  37 import com.sun.webkit.graphics.WCGraphicsManager;
  38 import com.sun.webkit.graphics.WCRectangle;
  39 
  40 /**
  41  * A scene graph node that renders a web resource
  42  *
  43  * @author Alexey Ushakov
  44  */
  45 public final class NGWebView extends NGGroup {
  46 
  47     private final static PlatformLogger log =
  48             PlatformLogger.getLogger(NGWebView.class.getName());
  49     private volatile WebPage page;
  50     private volatile float width, height;
  51 
  52     public void setPage(WebPage page) {
  53         this.page = page;
  54     }
  55 
  56     public void resize(float w, float h) {
  57         if (width != w || height != h) {
  58             width = w;
  59             height = h;
  60             geometryChanged();
  61             if (page != null) {
  62                 page.setBounds(0, 0, (int)w, (int)h);
  63             }
  64         }
  65     }
  66 
  67     // Invoked on JavaFX User Thread.
  68     public void update() {
  69         if (page != null) {
  70             BaseBounds clip = getClippedBounds(new RectBounds(), BaseTransform.IDENTITY_TRANSFORM);
  71             if (!clip.isEmpty()) {
  72                 log.finest("updating rectangle: {0}", clip);
  73                 page.updateContent(new WCRectangle(clip.getMinX(), clip.getMinY(),
  74                                                    clip.getWidth(), clip.getHeight()));
  75             }
  76         }
  77     }
  78 
  79     public void requestRender() {
  80         visualsChanged();
  81     }
  82 
  83     // Invoked on Render Thread.
  84     @Override protected void renderContent(Graphics g) {
  85         log.finest("rendering into {0}", g);
  86         if (g == null || page == null || width <= 0 || height <= 0)
  87             return;
  88 
  89         WCGraphicsContext gc =
  90                 WCGraphicsManager.getGraphicsManager().createGraphicsContext(g);
  91         try {
  92             if (g instanceof PrinterGraphics) {
  93                 page.print(gc, 0, 0, (int) width, (int) height);
  94             } else {
  95                 page.paint(gc, 0, 0, (int) width, (int) height);
  96             }
  97             gc.flush();
  98         } finally {
  99             gc.dispose();
 100         }
 101 
 102     }
 103 
 104     @Override public boolean hasOverlappingContents() {
 105         return false;
 106     }
 107 
 108     @Override protected boolean hasVisuals() {
 109         return true;
 110     }
 111 }