< prev index next >

modules/javafx.web/src/main/java/com/sun/javafx/sg/prism/web/NGWebView.java

Print this page




   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;


   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;
< prev index next >