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