nsIWebBrowserPrint

[email protected]
Newsgroups gmane.comp.mozilla.devel.java
Organization http://groups.google.com
Message-ID <1170424720.191584.46410__38877.1599195481$1170437235$gmane$org@s48g2000cws.googlegroups.com>
Hi!
I use JavaXPCOM to embed gecko into an awt canvas. A print button
should print the content.
If I compile and run the java class found below, wait for mozilla.org
to be rendered inside the canvas and then press the print button,
nothing is printed.
Sometimes (not always), a print dialog opens (but nothing happens
after pressing "ok")
Sometimes (not always) an exception is thrown:
 org.mozilla.xpcom.XPCOMException: The function "print" returned an
error condition  (0x80040111)

Can anyone tell me what's wrong?
Thanks in advance!
hs
-------------------------------------------
package bugs;

import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Canvas;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import org.mozilla.xpcom.GREVersionRange;
import org.mozilla.xpcom.IAppFileLocProvider;
import org.mozilla.xpcom.Mozilla;
import org.mozilla.xpcom.nsIAppStartup;
import org.mozilla.xpcom.nsIBaseWindow;
import org.mozilla.xpcom.nsIEventQueue;
import org.mozilla.xpcom.nsIEventQueueService;
import org.mozilla.xpcom.nsIInterfaceRequestor;
import org.mozilla.xpcom.nsIProxyObjectManager;
import org.mozilla.xpcom.nsIServiceManager;
import org.mozilla.xpcom.nsIWebBrowser;
import org.mozilla.xpcom.nsIWebBrowserPrint;
import org.mozilla.xpcom.nsIWebNavigation;

public class PrintTest extends Canvas {

  private nsIWebBrowser webBrowser;

  // used if no gre can be located automatically
  public static String FALLBACK_GRE_PATH = "/path/to/xulrunner";


  public static void main(String[] args) throws Exception {
    // init gtk
    // this line removes the warnings, but not the crash
    // sun.awt.X11.XToolkit.checkGTK();

    final PrintTest pt = new PrintTest();
    Button bt = new Button("print");
    bt.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        pt.print();
      }
    });
    Frame frame = new Frame("PrintTest");
    frame.setLayout(new BorderLayout());
    frame.add(bt, BorderLayout.NORTH);
    frame.add(pt, BorderLayout.CENTER);
    frame.setBounds(100, 100, 400, 400);
    frame.setVisible(true);
    Thread.sleep(2000);
    System.out.println("starting test...");
    pt.test();
  }


  public void test() throws Exception {

    Mozilla mozilla = Mozilla.getInstance();
    GREVersionRange[] range = new GREVersionRange[1];
    range[0] = new GREVersionRange("1.8.0", true, "1.9", false);

    File grePath = Mozilla.getGREPathWithProperties(range, null);
    if (null == grePath) grePath = new File(FALLBACK_GRE_PATH);
    LocationProvider locProvider = new LocationProvider(grePath);
    mozilla.initEmbedding(grePath, grePath, locProvider);

    Mozilla moz = Mozilla.getInstance();

    nsIServiceManager serviceManager = moz.getServiceManager();
    nsIAppStartup appStartup = (nsIAppStartup)
serviceManager.getServiceByContractID(
      "@mozilla.org/toolkit/app-startup;1",
nsIAppStartup.NS_IAPPSTARTUP_IID);

    String NS_IWEBBROWSER_CID = "F1EAC761-87E9-11d3-
AF80-00A024FFC08C"; //$NON-NLS-1$
    this.webBrowser = (nsIWebBrowser)
moz.getComponentManager().createInstance(NS_IWEBBROWSER_CID, null,
      nsIWebBrowser.NS_IWEBBROWSER_IID);

    nsIBaseWindow baseWindow = (nsIBaseWindow)
webBrowser.queryInterface(nsIBaseWindow.NS_IBASEWINDOW_IID);

    // long windowHandle = ((sun.awt.X11.XComponentPeer)
getPeer()).getWindow();
    long windowHandle = ((sun.awt.windows.WComponentPeer)
getPeer()).getHWnd();

    System.out.println("windowHandle " + windowHandle);
    baseWindow.initWindow(windowHandle, 0, 0, 0, getWidth(),
getHeight());
    System.out.println("inited");
    baseWindow.create();
    System.out.println("created");
    baseWindow.setVisibility(true);
    nsIWebNavigation webNavigation = (nsIWebNavigation) webBrowser
      .queryInterface(nsIWebNavigation.NS_IWEBNAVIGATION_IID);
    webNavigation.loadURI("http://www.mozilla.org",
nsIWebNavigation.LOAD_FLAGS_BYPASS_CACHE, null, null,
      null);

    appStartup.run();

  }


  public void print() {
    Mozilla moz = Mozilla.getInstance();
    nsIInterfaceRequestor ir = (nsIInterfaceRequestor) webBrowser
      .queryInterface(nsIInterfaceRequestor.NS_IINTERFACEREQUESTOR_IID);
    nsIWebBrowserPrint print = (nsIWebBrowserPrint) ir
      .getInterface(nsIWebBrowserPrint.NS_IWEBBROWSERPRINT_IID);

    nsIEventQueueService eventQueueServive = (nsIEventQueueService)
moz.getServiceManager()
      .getServiceByContractID("@mozilla.org/event-queue-service;1",
        nsIEventQueueService.NS_IEVENTQUEUESERVICE_IID);
    nsIEventQueue eventQueue = eventQueueServive
      .getSpecialEventQueue(nsIEventQueueService.UI_THREAD_EVENT_QUEUE);
    nsIProxyObjectManager proxyManager = (nsIProxyObjectManager)
moz.getComponentManager()
      .createInstanceByContractID("@mozilla.org/xpcomproxy;1", null,
        nsIProxyObjectManager.NS_IPROXYOBJECTMANAGER_IID);
    nsIWebBrowserPrint printProxy = (nsIWebBrowserPrint)
proxyManager.getProxyForObject(eventQueue,
      nsIWebBrowserPrint.NS_IWEBBROWSERPRINT_IID, print,
nsIProxyObjectManager.INVOKE_SYNC);
    printProxy.print(null, null);
  }

  public static class LocationProvider implements IAppFileLocProvider
{
    private final File libXULPath;
    int counter = 0;


    public LocationProvider(File grePath) {
      this.libXULPath = grePath;
    }


    public File getFile(String aProp, boolean[] aPersistent) {
      File file = null;
      if (aProp.equals("GreD") || aProp.equals("GreComsD")) {
        file = libXULPath;
        if (aProp.equals("GreComsD")) {
          file = new File(file, "components");
        }
      } else if (aProp.equals("MozBinD") || aProp.equals("CurProcD")
|| aProp.equals("ComsD")
        || aProp.equals("ProfD")) {
        file = libXULPath;
        if (aProp.equals("ComsD")) {
          file = new File(file, "components");
        }
      }
      return file;
    }


    public File[] getFiles(String aProp) {
      File[] files = null;
      if (aProp.equals("APluginsDL")) {
        files = new File[1];
        files[0] = new File(libXULPath, "plugins");
      }
      return files;
    }
  }
}
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.